Fix multiple schematic issues (which I caused)

This commit is contained in:
boy0001
2015-07-26 04:12:35 +10:00
parent c2fff8cbd7
commit b76899c6e2
9 changed files with 211 additions and 109 deletions

View File

@ -1,5 +1,6 @@
package com.intellectualcrafters.plot.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -10,7 +11,9 @@ 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.object.RunnableVal;
import com.intellectualcrafters.plot.util.SetBlockQueue.ChunkWrapper;
import com.intellectualcrafters.plot.util.bukkit.BukkitUtil;
public abstract class ChunkManager {
@ -27,6 +30,72 @@ public abstract class ChunkManager {
return new ChunkLoc(x, z);
}
/**
* The int[] will be in the form: [chunkx, chunkz, pos1x, pos1z, pos2x, pos2z, isedge] and will represent the bottom and top parts of the chunk
* @param pos1
* @param pos2
* @param task
* @param whenDone
*/
public static void chunkTask(Location pos1, Location pos2, final RunnableVal<int[]> task, final Runnable whenDone, final int allocate) {
final int p1x = pos1.getX();
final int p1z = pos1.getZ();
final int p2x = pos2.getX();
final int p2z = pos2.getZ();
final int bcx = p1x >> 4;
final int bcz = p1z >> 4;
final int tcx = p2x >> 4;
final int tcz = p2z >> 4;
final ArrayList<ChunkLoc> chunks = new ArrayList<ChunkLoc>();
for (int x = bcx; x <= tcx; x++) {
for (int z = bcz; z <= tcz; z++) {
chunks.add(new ChunkLoc(x, z));
}
}
TaskManager.runTask(new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
while (chunks.size() > 0 && System.currentTimeMillis() - start < allocate) {
ChunkLoc chunk = chunks.remove(0);
task.value = new int[7];
task.value[0] = chunk.x;
task.value[1] = chunk.z;
task.value[2] = task.value[0] << 4;
task.value[3] = task.value[1] << 4;
task.value[4] = task.value[2] + 15;
task.value[5] = task.value[3] + 15;
if (task.value[0] == bcx) {
task.value[2] = p1x;
task.value[6] = 1;
}
if (task.value[0] == tcx) {
task.value[4] = p2x;
task.value[6] = 1;
}
if (task.value[1] == bcz) {
task.value[3] = p1z;
task.value[6] = 1;
}
if (task.value[1] == tcz) {
task.value[5] = p2z;
task.value[6] = 1;
}
task.run();
}
if (chunks.size() != 0) {
TaskManager.runTaskLater(this, 1);
}
else {
TaskManager.runTask(whenDone);
}
}
});
}
public abstract void setChunk(ChunkWrapper loc, PlotBlock[][] result);
public abstract int[] countEntities(Plot plot);

View File

