Added experimental block setting method.

This commit is contained in:
boy0001 2015-04-27 00:56:10 +10:00
parent 2c76c0608c
commit 27c2b08c2f
6 changed files with 79 additions and 184 deletions

View File

@ -21,12 +21,8 @@
package com.intellectualcrafters.plot.commands;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.SetBlockQueue;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
import com.intellectualcrafters.plot.uuid.DefaultUUIDWrapper;
import com.intellectualcrafters.plot.uuid.LowerOfflineUUIDWrapper;

View File

@ -66,11 +66,11 @@ public class AugmentedPopulator extends BlockPopulator {
}
}
private static short[][] x_loc;
private static short[][] y_loc;
private static short[][] z_loc;
public static short[][] x_loc;
public static short[][] y_loc;
public static short[][] z_loc;
public AugmentedPopulator(final String world, final PlotGenerator generator, final PlotCluster cluster, final boolean p, final boolean b) {
public static void initCache() {
if (x_loc == null) {
x_loc = new short[16][4096];
y_loc = new short[16][4096];
@ -88,7 +88,10 @@ public class AugmentedPopulator extends BlockPopulator {
}
}
}
}
public AugmentedPopulator(final String world, final PlotGenerator generator, final PlotCluster cluster, final boolean p, final boolean b) {
initCache();
this.cluster = cluster;
this.generator = generator;
this.plotworld = PlotSquared.getPlotWorld(world);

View File

@ -6,9 +6,11 @@ import java.util.List;
import com.intellectualcrafters.plot.object.ChunkLoc;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotLoc;
import com.intellectualcrafters.plot.object.RegionWrapper;
import com.intellectualcrafters.plot.util.SetBlockQueue.ChunkWrapper;
public abstract class ChunkManager {
@ -25,6 +27,8 @@ public abstract class ChunkManager {
return new ChunkLoc(x, z);
}
public abstract void setChunk(ChunkWrapper loc, PlotBlock[][] result);
public abstract int[] countEntities(Plot plot);
public abstract boolean loadChunk(String world, ChunkLoc loc);

View File

@ -6,12 +6,13 @@ import java.util.Iterator;
import java.util.Map.Entry;
import com.intellectualcrafters.plot.PlotSquared;
import com.intellectualcrafters.plot.generator.AugmentedPopulator;
import com.intellectualcrafters.plot.object.PlotBlock;
public class SetBlockQueue {
private volatile static HashMap<ChunkWrapper, PlotBlock[][]> blocks;
private volatile static int allocate = 50;
private volatile static int allocate = 20;
private volatile static boolean running = false;
private volatile static boolean locked = false;
private volatile static HashSet<Runnable> runnables;
@ -31,6 +32,9 @@ public class SetBlockQueue {
public synchronized static void init() {
if (blocks == null) {
if (AugmentedPopulator.x_loc == null) {
AugmentedPopulator.initCache();
}
blocks = new HashMap<>();
runnables = new HashSet<>();
}
@ -66,16 +70,16 @@ public class SetBlockQueue {
int Z = chunk.z << 4;
PlotBlock[][] blocks = n.getValue();
String world = chunk.world;
// ChunkManager.manager.setChunk(chunk, blocks);
for (int j = 0; j < blocks.length; j++) {
PlotBlock[] blocksj = blocks[j];
if (blocksj != null) {
for (int k = 0; k < blocksj.length; k++) {
PlotBlock block = blocksj[k];
if (block != null) {
final int y = (j << 4) + (k >> 8);
final int a = (k - ((y & 0xF) << 8));
final int z = (a >> 4);
final int x = a - (z << 4);
int x = AugmentedPopulator.x_loc[j][k];
int y = AugmentedPopulator.y_loc[j][k];
int z = AugmentedPopulator.z_loc[j][k];
BlockManager.manager.functionSetBlock(world, X + x, y, Z + z, block.id, block.data);
}
}
@ -83,7 +87,7 @@ public class SetBlockQueue {
}
}
}
}, 5);
}, 2);
TaskManager.tasks.put(current, task);
running = true;
}
@ -146,9 +150,9 @@ public class SetBlockQueue {
}
public static class ChunkWrapper {
final int x;
final int z;
final String world;
public final int x;
public final int z;
public final String world;
public ChunkWrapper(String world, int x, int z) {
this.world = world;

View File

@ -44,13 +44,16 @@ import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import com.intellectualcrafters.plot.util.SetBlockQueue.ChunkWrapper;
import com.intellectualcrafters.plot.BukkitMain;
import com.intellectualcrafters.plot.PlotSquared;
import com.intellectualcrafters.plot.generator.AugmentedPopulator;
import com.intellectualcrafters.plot.listeners.APlotListener;
import com.intellectualcrafters.plot.object.BlockLoc;
import com.intellectualcrafters.plot.object.ChunkLoc;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotLoc;
import com.intellectualcrafters.plot.object.PlotPlayer;
@ -974,5 +977,56 @@ public class BukkitChunkManager extends ChunkManager {
count[4]++;
}
}
@Override
public void setChunk(ChunkWrapper loc, PlotBlock[][] blocks) {
CURRENT_PLOT_CLEAR = new RegionWrapper(0,0,0,0);
World world = Bukkit.getWorld(loc.world);
Chunk chunk = world.getChunkAt(loc.x, loc.z);
final int cx = chunk.getX();
final int cz = chunk.getZ();
if (!chunk.isLoaded()) {
chunk.load(true);
}
initMaps();
final int absX = cx << 4;
final int absZ = cz << 4;
boolean save = false;
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
saveBlocks(world, 255, absX + x, absZ + z);
PlotLoc pl = new PlotLoc(absX + x, absZ + z);
HashMap<Short, Short> ids = GENERATE_BLOCKS.get(pl);
HashMap<Short, Short> datas = GENERATE_BLOCKS.get(pl);
for (int i = 0; i < blocks.length; i++) {
if (blocks[i] != null) {
short y0 = (short) (i << 4);
for (short y = y0; y < y0 + 16; y++) {
int j = ((y & 0xF) << 8) | (z << 4) | x;
PlotBlock block = blocks[i][j];
if (block != null) {
ids.put(y, block.id);
if (block.data != 0) {
datas.put(y, block.id);
}
}
}
}
}
}
}
if (save) {
saveEntitiesOut(chunk, CURRENT_PLOT_CLEAR);
}
world.regenerateChunk(cx, cz);
if (save) {
restoreBlocks(world, 0, 0);
restoreEntities(world, 0, 0);
}
MainUtil.update(world.getName(), new ChunkLoc(chunk.getX(), chunk.getZ()));
BukkitSetBlockManager.setBlockManager.update(Arrays.asList(new Chunk[] { chunk }));
CURRENT_PLOT_CLEAR = null;
}
}

