From 305f53278c26248900e20e1050c3e83ec9532fc6 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 21 Nov 2016 22:10:31 -0500 Subject: [PATCH 01/10] here's what I've got --- .classpath | 8 + .gitignore | 1 + .project | 17 ++ .settings/org.eclipse.jdt.core.prefs | 11 + .../LoadSaveProcess.java | 158 ++++++++++++ src/iieLoadSaveEntireWorld/Main.java | 16 ++ src/iieLoadSaveEntireWorld/StartCommand.java | 230 +++++++++++++++++ src/iieLoadSaveEntireWorld/StopCommand.java | 5 + src/iieLoadSaveEntireWorld/UnusedPattern.java | 241 ++++++++++++++++++ target/config.yml | 0 target/plugin.yml | 6 + 11 files changed, 693 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 src/iieLoadSaveEntireWorld/LoadSaveProcess.java create mode 100644 src/iieLoadSaveEntireWorld/Main.java create mode 100644 src/iieLoadSaveEntireWorld/StartCommand.java create mode 100644 src/iieLoadSaveEntireWorld/StopCommand.java create mode 100644 src/iieLoadSaveEntireWorld/UnusedPattern.java create mode 100644 target/config.yml create mode 100644 target/plugin.yml diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..be492a2 --- /dev/null +++ b/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..6d94092 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + iieLoadSaveEntireWorld + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java new file mode 100644 index 0000000..3138976 --- /dev/null +++ b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java @@ -0,0 +1,158 @@ +package iieLoadSaveEntireWorld; + +import org.bukkit.scheduler.BukkitTask; + +public class LoadSaveProcess implements Runnable { + + + //=============================STATIC FIELDS============================ + + static boolean inProgress = false; + + private static int[] startRegion; + + private static int[] currentRegion; + + private static TranslatedCoordinates map; + + private static int[][][][][] allChunkCoords; + + public LoadSaveProcess(int width, int[] center, int[] lowCorner){ + currentRegion = startRegion = new int[] {center[0],center[1]}; + map = new TranslatedCoordinates(width,lowCorner[0],lowCorner[1]); + generateAllChunkCoords(width,lowCorner[0],lowCorner[1]); + } + + + //===============================PATTERN================================ + + private static 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. + */ + + private static int n = 1; //number + private static int c = 1; //direction of travel: E,N,W,S - 1,2,3,4 + private static int D = 1; //distance to travel + private static int d = 0; //distance already traveled + private static boolean B = false; //OK to change direction? + static void reset() + { + c = 1; + D = 1; + d = 0; + B = false; + } + static class Loc{//used when pausing the process + int c; int D; int d; boolean B; + Loc(){ + this.c = SavePattern.c; + this.D = SavePattern.D; + this.d = SavePattern.d; + this.B = SavePattern.B; + } + } + static void setNextRegion() + { + n++; + if (d != D) d++; + else + { + 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; + } + } + } + + + + //=================================UTIL================================= + + //TRACKER, COORDINATE TRANSLATOR + private static class TranslatedCoordinates { + + int xAdjust; + int zAdjust; + //boolean[][] savemap; + public TranslatedCoordinates(int w, int lowX, int lowZ) + { + xAdjust = 0 - lowX; + zAdjust = 0 - lowZ; + //savemap = new boolean[w][w]; + } + int x(int x){ return x + xAdjust; } + int z(int z){ return z + zAdjust; } + /* + void save(int x, int z) { savemap[x(x)][z(z)] = true; } + boolean isSaved(int x, int z) { return savemap[x(x)][z(z)]; } + + boolean allSaved(){ + for (boolean[] xRow : savemap){ + for (boolean region : xRow){ + if (!region) return false; + } + } + return true; + } + */ + } + + //GENERATE ALL CHUNK COORDINATES + private static void generateAllChunkCoords(int width, int lowX, int lowZ) { + allChunkCoords = new int[width][width][32][32][2]; + int regionX = lowX; + boolean negX = true; + for (int[][][][] xRowRegions : allChunkCoords){ + int regionZ = lowZ; + boolean negZ = true; + for (int[][][] region : xRowRegions){ + int chunkX = 0; + for (int[][] xRowChunks : region){ + int chunkZ = 0; + for (int[] chunk : xRowChunks){ + chunk[0] = (regionX * 32) + (negX ? 0 - chunkX : chunkX); + chunk[1] = (regionZ * 32) + (negZ ? 0 - chunkZ : chunkZ); + chunkZ++; + } + chunkX++; + } + regionZ++; + if (negZ) + negZ = regionZ < 0; + } + regionX++; + if (negX) + negX = regionX < 0; + } + } + + + //==================================RUN================================= + public void run() { + + } +} diff --git a/src/iieLoadSaveEntireWorld/Main.java b/src/iieLoadSaveEntireWorld/Main.java new file mode 100644 index 0000000..3158459 --- /dev/null +++ b/src/iieLoadSaveEntireWorld/Main.java @@ -0,0 +1,16 @@ +package iieLoadSaveEntireWorld; + +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitTask; + +public class Main extends JavaPlugin { + + static Main plugin; + static BukkitTask task; + + public void onEnable() { + plugin = this; + saveDefaultConfig(); + getCommand("loadsaveentireworld").setExecutor(new StartCommand(plugin)); + } +} diff --git a/src/iieLoadSaveEntireWorld/StartCommand.java b/src/iieLoadSaveEntireWorld/StartCommand.java new file mode 100644 index 0000000..8bb2ccf --- /dev/null +++ b/src/iieLoadSaveEntireWorld/StartCommand.java @@ -0,0 +1,230 @@ +package iieLoadSaveEntireWorld; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class StartCommand implements CommandExecutor { + private Main plugin; + StartCommand(Main Plugin){ + plugin = Plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command label, String command, String[] args) { + if (LoadSaveProcess.inProgress) + { + sender.sendMessage("a process is already running. /StopLoadSave to stop."); + return false; + } + else + LoadSaveProcess.inProgress = true; + Dimensions dimensions = new Dimensions(args); + Main.task = Bukkit.getScheduler().runTaskTimer + ( + plugin, + new LoadSaveProcess( + dimensions.width,dimensions.center,dimensions.lowCorner + ), + 0, 100 + ); + return false; + } + + private static final int[][] defaultDimensions = + new int[][] { {22528,0,0} , {44,-1,-1,-22,-22} }; + + private static class Dimensions{ + int width; + int[] center; + int[] lowCorner; + + Dimensions(String[] args){ + int length = args.length; + if (length == 0) + { + width = defaultDimensions[1][0]; + center = new int[] { defaultDimensions[1][1], defaultDimensions[1][2] }; + lowCorner = new int[] { defaultDimensions[1][3], defaultDimensions[1][4] }; + return; + } + int blockRadius = blockRadius(args[0]); + int[] blockCenter = length > 2 ? + blockCenter(args[1], args[2]) : + new int[] + { + defaultDimensions[0][1], + defaultDimensions[0][2] + }; + int[] blockBounds = blockBounds(blockCenter,blockRadius); + int[] regionBounds = regionBoundsAddMargins( + regionBounds(blockBounds), + blockCenter, + blockRadius + ); + int[] regionCenter = regionCenter(regionBounds); + //TODO + } + } + + //============================================================================================== + //this shit is only used when someone passes args to the command + //we're going to use the default dimensions so hardly any of this + //will get called + + + private static boolean isInteger(String arg){ + int length = arg.length(); + int i = 0; + if (arg.charAt(0) == '-') + if (length == 1) return false; + else i = 1; + for (; i < length; i++) { + char c = arg.charAt(i); + if (c < '0' || c > '9') + return false; + } + return true; + } + + + private static int blockRadius(String arg0) + { + int blockWidth = isInteger(arg0) ? + Integer.parseInt(arg0) : defaultDimensions[0][0]; + return blockWidth/2; + } + private static int[] blockCenter(String arg1, String arg2) + { + int xBlock = isInteger(arg1) ? Integer.parseInt(arg1) : 0; + int zBlock = isInteger(arg2) ? Integer.parseInt(arg2) : 0; + return new int[] {xBlock,zBlock}; + } + private static int[] blockBounds(int[] center, int blockRadius) + { + int xMinBlock = center[0] - blockRadius; + int xMaxBlock = center[0] + blockRadius; + int zMinBlock = center[1] - blockRadius; + int zMaxBlock = center[1] + blockRadius; + return new int[] {xMinBlock,xMaxBlock,zMinBlock,zMaxBlock}; + } + private static int[] regionBounds(int[] blockBounds) + { + int xMinRegion = Math.floorDiv(blockBounds[0],512); + int xMaxRegion = Math.floorDiv(blockBounds[1],512); + int zMinRegion = Math.floorDiv(blockBounds[2],512); + int zMaxRegion = Math.floorDiv(blockBounds[3],512); + return new int[] {xMinRegion,xMaxRegion,zMinRegion,zMaxRegion}; + } + private static int[] regionBoundsAddMargins(int[] regionBounds, int[] blockCenter, int blockRadius) + { + int[] regionBlockRadii = new int[4]; + boolean[] marginAdded = new boolean[4]; + + //get block edge farthest from center + int xMinRegionBlockEdge = regionBounds[0] *512; + int xMaxRegionBlockEdge = (regionBounds[1]+1) *512 - 1; + int zMinRegionBlockEdge = regionBounds[2] *512; + int zMaxRegionBlockEdge = (regionBounds[3]+1) *512 - 1; + + //get edge's block distance from center + regionBlockRadii[0] = Math.abs(blockCenter[0] - xMinRegionBlockEdge); + regionBlockRadii[1] = Math.abs(blockCenter[0] - xMaxRegionBlockEdge); + regionBlockRadii[2] = Math.abs(blockCenter[1] - zMinRegionBlockEdge); + regionBlockRadii[3] = Math.abs(blockCenter[1] - zMaxRegionBlockEdge); + + //compare to original block radius, if difference is < 4 chunks add a region width + if (regionBlockRadii[0] - blockRadius < 64) { regionBounds[0] -= 1; marginAdded[0] = true; } + if (regionBlockRadii[1] - blockRadius < 64) { regionBounds[1] += 1; marginAdded[1] = true; } + if (regionBlockRadii[2] - blockRadius < 64) { regionBounds[2] -= 1; marginAdded[2] = true; } + if (regionBlockRadii[3] - blockRadius < 64) { regionBounds[3] += 1; marginAdded[3] = true; } + + //resquare the selection + regionBounds = regionBoundsResquare(regionBounds,marginAdded,regionBlockRadii); + return regionBounds; + } + private static int[] regionCenter(int[] regionBounds){ + int regionCenterX = regionBounds[0] - 1 + (regionBounds[1]-regionBounds[0] + 1)/2; + int regionCenterZ = regionBounds[2] - 1 + (regionBounds[3]-regionBounds[2] + 1)/2; + return new int[] {regionCenterX, regionCenterZ}; + } + private static int[] regionBoundsResquare(int[] regionBounds, boolean[]marAdd, int[]radii){ + if (!marAdd[0]) + if (!marAdd[1]) + if (!marAdd[2]) + if (!marAdd[3])//0000 + return regionBounds; + else//0001 + { + int min = Math.min(radii[0], radii[1]); + if (min == radii[0]) + regionBounds[0]++; + else + regionBounds[1]++; + } + else + if (!marAdd[3])//0010 + { + int min = Math.min(radii[0], radii[1]); + if (min == radii[0]) + regionBounds[0]++; + else + regionBounds[1]++; + } + else//0011 + { + regionBounds[0]++; + regionBounds[1]++; + } + else + if (!marAdd[2]) + if (!marAdd[3])//0100 + { + int min = Math.min(radii[2], radii[3]); + if (min == radii[2]) + regionBounds[2]++; + else + regionBounds[3]++; + } + else//0101 + return regionBounds; + else + if (!marAdd[3])//0110 + return regionBounds; + else//0111 + regionBounds[0]++; + else + if (marAdd[1]) + if (marAdd[2]) + if (marAdd[3])//1111 + return regionBounds; + else//1110 + regionBounds[3]++; + else + if (marAdd[3])//1101 + regionBounds[2]++; + else//1100 + { + regionBounds[2]++; + regionBounds[3]++; + } + else + if (marAdd[2]) + if (marAdd[3])//1011 + regionBounds[1]++; + else //1010 + return regionBounds; + else //100 + if (marAdd[3])//1001 + return regionBounds; + else {//1000 + int min = Math.min(radii[2], radii[3]); + if (min == radii[2]) + regionBounds[0]++; + else + regionBounds[1]++; + }; + return regionBounds; + } +} diff --git a/src/iieLoadSaveEntireWorld/StopCommand.java b/src/iieLoadSaveEntireWorld/StopCommand.java new file mode 100644 index 0000000..296c814 --- /dev/null +++ b/src/iieLoadSaveEntireWorld/StopCommand.java @@ -0,0 +1,5 @@ +package iieLoadSaveEntireWorld; + +public class StopCommand { + +} diff --git a/src/iieLoadSaveEntireWorld/UnusedPattern.java b/src/iieLoadSaveEntireWorld/UnusedPattern.java new file mode 100644 index 0000000..6c2e96c --- /dev/null +++ b/src/iieLoadSaveEntireWorld/UnusedPattern.java @@ -0,0 +1,241 @@ +package iieLoadSaveEntireWorld; + +import org.bukkit.scheduler.BukkitTask; + +public class UnusedPattern { + + /* the contents of this class are static because + * loading and saving an entire world is an intensive process, + * and only one save process should be running at a time. + * + * so only one save process should be TRACKED at a time. + */ + + //=============================STATIC FIELDS============================ + + static BukkitTask task; + + static int[] startRegion; + + static int[] currentRegion; + + static RegionMap savedRegions; + + static int[][][][][] allChunkCoords; + + static RegionPattern regionPattern; + + //INITIALIZE FIELDS + static void init(int width, int x, int z){ + boolean even = width % 2 == 0; + int radius = Math.floorDiv(width,2); + if (even) radius -=1 ; + int lowX = x-radius; + int lowZ = z-radius; + + startRegion = new int[] {x,z}; + currentRegion = startRegion; + savedRegions = new RegionMap(width,lowX,lowZ); + generateAllChunkCoords(width,lowX,lowZ); + if (even) regionPattern = new OutwardSpiralPattern(); + else regionPattern = new CardinalPointsPattern(); + } + + + + //===============================PATTERNS=============================== + + //ABSTRACT PARENT CLASS + static abstract class RegionPattern { + static int n = 1; //iteration number + abstract void reset(); //reset fields + abstract void setNextRegion(); + } + + //EVEN DIAMETER: OUTWARD SPIRAL PATTERN + private static class OutwardSpiralPattern extends RegionPattern { + + /* 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. + */ + + static int c; //direction of travel: E,N,W,S - 1,2,3,4 + static int D; //distance to travel + static int d; //distance already traveled + static boolean B; //OK to change direction? + + OutwardSpiralPattern(){ + c = 1; + D = 1; + d = 0; + B = false; + } + + + + //interface methods + public void reset() { + + } + public void setNextRegion() { + + } + } + + + //ODD DIAMETER: CARDINAL POINTS PATTERN + private static class CardinalPointsPattern extends RegionPattern { + + /* The pattern: + * + * 2 | 23 18 10 14 22 + * | + * 1 | 15 07 02 06 21 + * | + * Z | 11 03 01 05 13 + * | + * -1 | 19 08 04 09 17 + * | + * -2 | 24 16 12 20 25 + * +------------------- + * -2 -1 X 1 2 + * etc. + */ + + private static int[] cardinalPoints; //midpoint of each side + + private static int c; //side: N,W,S,E = 1,2,3,4 + private static int r; //radius from square center + + private static int d; //distance from cardinal point + private static boolean B; //direction from cardinal point + + private static void expR(){ //expand radius, cardinal points + r++; + cardinalPoints[0]++; + cardinalPoints[1]--; + cardinalPoints[2]--; + cardinalPoints[4]++; + } + + CardinalPointsPattern(){ + reset(); + } + + //interface methods + void reset(){ + cardinalPoints = new int[] { //each cardinal point contains + startRegion[1]+1, //only the dimension that moves + startRegion[0]-1, + startRegion[1]-1, + startRegion[0]+1 + }; + n = 1; + c = 1; + r = 1; + d = 0; + B = false; + } + void setNextRegion(){ + n++; + switch (c){ + case 1 : + if (B) currentRegion = new int[] {startRegion[0] + d, cardinalPoints[0]}; + else; currentRegion = new int[] {startRegion[0] - d, cardinalPoints[0]}; + case 2 : + if (B) currentRegion = new int[] {cardinalPoints[1], startRegion[0] + d}; + else; currentRegion = new int[] {cardinalPoints[1], startRegion[0] - d}; + case 3 : + if (B) currentRegion = new int[] {startRegion[0] - d, cardinalPoints[2]}; + else; currentRegion = new int[] {startRegion[0] + d, cardinalPoints[2]}; + case 4 : + if (B) currentRegion = new int[] {cardinalPoints[3], startRegion[0] - d}; + else; currentRegion = new int[] {cardinalPoints[3], startRegion[0] + d}; + + if (r == d) { expR(); d = 0; c = 1; } + else { d++; c++; B = !B; } + + } + } + } + + + + //=================================UTIL================================= + + //CUSTOM MAP CLASS FOR TRACKING SAVED REGIONS + static class RegionMap { + + int xAdjust; + int zAdjust; + boolean[][] map; + + public RegionMap(int d, int lowX, int lowZ){ + xAdjust = 0 - lowX; + zAdjust = 0 - lowZ; + d++; + map = new boolean[d][d]; + } + void save(int x, int z){ + x += xAdjust; + z += zAdjust; + map[x][z] = true; + } + boolean isSaved(int x, int z){ + x += xAdjust; + z += zAdjust; + return map[x][z]; + } + boolean allSaved(){ + for (boolean[] xRow : map){ + for (boolean region : xRow){ + if (!region) return false; + } + } + return true; + } + } + + //GENERATE ALL CHUNK COORDINATES + private static void generateAllChunkCoords(int d, int lowX, int lowZ) { + allChunkCoords = new int[d][d][32][32][2]; + int regionX = lowX; + boolean negX = true; + for (int[][][][] xRowRegions : allChunkCoords){ + int regionZ = lowZ; + boolean negZ = true; + for (int[][][] region : xRowRegions){ + int chunkX = 0; + for (int[][] xRowChunks : region){ + int chunkZ = 0; + for (int[] chunk : xRowChunks){ + chunk[0] = (regionX * 32) + (negX ? 0 - chunkX : chunkX); + chunk[1] = (regionZ * 32) + (negZ ? 0 - chunkZ : chunkZ); + chunkZ++; + } + chunkX++; + } + regionZ++; + if (negZ) + negZ = regionZ < 0; + } + regionX++; + if (negX) + negX = regionX < 0; + } + } +} diff --git a/target/config.yml b/target/config.yml new file mode 100644 index 0000000..e69de29 diff --git a/target/plugin.yml b/target/plugin.yml new file mode 100644 index 0000000..4d5b3c3 --- /dev/null +++ b/target/plugin.yml @@ -0,0 +1,6 @@ + main: iieLoadSaveEntireWorld.Main + version: 1.0.0 + name: LoadSaveEntireWorld + commands: + loadsaveentireworld: + description: loads and saves the entire map, in 32x32 chunk sections \ No newline at end of file From 9c0b28ab4272be967608b15423cd5a9e9892dfb1 Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Tue, 22 Nov 2016 22:48:08 -0500 Subject: [PATCH 02/10] deleted ClassPath It creates conflicts when other users download this eclipse project. If your eclipse project glitches out after this change, re-add spigot and craftbukkit --- .gitignore | 2 ++ src/iieLoadSaveEntireWorld/ChunkLoaderAPI.java | 5 +++++ 2 files changed, 7 insertions(+) create mode 100644 src/iieLoadSaveEntireWorld/ChunkLoaderAPI.java diff --git a/.gitignore b/.gitignore index ae3c172..4545dc6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /bin/ +.classpath +*.classpath diff --git a/src/iieLoadSaveEntireWorld/ChunkLoaderAPI.java b/src/iieLoadSaveEntireWorld/ChunkLoaderAPI.java new file mode 100644 index 0000000..e47b37c --- /dev/null +++ b/src/iieLoadSaveEntireWorld/ChunkLoaderAPI.java @@ -0,0 +1,5 @@ +package iieLoadSaveEntireWorld; + +public class ChunkLoaderAPI { + +} From ddb27a47526f6c181b6e3e246921972c55cc0080 Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Tue, 22 Nov 2016 22:48:17 -0500 Subject: [PATCH 03/10] Removed classpath --- .classpath | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .classpath diff --git a/.classpath b/.classpath deleted file mode 100644 index be492a2..0000000 --- a/.classpath +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - From 4dd10489948da62110596003cda04e0e2fcb1678 Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Tue, 22 Nov 2016 22:54:36 -0500 Subject: [PATCH 04/10] Created methods that load and unload chunks --- .../ChunkLoaderAPI.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/iieLoadSaveEntireWorld/ChunkLoaderAPI.java b/src/iieLoadSaveEntireWorld/ChunkLoaderAPI.java index e47b37c..0a0fd58 100644 --- a/src/iieLoadSaveEntireWorld/ChunkLoaderAPI.java +++ b/src/iieLoadSaveEntireWorld/ChunkLoaderAPI.java @@ -1,5 +1,31 @@ package iieLoadSaveEntireWorld; +import org.bukkit.Location; + public class ChunkLoaderAPI { + /**This method loads a chunk if the chunk isn't loaded already. + * @param locationToLoad + * @return True if chunk was already loaded, False if chunk wasn't already loaded + */ + public static boolean loadChunk(Location locationToLoad){ + if(!(locationToLoad.getBlock().getChunk().isLoaded())){ + locationToLoad.getBlock().getChunk().load(); + return true; + } + return false; + } + + + /**This method loads a chunk if the chunk isn't loaded already. + * @param locationToLoad + * @return True if chunk was already unloaded, False if chunk wasn't already unloaded + */ + public static boolean unloadChunk(Location locationToUnload){ + if (locationToUnload.getBlock().getChunk().isLoaded()){ + locationToUnload.getBlock().getChunk().unload(); + return false; + } + return true; + } } From 7f67c7f6f95a63ef74f53ed831de9d0e91dadd79 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 23 Nov 2016 16:34:35 -0500 Subject: [PATCH 05/10] still not quite finished, and still not tested --- src/iieLoadSaveEntireWorld/Cache.java | 67 +++++ .../ConfirmCommand.java | 5 + src/iieLoadSaveEntireWorld/Dimensions.java | 184 ++++++++++++++ .../LoadSaveProcess.java | 195 +++++++++------ src/iieLoadSaveEntireWorld/Main.java | 14 +- src/iieLoadSaveEntireWorld/StartCommand.java | 228 ++---------------- src/iieLoadSaveEntireWorld/StopCommand.java | 15 +- src/iieLoadSaveEntireWorld/UnusedPattern.java | 33 ++- 8 files changed, 429 insertions(+), 312 deletions(-) create mode 100644 src/iieLoadSaveEntireWorld/Cache.java create mode 100644 src/iieLoadSaveEntireWorld/ConfirmCommand.java create mode 100644 src/iieLoadSaveEntireWorld/Dimensions.java diff --git a/src/iieLoadSaveEntireWorld/Cache.java b/src/iieLoadSaveEntireWorld/Cache.java new file mode 100644 index 0000000..d111170 --- /dev/null +++ b/src/iieLoadSaveEntireWorld/Cache.java @@ -0,0 +1,67 @@ +package iieLoadSaveEntireWorld; + +import java.util.Set; + +public class Cache { + + static int maxNameLength; + static char[][] worldsFinished; + static char[][] worldsUnfinished; + static void set() + { + Set fin = Main.config.getConfigurationSection("finished worlds").getKeys(false); + Set unfin = Main.config.getConfigurationSection("unfinished worlds").getKeys(false); + + maxNameLength = Main.config.getInt("max name length"); + worldsFinished = populate(fin); + worldsUnfinished = populate(unfin); + } + static char[][] populate(Set set) + { + char[][] worlds = new char[set.size()][maxNameLength]; + int i = 0; + int ii = 0; + for (String name : set) + { + for (char c : name.toCharArray()) + { + worlds[i][ii] = c; + ii++; + } + i++; + ii = 0; + } + return worlds; + } + static boolean isFinished(String name){ + int i = 0; + boolean match = true; + for (char[] world : worldsFinished) + { + for (char c : name.toCharArray()) + { + if (c != world[i]){ match = false; break; } + } + if (match) break; + i = 0; + match = true; + } + return match; + } + static boolean isUnfinished(String name){ + int i = 0; + boolean match = true; + for (char[] world : worldsUnfinished) + { + for (char c : name.toCharArray()) + { + if (c != world[i]){ match = false; break; } + } + if (match) break; + i = 0; + match = true; + } + return match; + } + +} diff --git a/src/iieLoadSaveEntireWorld/ConfirmCommand.java b/src/iieLoadSaveEntireWorld/ConfirmCommand.java new file mode 100644 index 0000000..acdcf99 --- /dev/null +++ b/src/iieLoadSaveEntireWorld/ConfirmCommand.java @@ -0,0 +1,5 @@ +package iieLoadSaveEntireWorld; + +public class ConfirmCommand { + +} diff --git a/src/iieLoadSaveEntireWorld/Dimensions.java b/src/iieLoadSaveEntireWorld/Dimensions.java new file mode 100644 index 0000000..7efc37a --- /dev/null +++ b/src/iieLoadSaveEntireWorld/Dimensions.java @@ -0,0 +1,184 @@ +package iieLoadSaveEntireWorld; + +public class Dimensions { + + private static final int[][] defaultDimensions = new int[][] { {22528,0,0} , {44,-1,-1,-22,-22} }; + int width; + int[] center; + int[] lowerleft; + + Dimensions(String[] args){ + int length = args.length; + if (length == 0) + { + width = defaultDimensions[1][0]; + center = new int[] { defaultDimensions[1][1], defaultDimensions[1][2] }; + lowerleft = new int[] { defaultDimensions[1][3], defaultDimensions[1][4] }; + return; + } + int blockRadius = blockRadius(args[0]); + int[] blockCenter = length > 2 ? + blockCenter(args[1], args[2]) : + new int[] + { + defaultDimensions[0][1], + defaultDimensions[0][2] + }; + int[] blockBounds = blockBounds(blockCenter,blockRadius); + int[] regionBounds = + addMargins( + regionBounds(blockBounds), + blockCenter, + blockRadius + ); + width = regionBounds[2] - regionBounds[0]; + center = regionCenter(regionBounds); + lowerleft[0] = regionBounds[0]; + lowerleft[1] = regionBounds[2]; + + } + + private static boolean isInt(String arg) + { + int length = arg.length(); + int i = 0; + if (arg.charAt(0) == '-') + if (length == 1) return false; + else i = 1; + for (; i < length; i++) { + char c = arg.charAt(i); + if (c < '0' || c > '9') + return false; + } + return true; + } + private static int blockRadius(String arg0) + { + int blockWidth; + if (isInt(arg0)) + { + blockWidth = Integer.parseInt(arg0); + if (blockWidth == 0) blockWidth = defaultDimensions[0][0]; + } + else blockWidth = defaultDimensions[0][0]; + return blockWidth/2; + } + private static int[] blockCenter(String arg1, String arg2) + { + int xBlock = isInt(arg1) ? Integer.parseInt(arg1) : 0; + int zBlock = isInt(arg2) ? Integer.parseInt(arg2) : 0; + return new int[] {xBlock,zBlock}; + } + private static int[] blockBounds(int[] center, int blockRadius) + { + int xMinBlock = center[0] - blockRadius; + int xMaxBlock = center[0] + blockRadius; + int zMinBlock = center[1] - blockRadius; + int zMaxBlock = center[1] + blockRadius; + return new int[] {xMinBlock,xMaxBlock,zMinBlock,zMaxBlock}; + } + private static int[] regionBounds(int[] blockBounds) + { + int xMinRegion = Math.floorDiv(blockBounds[0],512); + int xMaxRegion = Math.floorDiv(blockBounds[1],512); + int zMinRegion = Math.floorDiv(blockBounds[2],512); + int zMaxRegion = Math.floorDiv(blockBounds[3],512); + return new int[] {xMinRegion,xMaxRegion,zMinRegion,zMaxRegion}; + } + private static int[] regionCenter(int[] regionBounds){ + int regionCenterX = regionBounds[0] - 1 + (regionBounds[1]-regionBounds[0] + 1)/2; + int regionCenterZ = regionBounds[2] - 1 + (regionBounds[3]-regionBounds[2] + 1)/2; + return new int[] {regionCenterX, regionCenterZ}; + } + private static int[] addMargins(int[] regionBounds, int[] blockCenter, int blockRadius) + { + int[] radii = new int[4]; //region block edge radii + boolean[] marAdd = new boolean[4]; //margins added + + //get block edge farthest from center + int xMinRegionBlockEdge = regionBounds[0] *512; + int xMaxRegionBlockEdge = (regionBounds[1]+1) *512 - 1; + int zMinRegionBlockEdge = regionBounds[2] *512; + int zMaxRegionBlockEdge = (regionBounds[3]+1) *512 - 1; + + //get edge's block distance from center + radii[0] = Math.abs(blockCenter[0] - xMinRegionBlockEdge); + radii[1] = Math.abs(blockCenter[0] - xMaxRegionBlockEdge); + radii[2] = Math.abs(blockCenter[1] - zMinRegionBlockEdge); + radii[3] = Math.abs(blockCenter[1] - zMaxRegionBlockEdge); + + //compare to original block radius, if difference is < 4 chunks add a region width + if (radii[0] - blockRadius < 64) { regionBounds[0] -= 1; marAdd[0] = true; } + if (radii[1] - blockRadius < 64) { regionBounds[1] += 1; marAdd[1] = true; } + if (radii[2] - blockRadius < 64) { regionBounds[2] -= 1; marAdd[2] = true; } + if (radii[3] - blockRadius < 64) { regionBounds[3] += 1; marAdd[3] = true; } + + //resquare the selection + if (!marAdd[0]) + if (!marAdd[1]) + if (!marAdd[2]) + if (!marAdd[3])//-----------0000 + return regionBounds; + else//----------------------0001 + if (radii[0] < radii[1]) + regionBounds[0]++; + else + regionBounds[1]++; + else + if (!marAdd[3])//-----------0010 + if (radii[0] < radii[1]) + regionBounds[0]++; + else + regionBounds[1]++; + else//----------------------0011 + { + regionBounds[0]++; + regionBounds[1]++; + } + else + if (!marAdd[2]) + if (!marAdd[3])//-----------0100 + if (radii[2] < radii[3]) + regionBounds[2]++; + else + regionBounds[3]++; + else//----------------------0101 + return regionBounds; + else + if (!marAdd[3])//-----------0110 + return regionBounds; + else//----------------------0111 + regionBounds[0]++; + else + if (marAdd[1]) + if (marAdd[2]) + if (marAdd[3])//------------1111 + return regionBounds; + else//----------------------1110 + regionBounds[3]++; + else + if (marAdd[3])//------------1101 + regionBounds[2]++; + else//----------------------1100 + { + regionBounds[2]++; + regionBounds[3]++; + } + else + if (marAdd[2]) + if (marAdd[3])//------------1011 + regionBounds[1]++; + else//----------------------1010 + return regionBounds; + else + if (marAdd[3])//------------1001 + return regionBounds; + else//----------------------1000 + if (radii[2] == radii[3]) + regionBounds[0]++; + else + regionBounds[1]++; + + return regionBounds; + } +} diff --git a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java index 3138976..e0b4b3e 100644 --- a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java +++ b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java @@ -1,26 +1,39 @@ package iieLoadSaveEntireWorld; -import org.bukkit.scheduler.BukkitTask; +import org.bukkit.Bukkit; +import org.bukkit.World; public class LoadSaveProcess implements Runnable { //=============================STATIC FIELDS============================ - + static boolean inProgress = false; + static boolean taskRunning = false; - private static int[] startRegion; - + private static World world; + private static String worldName; + private static int[] currentRegion; + private static int totalRegions; - private static TranslatedCoordinates map; + private static int untilNextProgSave; - private static int[][][][][] allChunkCoords; - - public LoadSaveProcess(int width, int[] center, int[] lowCorner){ - currentRegion = startRegion = new int[] {center[0],center[1]}; - map = new TranslatedCoordinates(width,lowCorner[0],lowCorner[1]); - generateAllChunkCoords(width,lowCorner[0],lowCorner[1]); + + LoadSaveProcess(int width, int[] center, int[] lowerleft, String worldName) + { + world = Bukkit.getWorld(worldName); + LoadSaveProcess.worldName = worldName; + currentRegion = center; + totalRegions = width*width; + untilNextProgSave = 10; + Map.init(width, lowerleft); + + int length = worldName.length(); + if (length > Cache.maxNameLength) + Main.config.set("max namelength",length); + Main.config.set("unfinished worlds." + worldName + ".width", width); + Cache.set(); } @@ -46,11 +59,11 @@ public class LoadSaveProcess implements Runnable { * etc. */ - private static int n = 1; //number - private static int c = 1; //direction of travel: E,N,W,S - 1,2,3,4 - private static int D = 1; //distance to travel - private static int d = 0; //distance already traveled - private static boolean B = false; //OK to change direction? + static int n = 1; //number + static int c = 1; //direction of travel: E,N,W,S - 1,2,3,4 + static int D = 1; //distance to travel + static int d = 0; //distance already traveled + static boolean B = false; //OK to change direction? static void reset() { c = 1; @@ -58,15 +71,6 @@ public class LoadSaveProcess implements Runnable { d = 0; B = false; } - static class Loc{//used when pausing the process - int c; int D; int d; boolean B; - Loc(){ - this.c = SavePattern.c; - this.D = SavePattern.D; - this.d = SavePattern.d; - this.B = SavePattern.B; - } - } static void setNextRegion() { n++; @@ -85,74 +89,105 @@ public class LoadSaveProcess implements Runnable { } B = !B; } - } + } + static boolean complete(){ + return n == totalRegions; + } } + //===============================CHUNK MAP============================== - //=================================UTIL================================= - - //TRACKER, COORDINATE TRANSLATOR - private static class TranslatedCoordinates { - - int xAdjust; - int zAdjust; - //boolean[][] savemap; - public TranslatedCoordinates(int w, int lowX, int lowZ) + private static class Map + { + private static int[] lowerleft; + private static int[][][][][] allChunkCoords; + static void init(int w,int[] lowerleft) { - xAdjust = 0 - lowX; - zAdjust = 0 - lowZ; - //savemap = new boolean[w][w]; - } - int x(int x){ return x + xAdjust; } - int z(int z){ return z + zAdjust; } - /* - void save(int x, int z) { savemap[x(x)][z(z)] = true; } - boolean isSaved(int x, int z) { return savemap[x(x)][z(z)]; } - - boolean allSaved(){ - for (boolean[] xRow : savemap){ - for (boolean region : xRow){ - if (!region) return false; - } - } - return true; - } - */ - } - - //GENERATE ALL CHUNK COORDINATES - private static void generateAllChunkCoords(int width, int lowX, int lowZ) { - allChunkCoords = new int[width][width][32][32][2]; - int regionX = lowX; - boolean negX = true; - for (int[][][][] xRowRegions : allChunkCoords){ - int regionZ = lowZ; + Map.lowerleft = lowerleft; + allChunkCoords = new int[w][w][32][32][2]; + + int regionX = lowerleft[0]; + int regionZ = lowerleft[1]; + boolean negX = true; boolean negZ = true; - for (int[][][] region : xRowRegions){ - int chunkX = 0; - for (int[][] xRowChunks : region){ - int chunkZ = 0; - for (int[] chunk : xRowChunks){ - chunk[0] = (regionX * 32) + (negX ? 0 - chunkX : chunkX); - chunk[1] = (regionZ * 32) + (negZ ? 0 - chunkZ : chunkZ); - chunkZ++; + int chunkX = 0; + int chunkZ = 0; + for (int[][][][] xRowRegions : allChunkCoords) + { + regionZ = lowerleft[1]; + negZ = true; + for (int[][][] region : xRowRegions) + { + chunkX = 0; + for (int[][] xRowChunks : region) + { + chunkZ = 0; + for (int[] chunk : xRowChunks) + { + chunk[0] = (regionX * 32) + (negX ? 0 - chunkX : chunkX); + chunk[1] = (regionZ * 32) + (negZ ? 0 - chunkZ : chunkZ); + chunkZ++; + } + chunkX++; } - chunkX++; + regionZ++; + if (negZ) + negZ = regionZ < 0; } - regionZ++; - if (negZ) - negZ = regionZ < 0; + regionX++; + if (negX) + negX = regionX < 0; } - regionX++; - if (negX) - negX = regionX < 0; } - } + static int[][][] getChunksCurrentRegion(){ + return + allChunkCoords + [ currentRegion[0] - lowerleft[0] ] + [ currentRegion[1] - lowerleft[1] ]; + } + } //==================================RUN================================= - public void run() { + public void run() + { + if (taskRunning) return; + else taskRunning = true; + int[][][] r = Map.getChunksCurrentRegion(); + for (int[][] xRow : r) + for (int[] chunk : xRow){ + world.loadChunk(chunk[0], chunk[1], true); + world.unloadChunk(chunk[0], chunk[1]); + } + SavePattern.setNextRegion(); + } + //===============================CONTROLS=============================== + public static void saveProgress() + { + String path = "unfinishedWorlds." + worldName + "."; + Main.config.set(path + "current region.x", currentRegion[0]); + Main.config.set(path + "current region.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(); + } + public void stop() + { + saveProgress(); + SavePattern.reset(); + try { + wait(30000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public static void resume(String worldName) + { + String path = "unfinishedWorlds." + worldName + "."; } } diff --git a/src/iieLoadSaveEntireWorld/Main.java b/src/iieLoadSaveEntireWorld/Main.java index 3158459..3414afb 100644 --- a/src/iieLoadSaveEntireWorld/Main.java +++ b/src/iieLoadSaveEntireWorld/Main.java @@ -1,16 +1,26 @@ package iieLoadSaveEntireWorld; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitTask; public class Main extends JavaPlugin { static Main plugin; + static FileConfiguration config; + static StartCommand start; + static LoadSaveProcess process; static BukkitTask task; - public void onEnable() { + public void onEnable() + { plugin = this; + config = plugin.getConfig(); + start = new StartCommand(plugin); + saveDefaultConfig(); - getCommand("loadsaveentireworld").setExecutor(new StartCommand(plugin)); + getCommand("beginloadsave").setExecutor(start); + getCommand("stoploadsave").setExecutor(new StopCommand(plugin)); + Cache.set(); } } diff --git a/src/iieLoadSaveEntireWorld/StartCommand.java b/src/iieLoadSaveEntireWorld/StartCommand.java index 8bb2ccf..947009a 100644 --- a/src/iieLoadSaveEntireWorld/StartCommand.java +++ b/src/iieLoadSaveEntireWorld/StartCommand.java @@ -4,227 +4,35 @@ import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; public class StartCommand implements CommandExecutor { - private Main plugin; + + private static Main plugin; StartCommand(Main Plugin){ plugin = Plugin; } - @Override - public boolean onCommand(CommandSender sender, Command label, String command, String[] args) { + public synchronized boolean onCommand(CommandSender sender, Command label, String command, String[] args) + { + String worldName = ((Player)sender).getWorld().getName(); if (LoadSaveProcess.inProgress) { - sender.sendMessage("a process is already running. /StopLoadSave to stop."); + sender.sendMessage("a process is already running (" + worldName + "). /StopLoadSave to stop."); return false; } - else - LoadSaveProcess.inProgress = true; - Dimensions dimensions = new Dimensions(args); - Main.task = Bukkit.getScheduler().runTaskTimer - ( - plugin, - new LoadSaveProcess( - dimensions.width,dimensions.center,dimensions.lowCorner - ), - 0, 100 - ); - return false; - } - - private static final int[][] defaultDimensions = - new int[][] { {22528,0,0} , {44,-1,-1,-22,-22} }; - - private static class Dimensions{ - int width; - int[] center; - int[] lowCorner; - - Dimensions(String[] args){ - int length = args.length; - if (length == 0) - { - width = defaultDimensions[1][0]; - center = new int[] { defaultDimensions[1][1], defaultDimensions[1][2] }; - lowCorner = new int[] { defaultDimensions[1][3], defaultDimensions[1][4] }; - return; - } - int blockRadius = blockRadius(args[0]); - int[] blockCenter = length > 2 ? - blockCenter(args[1], args[2]) : - new int[] - { - defaultDimensions[0][1], - defaultDimensions[0][2] - }; - int[] blockBounds = blockBounds(blockCenter,blockRadius); - int[] regionBounds = regionBoundsAddMargins( - regionBounds(blockBounds), - blockCenter, - blockRadius - ); - int[] regionCenter = regionCenter(regionBounds); - //TODO + else LoadSaveProcess.inProgress = true; + if ((Cache.isUnfinished(worldName))) + { + sender.sendMessage("resuming..."); + LoadSaveProcess.resume(worldName); } - } - - //============================================================================================== - //this shit is only used when someone passes args to the command - //we're going to use the default dimensions so hardly any of this - //will get called - - - private static boolean isInteger(String arg){ - int length = arg.length(); - int i = 0; - if (arg.charAt(0) == '-') - if (length == 1) return false; - else i = 1; - for (; i < length; i++) { - char c = arg.charAt(i); - if (c < '0' || c > '9') - return false; - } - return true; - } - - - private static int blockRadius(String arg0) - { - int blockWidth = isInteger(arg0) ? - Integer.parseInt(arg0) : defaultDimensions[0][0]; - return blockWidth/2; - } - private static int[] blockCenter(String arg1, String arg2) - { - int xBlock = isInteger(arg1) ? Integer.parseInt(arg1) : 0; - int zBlock = isInteger(arg2) ? Integer.parseInt(arg2) : 0; - return new int[] {xBlock,zBlock}; - } - private static int[] blockBounds(int[] center, int blockRadius) - { - int xMinBlock = center[0] - blockRadius; - int xMaxBlock = center[0] + blockRadius; - int zMinBlock = center[1] - blockRadius; - int zMaxBlock = center[1] + blockRadius; - return new int[] {xMinBlock,xMaxBlock,zMinBlock,zMaxBlock}; - } - private static int[] regionBounds(int[] blockBounds) - { - int xMinRegion = Math.floorDiv(blockBounds[0],512); - int xMaxRegion = Math.floorDiv(blockBounds[1],512); - int zMinRegion = Math.floorDiv(blockBounds[2],512); - int zMaxRegion = Math.floorDiv(blockBounds[3],512); - return new int[] {xMinRegion,xMaxRegion,zMinRegion,zMaxRegion}; - } - private static int[] regionBoundsAddMargins(int[] regionBounds, int[] blockCenter, int blockRadius) - { - int[] regionBlockRadii = new int[4]; - boolean[] marginAdded = new boolean[4]; - - //get block edge farthest from center - int xMinRegionBlockEdge = regionBounds[0] *512; - int xMaxRegionBlockEdge = (regionBounds[1]+1) *512 - 1; - int zMinRegionBlockEdge = regionBounds[2] *512; - int zMaxRegionBlockEdge = (regionBounds[3]+1) *512 - 1; - - //get edge's block distance from center - regionBlockRadii[0] = Math.abs(blockCenter[0] - xMinRegionBlockEdge); - regionBlockRadii[1] = Math.abs(blockCenter[0] - xMaxRegionBlockEdge); - regionBlockRadii[2] = Math.abs(blockCenter[1] - zMinRegionBlockEdge); - regionBlockRadii[3] = Math.abs(blockCenter[1] - zMaxRegionBlockEdge); - - //compare to original block radius, if difference is < 4 chunks add a region width - if (regionBlockRadii[0] - blockRadius < 64) { regionBounds[0] -= 1; marginAdded[0] = true; } - if (regionBlockRadii[1] - blockRadius < 64) { regionBounds[1] += 1; marginAdded[1] = true; } - if (regionBlockRadii[2] - blockRadius < 64) { regionBounds[2] -= 1; marginAdded[2] = true; } - if (regionBlockRadii[3] - blockRadius < 64) { regionBounds[3] += 1; marginAdded[3] = true; } - - //resquare the selection - regionBounds = regionBoundsResquare(regionBounds,marginAdded,regionBlockRadii); - return regionBounds; - } - private static int[] regionCenter(int[] regionBounds){ - int regionCenterX = regionBounds[0] - 1 + (regionBounds[1]-regionBounds[0] + 1)/2; - int regionCenterZ = regionBounds[2] - 1 + (regionBounds[3]-regionBounds[2] + 1)/2; - return new int[] {regionCenterX, regionCenterZ}; - } - private static int[] regionBoundsResquare(int[] regionBounds, boolean[]marAdd, int[]radii){ - if (!marAdd[0]) - if (!marAdd[1]) - if (!marAdd[2]) - if (!marAdd[3])//0000 - return regionBounds; - else//0001 - { - int min = Math.min(radii[0], radii[1]); - if (min == radii[0]) - regionBounds[0]++; - else - regionBounds[1]++; - } - else - if (!marAdd[3])//0010 - { - int min = Math.min(radii[0], radii[1]); - if (min == radii[0]) - regionBounds[0]++; - else - regionBounds[1]++; - } - else//0011 - { - regionBounds[0]++; - regionBounds[1]++; - } - else - if (!marAdd[2]) - if (!marAdd[3])//0100 - { - int min = Math.min(radii[2], radii[3]); - if (min == radii[2]) - regionBounds[2]++; - else - regionBounds[3]++; - } - else//0101 - return regionBounds; - else - if (!marAdd[3])//0110 - return regionBounds; - else//0111 - regionBounds[0]++; else - if (marAdd[1]) - if (marAdd[2]) - if (marAdd[3])//1111 - return regionBounds; - else//1110 - regionBounds[3]++; - else - if (marAdd[3])//1101 - regionBounds[2]++; - else//1100 - { - regionBounds[2]++; - regionBounds[3]++; - } - else - if (marAdd[2]) - if (marAdd[3])//1011 - regionBounds[1]++; - else //1010 - return regionBounds; - else //100 - if (marAdd[3])//1001 - return regionBounds; - else {//1000 - int min = Math.min(radii[2], radii[3]); - if (min == radii[2]) - regionBounds[0]++; - else - regionBounds[1]++; - }; - return regionBounds; + { + Dimensions d = new Dimensions(args); + Main.process = new LoadSaveProcess( d.width, d.center, d.lowerleft, worldName ); + } + Main.task = Bukkit.getScheduler().runTaskTimer( plugin, Main.process, 0, 100 ); + return true; } } diff --git a/src/iieLoadSaveEntireWorld/StopCommand.java b/src/iieLoadSaveEntireWorld/StopCommand.java index 296c814..fef9ec9 100644 --- a/src/iieLoadSaveEntireWorld/StopCommand.java +++ b/src/iieLoadSaveEntireWorld/StopCommand.java @@ -1,5 +1,18 @@ package iieLoadSaveEntireWorld; -public class StopCommand { +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +public class StopCommand implements CommandExecutor{ + + private static Main plugin; + StopCommand(Main Plugin){ + plugin = Plugin; + } + @Override + public boolean onCommand(CommandSender sender, Command label, String command, String[] args) { + if (LoadSaveProcess.inProgress) Main.process.stop(); + return false; + } } diff --git a/src/iieLoadSaveEntireWorld/UnusedPattern.java b/src/iieLoadSaveEntireWorld/UnusedPattern.java index 6c2e96c..5dc1232 100644 --- a/src/iieLoadSaveEntireWorld/UnusedPattern.java +++ b/src/iieLoadSaveEntireWorld/UnusedPattern.java @@ -19,7 +19,7 @@ public class UnusedPattern { static int[] currentRegion; - static RegionMap savedRegions; + static TranslatedCoordinates savedRegions; static int[][][][][] allChunkCoords; @@ -35,7 +35,7 @@ public class UnusedPattern { startRegion = new int[] {x,z}; currentRegion = startRegion; - savedRegions = new RegionMap(width,lowX,lowZ); + savedRegions = new TranslatedCoordinates(width,lowX,lowZ); generateAllChunkCoords(width,lowX,lowZ); if (even) regionPattern = new OutwardSpiralPattern(); else regionPattern = new CardinalPointsPattern(); @@ -178,30 +178,25 @@ public class UnusedPattern { //=================================UTIL================================= //CUSTOM MAP CLASS FOR TRACKING SAVED REGIONS - static class RegionMap { + static class TranslatedCoordinates { int xAdjust; int zAdjust; - boolean[][] map; - - public RegionMap(int d, int lowX, int lowZ){ + boolean[][] savemap; + public TranslatedCoordinates(int w, int lowX, int lowZ) + { xAdjust = 0 - lowX; zAdjust = 0 - lowZ; - d++; - map = new boolean[d][d]; - } - void save(int x, int z){ - x += xAdjust; - z += zAdjust; - map[x][z] = true; - } - boolean isSaved(int x, int z){ - x += xAdjust; - z += zAdjust; - return map[x][z]; + savemap = new boolean[w][w]; } + int x(int x){ return x + xAdjust; } + int z(int z){ return z + zAdjust; } + + void save(int x, int z) { savemap[x(x)][z(z)] = true; } + boolean isSaved(int x, int z) { return savemap[x(x)][z(z)]; } + boolean allSaved(){ - for (boolean[] xRow : map){ + for (boolean[] xRow : savemap){ for (boolean region : xRow){ if (!region) return false; } From 841ee979be531f8b52db787e4f0eca41b64492a3 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 23 Nov 2016 18:46:52 -0500 Subject: [PATCH 06/10] update on my progress --- src/iieLoadSaveEntireWorld/Cache.java | 27 +---- .../ConfirmCommand.java | 5 - src/iieLoadSaveEntireWorld/Dimensions.java | 12 +- .../LoadSaveProcess.java | 109 +++++++++++------- src/iieLoadSaveEntireWorld/Main.java | 8 +- src/iieLoadSaveEntireWorld/StartCommand.java | 6 +- src/iieLoadSaveEntireWorld/StopCommand.java | 11 +- 7 files changed, 99 insertions(+), 79 deletions(-) delete mode 100644 src/iieLoadSaveEntireWorld/ConfirmCommand.java diff --git a/src/iieLoadSaveEntireWorld/Cache.java b/src/iieLoadSaveEntireWorld/Cache.java index d111170..ecf55d0 100644 --- a/src/iieLoadSaveEntireWorld/Cache.java +++ b/src/iieLoadSaveEntireWorld/Cache.java @@ -4,16 +4,16 @@ import java.util.Set; public class Cache { - static int maxNameLength; - static char[][] worldsFinished; - static char[][] worldsUnfinished; - static void set() + private static int maxNameLength; + private static char[][] worldsUnfinished; + static class WorldStatus { + + } + static void updateListUnfinished() { - Set fin = Main.config.getConfigurationSection("finished worlds").getKeys(false); Set unfin = Main.config.getConfigurationSection("unfinished worlds").getKeys(false); maxNameLength = Main.config.getInt("max name length"); - worldsFinished = populate(fin); worldsUnfinished = populate(unfin); } static char[][] populate(Set set) @@ -33,21 +33,6 @@ public class Cache { } return worlds; } - static boolean isFinished(String name){ - int i = 0; - boolean match = true; - for (char[] world : worldsFinished) - { - for (char c : name.toCharArray()) - { - if (c != world[i]){ match = false; break; } - } - if (match) break; - i = 0; - match = true; - } - return match; - } static boolean isUnfinished(String name){ int i = 0; boolean match = true; diff --git a/src/iieLoadSaveEntireWorld/ConfirmCommand.java b/src/iieLoadSaveEntireWorld/ConfirmCommand.java deleted file mode 100644 index acdcf99..0000000 --- a/src/iieLoadSaveEntireWorld/ConfirmCommand.java +++ /dev/null @@ -1,5 +0,0 @@ -package iieLoadSaveEntireWorld; - -public class ConfirmCommand { - -} diff --git a/src/iieLoadSaveEntireWorld/Dimensions.java b/src/iieLoadSaveEntireWorld/Dimensions.java index 7efc37a..d847d2f 100644 --- a/src/iieLoadSaveEntireWorld/Dimensions.java +++ b/src/iieLoadSaveEntireWorld/Dimensions.java @@ -33,9 +33,11 @@ public class Dimensions { ); width = regionBounds[2] - regionBounds[0]; center = regionCenter(regionBounds); - lowerleft[0] = regionBounds[0]; - lowerleft[1] = regionBounds[2]; - + lowerleft = new int[] + { + regionBounds[0], + regionBounds[2] + }; } private static boolean isInt(String arg) @@ -65,8 +67,8 @@ public class Dimensions { } private static int[] blockCenter(String arg1, String arg2) { - int xBlock = isInt(arg1) ? Integer.parseInt(arg1) : 0; - int zBlock = isInt(arg2) ? Integer.parseInt(arg2) : 0; + int xBlock = isInt(arg1) ? Integer.parseInt(arg1) : defaultDimensions[0][1]; + int zBlock = isInt(arg2) ? Integer.parseInt(arg2) : defaultDimensions[0][2]; return new int[] {xBlock,zBlock}; } private static int[] blockBounds(int[] center, int blockRadius) diff --git a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java index e0b4b3e..e01649e 100644 --- a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java +++ b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java @@ -17,23 +17,62 @@ public class LoadSaveProcess implements Runnable { private static int[] currentRegion; private static int totalRegions; - private static int untilNextProgSave; + private static int untilSaveProg; - - LoadSaveProcess(int width, int[] center, int[] lowerleft, String worldName) + //===============================CONTROLS=============================== + static void start(int width, int[] center, int[] lowerleft, String name) { - world = Bukkit.getWorld(worldName); - LoadSaveProcess.worldName = worldName; - currentRegion = center; - totalRegions = width*width; - untilNextProgSave = 10; Map.init(width, lowerleft); + world = Bukkit.getWorld(worldName); + worldName = name; + currentRegion = center; + totalRegions = width*width; + untilSaveProg = 9; + } + public 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"); + } + public 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(); + } + public static void stop() + { + saveProgress(); + SavePattern.reset(); - int length = worldName.length(); - if (length > Cache.maxNameLength) - Main.config.set("max namelength",length); - Main.config.set("unfinished worlds." + worldName + ".width", width); - Cache.set(); } @@ -154,40 +193,28 @@ public class LoadSaveProcess implements Runnable { { if (taskRunning) return; else taskRunning = true; + int[][][] r = Map.getChunksCurrentRegion(); for (int[][] xRow : r) - for (int[] chunk : xRow){ + { + for (int[] chunk : xRow) + { world.loadChunk(chunk[0], chunk[1], true); world.unloadChunk(chunk[0], chunk[1]); } - SavePattern.setNextRegion(); - } - //===============================CONTROLS=============================== - public static void saveProgress() - { - String path = "unfinishedWorlds." + worldName + "."; - Main.config.set(path + "current region.x", currentRegion[0]); - Main.config.set(path + "current region.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(); - } - public void stop() - { - saveProgress(); - SavePattern.reset(); - try { - wait(30000); - } catch (InterruptedException e) { - e.printStackTrace(); } - } - - public static void resume(String worldName) - { - String path = "unfinishedWorlds." + worldName + "."; + SavePattern.setNextRegion(); + if (SavePattern.complete()) + { + + } + if (untilSaveProg == 0) + { + untilSaveProg = 9; + saveProgress(); + }else + untilSaveProg--; } + } diff --git a/src/iieLoadSaveEntireWorld/Main.java b/src/iieLoadSaveEntireWorld/Main.java index 3414afb..71680e6 100644 --- a/src/iieLoadSaveEntireWorld/Main.java +++ b/src/iieLoadSaveEntireWorld/Main.java @@ -9,7 +9,8 @@ public class Main extends JavaPlugin { static Main plugin; static FileConfiguration config; static StartCommand start; - static LoadSaveProcess process; + static StopCommand stop; + static BukkitTask task; public void onEnable() @@ -17,10 +18,11 @@ public class Main extends JavaPlugin { plugin = this; config = plugin.getConfig(); start = new StartCommand(plugin); + stop = new StopCommand(plugin); saveDefaultConfig(); getCommand("beginloadsave").setExecutor(start); - getCommand("stoploadsave").setExecutor(new StopCommand(plugin)); - Cache.set(); + getCommand("stoploadsave").setExecutor(stop); + Cache.updateListUnfinished(); } } diff --git a/src/iieLoadSaveEntireWorld/StartCommand.java b/src/iieLoadSaveEntireWorld/StartCommand.java index 947009a..318be39 100644 --- a/src/iieLoadSaveEntireWorld/StartCommand.java +++ b/src/iieLoadSaveEntireWorld/StartCommand.java @@ -22,6 +22,8 @@ public class StartCommand implements CommandExecutor { return false; } else LoadSaveProcess.inProgress = true; + + if ((Cache.isUnfinished(worldName))) { sender.sendMessage("resuming..."); @@ -30,8 +32,10 @@ public class StartCommand implements CommandExecutor { else { Dimensions d = new Dimensions(args); - Main.process = new LoadSaveProcess( d.width, d.center, d.lowerleft, worldName ); + LoadSaveProcess.start( d.width, d.center, d.lowerleft, worldName ); } + + Main.task = Bukkit.getScheduler().runTaskTimer( plugin, Main.process, 0, 100 ); return true; } diff --git a/src/iieLoadSaveEntireWorld/StopCommand.java b/src/iieLoadSaveEntireWorld/StopCommand.java index fef9ec9..a968876 100644 --- a/src/iieLoadSaveEntireWorld/StopCommand.java +++ b/src/iieLoadSaveEntireWorld/StopCommand.java @@ -11,8 +11,13 @@ public class StopCommand implements CommandExecutor{ plugin = Plugin; } @Override - public boolean onCommand(CommandSender sender, Command label, String command, String[] args) { - if (LoadSaveProcess.inProgress) Main.process.stop(); - return false; + public synchronized boolean onCommand(CommandSender sender, Command label, String command, String[] args) { + if (LoadSaveProcess.inProgress) + { + LoadSaveProcess.stop(); + return true; + } + else + return false; } } From f978862cfae47f2097f99546a10c389a2cbe14ea Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 23 Nov 2016 22:08:58 -0500 Subject: [PATCH 07/10] progress update --- src/iieLoadSaveEntireWorld/Cache.java | 53 ++++++++++++++----- .../LoadSaveProcess.java | 12 ++--- src/iieLoadSaveEntireWorld/Main.java | 8 ++- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/iieLoadSaveEntireWorld/Cache.java b/src/iieLoadSaveEntireWorld/Cache.java index ecf55d0..4c9e675 100644 --- a/src/iieLoadSaveEntireWorld/Cache.java +++ b/src/iieLoadSaveEntireWorld/Cache.java @@ -1,22 +1,17 @@ package iieLoadSaveEntireWorld; +import java.util.HashMap; +import java.util.Map; import java.util.Set; public class Cache { private static int maxNameLength; - private static char[][] worldsUnfinished; - static class WorldStatus { - - } - static void updateListUnfinished() - { - Set unfin = Main.config.getConfigurationSection("unfinished worlds").getKeys(false); - - maxNameLength = Main.config.getInt("max name length"); - worldsUnfinished = populate(unfin); - } - static char[][] populate(Set set) + private static char[][] listUnfinished; + private static Map cacheWorldStatus; + + //PRIVATE METHODS=============================== + private static char[][] populate(Set set) { char[][] worlds = new char[set.size()][maxNameLength]; int i = 0; @@ -32,11 +27,27 @@ public class Cache { ii = 0; } return worlds; + } + private static void cache() + { + + } + + //PUBLIC METHODS================================ + static void generate() + { + maxNameLength = Main.config.getInt("max name length"); + listUnfinished = populate(Main.unfinished.getKeys(false)); + cache(); + } + static void saveProgress() + { + } static boolean isUnfinished(String name){ int i = 0; boolean match = true; - for (char[] world : worldsUnfinished) + for (char[] world : listUnfinished) { for (char c : name.toCharArray()) { @@ -48,5 +59,21 @@ public class Cache { } 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; + } } diff --git a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java index e01649e..c93588e 100644 --- a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java +++ b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java @@ -7,7 +7,7 @@ public class LoadSaveProcess implements Runnable { //=============================STATIC FIELDS============================ - + static boolean inProgress = false; static boolean taskRunning = false; @@ -29,7 +29,7 @@ public class LoadSaveProcess implements Runnable { totalRegions = width*width; untilSaveProg = 9; } - public static void resume(String name) + static void resume(String name) { String path = "unfinishedWorlds." + name + "."; @@ -57,7 +57,7 @@ public class LoadSaveProcess implements Runnable { SavePattern.d = Main.config.getInt(path + "d"); SavePattern.B = Main.config.getBoolean(path + "B"); } - public static void saveProgress() + static void saveProgress() { String path = "unfinishedWorlds." + worldName + "."; Main.config.set(path + "currentRegion.x", currentRegion[0]); @@ -68,7 +68,7 @@ public class LoadSaveProcess implements Runnable { Main.config.set(path + "B", SavePattern.B); Main.plugin.saveConfig(); } - public static void stop() + static void stop() { saveProgress(); SavePattern.reset(); @@ -78,7 +78,7 @@ public class LoadSaveProcess implements Runnable { //===============================PATTERN================================ - private static class SavePattern { + static final class SavePattern { /* The pattern: * @@ -137,7 +137,7 @@ public class LoadSaveProcess implements Runnable { //===============================CHUNK MAP============================== - private static class Map + private static final class Map { private static int[] lowerleft; private static int[][][][][] allChunkCoords; diff --git a/src/iieLoadSaveEntireWorld/Main.java b/src/iieLoadSaveEntireWorld/Main.java index 71680e6..16bcfdd 100644 --- a/src/iieLoadSaveEntireWorld/Main.java +++ b/src/iieLoadSaveEntireWorld/Main.java @@ -1,5 +1,6 @@ package iieLoadSaveEntireWorld; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitTask; @@ -8,21 +9,26 @@ public class Main extends JavaPlugin { static Main plugin; static FileConfiguration config; + static ConfigurationSection unfinished; + static StartCommand start; static StopCommand stop; + static LoadSaveProcess process; static BukkitTask task; public void onEnable() { plugin = this; config = plugin.getConfig(); + unfinished = config.getConfigurationSection("unfinished worlds"); start = new StartCommand(plugin); stop = new StopCommand(plugin); + process = new LoadSaveProcess(); saveDefaultConfig(); getCommand("beginloadsave").setExecutor(start); getCommand("stoploadsave").setExecutor(stop); - Cache.updateListUnfinished(); + Cache.generate(); } } From fb3f2a1cb60a50015960360210bad3d9062f24bb Mon Sep 17 00:00:00 2001 From: BuildTools Date: Thu, 24 Nov 2016 03:41:55 -0500 Subject: [PATCH 08/10] made LoadSaveProcess non-static, mostly --- src/iieLoadSaveEntireWorld/Cache.java | 100 +++++--- .../LoadSaveProcess.java | 236 ++++++++---------- src/iieLoadSaveEntireWorld/Main.java | 1 - src/iieLoadSaveEntireWorld/StopCommand.java | 4 +- 4 files changed, 177 insertions(+), 164 deletions(-) diff --git a/src/iieLoadSaveEntireWorld/Cache.java b/src/iieLoadSaveEntireWorld/Cache.java index 4c9e675..768763d 100644 --- a/src/iieLoadSaveEntireWorld/Cache.java +++ b/src/iieLoadSaveEntireWorld/Cache.java @@ -8,41 +8,102 @@ public class Cache { private static int maxNameLength; private static char[][] listUnfinished; - private static Map cacheWorldStatus; - - //PRIVATE METHODS=============================== - private static char[][] populate(Set set) + private static Map cacheUnfinished; + + static final class WorldStatus { + 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 set) { - char[][] worlds = new char[set.size()][maxNameLength]; + char[][] listUnfinished = new char[set.size()][maxNameLength]; int i = 0; int ii = 0; for (String name : set) { for (char c : name.toCharArray()) { - worlds[i][ii] = c; + listUnfinished[i][ii] = c; ii++; } i++; ii = 0; } - return worlds; } - private static void cache() + private static void populateCache(Set 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() { maxNameLength = Main.config.getInt("max name length"); - listUnfinished = populate(Main.unfinished.getKeys(false)); - cache(); + Set set = Main.unfinished.getKeys(false); + populateList(set); + populateCache(set); } 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){ int i = 0; @@ -59,21 +120,4 @@ public class Cache { } 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; - } - } diff --git a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java index c93588e..413362c 100644 --- a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java +++ b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java @@ -2,136 +2,115 @@ package iieLoadSaveEntireWorld; import org.bukkit.Bukkit; import org.bukkit.World; +import org.bukkit.scheduler.BukkitTask; public class LoadSaveProcess implements Runnable { - - //=============================STATIC FIELDS============================ + //=================================INIT================================= + static boolean processOngoing = false; + static LoadSaveProcess process; + static BukkitTask bukkitTask; - static boolean inProgress = false; - static boolean taskRunning = false; - - private static World world; - private static String worldName; - - private static int[] currentRegion; - private static int totalRegions; + boolean ready = true; + final Map map; + final World world; + final String worldname; + final int totalRegions; + int[] currentRegion; - private static int untilSaveProg; + LoadSaveProcess( + 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; + } + //===============================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() + { + //TODO + } + static void finish() { - saveProgress(); - SavePattern.reset(); - + bukkitTask.cancel(); + process = null; + System.gc(); + processOngoing = false; } //===============================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. - */ - - static int n = 1; //number - static int c = 1; //direction of travel: E,N,W,S - 1,2,3,4 - static int D = 1; //distance to travel - static int d = 0; //distance already traveled - static boolean B = false; //OK to change direction? - static void reset() + /* 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. + */ + + int n = 1; //number + int c = 1; //direction of travel: E,N,W,S - 1,2,3,4 + int D = 1; //distance to travel + int d = 0; //distance already traveled + boolean B = false; //OK to change direction? + + private void setNextRegion() + { + n++; + if (d != D) d++; + else { - c = 1; - D = 1; - d = 0; - B = false; - } - static void setNextRegion() - { - n++; - if (d != D) d++; - else - { - 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; + 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; } - 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 int[] lowerleft; - private static int[][][][][] allChunkCoords; - static void init(int w,int[] lowerleft) + private int[] lowerleft; + private int[][][][][] allChunkCoords; + Map(int w,int[] lowerleft) { - Map.lowerleft = lowerleft; + this.lowerleft = lowerleft; allChunkCoords = new int[w][w][32][32][2]; int regionX = lowerleft[0]; @@ -179,11 +158,11 @@ public class LoadSaveProcess implements Runnable { negX = regionX < 0; } } - static int[][][] getChunksCurrentRegion(){ + int[][][] getChunksCurrentRegion(){ return allChunkCoords - [ currentRegion[0] - lowerleft[0] ] - [ currentRegion[1] - lowerleft[1] ]; + [ process.currentRegion[0] - lowerleft[0] ] + [ process.currentRegion[1] - lowerleft[1] ]; } } @@ -191,10 +170,10 @@ public class LoadSaveProcess implements Runnable { //==================================RUN================================= public void run() { - if (taskRunning) return; - else taskRunning = true; + if (!ready) return; + else ready = false; - int[][][] r = Map.getChunksCurrentRegion(); + int[][][] r = map.getChunksCurrentRegion(); for (int[][] xRow : r) { for (int[] chunk : xRow) @@ -203,18 +182,9 @@ public class LoadSaveProcess implements Runnable { world.unloadChunk(chunk[0], chunk[1]); } } - SavePattern.setNextRegion(); - if (SavePattern.complete()) - { - - } - if (untilSaveProg == 0) - { - untilSaveProg = 9; - saveProgress(); - }else - untilSaveProg--; + setNextRegion(); - } - + if (complete()) finish(); + else ready = true; + } } diff --git a/src/iieLoadSaveEntireWorld/Main.java b/src/iieLoadSaveEntireWorld/Main.java index 16bcfdd..ca4cde1 100644 --- a/src/iieLoadSaveEntireWorld/Main.java +++ b/src/iieLoadSaveEntireWorld/Main.java @@ -24,7 +24,6 @@ public class Main extends JavaPlugin { unfinished = config.getConfigurationSection("unfinished worlds"); start = new StartCommand(plugin); stop = new StopCommand(plugin); - process = new LoadSaveProcess(); saveDefaultConfig(); getCommand("beginloadsave").setExecutor(start); diff --git a/src/iieLoadSaveEntireWorld/StopCommand.java b/src/iieLoadSaveEntireWorld/StopCommand.java index a968876..f2b3ad0 100644 --- a/src/iieLoadSaveEntireWorld/StopCommand.java +++ b/src/iieLoadSaveEntireWorld/StopCommand.java @@ -7,8 +7,8 @@ import org.bukkit.command.CommandSender; public class StopCommand implements CommandExecutor{ private static Main plugin; - StopCommand(Main Plugin){ - plugin = Plugin; + StopCommand(Main plugin){ + this.plugin = plugin; } @Override public synchronized boolean onCommand(CommandSender sender, Command label, String command, String[] args) { From 6652583ca72d97572a109248190cd0b116f925cc Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 25 Nov 2016 15:58:35 -0500 Subject: [PATCH 09/10] almost done --- .../{Cache.java => CacheOld.java} | 153 +++++----- src/iieLoadSaveEntireWorld/ConfigManager.java | 44 +++ src/iieLoadSaveEntireWorld/Dimensions.java | 268 ++++++++---------- .../LoadSaveProcess.java | 39 +-- ...edPattern.java => LoadSaveProcessOld.java} | 2 +- src/iieLoadSaveEntireWorld/Main.java | 1 - src/iieLoadSaveEntireWorld/StartCommand.java | 19 +- src/iieLoadSaveEntireWorld/StopCommand.java | 2 +- src/iieLoadSaveEntireWorld/WorldObject.java | 43 +++ 9 files changed, 316 insertions(+), 255 deletions(-) rename src/iieLoadSaveEntireWorld/{Cache.java => CacheOld.java} (64%) create mode 100644 src/iieLoadSaveEntireWorld/ConfigManager.java rename src/iieLoadSaveEntireWorld/{UnusedPattern.java => LoadSaveProcessOld.java} (99%) create mode 100644 src/iieLoadSaveEntireWorld/WorldObject.java diff --git a/src/iieLoadSaveEntireWorld/Cache.java b/src/iieLoadSaveEntireWorld/CacheOld.java similarity index 64% rename from src/iieLoadSaveEntireWorld/Cache.java rename to src/iieLoadSaveEntireWorld/CacheOld.java index 768763d..0c0522f 100644 --- a/src/iieLoadSaveEntireWorld/Cache.java +++ b/src/iieLoadSaveEntireWorld/CacheOld.java @@ -4,94 +4,21 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; -public class Cache { - +public class CacheOld +{ private static int maxNameLength; private static char[][] listUnfinished; private static Map cacheUnfinished; - - static final class WorldStatus { - 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 set) - { - char[][] listUnfinished = new char[set.size()][maxNameLength]; - int i = 0; - int ii = 0; - for (String name : set) - { - for (char c : name.toCharArray()) - { - listUnfinished[i][ii] = c; - ii++; - } - i++; - ii = 0; - } - } - private static void populateCache(Set 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================================ static void generate() { maxNameLength = Main.config.getInt("max name length"); Set set = Main.unfinished.getKeys(false); populateList(set); populateCache(set); + } + static void addWorld() + { + } static void saveProgress() { @@ -120,4 +47,72 @@ public class Cache { } return match; } + private static void populateList(Set set) + { + char[][] listUnfinished = new char[set.size()][maxNameLength]; + int i = 0; + int ii = 0; + for (String name : set) + { + for (char c : name.toCharArray()) + { + listUnfinished[i][ii] = c; + ii++; + } + i++; + ii = 0; + } + } + private static void populateCache(Set set) + { + String path; + for (String name : set){ + path = "unfinishedWorlds." + name + "."; + cacheUnfinished.put( + name, + new WorldStatus( + Main.config.getInt(path + "width"), + new int[]{ + Main.config.getInt(path + "lowerleft.x"), + Main.config.getInt(path + "lowerleft.z")}, + new int[]{ + Main.config.getInt(path + "currentRegion.x"), + Main.config.getInt(path + "currentRegion.z")}, + Main.config.getInt(path + "n"), + Main.config.getInt(path + "c"), + Main.config.getInt(path + "D"), + Main.config.getInt(path + "d"), + Main.config.getBoolean(path + "B"))); + } + } + static final class WorldStatus + { + 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; + } + } } diff --git a/src/iieLoadSaveEntireWorld/ConfigManager.java b/src/iieLoadSaveEntireWorld/ConfigManager.java new file mode 100644 index 0000000..2354a21 --- /dev/null +++ b/src/iieLoadSaveEntireWorld/ConfigManager.java @@ -0,0 +1,44 @@ +package iieLoadSaveEntireWorld; + +public class ConfigManager implements Runnable { + + static boolean isNew(String name) + { + return !Main.config.contains("unfinished." + name); + } + static void addNew(String name, int width, int[] lowerleft, int[] center) + { + String path = "unfinished." + name + "."; + Main.config.set(path + "width", width); + Main.config.set(path + "currentRegion.x", center[0]); + Main.config.set(path + "currentRegion.z", center[1]); + Main.config.set(path + "lowerleft.x", lowerleft[0]); + Main.config.set(path + "lowerleft.z", lowerleft[1]); + Main.config.set(path + "n", 1); + Main.config.set(path + "c", 1); + Main.config.set(path + "D", 1); + Main.config.set(path + "d", 0); + Main.config.set(path + "B", 0); + } + static void resume(String name) + { + + } + static void saveProgress() + { + if (LoadSaveProcess.inProgress) + { + String path = "unfinished." + LoadSaveProcess.process.worldname + "."; + + } + + } + static void finish() + { + + } + public void run() { + // TODO Auto-generated method stub + + } +} diff --git a/src/iieLoadSaveEntireWorld/Dimensions.java b/src/iieLoadSaveEntireWorld/Dimensions.java index d847d2f..b4ecbb3 100644 --- a/src/iieLoadSaveEntireWorld/Dimensions.java +++ b/src/iieLoadSaveEntireWorld/Dimensions.java @@ -1,186 +1,166 @@ package iieLoadSaveEntireWorld; -public class Dimensions { +public class Dimensions +{ + private static final int[] defaultDimensions = new int[]{44,-1,-1,-22,-22}; - private static final int[][] defaultDimensions = new int[][] { {22528,0,0} , {44,-1,-1,-22,-22} }; - int width; - int[] center; - int[] lowerleft; - - Dimensions(String[] args){ - int length = args.length; - if (length == 0) - { - width = defaultDimensions[1][0]; - center = new int[] { defaultDimensions[1][1], defaultDimensions[1][2] }; - lowerleft = new int[] { defaultDimensions[1][3], defaultDimensions[1][4] }; - return; + static WorldObject generate(String[] args) + { + if (args.length == 0) + { + return new WorldObject(); } - int blockRadius = blockRadius(args[0]); - int[] blockCenter = length > 2 ? - blockCenter(args[1], args[2]) : - new int[] - { - defaultDimensions[0][1], - defaultDimensions[0][2] - }; - int[] blockBounds = blockBounds(blockCenter,blockRadius); - int[] regionBounds = - addMargins( - regionBounds(blockBounds), - blockCenter, - blockRadius - ); - width = regionBounds[2] - regionBounds[0]; - center = regionCenter(regionBounds); - lowerleft = new int[] + ParsedArgs parsedArgs = new ParsedArgs(args); + + int[] bounds = regionBounds(parsedArgs.radius, parsedArgs.center); + + int width = bounds[2] - bounds[0]; + + int[] lowerleft = new int[]{ bounds[0], bounds[2] }; + + int[] center = new int[]{ bounds[0] - 1 + (bounds[1]-bounds[0] + 1)/2, + bounds[2] - 1 + (bounds[3]-bounds[2] + 1)/2 + }; + + return new WorldObject( width, lowerleft, center ); + } + //============================================================================== + private static final class ParsedArgs + { + private static final int defaultRadius = 11264; + private static final int[] defaultCenter = new int[]{0,0}; + int radius; + int[] center; + ParsedArgs (String[] args) + { + if (isInt(args[0]) && args[0] != "0") + radius = Integer.parseInt(args[0]); + else radius = defaultRadius; + + if (args.length > 2 && isInt(args[1]) && isInt(args[2])) + center = new int[]{ Integer.parseInt(args[1]), Integer.parseInt(args[2]) }; + else center = defaultCenter; + } + private static boolean isInt(String arg) + { + int length = arg.length(); + int i = 0; + if (arg.charAt(0) == '-') + { + if (length == 1) return false; + else i = 1; + } + for (; i < length; i++) + { + char c = arg.charAt(i); + if (c < '0' || c > '9') + return false; + } + return true; + } + } + //============================================================================== + private static int[] regionBounds(int radius, int[] center) + { + int[] bounds = new int[] { - regionBounds[0], - regionBounds[2] - }; - } - - private static boolean isInt(String arg) - { - int length = arg.length(); - int i = 0; - if (arg.charAt(0) == '-') - if (length == 1) return false; - else i = 1; - for (; i < length; i++) { - char c = arg.charAt(i); - if (c < '0' || c > '9') - return false; - } - return true; - } - private static int blockRadius(String arg0) - { - int blockWidth; - if (isInt(arg0)) - { - blockWidth = Integer.parseInt(arg0); - if (blockWidth == 0) blockWidth = defaultDimensions[0][0]; - } - else blockWidth = defaultDimensions[0][0]; - return blockWidth/2; - } - private static int[] blockCenter(String arg1, String arg2) - { - int xBlock = isInt(arg1) ? Integer.parseInt(arg1) : defaultDimensions[0][1]; - int zBlock = isInt(arg2) ? Integer.parseInt(arg2) : defaultDimensions[0][2]; - return new int[] {xBlock,zBlock}; - } - private static int[] blockBounds(int[] center, int blockRadius) - { - int xMinBlock = center[0] - blockRadius; - int xMaxBlock = center[0] + blockRadius; - int zMinBlock = center[1] - blockRadius; - int zMaxBlock = center[1] + blockRadius; - return new int[] {xMinBlock,xMaxBlock,zMinBlock,zMaxBlock}; - } - private static int[] regionBounds(int[] blockBounds) - { - int xMinRegion = Math.floorDiv(blockBounds[0],512); - int xMaxRegion = Math.floorDiv(blockBounds[1],512); - int zMinRegion = Math.floorDiv(blockBounds[2],512); - int zMaxRegion = Math.floorDiv(blockBounds[3],512); - return new int[] {xMinRegion,xMaxRegion,zMinRegion,zMaxRegion}; - } - private static int[] regionCenter(int[] regionBounds){ - int regionCenterX = regionBounds[0] - 1 + (regionBounds[1]-regionBounds[0] + 1)/2; - int regionCenterZ = regionBounds[2] - 1 + (regionBounds[3]-regionBounds[2] + 1)/2; - return new int[] {regionCenterX, regionCenterZ}; - } - private static int[] addMargins(int[] regionBounds, int[] blockCenter, int blockRadius) - { - int[] radii = new int[4]; //region block edge radii - boolean[] marAdd = new boolean[4]; //margins added + // [ get region ] [ get block ] + Math.floorDiv( center[0] - radius, 512 ), + Math.floorDiv( center[0] + radius, 512 ), + Math.floorDiv( center[1] - radius, 512 ), + Math.floorDiv( center[1] + radius, 512 ) + }; + + //add margins------------ + + int[] edges = new int[4]; + int[] radii = new int[4]; + boolean[] margin = new boolean[4]; //get block edge farthest from center - int xMinRegionBlockEdge = regionBounds[0] *512; - int xMaxRegionBlockEdge = (regionBounds[1]+1) *512 - 1; - int zMinRegionBlockEdge = regionBounds[2] *512; - int zMaxRegionBlockEdge = (regionBounds[3]+1) *512 - 1; + edges[0] = bounds[0] *512; + edges[0] = (bounds[1]+1) *512 - 1; + edges[0] = bounds[2] *512; + edges[0] = (bounds[3]+1) *512 - 1; - //get edge's block distance from center - radii[0] = Math.abs(blockCenter[0] - xMinRegionBlockEdge); - radii[1] = Math.abs(blockCenter[0] - xMaxRegionBlockEdge); - radii[2] = Math.abs(blockCenter[1] - zMinRegionBlockEdge); - radii[3] = Math.abs(blockCenter[1] - zMaxRegionBlockEdge); + //get radius from center to far block edge of region + radii[0] = Math.abs(center[0] - edges[0]); + radii[1] = Math.abs(center[0] - edges[1]); + radii[2] = Math.abs(center[1] - edges[2]); + radii[3] = Math.abs(center[1] - edges[3]); //compare to original block radius, if difference is < 4 chunks add a region width - if (radii[0] - blockRadius < 64) { regionBounds[0] -= 1; marAdd[0] = true; } - if (radii[1] - blockRadius < 64) { regionBounds[1] += 1; marAdd[1] = true; } - if (radii[2] - blockRadius < 64) { regionBounds[2] -= 1; marAdd[2] = true; } - if (radii[3] - blockRadius < 64) { regionBounds[3] += 1; marAdd[3] = true; } + if (radii[0] - radius < 64) { bounds[0] -= 1; margin[0] = true; } + if (radii[1] - radius < 64) { bounds[1] += 1; margin[1] = true; } + if (radii[2] - radius < 64) { bounds[2] -= 1; margin[2] = true; } + if (radii[3] - radius < 64) { bounds[3] += 1; margin[3] = true; } //resquare the selection - if (!marAdd[0]) - if (!marAdd[1]) - if (!marAdd[2]) - if (!marAdd[3])//-----------0000 - return regionBounds; + if (!margin[0]) + if (!margin[1]) + if (!margin[2]) + if (!margin[3])//-----------0000 + return bounds; else//----------------------0001 if (radii[0] < radii[1]) - regionBounds[0]++; + bounds[0]++; else - regionBounds[1]++; + bounds[1]++; else - if (!marAdd[3])//-----------0010 + if (!margin[3])//-----------0010 if (radii[0] < radii[1]) - regionBounds[0]++; + bounds[0]++; else - regionBounds[1]++; + bounds[1]++; else//----------------------0011 { - regionBounds[0]++; - regionBounds[1]++; + bounds[0]++; + bounds[1]++; } else - if (!marAdd[2]) - if (!marAdd[3])//-----------0100 + if (!margin[2]) + if (!margin[3])//-----------0100 if (radii[2] < radii[3]) - regionBounds[2]++; + bounds[2]++; else - regionBounds[3]++; + bounds[3]++; else//----------------------0101 - return regionBounds; + return bounds; else - if (!marAdd[3])//-----------0110 - return regionBounds; + if (!margin[3])//-----------0110 + return bounds; else//----------------------0111 - regionBounds[0]++; + bounds[0]++; else - if (marAdd[1]) - if (marAdd[2]) - if (marAdd[3])//------------1111 - return regionBounds; + if (margin[1]) + if (margin[2]) + if (margin[3])//------------1111 + return bounds; else//----------------------1110 - regionBounds[3]++; + bounds[3]++; else - if (marAdd[3])//------------1101 - regionBounds[2]++; + if (margin[3])//------------1101 + bounds[2]++; else//----------------------1100 { - regionBounds[2]++; - regionBounds[3]++; + bounds[2]++; + bounds[3]++; } else - if (marAdd[2]) - if (marAdd[3])//------------1011 - regionBounds[1]++; + if (margin[2]) + if (margin[3])//------------1011 + bounds[1]++; else//----------------------1010 - return regionBounds; + return bounds; else - if (marAdd[3])//------------1001 - return regionBounds; + if (margin[3])//------------1001 + return bounds; else//----------------------1000 if (radii[2] == radii[3]) - regionBounds[0]++; + bounds[0]++; else - regionBounds[1]++; + bounds[1]++; - return regionBounds; + return bounds; } } diff --git a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java index 413362c..9c45599 100644 --- a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java +++ b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java @@ -4,23 +4,22 @@ import org.bukkit.Bukkit; 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; - - boolean ready = true; - final Map map; - final World world; - final String worldname; - final int totalRegions; - int[] currentRegion; + static LoadSaveProcess process; + static BukkitTask bukkitTask; + static boolean inProgress = false; + boolean ready = true; + + final Map map; + final World world; + final String worldname; + final int totalRegions; + int[] currentRegion; - LoadSaveProcess( - String name, int width, int[] center, int[] lowerleft - ) + LoadSaveProcess(String name, int width, int[] lowerleft, int[] center) { map = new Map(width, lowerleft); world = Bukkit.getWorld(name); @@ -30,8 +29,7 @@ public class LoadSaveProcess implements Runnable { } LoadSaveProcess( String name, int width, int[] lowerleft, - int[] current, int n, int c, int D, int d, boolean B - ) + int[] current, int n, int c, int D, int d, boolean B) { map = new Map(width, lowerleft); world = Bukkit.getWorld(name); @@ -47,7 +45,12 @@ public class LoadSaveProcess implements Runnable { //===============================CONTROLS=============================== - static void start(String name) + static void start(String name,WorldObject d) + { + process = new LoadSaveProcess (name, d.width, d.lowerleft, d.current); + ConfigManager.addNew (name, d.width, d.lowerleft, d.current); + } + static void resume(String name) { } @@ -60,7 +63,7 @@ public class LoadSaveProcess implements Runnable { bukkitTask.cancel(); process = null; System.gc(); - processOngoing = false; + inProgress = false; } diff --git a/src/iieLoadSaveEntireWorld/UnusedPattern.java b/src/iieLoadSaveEntireWorld/LoadSaveProcessOld.java similarity index 99% rename from src/iieLoadSaveEntireWorld/UnusedPattern.java rename to src/iieLoadSaveEntireWorld/LoadSaveProcessOld.java index 5dc1232..5b169a0 100644 --- a/src/iieLoadSaveEntireWorld/UnusedPattern.java +++ b/src/iieLoadSaveEntireWorld/LoadSaveProcessOld.java @@ -2,7 +2,7 @@ package iieLoadSaveEntireWorld; import org.bukkit.scheduler.BukkitTask; -public class UnusedPattern { +public class LoadSaveProcessOld { /* the contents of this class are static because * loading and saving an entire world is an intensive process, diff --git a/src/iieLoadSaveEntireWorld/Main.java b/src/iieLoadSaveEntireWorld/Main.java index ca4cde1..599359b 100644 --- a/src/iieLoadSaveEntireWorld/Main.java +++ b/src/iieLoadSaveEntireWorld/Main.java @@ -28,6 +28,5 @@ public class Main extends JavaPlugin { saveDefaultConfig(); getCommand("beginloadsave").setExecutor(start); getCommand("stoploadsave").setExecutor(stop); - Cache.generate(); } } diff --git a/src/iieLoadSaveEntireWorld/StartCommand.java b/src/iieLoadSaveEntireWorld/StartCommand.java index 318be39..b771af2 100644 --- a/src/iieLoadSaveEntireWorld/StartCommand.java +++ b/src/iieLoadSaveEntireWorld/StartCommand.java @@ -1,5 +1,6 @@ package iieLoadSaveEntireWorld; + import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -15,27 +16,23 @@ public class StartCommand implements CommandExecutor { @Override public synchronized boolean onCommand(CommandSender sender, Command label, String command, String[] args) { - String worldName = ((Player)sender).getWorld().getName(); + String worldname = ((Player)sender).getWorld().getName(); if (LoadSaveProcess.inProgress) { - sender.sendMessage("a process is already running (" + worldName + "). /StopLoadSave to stop."); + sender.sendMessage("a process is already running (" + worldname + "). /StopLoadSave to stop."); return false; } else LoadSaveProcess.inProgress = true; - - - if ((Cache.isUnfinished(worldName))) + if (ConfigManager.isNew(worldname)) { - sender.sendMessage("resuming..."); - LoadSaveProcess.resume(worldName); + sender.sendMessage("starting..."); + LoadSaveProcess.start(worldname, Dimensions.generate(args)); } else { - Dimensions d = new Dimensions(args); - LoadSaveProcess.start( d.width, d.center, d.lowerleft, worldName ); + sender.sendMessage("resuming..."); + LoadSaveProcess.resume(worldname); } - - Main.task = Bukkit.getScheduler().runTaskTimer( plugin, Main.process, 0, 100 ); return true; } diff --git a/src/iieLoadSaveEntireWorld/StopCommand.java b/src/iieLoadSaveEntireWorld/StopCommand.java index f2b3ad0..6206391 100644 --- a/src/iieLoadSaveEntireWorld/StopCommand.java +++ b/src/iieLoadSaveEntireWorld/StopCommand.java @@ -11,7 +11,7 @@ public class StopCommand implements CommandExecutor{ this.plugin = plugin; } @Override - public synchronized boolean onCommand(CommandSender sender, Command label, String command, String[] args) { + public boolean onCommand(CommandSender sender, Command label, String command, String[] args) { if (LoadSaveProcess.inProgress) { LoadSaveProcess.stop(); diff --git a/src/iieLoadSaveEntireWorld/WorldObject.java b/src/iieLoadSaveEntireWorld/WorldObject.java new file mode 100644 index 0000000..8741736 --- /dev/null +++ b/src/iieLoadSaveEntireWorld/WorldObject.java @@ -0,0 +1,43 @@ +package iieLoadSaveEntireWorld; + +public class WorldObject { + + final int width; + final int[] lowerleft; + + int[] current = new int[] { -1, -1 }; + int n = 1; + int c = 1; + int D = 1; + int d = 0; + boolean B = false; + + WorldObject() + { + width = 44; + lowerleft = new int[] { -22, -22 }; + } + WorldObject(int width) + { + this.width = width; + lowerleft = new int[] { -22, -22 }; + } + WorldObject(int width, int[] lowerleft, int[] center) + { + this.width = width; + this.lowerleft = lowerleft; + this.current = center; + } + WorldObject(int width, int[] lowerleft, + int[] current, int n, int c, int D, int d, boolean B) + { + this.width = width; + this.lowerleft = lowerleft; + this.current = current; + this.n = n; + this.c = c; + this.D = D; + this.D = d; + this.B = B; + } +} From aec8cc506e42b611bd8056e1d4e164490ea25f99 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 25 Nov 2016 23:04:03 -0500 Subject: [PATCH 10/10] I think it's done --- src/iieLoadSaveEntireWorld/ConfigManager.java | 44 ---- src/iieLoadSaveEntireWorld/ConfigProcess.java | 58 ++++++ src/iieLoadSaveEntireWorld/Dimensions.java | 166 --------------- src/iieLoadSaveEntireWorld/LoadProcess.java | 170 +++++++++++++++ .../LoadSaveProcess.java | 193 ------------------ src/iieLoadSaveEntireWorld/Main.java | 23 +-- src/iieLoadSaveEntireWorld/StartCommand.java | 39 ---- src/iieLoadSaveEntireWorld/StopCommand.java | 23 --- src/iieLoadSaveEntireWorld/TaskManager.java | 107 ++++++++++ src/iieLoadSaveEntireWorld/WorldObject.java | 193 ++++++++++++++++-- .../CacheOld.java | 7 +- .../LoadSaveProcessOld.java | 2 +- 12 files changed, 523 insertions(+), 502 deletions(-) delete mode 100644 src/iieLoadSaveEntireWorld/ConfigManager.java create mode 100644 src/iieLoadSaveEntireWorld/ConfigProcess.java delete mode 100644 src/iieLoadSaveEntireWorld/Dimensions.java create mode 100644 src/iieLoadSaveEntireWorld/LoadProcess.java delete mode 100644 src/iieLoadSaveEntireWorld/LoadSaveProcess.java delete mode 100644 src/iieLoadSaveEntireWorld/StartCommand.java delete mode 100644 src/iieLoadSaveEntireWorld/StopCommand.java create mode 100644 src/iieLoadSaveEntireWorld/TaskManager.java rename src/{iieLoadSaveEntireWorld => unused}/CacheOld.java (97%) rename src/{iieLoadSaveEntireWorld => unused}/LoadSaveProcessOld.java (99%) diff --git a/src/iieLoadSaveEntireWorld/ConfigManager.java b/src/iieLoadSaveEntireWorld/ConfigManager.java deleted file mode 100644 index 2354a21..0000000 --- a/src/iieLoadSaveEntireWorld/ConfigManager.java +++ /dev/null @@ -1,44 +0,0 @@ -package iieLoadSaveEntireWorld; - -public class ConfigManager implements Runnable { - - static boolean isNew(String name) - { - return !Main.config.contains("unfinished." + name); - } - static void addNew(String name, int width, int[] lowerleft, int[] center) - { - String path = "unfinished." + name + "."; - Main.config.set(path + "width", width); - Main.config.set(path + "currentRegion.x", center[0]); - Main.config.set(path + "currentRegion.z", center[1]); - Main.config.set(path + "lowerleft.x", lowerleft[0]); - Main.config.set(path + "lowerleft.z", lowerleft[1]); - Main.config.set(path + "n", 1); - Main.config.set(path + "c", 1); - Main.config.set(path + "D", 1); - Main.config.set(path + "d", 0); - Main.config.set(path + "B", 0); - } - static void resume(String name) - { - - } - static void saveProgress() - { - if (LoadSaveProcess.inProgress) - { - String path = "unfinished." + LoadSaveProcess.process.worldname + "."; - - } - - } - static void finish() - { - - } - public void run() { - // TODO Auto-generated method stub - - } -} diff --git a/src/iieLoadSaveEntireWorld/ConfigProcess.java b/src/iieLoadSaveEntireWorld/ConfigProcess.java new file mode 100644 index 0000000..104c2f1 --- /dev/null +++ b/src/iieLoadSaveEntireWorld/ConfigProcess.java @@ -0,0 +1,58 @@ +package iieLoadSaveEntireWorld; + +import org.bukkit.configuration.file.FileConfiguration; + +public class ConfigProcess implements Runnable { + + //STATIC + private static final Main plugin = new Main(); + private static final FileConfiguration config = plugin.getConfig(); + + static final boolean isNew(String name) + { + return !config.contains(name); + } + static final WorldObject getUnfinished(String name) + { + return new WorldObject + ( + config.getInt(name + ".width"), + new int[] + { + config.getInt(name + ".currentRegion.x"), + config.getInt(name + ".currentRegion.z") + }, + new int[] + { + config.getInt(name + ".lowerleft.x"), + config.getInt(name + ".lowerleft.z") + }, + config.getInt(name + ".n"), + config.getInt(name + ".c"), + config.getInt(name + ".D"), + config.getInt(name + ".d"), + config.getInt(name + ".B") == 1 + ); + } + + + //INSTANCE + private final String name = TaskManager.loadProcess.worldname; + public final void run() + { + config.set(".currentRegion.x", TaskManager.loadProcess.currentRegion[0]); + config.set(".currentRegion.z", TaskManager.loadProcess.currentRegion[1]); + config.set(".n", TaskManager.loadProcess.n); + config.set(".c", TaskManager.loadProcess.c); + config.set(".D", TaskManager.loadProcess.D); + config.set(".d", TaskManager.loadProcess.d); + config.set(".B", TaskManager.loadProcess.B ? 1 : 0); + plugin.saveConfig(); + } + final void finish() + { + config.set("finished", name); + config.set(name, null); + plugin.saveConfig(); + } +} diff --git a/src/iieLoadSaveEntireWorld/Dimensions.java b/src/iieLoadSaveEntireWorld/Dimensions.java deleted file mode 100644 index b4ecbb3..0000000 --- a/src/iieLoadSaveEntireWorld/Dimensions.java +++ /dev/null @@ -1,166 +0,0 @@ -package iieLoadSaveEntireWorld; - -public class Dimensions -{ - private static final int[] defaultDimensions = new int[]{44,-1,-1,-22,-22}; - - static WorldObject generate(String[] args) - { - if (args.length == 0) - { - return new WorldObject(); - } - ParsedArgs parsedArgs = new ParsedArgs(args); - - int[] bounds = regionBounds(parsedArgs.radius, parsedArgs.center); - - int width = bounds[2] - bounds[0]; - - int[] lowerleft = new int[]{ bounds[0], bounds[2] }; - - int[] center = new int[]{ bounds[0] - 1 + (bounds[1]-bounds[0] + 1)/2, - bounds[2] - 1 + (bounds[3]-bounds[2] + 1)/2 - }; - - return new WorldObject( width, lowerleft, center ); - } - //============================================================================== - private static final class ParsedArgs - { - private static final int defaultRadius = 11264; - private static final int[] defaultCenter = new int[]{0,0}; - int radius; - int[] center; - ParsedArgs (String[] args) - { - if (isInt(args[0]) && args[0] != "0") - radius = Integer.parseInt(args[0]); - else radius = defaultRadius; - - if (args.length > 2 && isInt(args[1]) && isInt(args[2])) - center = new int[]{ Integer.parseInt(args[1]), Integer.parseInt(args[2]) }; - else center = defaultCenter; - } - private static boolean isInt(String arg) - { - int length = arg.length(); - int i = 0; - if (arg.charAt(0) == '-') - { - if (length == 1) return false; - else i = 1; - } - for (; i < length; i++) - { - char c = arg.charAt(i); - if (c < '0' || c > '9') - return false; - } - return true; - } - } - //============================================================================== - private static int[] regionBounds(int radius, int[] center) - { - int[] bounds = new int[] - { - // [ get region ] [ get block ] - Math.floorDiv( center[0] - radius, 512 ), - Math.floorDiv( center[0] + radius, 512 ), - Math.floorDiv( center[1] - radius, 512 ), - Math.floorDiv( center[1] + radius, 512 ) - }; - - //add margins------------ - - int[] edges = new int[4]; - int[] radii = new int[4]; - boolean[] margin = new boolean[4]; - - //get block edge farthest from center - edges[0] = bounds[0] *512; - edges[0] = (bounds[1]+1) *512 - 1; - edges[0] = bounds[2] *512; - edges[0] = (bounds[3]+1) *512 - 1; - - //get radius from center to far block edge of region - radii[0] = Math.abs(center[0] - edges[0]); - radii[1] = Math.abs(center[0] - edges[1]); - radii[2] = Math.abs(center[1] - edges[2]); - radii[3] = Math.abs(center[1] - edges[3]); - - //compare to original block radius, if difference is < 4 chunks add a region width - if (radii[0] - radius < 64) { bounds[0] -= 1; margin[0] = true; } - if (radii[1] - radius < 64) { bounds[1] += 1; margin[1] = true; } - if (radii[2] - radius < 64) { bounds[2] -= 1; margin[2] = true; } - if (radii[3] - radius < 64) { bounds[3] += 1; margin[3] = true; } - - //resquare the selection - if (!margin[0]) - if (!margin[1]) - if (!margin[2]) - if (!margin[3])//-----------0000 - return bounds; - else//----------------------0001 - if (radii[0] < radii[1]) - bounds[0]++; - else - bounds[1]++; - else - if (!margin[3])//-----------0010 - if (radii[0] < radii[1]) - bounds[0]++; - else - bounds[1]++; - else//----------------------0011 - { - bounds[0]++; - bounds[1]++; - } - else - if (!margin[2]) - if (!margin[3])//-----------0100 - if (radii[2] < radii[3]) - bounds[2]++; - else - bounds[3]++; - else//----------------------0101 - return bounds; - else - if (!margin[3])//-----------0110 - return bounds; - else//----------------------0111 - bounds[0]++; - else - if (margin[1]) - if (margin[2]) - if (margin[3])//------------1111 - return bounds; - else//----------------------1110 - bounds[3]++; - else - if (margin[3])//------------1101 - bounds[2]++; - else//----------------------1100 - { - bounds[2]++; - bounds[3]++; - } - else - if (margin[2]) - if (margin[3])//------------1011 - bounds[1]++; - else//----------------------1010 - return bounds; - else - if (margin[3])//------------1001 - return bounds; - else//----------------------1000 - if (radii[2] == radii[3]) - bounds[0]++; - else - bounds[1]++; - - return bounds; - } -} diff --git a/src/iieLoadSaveEntireWorld/LoadProcess.java b/src/iieLoadSaveEntireWorld/LoadProcess.java new file mode 100644 index 0000000..c87ab31 --- /dev/null +++ b/src/iieLoadSaveEntireWorld/LoadProcess.java @@ -0,0 +1,170 @@ +package iieLoadSaveEntireWorld; + +import org.bukkit.Bukkit; +import org.bukkit.World; + +public class LoadProcess implements Runnable +{ + //=================================INIT================================= + + final World world; + final String worldname; + final int totalRegions; + int[] currentRegion; + + + LoadProcess(String name, WorldObject newWorld) + { + world = Bukkit.getWorld(name); + worldname = name; + + totalRegions = newWorld.width * newWorld.width; + currentRegion = newWorld.current; + + this.lowerleft = newWorld.lowerleft; + allChunkCoords = generateAllChunkCoords(newWorld.width, newWorld.lowerleft); + } + LoadProcess(String name) + { + final WorldObject unfinished = ConfigProcess.getUnfinished(name); + + world = Bukkit.getWorld(name); + worldname = name; + + totalRegions = unfinished.width * unfinished.width; + currentRegion = unfinished.current; + + this.lowerleft = unfinished.lowerleft; + allChunkCoords = generateAllChunkCoords(unfinished.width, unfinished.lowerleft); + + this.n = unfinished.n; + this.c = unfinished.c; + this.D = unfinished.D; + this.d = unfinished.d; + this.B = unfinished.B; + } + + + //===============================PATTERN================================ + + /* 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. + */ + + int n = 1; //number + int c = 1; //direction of travel: E,N,W,S - 1,2,3,4 + int D = 1; //distance to travel + int d = 0; //distance already traveled + boolean B = false; //OK to change direction? + + private final void setNextRegion() + { + n++; + if (d != D) d++; + else + { + 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; + } + } + final boolean isFinished(){ + return n == totalRegions; + } + + + //===============================CHUNK MAP============================== + + private final int[] lowerleft; + private final int[][][][][] allChunkCoords; + private final int[][][][][] generateAllChunkCoords(int w,int[] lowerleft) + { + int[][][][][] allChunkCoords = new int[w][w][32][32][2]; + + int regionX = lowerleft[0]; + int regionZ = lowerleft[1]; + boolean negX = true; + boolean negZ = true; + int chunkX = 0; + int chunkZ = 0; + for (int[][][][] xRowRegions : allChunkCoords) + { + regionZ = lowerleft[1]; + negZ = true; + for (int[][][] region : xRowRegions) + { + chunkX = 0; + for (int[][] xRowChunks : region) + { + chunkZ = 0; + for (int[] chunk : xRowChunks) + { + chunk[0] = (regionX * 32) + (negX ? 0 - chunkX : chunkX); + chunk[1] = (regionZ * 32) + (negZ ? 0 - chunkZ : chunkZ); + chunkZ++; + } + chunkX++; + } + regionZ++; + if (negZ) + negZ = regionZ < 0; + } + regionX++; + if (negX) + negX = regionX < 0; + } + return allChunkCoords; + } + private final int[][][] getChunksCurrentRegion(){ + return + allChunkCoords + [ currentRegion[0] - lowerleft[0] ] + [ currentRegion[1] - lowerleft[1] ]; + } + + + //==================================RUN================================= + + boolean ready = true; + public final void run() + { + if (!ready) return; + else ready = false; + + final int[][][] r = getChunksCurrentRegion(); + for (int[][] xRow : r) + { + for (int[] chunk : xRow) + { + world.loadChunk(chunk[0], chunk[1], true); + world.unloadChunk(chunk[0], chunk[1]); + } + } + setNextRegion(); + + if (isFinished()) TaskManager.finish(); + else ready = true; + } +} diff --git a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java b/src/iieLoadSaveEntireWorld/LoadSaveProcess.java deleted file mode 100644 index 9c45599..0000000 --- a/src/iieLoadSaveEntireWorld/LoadSaveProcess.java +++ /dev/null @@ -1,193 +0,0 @@ -package iieLoadSaveEntireWorld; - -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.scheduler.BukkitTask; - -public class LoadSaveProcess implements Runnable -{ - - //=================================INIT================================= - static LoadSaveProcess process; - static BukkitTask bukkitTask; - static boolean inProgress = false; - boolean ready = true; - - final Map map; - final World world; - final String worldname; - final int totalRegions; - int[] currentRegion; - - LoadSaveProcess(String name, int width, int[] lowerleft, int[] center) - { - 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; - } - - - //===============================CONTROLS=============================== - static void start(String name,WorldObject d) - { - process = new LoadSaveProcess (name, d.width, d.lowerleft, d.current); - ConfigManager.addNew (name, d.width, d.lowerleft, d.current); - } - static void resume(String name) - { - - } - static void stop() - { - //TODO - } - static void finish() - { - bukkitTask.cancel(); - process = null; - System.gc(); - inProgress = false; - } - - - //===============================PATTERN================================ - - /* 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. - */ - - int n = 1; //number - int c = 1; //direction of travel: E,N,W,S - 1,2,3,4 - int D = 1; //distance to travel - int d = 0; //distance already traveled - boolean B = false; //OK to change direction? - - private void setNextRegion() - { - n++; - if (d != D) d++; - else - { - 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; - } - } - private boolean complete(){ - return n == process.totalRegions; - } - - - //===============================CHUNK MAP============================== - - private static final class Map - { - private int[] lowerleft; - private int[][][][][] allChunkCoords; - Map(int w,int[] lowerleft) - { - this.lowerleft = lowerleft; - allChunkCoords = new int[w][w][32][32][2]; - - int regionX = lowerleft[0]; - int regionZ = lowerleft[1]; - boolean negX = true; - boolean negZ = true; - int chunkX = 0; - int chunkZ = 0; - for (int[][][][] xRowRegions : allChunkCoords) - { - regionZ = lowerleft[1]; - negZ = true; - for (int[][][] region : xRowRegions) - { - chunkX = 0; - for (int[][] xRowChunks : region) - { - chunkZ = 0; - for (int[] chunk : xRowChunks) - { - chunk[0] = (regionX * 32) + (negX ? 0 - chunkX : chunkX); - chunk[1] = (regionZ * 32) + (negZ ? 0 - chunkZ : chunkZ); - chunkZ++; - } - chunkX++; - } - regionZ++; - if (negZ) - negZ = regionZ < 0; - } - regionX++; - if (negX) - negX = regionX < 0; - } - } - int[][][] getChunksCurrentRegion(){ - return - allChunkCoords - [ process.currentRegion[0] - lowerleft[0] ] - [ process.currentRegion[1] - lowerleft[1] ]; - } - } - - - //==================================RUN================================= - public void run() - { - if (!ready) return; - else ready = false; - - int[][][] r = map.getChunksCurrentRegion(); - for (int[][] xRow : r) - { - for (int[] chunk : xRow) - { - world.loadChunk(chunk[0], chunk[1], true); - world.unloadChunk(chunk[0], chunk[1]); - } - } - setNextRegion(); - - if (complete()) finish(); - else ready = true; - } -} diff --git a/src/iieLoadSaveEntireWorld/Main.java b/src/iieLoadSaveEntireWorld/Main.java index 599359b..fdcc8d7 100644 --- a/src/iieLoadSaveEntireWorld/Main.java +++ b/src/iieLoadSaveEntireWorld/Main.java @@ -1,32 +1,13 @@ package iieLoadSaveEntireWorld; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitTask; public class Main extends JavaPlugin { - static Main plugin; - static FileConfiguration config; - static ConfigurationSection unfinished; - - static StartCommand start; - static StopCommand stop; - - static LoadSaveProcess process; - static BukkitTask task; - public void onEnable() { - plugin = this; - config = plugin.getConfig(); - unfinished = config.getConfigurationSection("unfinished worlds"); - start = new StartCommand(plugin); - stop = new StopCommand(plugin); - saveDefaultConfig(); - getCommand("beginloadsave").setExecutor(start); - getCommand("stoploadsave").setExecutor(stop); + getCommand("beginfullmapload").setExecutor(new TaskManager.StartCommand()); + getCommand("stopfullmapload").setExecutor(new TaskManager.StopCommand()); } } diff --git a/src/iieLoadSaveEntireWorld/StartCommand.java b/src/iieLoadSaveEntireWorld/StartCommand.java deleted file mode 100644 index b771af2..0000000 --- a/src/iieLoadSaveEntireWorld/StartCommand.java +++ /dev/null @@ -1,39 +0,0 @@ -package iieLoadSaveEntireWorld; - - -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class StartCommand implements CommandExecutor { - - private static Main plugin; - StartCommand(Main Plugin){ - plugin = Plugin; - } - @Override - public synchronized boolean onCommand(CommandSender sender, Command label, String command, String[] args) - { - String worldname = ((Player)sender).getWorld().getName(); - if (LoadSaveProcess.inProgress) - { - sender.sendMessage("a process is already running (" + worldname + "). /StopLoadSave to stop."); - return false; - } - else LoadSaveProcess.inProgress = true; - if (ConfigManager.isNew(worldname)) - { - sender.sendMessage("starting..."); - LoadSaveProcess.start(worldname, Dimensions.generate(args)); - } - else - { - sender.sendMessage("resuming..."); - LoadSaveProcess.resume(worldname); - } - Main.task = Bukkit.getScheduler().runTaskTimer( plugin, Main.process, 0, 100 ); - return true; - } -} diff --git a/src/iieLoadSaveEntireWorld/StopCommand.java b/src/iieLoadSaveEntireWorld/StopCommand.java deleted file mode 100644 index 6206391..0000000 --- a/src/iieLoadSaveEntireWorld/StopCommand.java +++ /dev/null @@ -1,23 +0,0 @@ -package iieLoadSaveEntireWorld; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; - -public class StopCommand implements CommandExecutor{ - - private static Main plugin; - StopCommand(Main plugin){ - this.plugin = plugin; - } - @Override - public boolean onCommand(CommandSender sender, Command label, String command, String[] args) { - if (LoadSaveProcess.inProgress) - { - LoadSaveProcess.stop(); - return true; - } - else - return false; - } -} diff --git a/src/iieLoadSaveEntireWorld/TaskManager.java b/src/iieLoadSaveEntireWorld/TaskManager.java new file mode 100644 index 0000000..88bb32f --- /dev/null +++ b/src/iieLoadSaveEntireWorld/TaskManager.java @@ -0,0 +1,107 @@ +package iieLoadSaveEntireWorld; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; + +public class TaskManager { + + private static final Main plugin = new Main(); + + static boolean inProgress = false; + static LoadProcess loadProcess; + static ConfigProcess configProcess; + static BukkitTask loadTask; + static BukkitTask configTask; + + //===================================CONTROLS=================================== + private static final void start(LoadProcess loadProcess) + { + inProgress = true; + TaskManager.loadProcess = loadProcess; + TaskManager.loadTask = Bukkit.getScheduler().runTaskTimer( plugin, loadProcess, 0, 10 ); + TaskManager.configTask = Bukkit.getScheduler().runTaskTimer( plugin, new ConfigProcess(), 0, 200 ); + } + static final void finish() + { + configProcess.finish(); + loadTask.cancel(); + configTask.cancel(); + + loadProcess = null; + configProcess = null; + loadTask = null; + configTask = null; + + inProgress = false; + } + static final boolean stop() + { + if (inProgress) + { + if (loadProcess.isFinished()) finish(); + else + { + loadTask.cancel(); + configTask.cancel(); + configProcess.run(); + + loadProcess = null; + configProcess = null; + loadTask = null; + configTask = null; + + inProgress = false; + } + return true; + } + return false; + } + + + //===================================COMMANDS=================================== + + static final class StartCommand implements CommandExecutor + { + @Override + public final boolean onCommand(CommandSender sender, Command label, String command, String[] args) + { + String name = ((Player)sender).getWorld().getName(); + if (inProgress) + { + sender.sendMessage("a process is already running (" + name + "). /StopLoadSave to stop."); + return false; + } + else inProgress = true; + + if (ConfigProcess.isNew(name)) + { + sender.sendMessage("starting..."); + start(new LoadProcess(name, WorldObject.generate(args))); + } + else + { + sender.sendMessage("resuming..."); + start(new LoadProcess(name)); + } + return true; + } + } + static final class StopCommand implements CommandExecutor + { + @Override + public final boolean onCommand(CommandSender sender, Command label, String command, String[] args) + { + if (stop()) + { + sender.sendMessage("stopped."); + return true; + } + sender.sendMessage("nothing to stop."); + return false; + } + } +} diff --git a/src/iieLoadSaveEntireWorld/WorldObject.java b/src/iieLoadSaveEntireWorld/WorldObject.java index 8741736..9380c0c 100644 --- a/src/iieLoadSaveEntireWorld/WorldObject.java +++ b/src/iieLoadSaveEntireWorld/WorldObject.java @@ -4,23 +4,18 @@ public class WorldObject { final int width; final int[] lowerleft; - - int[] current = new int[] { -1, -1 }; - int n = 1; - int c = 1; - int D = 1; - int d = 0; - boolean B = false; + int[] current; + int n; + int c; + int D; + int d; + boolean B; WorldObject() { width = 44; lowerleft = new int[] { -22, -22 }; - } - WorldObject(int width) - { - this.width = width; - lowerleft = new int[] { -22, -22 }; + current = new int[] { -1, -1 }; } WorldObject(int width, int[] lowerleft, int[] center) { @@ -28,8 +23,10 @@ public class WorldObject { this.lowerleft = lowerleft; this.current = center; } - WorldObject(int width, int[] lowerleft, - int[] current, int n, int c, int D, int d, boolean B) + WorldObject( + int width, int[] lowerleft, int[] current, + int n, int c, int D, int d, boolean B + ) { this.width = width; this.lowerleft = lowerleft; @@ -40,4 +37,172 @@ public class WorldObject { this.D = d; this.B = B; } + + static final WorldObject generate(String[] args) + { + if (args.length == 0) + { + return new WorldObject(); + } + int[] bounds = regionBounds(new ParsedArgs(args)); + + return new WorldObject( + bounds[2] - bounds[0], + new int[]{ bounds[0], bounds[2] }, + new int[]{ + bounds[0] - 1 + (bounds[1]-bounds[0] + 1)/2, + bounds[2] - 1 + (bounds[3]-bounds[2] + 1)/2 + } + ); + } + //============================================================================== + private static final class ParsedArgs + { + private static final int defaultRadius = 11264; + private static final int[] defaultCenter = new int[]{0,0}; + + final int radius; + final int[] center; + ParsedArgs (String[] args) + { + if (isInt(args[0]) && args[0] != "0") + { + radius = Integer.parseInt(args[0]); + } + else + { + radius = defaultRadius; + } + if (args.length > 2 && isInt(args[1]) && isInt(args[2])) + { + center = new int[]{ Integer.parseInt(args[1]), + Integer.parseInt(args[2]) }; + } + else + { + center = defaultCenter; + } + } + private static final boolean isInt(String arg) + { + int length = arg.length(); + int i = 0; + if (arg.charAt(0) == '-') + { + if (length == 1) return false; + else i = 1; + } + for (; i < length; i++) + { + char c = arg.charAt(i); + if (c < '0' || c > '9') + return false; + } + return true; + } + } + //============================================================================== + private static final int[] regionBounds(ParsedArgs a) + { + int[] bounds = new int[] + { + // [ get region ] [ get block ] + Math.floorDiv( a.center[0] - a.radius, 512 ), + Math.floorDiv( a.center[0] + a.radius, 512 ), + Math.floorDiv( a.center[1] - a.radius, 512 ), + Math.floorDiv( a.center[1] + a.radius, 512 ) + }; + + //add margins------------ + + final int[] edges = new int[4]; + final int[] radii = new int[4]; + final boolean[] margin = new boolean[4]; + + //get block edge farthest from center + edges[0] = bounds[0] *512; + edges[0] = (bounds[1]+1) *512 - 1; + edges[0] = bounds[2] *512; + edges[0] = (bounds[3]+1) *512 - 1; + + //get radius from center to far block edge of region + radii[0] = Math.abs(a.center[0] - edges[0]); + radii[1] = Math.abs(a.center[0] - edges[1]); + radii[2] = Math.abs(a.center[1] - edges[2]); + radii[3] = Math.abs(a.center[1] - edges[3]); + + //compare to original block radius, if difference is < 4 chunks add a region width + if (radii[0] - a.radius < 64) { bounds[0] -= 1; margin[0] = true; } + if (radii[1] - a.radius < 64) { bounds[1] += 1; margin[1] = true; } + if (radii[2] - a.radius < 64) { bounds[2] -= 1; margin[2] = true; } + if (radii[3] - a.radius < 64) { bounds[3] += 1; margin[3] = true; } + + //resquare the selection + if (!margin[0]) + if (!margin[1]) + if (!margin[2]) + if (!margin[3])//-----------0000 + return bounds; + else//----------------------0001 + if (radii[0] < radii[1]) + bounds[0]++; + else + bounds[1]++; + else + if (!margin[3])//-----------0010 + if (radii[0] < radii[1]) + bounds[0]++; + else + bounds[1]++; + else//----------------------0011 + { + bounds[0]++; + bounds[1]++; + } + else + if (!margin[2]) + if (!margin[3])//-----------0100 + if (radii[2] < radii[3]) + bounds[2]++; + else + bounds[3]++; + else//----------------------0101 + return bounds; + else + if (!margin[3])//-----------0110 + return bounds; + else//----------------------0111 + bounds[0]++; + else + if (margin[1]) + if (margin[2]) + if (margin[3])//------------1111 + return bounds; + else//----------------------1110 + bounds[3]++; + else + if (margin[3])//------------1101 + bounds[2]++; + else//----------------------1100 + { + bounds[2]++; + bounds[3]++; + } + else + if (margin[2]) + if (margin[3])//------------1011 + bounds[1]++; + else//----------------------1010 + return bounds; + else + if (margin[3])//------------1001 + return bounds; + else//----------------------1000 + if (radii[2] == radii[3]) + bounds[0]++; + else + bounds[1]++; + + return bounds; + } } diff --git a/src/iieLoadSaveEntireWorld/CacheOld.java b/src/unused/CacheOld.java similarity index 97% rename from src/iieLoadSaveEntireWorld/CacheOld.java rename to src/unused/CacheOld.java index 0c0522f..2267c14 100644 --- a/src/iieLoadSaveEntireWorld/CacheOld.java +++ b/src/unused/CacheOld.java @@ -1,11 +1,15 @@ -package iieLoadSaveEntireWorld; +package unused; import java.util.HashMap; import java.util.Map; import java.util.Set; +import iieLoadSaveEntireWorld.Main; + public class CacheOld { +/* + private static int maxNameLength; private static char[][] listUnfinished; private static Map cacheUnfinished; @@ -115,4 +119,5 @@ public class CacheOld this.B = B; } } +*/ } diff --git a/src/iieLoadSaveEntireWorld/LoadSaveProcessOld.java b/src/unused/LoadSaveProcessOld.java similarity index 99% rename from src/iieLoadSaveEntireWorld/LoadSaveProcessOld.java rename to src/unused/LoadSaveProcessOld.java index 5b169a0..c141df4 100644 --- a/src/iieLoadSaveEntireWorld/LoadSaveProcessOld.java +++ b/src/unused/LoadSaveProcessOld.java @@ -1,4 +1,4 @@ -package iieLoadSaveEntireWorld; +package unused; import org.bukkit.scheduler.BukkitTask;