PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockQueue.java

277 lines
10 KiB
Java
Raw Normal View History

2015-04-21 14:48:18 +02:00
package com.intellectualcrafters.plot.util;
2015-07-05 17:44:10 +02:00
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.generator.AugmentedPopulator;
import com.intellectualcrafters.plot.object.ChunkLoc;
2015-04-21 14:48:18 +02:00
import com.intellectualcrafters.plot.object.PlotBlock;
public class SetBlockQueue {
private volatile static HashMap<ChunkWrapper, PlotBlock[][]> blocks;
2015-04-30 15:57:44 +02:00
private volatile static int allocate = 25;
2015-04-21 14:48:18 +02:00
private volatile static boolean running = false;
private volatile static boolean locked = false;
private volatile static HashSet<Runnable> runnables;
2015-05-04 15:53:24 +02:00
private volatile static boolean slow = false;
2015-07-03 13:21:21 +02:00
private static long last;
private static int lastInt = 0;
private static PlotBlock lastBlock = new PlotBlock((short) 0, (byte) 0);
2015-04-21 14:48:18 +02:00
public synchronized static void allocate(int t) {
allocate = t;
}
2015-04-30 15:57:44 +02:00
public static int getAllocate() {
return allocate;
}
2015-05-04 15:53:24 +02:00
public static void setSlow(boolean value) {
slow = value;
}
2015-04-21 14:48:18 +02:00
public synchronized static void addNotify(Runnable whenDone) {
if (runnables == null) {
TaskManager.runTask(whenDone);
2015-05-04 15:53:24 +02:00
slow = false;
locked = false;
2015-04-21 14:48:18 +02:00
}
else {
runnables.add(whenDone);
}
}
2015-07-03 13:21:21 +02:00
2015-04-21 14:48:18 +02:00
public synchronized static void init() {
if (blocks == null) {
if (AugmentedPopulator.x_loc == null) {
AugmentedPopulator.initCache();
}
2015-04-21 14:48:18 +02:00
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;
}
2015-07-17 05:48:23 +02:00
if (blocks == null || blocks.size() == 0) {
PS.get().TASK.cancelTask(TaskManager.tasks.get(current));
2015-07-17 05:48:23 +02:00
if (runnables != null) {
for (Runnable runnable : runnables) {
TaskManager.runTask(runnable);
}
2015-04-21 14:48:18 +02:00
}
2015-07-23 19:14:36 +02:00
lastBlock = null;
2015-04-21 14:48:18 +02:00
runnables = null;
2015-07-17 05:48:23 +02:00
blocks = new HashMap<>();
2015-04-21 14:48:18 +02:00
running = false;
2015-05-04 15:53:24 +02:00
slow = false;
2015-04-21 14:48:18 +02:00
return;
}
2015-05-04 15:53:24 +02:00
long newLast = System.currentTimeMillis();
last = Math.max(newLast - 100, last);
while (blocks.size() > 0 && (System.currentTimeMillis() - last < 100 + allocate)) {
2015-04-21 14:48:18 +02:00
if (locked) {
return;
}
2015-05-04 15:53:24 +02:00
Entry<ChunkWrapper, PlotBlock[][]> n = blocks.entrySet().iterator().next();
2015-04-21 14:48:18 +02:00
ChunkWrapper chunk = n.getKey();
2015-05-04 15:53:24 +02:00
PlotBlock[][] blocks = n.getValue();
2015-04-21 14:48:18 +02:00
int X = chunk.x << 4;
int Z = chunk.z << 4;
String world = chunk.world;
2015-05-04 15:53:24 +02:00
if (slow) {
boolean once = false;
for (int j = 0; j < blocks.length; j++) {
PlotBlock[] blocksj = blocks[j];
if (blocksj != null) {
long start = System.currentTimeMillis();
for (int k = 0; k < blocksj.length; k++) {
if (once && (System.currentTimeMillis() - start > allocate)) {
SetBlockQueue.blocks.put(n.getKey(), blocks);
return;
}
PlotBlock block = blocksj[k];
if (block != null) {
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);
blocks[j][k] = null;
once = true;
}
}
}
}
SetBlockQueue.blocks.remove(n.getKey());
return;
}
SetBlockQueue.blocks.remove(n.getKey());
2015-04-21 14:48:18 +02:00
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) {
int x = AugmentedPopulator.x_loc[j][k];
int y = AugmentedPopulator.y_loc[j][k];
int z = AugmentedPopulator.z_loc[j][k];
2015-04-21 14:48:18 +02:00
BlockManager.manager.functionSetBlock(world, X + x, y, Z + z, block.id, block.data);
}
}
}
}
}
}
}, 2);
2015-04-21 14:48:18 +02:00
TaskManager.tasks.put(current, task);
running = true;
}
}
public static void setChunk(final String world, ChunkLoc loc, PlotBlock[][] result) {
locked = true;
if (!running) {
init();
}
ChunkWrapper wrap = new ChunkWrapper(world, loc.x, loc.z);
blocks.put(wrap, result);
locked = false;
}
2015-07-03 13:21:21 +02:00
2015-04-21 14:48:18 +02:00
public static void setBlock(final String world, int x, final int y, int z, final PlotBlock block) {
locked = true;
if (!running) {
init();
}
int X = x >> 4;
int Z = z >> 4;
x -= X << 4;
z -= Z << 4;
2015-07-03 13:21:21 +02:00
2015-04-21 14:48:18 +02:00
ChunkWrapper wrap = new ChunkWrapper(world, X, Z);
2015-07-03 13:21:21 +02:00
PlotBlock[][] result;
2015-05-04 15:53:24 +02:00
result = blocks.get(wrap);
2015-04-21 14:48:18 +02:00
if (!blocks.containsKey(wrap)) {
result = new PlotBlock[16][];
blocks.put(wrap, result);
}
2015-07-03 13:21:21 +02:00
2015-04-21 14:48:18 +02:00
if (result[y >> 4] == null) {
result[y >> 4] = new PlotBlock[4096];
}
result[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = block;
locked = false;
}
public static void setData(final String world, int x, final int y, int z, final byte data) {
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);
PlotBlock[][] result;
result = blocks.get(wrap);
if (!blocks.containsKey(wrap)) {
result = new PlotBlock[16][];
blocks.put(wrap, result);
}
if (result[y >> 4] == null) {
result[y >> 4] = new PlotBlock[4096];
}
result[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = new PlotBlock((short) -1, data);
locked = false;
}
2015-04-21 14:48:18 +02:00
public static void setBlock(final String world, int x, final int y, int z, final int id) {
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);
2015-05-04 15:53:24 +02:00
PlotBlock[][] result;
result = blocks.get(wrap);
2015-04-21 14:48:18 +02:00
if (!blocks.containsKey(wrap)) {
result = new PlotBlock[16][];
blocks.put(wrap, result);
}
if (result[y >> 4] == null) {
result[y >> 4] = new PlotBlock[4096];
}
if (id == lastInt) {
result[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = lastBlock;
}
else {
lastInt = id;
lastBlock = new PlotBlock((short) id, (byte) 0);
}
result[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = lastBlock;
locked = false;
}
public static class ChunkWrapper {
public final int x;
public final int z;
public final String world;
2015-04-21 14:48:18 +02:00
public ChunkWrapper(String 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)));
}
}
}