almost done

This commit is contained in:
BuildTools 2016-11-25 15:58:35 -05:00
parent fb3f2a1cb6
commit 6652583ca7
9 changed files with 316 additions and 255 deletions

View file

@ -4,94 +4,21 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
public class Cache { public class CacheOld
{
private static int maxNameLength; private static int maxNameLength;
private static char[][] listUnfinished; private static char[][] listUnfinished;
private static Map<String,WorldStatus> cacheUnfinished; private static Map<String,WorldStatus> 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<String> 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<String> set)
{
String path;
int width;
int[] lowerleft;
int[] current;
int n; int c; int D; int d; boolean B;
for (String name : set){
path = "unfinishedWorlds." + name + ".";
width = Main.config.getInt(path + "width");
lowerleft = new int[]{
Main.config.getInt(path + "lowerleft.x"),
Main.config.getInt(path + "lowerleft.z")
};
current = new int[]{
Main.config.getInt(path + "currentRegion.x"),
Main.config.getInt(path + "currentRegion.z")
};
n = Main.config.getInt(path + "n");
c = Main.config.getInt(path + "c");
D = Main.config.getInt(path + "D");
d = Main.config.getInt(path + "d");
B = Main.config.getBoolean(path + "B");
cacheUnfinished.put(name, new WorldStatus( width,lowerleft,current,n,c,D,d,B ));
}
}
//================================PUBLIC METHODS================================
static void generate() static void generate()
{ {
maxNameLength = Main.config.getInt("max name length"); maxNameLength = Main.config.getInt("max name length");
Set<String> set = Main.unfinished.getKeys(false); Set<String> set = Main.unfinished.getKeys(false);
populateList(set); populateList(set);
populateCache(set); populateCache(set);
}
static void addWorld()
{
} }
static void saveProgress() static void saveProgress()
{ {
@ -120,4 +47,72 @@ public class Cache {
} }
return match; return match;
} }
private static void populateList(Set<String> 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<String> 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;
}
}
} }

View file

@ -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
}
}

View file

