Fix schematic creation

Seems fairly ugly, but only because the format requires the blocks be inputted in that exact order, and it prevents us from having to cache the blocks per chunk to then insert into the relevant tags.
This commit is contained in:
dordsor21 2018-12-21 00:49:15 +00:00
parent c53cd1ec91
commit 28f3be56b3
2 changed files with 243 additions and 258 deletions

View File

@ -1,7 +1,6 @@
package com.github.intellectualsites.plotsquared.bukkit.util; package com.github.intellectualsites.plotsquared.bukkit.util;
import com.github.intellectualsites.plotsquared.bukkit.object.schematic.StateWrapper; import com.github.intellectualsites.plotsquared.bukkit.object.schematic.StateWrapper;
import com.github.intellectualsites.plotsquared.plot.object.ChunkLoc;
import com.github.intellectualsites.plotsquared.plot.object.Location; import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.RegionWrapper; import com.github.intellectualsites.plotsquared.plot.object.RegionWrapper;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; import com.github.intellectualsites.plotsquared.plot.object.RunnableVal;
@ -13,12 +12,10 @@ import com.sk89q.jnbt.*;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BaseBlock;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.*; import java.util.*;
import java.util.stream.IntStream;
/** /**
* Schematic Handler. * Schematic Handler.
@ -39,9 +36,9 @@ public class BukkitSchematicHandler extends SchematicHandler {
new CuboidRegion(BukkitUtil.IMP.getWeWorld(world), bot.getBlockVector3(), new CuboidRegion(BukkitUtil.IMP.getWeWorld(world), bot.getBlockVector3(),
top.getBlockVector3()); top.getBlockVector3());
final int width = top.getX() - bot.getX() + 1; final int width = cuboidRegion.getWidth();
int height = top.getY() - bot.getY() + 1; int height = cuboidRegion.getHeight();
final int length = top.getZ() - bot.getZ() + 1; final int length = cuboidRegion.getLength();
Map<String, Tag> schematic = new HashMap<>(); Map<String, Tag> schematic = new HashMap<>();
schematic.put("Version", new IntTag(1)); schematic.put("Version", new IntTag(1));
@ -89,109 +86,106 @@ public class BukkitSchematicHandler extends SchematicHandler {
RegionWrapper region = queue.poll(); RegionWrapper region = queue.poll();
Location pos1 = new Location(world, region.minX, region.minY, region.minZ); Location pos1 = new Location(world, region.minX, region.minY, region.minZ);
Location pos2 = new Location(world, region.maxX, region.maxY, region.maxZ); Location pos2 = new Location(world, region.maxX, region.maxY, region.maxZ);
final int bx = bot.getX();
final int bz = bot.getZ();
final int p1x = pos1.getX(); final int p1x = pos1.getX();
final int p1z = pos1.getZ(); final int p1z = pos1.getZ();
final int p2x = pos2.getX(); final int p2x = pos2.getX();
final int p2z = pos2.getZ(); 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 int sy = pos1.getY(); final int sy = pos1.getY();
final int ey = pos2.getY(); final int ey = pos2.getY();
// Generate list of chunks Iterator<Integer> yiter = IntStream.range(sy, ey).iterator();
final ArrayList<ChunkLoc> chunks = new ArrayList<>();
for (int x = bcx; x <= tcx; x++) {
for (int z = bcz; z <= tcz; z++) {
chunks.add(new ChunkLoc(x, z));
}
}
final World worldObj = Bukkit.getWorld(world);
// Main thread
TaskManager.runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override public void run() { @Override public void run() {
long start = System.currentTimeMillis(); final Runnable yTask = this;
while (!chunks.isEmpty() long ystart = System.currentTimeMillis();
&& System.currentTimeMillis() - start < 20) { while (yiter.hasNext()
// save schematics && System.currentTimeMillis() - ystart < 20) {
ChunkLoc chunk = chunks.remove(0); final int fy = yiter.next();
Chunk bc = worldObj.getChunkAt(chunk.x, chunk.z); Iterator<Integer> ziter = IntStream.range(p1z, p2z).iterator();
if (!bc.load(false)) { TaskManager.runTask(new Runnable() {
continue; @Override public void run() {
} final Runnable zTask = this;
int X = chunk.x; long zstart = System.currentTimeMillis();
int Z = chunk.z; Iterator<Integer> xiter =
int xxb = X << 4; IntStream.range(p1x, p2x).iterator();
int zzb = Z << 4; while (ziter.hasNext()
int xxt = xxb + 15; && System.currentTimeMillis() - zstart < 20) {
int zzt = zzb + 15; final int fz = ziter.next();
TaskManager.runTask(new Runnable() {
@Override public void run() {
long xstart = System.currentTimeMillis();
while (xiter.hasNext()
&& System.currentTimeMillis() - xstart
< 20) {
final int x = xiter.next();
int rx = x - p1x;
int ry = fy - sy;
int rz = fz - p1z;
BlockVector3 point =
BlockVector3.at(x, fy, fz);
BaseBlock block =
cuboidRegion.getWorld()
.getFullBlock(point);
if (block.getNbtData() != null) {
Map<String, Tag> values =
new HashMap<>();
for (Map.Entry<String, Tag> entry : block
.getNbtData().getValue()
.entrySet()) {
values.put(entry.getKey(),
entry.getValue());
}
// Remove 'id' if it exists. We want 'Id'
values.remove("id");
if (X == bcx) { // Positions are kept in NBT, we don't want that.
xxb = p1x; values.remove("x");
} values.remove("y");
if (X == tcx) { values.remove("z");
xxt = p2x;
} values.put("Id", new StringTag(
if (Z == bcz) { block.getNbtId()));
zzb = p1z; values.put("Pos", new IntArrayTag(
} new int[] {rx, ry, rz}));
if (Z == tcz) {
zzt = p2z; tileEntities
} .add(new CompoundTag(values));
for (int y = sy; y <= Math.min(255, ey); y++) { }
int ry = y - sy; String blockKey =
for (int z = zzb; z <= zzt; z++) { block.toImmutableState()
int rz = z - bz; .getAsString();
for (int x = xxb; x <= xxt; x++) { int blockId;
int rx = x - bx; if (palette.containsKey(blockKey)) {
BlockVector3 point = BlockVector3.at(x, y, z); blockId = palette.get(blockKey);
BaseBlock block = } else {
cuboidRegion.getWorld().getFullBlock(point); blockId = palette.size();
if (block.getNbtData() != null) { palette
Map<String, Tag> values = new HashMap<>(); .put(blockKey, palette.size());
for (Map.Entry<String, Tag> entry : block }
.getNbtData().getValue().entrySet()) {
values while ((blockId & -128) != 0) {
.put(entry.getKey(), entry.getValue()); buffer.write(blockId & 127 | 128);
blockId >>>= 7;
}
buffer.write(blockId);
}
if (xiter.hasNext()) {
TaskManager.runTaskLater(this, 1);
} else {
zTask.run();
}
} }
// Remove 'id' if it exists. We want 'Id' });
values.remove("id"); }
if (ziter.hasNext()) {
// Positions are kept in NBT, we don't want that. TaskManager.runTaskLater(zTask, 1);
values.remove("x"); } else {
values.remove("y"); yTask.run();
values.remove("z");
values
.put("Id", new StringTag(block.getNbtId()));
values.put("Pos",
new IntArrayTag(new int[] {rx, ry, rz}));
tileEntities.add(new CompoundTag(values));
}
String blockKey =
block.toImmutableState().getAsString();
int blockId;
if (palette.containsKey(blockKey)) {
blockId = palette.get(blockKey);
} else {
blockId = palette.size();
palette.put(blockKey, palette.size());
}
while ((blockId & -128) != 0) {
buffer.write(blockId & 127 | 128);
blockId >>>= 7;
}
buffer.write(blockId);
} }
} }
} });
} }
if (!chunks.isEmpty()) { if (yiter.hasNext()) {
TaskManager.runTaskLater(this, 1); TaskManager.runTaskLater(yTask, 1);
} else { } else {
regionTask.run(); regionTask.run();
} }

View File

@ -77,24 +77,17 @@ public abstract class SchematicHandler {
if (value == null) { if (value == null) {
MainUtil.sendMessage(null, "&7 - Skipped plot &c" + plot.getId()); MainUtil.sendMessage(null, "&7 - Skipped plot &c" + plot.getId());
} else { } else {
TaskManager.runTaskAsync(new Runnable() { TaskManager.runTaskAsync(() -> {
@Override public void run() { MainUtil.sendMessage(null, "&6ID: " + plot.getId());
MainUtil.sendMessage(null, "&6ID: " + plot.getId()); boolean result = SchematicHandler.manager
boolean result = SchematicHandler.manager.save(value, .save(value, directory + File.separator + name + ".schematic");
directory + File.separator + name + ".schematic"); if (!result) {
if (!result) { MainUtil
MainUtil.sendMessage(null, .sendMessage(null, "&7 - Failed to save &c" + plot.getId());
"&7 - Failed to save &c" + plot.getId()); } else {
} else { MainUtil.sendMessage(null, "&7 - &a success: " + plot.getId());
MainUtil
.sendMessage(null, "&7 - &a success: " + plot.getId());
}
TaskManager.runTask(new Runnable() {
@Override public void run() {
THIS.run();
}
});
} }
TaskManager.runTask(() -> THIS.run());
}); });
} }
} }
@ -117,158 +110,156 @@ public abstract class SchematicHandler {
final int yOffset, final int zOffset, final boolean autoHeight, final int yOffset, final int zOffset, final boolean autoHeight,
final RunnableVal<Boolean> whenDone) { final RunnableVal<Boolean> whenDone) {
TaskManager.runTask(new Runnable() { TaskManager.runTask(() -> {
@Override public void run() { if (whenDone != null) {
if (whenDone != null) { whenDone.value = false;
whenDone.value = false; }
if (schematic == null) {
PlotSquared.debug("Schematic == null :|");
TaskManager.runTask(whenDone);
return;
}
try {
// Set flags
if (plot.hasOwner()) {
Map<String, Tag> flags = schematic.getFlags();
if (!flags.isEmpty()) {
for (Map.Entry<String, Tag> entry : flags.entrySet()) {
plot.setFlag(Flags.getFlag(entry.getKey()),
StringTag.class.cast(entry.getValue()).getValue());
}
}
} }
if (schematic == null) { final LocalBlockQueue queue = plot.getArea().getQueue(false);
PlotSquared.debug("Schematic == null :|"); BlockVector3 dimension = schematic.getClipboard().getDimensions();
final int WIDTH = dimension.getX();
final int LENGTH = dimension.getZ();
final int HEIGHT = dimension.getY();
// Validate dimensions
RegionWrapper region = plot.getLargestRegion();
if (((region.maxX - region.minX + xOffset + 1) < WIDTH) || (
(region.maxZ - region.minZ + zOffset + 1) < LENGTH) || (HEIGHT > 256)) {
PlotSquared.debug("Schematic is too large");
PlotSquared.debug(
"(" + WIDTH + ',' + LENGTH + ',' + HEIGHT + ") is bigger than (" + (
region.maxX - region.minX) + ',' + (region.maxZ - region.minZ)
+ ",256)");
TaskManager.runTask(whenDone); TaskManager.runTask(whenDone);
return; return;
} }
try { // block type and data arrays
// Set flags final BlockArrayClipboard blockArrayClipboard = schematic.getClipboard();
if (plot.hasOwner()) { // Calculate the optimal height to paste the schematic at
Map<String, Tag> flags = schematic.getFlags(); final int y_offset_actual;
if (!flags.isEmpty()) { if (autoHeight) {
for (Map.Entry<String, Tag> entry : flags.entrySet()) { if (HEIGHT >= 256) {
plot.setFlag(Flags.getFlag(entry.getKey()),
StringTag.class.cast(entry.getValue()).getValue());
}
}
}
final LocalBlockQueue queue = plot.getArea().getQueue(false);
BlockVector3 dimension = schematic.getClipboard().getDimensions();
final int WIDTH = dimension.getX();
final int LENGTH = dimension.getZ();
final int HEIGHT = dimension.getY();
// Validate dimensions
RegionWrapper region = plot.getLargestRegion();
if (((region.maxX - region.minX + xOffset + 1) < WIDTH) || (
(region.maxZ - region.minZ + zOffset + 1) < LENGTH) || (HEIGHT > 256)) {
PlotSquared.debug("Schematic is too large");
PlotSquared.debug(
"(" + WIDTH + ',' + LENGTH + ',' + HEIGHT + ") is bigger than (" + (
region.maxX - region.minX) + ',' + (region.maxZ - region.minZ)
+ ",256)");
TaskManager.runTask(whenDone);
return;
}
// block type and data arrays
final BlockArrayClipboard blockArrayClipboard = schematic.getClipboard();
// Calculate the optimal height to paste the schematic at
final int y_offset_actual;
if (autoHeight) {
if (HEIGHT >= 256) {
y_offset_actual = yOffset;
} else {
PlotArea pw = plot.getArea();
if (pw instanceof ClassicPlotWorld) {
y_offset_actual = yOffset + ((ClassicPlotWorld) pw).PLOT_HEIGHT;
} else {
y_offset_actual = yOffset + 1 + MainUtil
.getHeighestBlock(plot.getWorldName(), region.minX + 1,
region.minZ + 1);
}
}
} else {
y_offset_actual = yOffset; y_offset_actual = yOffset;
} } else {
Location pos1 = PlotArea pw = plot.getArea();
new Location(plot.getWorldName(), region.minX + xOffset, y_offset_actual, if (pw instanceof ClassicPlotWorld) {
region.minZ + zOffset); y_offset_actual = yOffset + ((ClassicPlotWorld) pw).PLOT_HEIGHT;
Location pos2 = pos1.clone().add(WIDTH - 1, HEIGHT - 1, LENGTH - 1); } else {
final int p1x = pos1.getX(); y_offset_actual = yOffset + 1 + MainUtil
final int p1z = pos1.getZ(); .getHeighestBlock(plot.getWorldName(), region.minX + 1,
final int p2x = pos2.getX(); region.minZ + 1);
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<>();
for (int x = bcx; x <= tcx; x++) {
for (int z = bcz; z <= tcz; z++) {
chunks.add(new ChunkLoc(x, z));
} }
}*/
ChunkManager.chunkTask(pos1, pos2, new RunnableVal<int[]>() {
@Override public void run(int[] value) {
//int count = 0;
//while (!chunks.isEmpty() && count < 256) {
//count++;
ChunkLoc chunk = new ChunkLoc(value[0], value[1]);
PlotSquared.log(chunk.toString());
int x = chunk.x;
int z = chunk.z;
int xxb = x << 4;
int zzb = z << 4;
int xxt = xxb + 15;
int zzt = zzb + 15;
if (x == bcx) {
xxb = p1x;
}
if (x == tcx) {
xxt = p2x;
}
if (z == bcz) {
zzb = p1z;
}
if (z == tcz) {
zzt = p2z;
}
// Paste schematic here
for (int ry = 0; ry < Math.min(256, HEIGHT); ry++) {
int yy = y_offset_actual + ry;
if (yy > 255) {
continue;
}
int i1 = ry * WIDTH * LENGTH;
for (int rz = zzb - p1z; rz <= (zzt - p1z); rz++) {
int i2 = (rz * WIDTH) + i1;
for (int rx = xxb - p1x; rx <= (xxt - p1x); rx++) {
int i = i2 + rx;
int xx = p1x + rx;
int zz = p1z + rz;
BaseBlock id = blockArrayClipboard
.getFullBlock(BlockVector3.at(rx, ry, rz));
queue.setBlock(xx, yy, zz, id);
}
}
}
/* }
if (!chunks.isEmpty()) {
this.run();
} else {
queue.flush();
HashMap<BlockLoc, CompoundTag> tiles = schematic.getClipboard().getTiles();
if (!tiles.isEmpty()) {
TaskManager.IMP.sync(new RunnableVal<Object>() {
@Override public void run(Object value) {
for (Map.Entry<BlockLoc, CompoundTag> entry : schematic
.getTiles().entrySet()) {
BlockLoc loc = entry.getKey();
restoreTile(queue, entry.getValue(),
p1x + xOffset + loc.x, loc.y + y_offset_actual,
p1z + zOffset + loc.z);
}
}
});
}
}*/
}
}, null, 10);
if (whenDone != null) {
whenDone.value = true;
whenDone.run();
} }
} catch (Exception e) { } else {
e.printStackTrace(); y_offset_actual = yOffset;
TaskManager.runTask(whenDone);
} }
Location pos1 =
new Location(plot.getWorldName(), region.minX + xOffset, y_offset_actual,
region.minZ + zOffset);
Location pos2 = pos1.clone().add(WIDTH - 1, HEIGHT - 1, LENGTH - 1);
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<>();
for (int x = bcx; x <= tcx; x++) {
for (int z = bcz; z <= tcz; z++) {
chunks.add(new ChunkLoc(x, z));
}
}*/
ChunkManager.chunkTask(pos1, pos2, new RunnableVal<int[]>() {
@Override public void run(int[] value) {
//int count = 0;
//while (!chunks.isEmpty() && count < 256) {
//count++;
ChunkLoc chunk = new ChunkLoc(value[0], value[1]);
PlotSquared.log(chunk.toString());
int x = chunk.x;
int z = chunk.z;
int xxb = x << 4;
int zzb = z << 4;
int xxt = xxb + 15;
int zzt = zzb + 15;
if (x == bcx) {
xxb = p1x;
}
if (x == tcx) {
xxt = p2x;
}
if (z == bcz) {
zzb = p1z;
}
if (z == tcz) {
zzt = p2z;
}
// Paste schematic here
for (int ry = 0; ry < Math.min(256, HEIGHT); ry++) {
int yy = y_offset_actual + ry;
if (yy > 255) {
continue;
}
int i1 = ry * WIDTH * LENGTH;
for (int rz = zzb - p1z; rz <= (zzt - p1z); rz++) {
int i2 = (rz * WIDTH) + i1;
for (int rx = xxb - p1x; rx <= (xxt - p1x); rx++) {
int i = i2 + rx;
int xx = p1x + rx;
int zz = p1z + rz;
BaseBlock id = blockArrayClipboard
.getFullBlock(BlockVector3.at(rx, ry, rz));
queue.setBlock(xx, yy, zz, id);
}
}
}
/* }
if (!chunks.isEmpty()) {
this.run();
} else {
queue.flush();
HashMap<BlockLoc, CompoundTag> tiles = schematic.getClipboard().getTiles();
if (!tiles.isEmpty()) {
TaskManager.IMP.sync(new RunnableVal<Object>() {
@Override public void run(Object value) {
for (Map.Entry<BlockLoc, CompoundTag> entry : schematic
.getTiles().entrySet()) {
BlockLoc loc = entry.getKey();
restoreTile(queue, entry.getValue(),
p1x + xOffset + loc.x, loc.y + y_offset_actual,
p1z + zOffset + loc.z);
}
}
});
}
}*/
}
}, null, 10);
if (whenDone != null) {
whenDone.value = true;
whenDone.run();
}
} catch (Exception e) {
e.printStackTrace();
TaskManager.runTask(whenDone);
} }
}); });
} }