made LoadSaveProcess non-static, mostly

This commit is contained in:
BuildTools 2016-11-24 03:41:55 -05:00
parent f978862cfa
commit fb3f2a1cb6
4 changed files with 177 additions and 164 deletions

View file

@ -8,41 +8,102 @@ public class Cache {
private static int maxNameLength; private static int maxNameLength;
private static char[][] listUnfinished; private static char[][] listUnfinished;
private static Map<String,WorldStatus> cacheWorldStatus; private static Map<String,WorldStatus> cacheUnfinished;
//PRIVATE METHODS=============================== static final class WorldStatus {
private static char[][] populate(Set<String> set) final int width;
final int[] lowerleft;
int[] currentRegion;
int n = 1;
int c = 1;
int D = 1;
int d = 0;
boolean B = false;
WorldStatus(int width, int[] lowerleft, int[]center)
{
this.width = width;
this.lowerleft = lowerleft;
currentRegion = center;
}
WorldStatus(
int width, int[] lowerleft,
int[] current, int n, int c, int D, int d, boolean B
)
{
this.width = width;
this.lowerleft = lowerleft;
currentRegion = current;
this.n = n;
this.c = c;
this.D = D;
this.d = d;
this.B = B;
}
}
private static void populateList(Set<String> set)
{ {
char[][] worlds = new char[set.size()][maxNameLength]; char[][] listUnfinished = new char[set.size()][maxNameLength];
int i = 0; int i = 0;
int ii = 0; int ii = 0;
for (String name : set) for (String name : set)
{ {
for (char c : name.toCharArray()) for (char c : name.toCharArray())
{ {
worlds[i][ii] = c; listUnfinished[i][ii] = c;
ii++; ii++;
} }
i++; i++;
ii = 0; ii = 0;
} }
return worlds;
} }
private static void cache() private static void populateCache(Set<String> set)
{ {
String path;
int width;
int[] lowerleft;
int[] current;
int n; int c; int D; int d; boolean B;
for (String name : set){
path = "unfinishedWorlds." + name + ".";
width = Main.config.getInt(path + "width");
lowerleft = new int[]{
Main.config.getInt(path + "lowerleft.x"),
Main.config.getInt(path + "lowerleft.z")
};
current = new int[]{
Main.config.getInt(path + "currentRegion.x"),
Main.config.getInt(path + "currentRegion.z")
};
n = Main.config.getInt(path + "n");
c = Main.config.getInt(path + "c");
D = Main.config.getInt(path + "D");
d = Main.config.getInt(path + "d");
B = Main.config.getBoolean(path + "B");
cacheUnfinished.put(name, new WorldStatus( width,lowerleft,current,n,c,D,d,B ));
}
} }
//PUBLIC METHODS================================ //================================PUBLIC METHODS================================
static void generate() static void generate()
{ {
maxNameLength = Main.config.getInt("max name length"); maxNameLength = Main.config.getInt("max name length");
listUnfinished = populate(Main.unfinished.getKeys(false)); Set<String> set = Main.unfinished.getKeys(false);
cache(); populateList(set);
populateCache(set);
} }
static void saveProgress() static void saveProgress()
{ {
}
static void finish(String name)
{
//TODO
}
static void addUnfinished(int width, int[] center, int[] lowerleft, String name)
{
} }
static boolean isUnfinished(String name){ static boolean isUnfinished(String name){
int i = 0; int i = 0;
@ -59,21 +120,4 @@ public class Cache {
} }
return match; return match;
} }
static void addUnfinished(int width, int[] center, int[] lowerleft, String name)
{
}
static final class WorldStatus {
int width;
int[] center;
int[] lowerleft;
int[] currentRegion;
int n;
int D;
int d;
boolean B;
}
} }

View file

