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,72 +86,53 @@ 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() {
if (X == bcx) { @Override public void run() {
xxb = p1x; long xstart = System.currentTimeMillis();
} while (xiter.hasNext()
if (X == tcx) { && System.currentTimeMillis() - xstart
xxt = p2x; < 20) {
} final int x = xiter.next();
if (Z == bcz) { int rx = x - p1x;
zzb = p1z; int ry = fy - sy;
} int rz = fz - p1z;
if (Z == tcz) { BlockVector3 point =
zzt = p2z; BlockVector3.at(x, fy, fz);
}
for (int y = sy; y <= Math.min(255, ey); y++) {
int ry = y - sy;
for (int z = zzb; z <= zzt; z++) {
int rz = z - bz;
for (int x = xxb; x <= xxt; x++) {
int rx = x - bx;
BlockVector3 point = BlockVector3.at(x, y, z);
BaseBlock block = BaseBlock block =
cuboidRegion.getWorld().getFullBlock(point); cuboidRegion.getWorld()
.getFullBlock(point);
if (block.getNbtData() != null) { if (block.getNbtData() != null) {
Map<String, Tag> values = new HashMap<>(); Map<String, Tag> values =
new HashMap<>();
for (Map.Entry<String, Tag> entry : block for (Map.Entry<String, Tag> entry : block
.getNbtData().getValue().entrySet()) { .getNbtData().getValue()
values .entrySet()) {
.put(entry.getKey(), entry.getValue()); values.put(entry.getKey(),
entry.getValue());
} }
// Remove 'id' if it exists. We want 'Id' // Remove 'id' if it exists. We want 'Id'
values.remove("id"); values.remove("id");
@ -164,21 +142,24 @@ public class BukkitSchematicHandler extends SchematicHandler {
values.remove("y"); values.remove("y");
values.remove("z"); values.remove("z");
values values.put("Id", new StringTag(
.put("Id", new StringTag(block.getNbtId())); block.getNbtId()));
values.put("Pos", values.put("Pos", new IntArrayTag(
new IntArrayTag(new int[] {rx, ry, rz})); new int[] {rx, ry, rz}));
tileEntities.add(new CompoundTag(values)); tileEntities
.add(new CompoundTag(values));
} }
String blockKey = String blockKey =
block.toImmutableState().getAsString(); block.toImmutableState()
.getAsString();
int blockId; int blockId;
if (palette.containsKey(blockKey)) { if (palette.containsKey(blockKey)) {
blockId = palette.get(blockKey); blockId = palette.get(blockKey);
} else { } else {
blockId = palette.size(); blockId = palette.size();
palette.put(blockKey, palette.size()); palette
.put(blockKey, palette.size());
} }
while ((blockId & -128) != 0) { while ((blockId & -128) != 0) {
@ -187,11 +168,24 @@ public class BukkitSchematicHandler extends SchematicHandler {
} }
buffer.write(blockId); buffer.write(blockId);
} }
} if (xiter.hasNext()) {
}
}
if (!chunks.isEmpty()) {
TaskManager.runTaskLater(this, 1); TaskManager.runTaskLater(this, 1);
} else {
zTask.run();
}
}
});
}
if (ziter.hasNext()) {
TaskManager.runTaskLater(zTask, 1);
} else {
yTask.run();
}
}
});
}
if (yiter.hasNext()) {
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.save(value, boolean result = SchematicHandler.manager
directory + File.separator + name + ".schematic"); .save(value, directory + File.separator + name + ".schematic");
if (!result) { if (!result) {
MainUtil.sendMessage(null,
"&7 - Failed to save &c" + plot.getId());
} else {
MainUtil MainUtil
.sendMessage(null, "&7 - &a success: " + plot.getId()); .sendMessage(null, "&7 - Failed to save &c" + plot.getId());
} } else {
TaskManager.runTask(new Runnable() { MainUtil.sendMessage(null, "&7 - &a success: " + plot.getId());
@Override public void run() {
THIS.run();
}
});
} }
TaskManager.runTask(() -> THIS.run());
}); });
} }
} }
@ -117,8 +110,7 @@ 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;
} }
@ -269,7 +261,6 @@ public abstract class SchematicHandler {
e.printStackTrace(); e.printStackTrace();
TaskManager.runTask(whenDone); TaskManager.runTask(whenDone);
} }
}
}); });
} }