@ -1,186 +1,166 @@
package iieLoadSaveEntireWorld; 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} }; static WorldObject generate(String[] args)
int width; {
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; int[] center;
int[] lowerleft; ParsedArgs (String[] args)
{
if (isInt(args[0]) && args[0] != "0")
radius = Integer.parseInt(args[0]);
else radius = defaultRadius;
Dimensions(String[] args){ if (args.length > 2 && isInt(args[1]) && isInt(args[2]))
int length = args.length; center = new int[]{ Integer.parseInt(args[1]), Integer.parseInt(args[2]) };
if (length == 0) else center = defaultCenter;
{
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 = new int[]
{
regionBounds[0],
regionBounds[2]
};
}
private static boolean isInt(String arg) private static boolean isInt(String arg)
{ {
int length = arg.length(); int length = arg.length();
int i = 0; int i = 0;
if (arg.charAt(0) == '-') if (arg.charAt(0) == '-')
{
if (length == 1) return false; if (length == 1) return false;
else i = 1; else i = 1;
for (; i < length; i++) { }
for (; i < length; i++)
{
char c = arg.charAt(i); char c = arg.charAt(i);
if (c < '0' || c > '9') if (c < '0' || c > '9')
return false; return false;
} }
return true; 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[] regionBounds(int radius, int[] center)
}
private static int[] blockCenter(String arg1, String arg2)
{ {
int xBlock = isInt(arg1) ? Integer.parseInt(arg1) : defaultDimensions[0][1]; int[] bounds = new int[]
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; // [ get region ] [ get block ]
int xMaxBlock = center[0] + blockRadius; Math.floorDiv( center[0] - radius, 512 ),
int zMinBlock = center[1] - blockRadius; Math.floorDiv( center[0] + radius, 512 ),
int zMaxBlock = center[1] + blockRadius; Math.floorDiv( center[1] - radius, 512 ),
return new int[] {xMinBlock,xMaxBlock,zMinBlock,zMaxBlock}; Math.floorDiv( center[1] + radius, 512 )
} };
private static int[] regionBounds(int[] blockBounds)
{ //add margins------------
int xMinRegion = Math.floorDiv(blockBounds[0],512);
int xMaxRegion = Math.floorDiv(blockBounds[1],512); int[] edges = new int[4];
int zMinRegion = Math.floorDiv(blockBounds[2],512); int[] radii = new int[4];
int zMaxRegion = Math.floorDiv(blockBounds[3],512); boolean[] margin = new boolean[4];
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 //get block edge farthest from center
int xMinRegionBlockEdge = regionBounds[0] *512; edges[0] = bounds[0] *512;
int xMaxRegionBlockEdge = (regionBounds[1]+1) *512 - 1; edges[0] = (bounds[1]+1) *512 - 1;
int zMinRegionBlockEdge = regionBounds[2] *512; edges[0] = bounds[2] *512;
int zMaxRegionBlockEdge = (regionBounds[3]+1) *512 - 1; edges[0] = (bounds[3]+1) *512 - 1;
//get edge's block distance from center //get radius from center to far block edge of region
radii[0] = Math.abs(blockCenter[0] - xMinRegionBlockEdge); radii[0] = Math.abs(center[0] - edges[0]);
radii[1] = Math.abs(blockCenter[0] - xMaxRegionBlockEdge); radii[1] = Math.abs(center[0] - edges[1]);
radii[2] = Math.abs(blockCenter[1] - zMinRegionBlockEdge); radii[2] = Math.abs(center[1] - edges[2]);
radii[3] = Math.abs(blockCenter[1] - zMaxRegionBlockEdge); radii[3] = Math.abs(center[1] - edges[3]);
//compare to original block radius, if difference is < 4 chunks add a region width //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[0] - radius < 64) { bounds[0] -= 1; margin[0] = true; }
if (radii[1] - blockRadius < 64) { regionBounds[1] += 1; marAdd[1] = true; } if (radii[1] - radius < 64) { bounds[1] += 1; margin[1] = true; }
if (radii[2] - blockRadius < 64) { regionBounds[2] -= 1; marAdd[2] = true; } if (radii[2] - radius < 64) { bounds[2] -= 1; margin[2] = true; }
if (radii[3] - blockRadius < 64) { regionBounds[3] += 1; marAdd[3] = true; } if (radii[3] - radius < 64) { bounds[3] += 1; margin[3] = true; }
//resquare the selection //resquare the selection
if (!marAdd[0]) if (!margin[0])
if (!marAdd[1]) if (!margin[1])
if (!marAdd[2]) if (!margin[2])
if (!marAdd[3])//-----------0000 if (!margin[3])//-----------0000
return regionBounds; return bounds;
else//----------------------0001 else//----------------------0001
if (radii[0] < radii[1]) if (radii[0] < radii[1])
regionBounds[0]++; bounds[0]++;
else else
regionBounds[1]++; bounds[1]++;
else else
if (!marAdd[3])//-----------0010 if (!margin[3])//-----------0010
if (radii[0] < radii[1]) if (radii[0] < radii[1])
regionBounds[0]++; bounds[0]++;
else else
regionBounds[1]++; bounds[1]++;
else//----------------------0011 else//----------------------0011
{ {
regionBounds[0]++; bounds[0]++;
regionBounds[1]++; bounds[1]++;
} }
else else
if (!marAdd[2]) if (!margin[2])
if (!marAdd[3])//-----------0100 if (!margin[3])//-----------0100
if (radii[2] < radii[3]) if (radii[2] < radii[3])
regionBounds[2]++; bounds[2]++;
else else
regionBounds[3]++; bounds[3]++;
else//----------------------0101 else//----------------------0101
return regionBounds; return bounds;
else else
if (!marAdd[3])//-----------0110 if (!margin[3])//-----------0110
return regionBounds; return bounds;
else//----------------------0111 else//----------------------0111
regionBounds[0]++; bounds[0]++;
else else
if (marAdd[1]) if (margin[1])
if (marAdd[2]) if (margin[2])
if (marAdd[3])//------------1111 if (margin[3])//------------1111
return regionBounds; return bounds;
else//----------------------1110 else//----------------------1110
regionBounds[3]++; bounds[3]++;
else else
if (marAdd[3])//------------1101 if (margin[3])//------------1101
regionBounds[2]++; bounds[2]++;
else//----------------------1100 else//----------------------1100
{ {
regionBounds[2]++; bounds[2]++;
regionBounds[3]++; bounds[3]++;
} }
else else
if (marAdd[2]) if (margin[2])
if (marAdd[3])//------------1011 if (margin[3])//------------1011
regionBounds[1]++; bounds[1]++;
else//----------------------1010 else//----------------------1010
return regionBounds; return bounds;
else else
if (marAdd[3])//------------1001 if (margin[3])//------------1001
return regionBounds; return bounds;
else//----------------------1000 else//----------------------1000
if (radii[2] == radii[3]) if (radii[2] == radii[3])
regionBounds[0]++; bounds[0]++;
else else
regionBounds[1]++; bounds[1]++;
return regionBounds; return bounds;
} }
} }

View file

@ -4,23 +4,22 @@ import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
public class LoadSaveProcess implements Runnable { public class LoadSaveProcess implements Runnable
{
//=================================INIT================================= //=================================INIT=================================
static boolean processOngoing = false;
static LoadSaveProcess process; static LoadSaveProcess process;
static BukkitTask bukkitTask; static BukkitTask bukkitTask;
static boolean inProgress = false;
boolean ready = true; boolean ready = true;
final Map map; final Map map;
final World world; final World world;
final String worldname; final String worldname;
final int totalRegions; final int totalRegions;
int[] currentRegion; int[] currentRegion;
LoadSaveProcess( LoadSaveProcess(String name, int width, int[] lowerleft, int[] center)
String name, int width, int[] center, int[] lowerleft
)
{ {
map = new Map(width, lowerleft); map = new Map(width, lowerleft);
world = Bukkit.getWorld(name); world = Bukkit.getWorld(name);
@ -30,8 +29,7 @@ public class LoadSaveProcess implements Runnable {
} }
LoadSaveProcess( LoadSaveProcess(
String name, int width, int[] lowerleft, 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); map = new Map(width, lowerleft);
world = Bukkit.getWorld(name); world = Bukkit.getWorld(name);
@ -47,7 +45,12 @@ public class LoadSaveProcess implements Runnable {
//===============================CONTROLS=============================== //===============================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(); bukkitTask.cancel();
process = null; process = null;
System.gc(); System.gc();
processOngoing = false; inProgress = false;
} }

View file

@ -2,7 +2,7 @@ package iieLoadSaveEntireWorld;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
public class UnusedPattern { public class LoadSaveProcessOld {
/* the contents of this class are static because /* the contents of this class are static because
* loading and saving an entire world is an intensive process, * loading and saving an entire world is an intensive process,

View file

@ -28,6 +28,5 @@ public class Main extends JavaPlugin {
saveDefaultConfig(); saveDefaultConfig();
getCommand("beginloadsave").setExecutor(start); getCommand("beginloadsave").setExecutor(start);
getCommand("stoploadsave").setExecutor(stop); getCommand("stoploadsave").setExecutor(stop);
Cache.generate();
} }
} }

View file

@ -1,5 +1,6 @@
package iieLoadSaveEntireWorld; package iieLoadSaveEntireWorld;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -15,27 +16,23 @@ public class StartCommand implements CommandExecutor {
@Override @Override
public synchronized boolean onCommand(CommandSender sender, Command label, String command, String[] args) public synchronized boolean onCommand(CommandSender sender, Command label, String command, String[] args)
{ {
String worldName = ((Player)sender).getWorld().getName(); String worldname = ((Player)sender).getWorld().getName();
if (LoadSaveProcess.inProgress) 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; return false;
} }
else LoadSaveProcess.inProgress = true; else LoadSaveProcess.inProgress = true;
if (ConfigManager.isNew(worldname))
if ((Cache.isUnfinished(worldName)))
{ {
sender.sendMessage("resuming..."); sender.sendMessage("starting...");
LoadSaveProcess.resume(worldName); LoadSaveProcess.start(worldname, Dimensions.generate(args));
} }
else else
{ {
Dimensions d = new Dimensions(args); sender.sendMessage("resuming...");
LoadSaveProcess.start( d.width, d.center, d.lowerleft, worldName ); LoadSaveProcess.resume(worldname);
} }
Main.task = Bukkit.getScheduler().runTaskTimer( plugin, Main.process, 0, 100 ); Main.task = Bukkit.getScheduler().runTaskTimer( plugin, Main.process, 0, 100 );
return true; return true;
} }

View file

@ -11,7 +11,7 @@ public class StopCommand implements CommandExecutor{
this.plugin = plugin; this.plugin = plugin;
} }
@Override @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) if (LoadSaveProcess.inProgress)
{ {
LoadSaveProcess.stop(); LoadSaveProcess.stop();

View file

@ -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;
}
}