@ -2,136 +2,115 @@ package iieLoadSaveEntireWorld;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.scheduler.BukkitTask;
public class LoadSaveProcess implements Runnable { public class LoadSaveProcess implements Runnable {
//=================================INIT=================================
static boolean processOngoing = false;
static LoadSaveProcess process;
static BukkitTask bukkitTask;
//=============================STATIC FIELDS============================ boolean ready = true;
final Map map;
final World world;
final String worldname;
final int totalRegions;
int[] currentRegion;
static boolean inProgress = false; LoadSaveProcess(
static boolean taskRunning = false; String name, int width, int[] center, int[] lowerleft
)
{
map = new Map(width, lowerleft);
world = Bukkit.getWorld(name);
worldname = name;
totalRegions = width*width;
currentRegion = center;
}
LoadSaveProcess(
String name, int width, int[] lowerleft,
int[] current, int n, int c, int D, int d, boolean B
)
{
map = new Map(width, lowerleft);
world = Bukkit.getWorld(name);
worldname = name;
totalRegions = width*width;
currentRegion = current;
this.n = n;
this.c = c;
this.D = D;
this.d = d;
this.B = B;
}
private static World world;
private static String worldName;
private static int[] currentRegion;
private static int totalRegions;
private static int untilSaveProg;
//===============================CONTROLS=============================== //===============================CONTROLS===============================
static void start(int width, int[] center, int[] lowerleft, String name) static void start(String name)
{ {
Map.init(width, lowerleft);
world = Bukkit.getWorld(worldName);
worldName = name;
currentRegion = center;
totalRegions = width*width;
untilSaveProg = 9;
}
static void resume(String name)
{
String path = "unfinishedWorlds." + name + ".";
int width = Main.config.getInt(path + "width");
int[] center = new int[]
{
Main.config.getInt(path + "center.x"),
Main.config.getInt(path + "center.z")
};
int[] lowerleft = new int[]
{
Main.config.getInt(path + "lowerleft.x"),
Main.config.getInt(path + "lowerleft.z")
};
currentRegion = new int[]
{
Main.config.getInt(path + "currentRegion.x"),
Main.config.getInt(path + "currentRegion.z")
};
totalRegions = Main.config.getInt(path + "width");
Map.init(w, lowerleft);
SavePattern.n = Main.config.getInt(path + "n");
SavePattern.D = Main.config.getInt(path + "D");
SavePattern.d = Main.config.getInt(path + "d");
SavePattern.B = Main.config.getBoolean(path + "B");
}
static void saveProgress()
{
String path = "unfinishedWorlds." + worldName + ".";
Main.config.set(path + "currentRegion.x", currentRegion[0]);
Main.config.set(path + "currentRegion.z", currentRegion[1]);
Main.config.set(path + "n", SavePattern.n);
Main.config.set(path + "D", SavePattern.D);
Main.config.set(path + "d", SavePattern.d);
Main.config.set(path + "B", SavePattern.B);
Main.plugin.saveConfig();
} }
static void stop() static void stop()
{ {
saveProgress(); //TODO
SavePattern.reset(); }
static void finish()
{
bukkitTask.cancel();
process = null;
System.gc();
processOngoing = false;
} }
//===============================PATTERN================================ //===============================PATTERN================================
static final class SavePattern { /* The pattern:
*
* 3 | 36 35 34 33 32 31
* |
* 2 | 17 16 15 14 13 30
* |
* 1 | 18 05 04 03 12 29
* |
* Z | 19 06 01 02 11 28
* |
* -1 | 20 07 08 09 10 27
* |
* -2 | 21 22 23 24 25 26
* +-----------------------
* -2 -1 X 1 2 3
* etc.
*/
/* The pattern: int n = 1; //number
* int c = 1; //direction of travel: E,N,W,S - 1,2,3,4
* 3 | 36 35 34 33 32 31 int D = 1; //distance to travel
* | int d = 0; //distance already traveled
* 2 | 17 16 15 14 13 30 boolean B = false; //OK to change direction?
* |
* 1 | 18 05 04 03 12 29
* |
* Z | 19 06 01 02 11 28
* |
* -1 | 20 07 08 09 10 27
* |
* -2 | 21 22 23 24 25 26
* +-----------------------
* -2 -1 X 1 2 3
* etc.
*/
static int n = 1; //number private void setNextRegion()
static int c = 1; //direction of travel: E,N,W,S - 1,2,3,4 {
static int D = 1; //distance to travel n++;
static int d = 0; //distance already traveled if (d != D) d++;
static boolean B = false; //OK to change direction? else
static void reset()
{ {
c = 1;
D = 1;
d = 0; d = 0;
B = false; D++;
} switch (c){
static void setNextRegion() case 1 : currentRegion[0]++;
{ case 2 : currentRegion[1]++;
n++; case 3 : currentRegion[0]--;
if (d != D) d++; case 4 :
else currentRegion[1]--;
{ c = B ? 1 : c + 1;
d = 0;
D++;
switch (c){
case 1 : currentRegion[0]++;
case 2 : currentRegion[1]++;
case 3 : currentRegion[0]--;
case 4 :
currentRegion[1]--;
c = B ? 1 : c + 1;
}
B = !B;
} }
B = !B;
} }
static boolean complete(){ }
return n == totalRegions; private boolean complete(){
} return n == process.totalRegions;
} }
@ -139,11 +118,11 @@ public class LoadSaveProcess implements Runnable {
private static final class Map private static final class Map
{ {
private static int[] lowerleft; private int[] lowerleft;
private static int[][][][][] allChunkCoords; private int[][][][][] allChunkCoords;
static void init(int w,int[] lowerleft) Map(int w,int[] lowerleft)
{ {
Map.lowerleft = lowerleft; this.lowerleft = lowerleft;
allChunkCoords = new int[w][w][32][32][2]; allChunkCoords = new int[w][w][32][32][2];
int regionX = lowerleft[0]; int regionX = lowerleft[0];
@ -179,11 +158,11 @@ public class LoadSaveProcess implements Runnable {
negX = regionX < 0; negX = regionX < 0;
} }
} }
static int[][][] getChunksCurrentRegion(){ int[][][] getChunksCurrentRegion(){
return return
allChunkCoords allChunkCoords
[ currentRegion[0] - lowerleft[0] ] [ process.currentRegion[0] - lowerleft[0] ]
[ currentRegion[1] - lowerleft[1] ]; [ process.currentRegion[1] - lowerleft[1] ];
} }
} }
@ -191,10 +170,10 @@ public class LoadSaveProcess implements Runnable {
//==================================RUN================================= //==================================RUN=================================
public void run() public void run()
{ {
if (taskRunning) return; if (!ready) return;
else taskRunning = true; else ready = false;
int[][][] r = Map.getChunksCurrentRegion(); int[][][] r = map.getChunksCurrentRegion();
for (int[][] xRow : r) for (int[][] xRow : r)
{ {
for (int[] chunk : xRow) for (int[] chunk : xRow)
@ -203,18 +182,9 @@ public class LoadSaveProcess implements Runnable {
world.unloadChunk(chunk[0], chunk[1]); world.unloadChunk(chunk[0], chunk[1]);
} }
} }
SavePattern.setNextRegion(); setNextRegion();
if (SavePattern.complete())
{
}
if (untilSaveProg == 0)
{
untilSaveProg = 9;
saveProgress();
}else
untilSaveProg--;
if (complete()) finish();
else ready = true;
} }
} }

View file

@ -24,7 +24,6 @@ public class Main extends JavaPlugin {
unfinished = config.getConfigurationSection("unfinished worlds"); unfinished = config.getConfigurationSection("unfinished worlds");
start = new StartCommand(plugin); start = new StartCommand(plugin);
stop = new StopCommand(plugin); stop = new StopCommand(plugin);
process = new LoadSaveProcess();
saveDefaultConfig(); saveDefaultConfig();
getCommand("beginloadsave").setExecutor(start); getCommand("beginloadsave").setExecutor(start);

View file

@ -7,8 +7,8 @@ import org.bukkit.command.CommandSender;
public class StopCommand implements CommandExecutor{ public class StopCommand implements CommandExecutor{
private static Main plugin; private static Main plugin;
StopCommand(Main Plugin){ StopCommand(Main plugin){
plugin = Plugin; this.plugin = plugin;
} }
@Override @Override
public synchronized boolean onCommand(CommandSender sender, Command label, String command, String[] args) { public synchronized boolean onCommand(CommandSender sender, Command label, String command, String[] args) {