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.Set;
public class Cache {
public class CacheOld
{
private static int maxNameLength;
private static char[][] listUnfinished;
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()
{
maxNameLength = Main.config.getInt("max name length");
Set<String> 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<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;
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;
}
}

View file

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

View file

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

View file

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

View file

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

View file

@ -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();

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