View File

@ -1,166 +0,0 @@
package com.intellectualcrafters.plot.util.bukkit;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.World;
import com.intellectualcrafters.plot.util.TaskManager;
public class DebugSetBlockQueue {
private volatile static HashMap<ChunkWrapper, short[][]> blocks;
private volatile static int allocate = 50;
private volatile static boolean running = false;
private volatile static boolean locked = false;
private volatile static HashSet<Runnable> runnables;
public synchronized static void allocate(int t) {
allocate = t;
}
public synchronized static void addNotify(Runnable whenDone) {
if (runnables == null) {
TaskManager.runTask(whenDone);
}
else {
runnables.add(whenDone);
}
}
public synchronized static void init() {
if (blocks == null) {
blocks = new HashMap<>();
runnables = new HashSet<>();
}
if (!running) {
TaskManager.index.increment();
final int current = TaskManager.index.intValue();
int task = TaskManager.runTaskRepeat(new Runnable() {
@Override
public void run() {
if (locked) {
return;
}
if (blocks.size() == 0) {
Bukkit.getScheduler().cancelTask(TaskManager.tasks.get(current));
for (Runnable runnable : runnables) {
TaskManager.runTask(runnable);
}
runnables = null;
blocks = null;
running = false;
return;
}
long start = System.currentTimeMillis() + allocate;
Iterator<Entry<ChunkWrapper, short[][]>> i = blocks.entrySet().iterator();
while (System.currentTimeMillis() < start && i.hasNext()) {
if (locked) {
return;
}
Entry<ChunkWrapper, short[][]> n = i.next();
i.remove();
ChunkWrapper chunk = n.getKey();
int X = chunk.x << 4;
int Z = chunk.z << 4;
short[][] blocks = n.getValue();
World world = chunk.world;
for (int j = 0; j < blocks.length; j++) {
short[] blocksj = blocks[j];
if (blocksj != null) {
for (int k = 0; k < blocksj.length; k++) {
short id = blocksj[k];
if (id != 0) {
final int y = (j << 4) + (k >> 8);
final int a = (k - ((y & 0xF) << 8));
final int z = (a >> 4);
final int x = a - (z << 4);
BukkitSetBlockManager.setBlockManager.set(world, X + x, y, Z + z, id, (byte) 0);
}
}
}
}
}
}
}, 5);
TaskManager.tasks.put(current, task);
running = true;
}
}
public static void setBlock(final World world, int x, final int y, int z, final short blkid) {
locked = true;
if (!running) {
init();
}
int X = x >> 4;
int Z = z >> 4;
x -= X << 4;
z -= Z << 4;
ChunkWrapper wrap = new ChunkWrapper(world, X, Z);
short[][] result = blocks.get(wrap);
if (!blocks.containsKey(wrap)) {
result = new short[16][];
blocks.put(wrap, result);
}
if (result[y >> 4] == null) {
result[y >> 4] = new short[4096];
}
result[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = blkid;
locked = false;
}
public static class ChunkWrapper {
final int x;
final int z;
final World world;
public ChunkWrapper(World world, int x, int z) {
this.world = world;
this.x = x;
this.z = z;
}
@Override
public int hashCode() {
int result;
if (this.x >= 0) {
if (this.z >= 0) {
result = (this.x * this.x) + (3 * this.x) + (2 * this.x * this.z) + this.z + (this.z * this.z);
} else {
final int y1 = -this.z;
result = (this.x * this.x) + (3 * this.x) + (2 * this.x * y1) + y1 + (y1 * y1) + 1;
}
} else {
final int x1 = -this.x;
if (this.z >= 0) {
result = -((x1 * x1) + (3 * x1) + (2 * x1 * this.z) + this.z + (this.z * this.z));
} else {
final int y1 = -this.z;
result = -((x1 * x1) + (3 * x1) + (2 * x1 * y1) + y1 + (y1 * y1) + 1);
}
}
result = result * 31 + world.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ChunkWrapper other = (ChunkWrapper) obj;
return ((this.x == other.x) && (this.z == other.z) && (this.world.equals(other.world)));
}
}
}