@ -44,6 +44,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotSettings;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.PseudoRandom;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.util.bukkit.BukkitUtil;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
@ -957,7 +958,6 @@ public class MainUtil {
final Runnable run = new Runnable() {
@Override
public void run() {
// MainUtil.setBiome(world, plot, "FOREST");
runners.remove(plot);
TaskManager.runTask(whenDone);
}
@ -1043,15 +1043,25 @@ public class MainUtil {
}
}
public static void setBiome(final Plot plot, final String biome) {
final int bottomX = getPlotBottomLoc(plot.world, plot.id).getX() + 1;
final int topX = getPlotTopLoc(plot.world, plot.id).getX();
final int bottomZ = getPlotBottomLoc(plot.world, plot.id).getZ() + 1;
final int topZ = getPlotTopLoc(plot.world, plot.id).getZ();
BukkitUtil.setBiome(plot.world, bottomX, bottomZ, topX, topZ, biome);
update(plot);
public static void setBiome(final Plot plot, final String biome, final Runnable whenDone) {
Location pos1 = plot.getBottom().add(1, 0, 1);
Location pos2 = plot.getTop();
ChunkManager.chunkTask(pos1, pos2, new RunnableVal<int[]>() {
@Override
public void run() {
BukkitUtil.loadChunkAt(plot.world, value[0], value[1], false);
BukkitUtil.setBiome(plot.world, value[2], value[3], value[4], value[4], biome);
BukkitUtil.unloadChunkAt(plot.world, value[0], value[1], true, true);
}
}, new Runnable() {
@Override
public void run() {
update(plot);
TaskManager.runTask(whenDone);
}
}, 5);
}
public static int getHeighestBlock(final String world, final int x, final int z) {
final int result = BukkitUtil.getHeighestBlock(world, x, z);
if (result == 0) {

View File

@ -166,7 +166,6 @@ public abstract class SchematicHandler {
final int WIDTH = demensions.getX();
final int LENGTH = demensions.getZ();
final int HEIGHT = demensions.getY();
// Validate dimensions
Location bottom = plot.getBottom();
Location top = plot.getTop();
@ -175,21 +174,20 @@ public abstract class SchematicHandler {
TaskManager.runTask(whenDone);
return;
}
final byte[] ids = schematic.ids;
// block id and data arrays
final short[] ids = schematic.ids;
final byte[] datas = schematic.datas;
// Calculate the optimal height to paste the schematic at
final int y_offset;
if (HEIGHT >= 256) {
y_offset = 0;
}
else {
y_offset = BukkitUtil.getMaxHeight(plot.world);
y_offset = BukkitUtil.getHeighestBlock(plot.world, bottom.getX() + 1, bottom.getZ() + 1);
}
Location pos1 = MainUtil.getPlotBottomLoc(plot.world, plot.id).add(1 + x_offset, y_offset - 1, 1 + z_offset);
Location pos2 = pos1.clone().add(WIDTH - 1, HEIGHT - 1, LENGTH - 1);
// TODO switch to ChunkManager.chunkTask(pos1, pos2, task, whenDone, allocate);
final int p1x = pos1.getX();
final int p1z = pos1.getZ();
final int p2x = pos2.getX();
@ -198,15 +196,12 @@ public abstract class SchematicHandler {
final int bcz = p1z >> 4;
final int tcx = p2x >> 4;
final int tcz = p2z >> 4;
final ArrayList<ChunkLoc> chunks = new ArrayList<ChunkLoc>();
for (int x = bcx; x <= tcx; x++) {
for (int z = bcz; z <= tcz; z++) {
chunks.add(new ChunkLoc(x, z));
}
}
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
@ -235,7 +230,12 @@ public abstract class SchematicHandler {
// Paste schematic here
int id;
for (int ry = 0; ry < Math.max(256, HEIGHT); ry++) {
for (int ry = 0; ry < Math.min(256, HEIGHT); ry++) {
int yy = y_offset + ry;
if (yy > 255) {
System.out.print("TOO HIGH: " + ry);
continue;
}
int i1 = ry * WIDTH * LENGTH;
for (int rz = zzb - p1z; rz <= zzt - p1z; rz++) {
int i2 = rz * WIDTH + i1;
@ -244,7 +244,6 @@ public abstract class SchematicHandler {
int xx = p1x + rx;
int zz = p1z + rz;
int yy = y_offset + ry;
id = ids[i];
@ -324,6 +323,7 @@ public abstract class SchematicHandler {
break;
}
default: {
System.out.print(id +","+datas[i]);
SetBlockQueue.setBlock(plot.world, xx, yy, zz, new PlotBlock((short) id, (byte) datas[i]));
break;
}
@ -411,9 +411,20 @@ public abstract class SchematicHandler {
final short width = ShortTag.class.cast(tagMap.get("Width")).getValue();
final short length = ShortTag.class.cast(tagMap.get("Length")).getValue();
final short height = ShortTag.class.cast(tagMap.get("Height")).getValue();
final byte[] block = ByteArrayTag.class.cast(tagMap.get("Blocks")).getValue();
final byte[] block_sml = ByteArrayTag.class.cast(tagMap.get("Blocks")).getValue();
final byte[] data = ByteArrayTag.class.cast(tagMap.get("Data")).getValue();
final short[] block = new short[block_sml.length];
for (int i = 0; i < block.length; i++) {
short id = block_sml[i];
if (id < 0) {
id = (short) (id & 0xFF);
}
block[i] = id;
}
// Slow + has code for exceptions (addId) inside the loop rather than outside
// for (int index = 0; index < b.length; index++) {
// if ((index >> 1) >= addId.length) {
@ -433,6 +444,8 @@ public abstract class SchematicHandler {
// }
// Schematic schem = new Schematic(collection, dimension, file);
System.out.print(width + "," + height +"," + length);
Dimension dimensions = new Dimension(width, height, length);
Schematic schem = new Schematic(block, data, dimensions);
@ -747,7 +760,7 @@ public abstract class SchematicHandler {
*/
public class Schematic {
// Lossy but fast
private final byte[] ids;
private final short[] ids;
private final byte[] datas;
@Deprecated
@ -766,7 +779,7 @@ public abstract class SchematicHandler {
*/
@Deprecated
public Schematic(final DataCollection[] blockCollection, final Dimension schematicDimension) {
ids = new byte[blockCollection.length];
ids = new short[blockCollection.length];
datas = new byte[blockCollection.length];
for (int i = 0; i < blockCollection.length; i++) {
DataCollection block = blockCollection[i];
@ -777,7 +790,7 @@ public abstract class SchematicHandler {
this.schematicDimension = schematicDimension;
}
public Schematic(final byte[] i, final byte[] b, final Dimension d) {
public Schematic(final short[] i, final byte[] b, final Dimension d) {
ids = i;
datas = b;
schematicDimension = d;
@ -814,7 +827,7 @@ public abstract class SchematicHandler {
* Get the block id array
* @return
*/
public byte[] getIds() {
public short[] getIds() {
return ids;
}

View File

@ -112,6 +112,16 @@ public class BukkitUtil extends BlockManager {
return getWorld(world).getHighestBlockYAt(x, z);
}
public static void unloadChunkAt(String worldname, int X, int Z, boolean save, boolean safe) {
final World world = getWorld(worldname);
world.unloadChunk(X, Z, save, safe);
}
public static void loadChunkAt(final String worldname, int X, int Z, boolean force) {
final World world = getWorld(worldname);
world.loadChunk(X, Z, force);
}
public static Chunk getChunkAt(final String worldname, final int x, final int z) {
final World world = getWorld(worldname);
return world.getChunkAt(x, z);
@ -156,13 +166,10 @@ public class BukkitUtil extends BlockManager {
final World world = getWorld(worldname);
for (int x = pos1_x; x <= pos2_x; x++) {
for (int z = pos1_z; z <= pos2_z; z++) {
final Block blk = world.getBlockAt(x, 0, z);
final Biome c = blk.getBiome();
if (c.equals(b)) {
x += 15;
if (world.getBiome(x, z) == b) {
continue;
}
blk.setBiome(b);
world.setBiome(x, z, b);
}
}
}