From 70ff6d87c8dbc91f64091333fdbd10f4183802ca Mon Sep 17 00:00:00 2001 From: boy0001 Date: Thu, 19 Feb 2015 22:06:27 +1100 Subject: [PATCH] schematic --- .../plot/commands/DebugExec.java | 2 +- .../plot/generator/HybridPlotManager.java | 2 +- .../plot/util/SchematicHandler.java | 78 ++++++++++--------- .../plot/util/bukkit/BukkitUtil.java | 18 +++++ 4 files changed, 60 insertions(+), 40 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java index b774f2f59..50ad38b72 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java @@ -146,7 +146,7 @@ public class DebugExec extends SubCommand { Trim.sendMessage(" - MCA #: " + empty.size()); Trim.sendMessage(" - CHUNKS: " + (empty.size() * 1024) + " (max)"); Trim.sendMessage("Exporting log for manual approval..."); - final File file = new File(PlotSquared.getMain().getDirectory() + File.separator + "trim.txt"); + final File file = new File(PlotSquared.IMP.getDirectory() + File.separator + "trim.txt"); PrintWriter writer; try { writer = new PrintWriter(file); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java index eb07a6f95..cef663749 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java @@ -145,7 +145,7 @@ import com.intellectualcrafters.plot.util.bukkit.BukkitTaskManager; CompoundTag sideroad = SchematicHandler.getCompoundTag(world, pos1, pos2); CompoundTag intersection = SchematicHandler.getCompoundTag(world, pos3, pos4); - String dir = PlotSquared.getMain().getDirectory() + File.separator + "schematics" + File.separator + "GEN_ROAD_SCHEMATIC" + File.separator + plot.world + File.separator; + String dir = PlotSquared.IMP.getDirectory() + File.separator + "schematics" + File.separator + "GEN_ROAD_SCHEMATIC" + File.separator + plot.world + File.separator; SchematicHandler.save(sideroad, dir + "sideroad.schematic"); SchematicHandler.save(intersection, dir + "intersection.schematic"); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java index 74a2bcc23..20fff2e99 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java @@ -50,6 +50,7 @@ import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; +import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; /** * Schematic Handler @@ -83,40 +84,38 @@ public class SchematicHandler { Location l1 = PlotHelper.getPlotBottomLoc(plot.world, plot.getId()); - final int sy = location.getWorld().getHighestBlockYAt(l1.getX() + 1, l1.getZ() + 1); + final int sy = BukkitUtil.getHeighestBlock(location.getWorld(), l1.getX() + 1, l1.getZ() + 1); l1 = l1.add(1, sy - 1, 1); - final World world = location.getWorld(); - int y_offset; - if (HEIGHT == location.getWorld().getMaxHeight()) { + if (HEIGHT == BukkitUtil.getMaxHeight(location.getWorld())) { y_offset = 0; } else { y_offset = l1.getY(); } + + int[] xl = new int[blocks.length]; + int[] yl = new int[blocks.length]; + int[] zl = new int[blocks.length]; + int[] ids = new int[blocks.length]; + byte[] data = new byte[blocks.length]; for (int x = 0; x < WIDTH; x++) { for (int z = 0; z < LENGTH; z++) { for (int y = 0; y < HEIGHT; y++) { final int index = (y * WIDTH * LENGTH) + (z * WIDTH) + x; - final DataCollection block = blocks[index]; - - final short id = block.getBlock(); - final byte data = block.getData(); - - // if (block.tag != null) { - // WorldEditUtils.setNBT(world, id, data, l1.getX() - // + x + x_offset, y + y_offset, l1.getZ() + z + - // z_offset, block.tag); - // } - // else { - PlotHelper.setBlock(world, l1.getX() + x + x_offset, y + y_offset, l1.getZ() + z + z_offset, id, data); - // } + xl[index] = x; + yl[index] = y; + zl[index] = z; + ids[index] = block.block; + data[index] = block.data; } } } + BlockManager.setBlocks(plot.world, xl, yl, zl, ids, data); + } catch (final Exception e) { return false; } @@ -170,14 +169,14 @@ public class SchematicHandler { */ public static Schematic getSchematic(final String name) { { - final File parent = new File(PlotSquared.getMain().getDirectory() + File.separator + "schematics"); + final File parent = new File(PlotSquared.IMP.getDirectory() + File.separator + "schematics"); if (!parent.exists()) { if (!parent.mkdir()) { throw new RuntimeException("Could not create schematic parent directory"); } } } - final File file = new File(PlotSquared.getMain().getDirectory() + File.separator + "schematics" + File.separator + name + ".schematic"); + final File file = new File(PlotSquared.IMP.getDirectory() + File.separator + "schematics" + File.separator + name + ".schematic"); if (!file.exists()) { PlotSquared.log(file.toString() + " doesn't exist"); return null; @@ -244,17 +243,14 @@ public class SchematicHandler { } @SuppressWarnings("deprecation") - public static CompoundTag getCompoundTag(final World world, Location pos1, Location pos2) { - - - + public static CompoundTag getCompoundTag(final String world, Location pos1, Location pos2) { // loading chunks int i = 0; int j = 0; try { for (i = (pos1.getX() / 16) * 16; i < (16 + ((pos2.getX() / 16) * 16)); i += 16) { for (j = (pos1.getZ() / 16) * 16; j < (16 + ((pos2.getZ() / 16) * 16)); j += 16) { - final Chunk chunk = world.getChunkAt(i, j); + final Chunk chunk = BukkitUtil.getChunkAt(world, i, j); final boolean result = chunk.load(false); if (!result) { @@ -301,8 +297,7 @@ public class SchematicHandler { for (int y = 0; y < height; y++) { final int index = (y * width * length) + (z * width) + x; - final Block block = world.getBlockAt(new Location(world, sx + x, sy + y, sz + z)); - + block = BukkitUtil.getBlock(new Location(world, sx + x, sy + y, sz + z)); @SuppressWarnings("deprecation") final int id2 = block.getTypeId(); if (id2 > 255) { @@ -335,17 +330,25 @@ public class SchematicHandler { } public static boolean pastePart(final String world, final DataCollection[] blocks, final Location l1, final int x_offset, final int z_offset, final int i1, final int i2, final int WIDTH, final int LENGTH) { - int[] xl = new int[i2]; - int[] yl = new int[i2]; - int[] zl = new int[i2]; - int[] ids = new int[i2]; - byte[] data = new byte[i2]; - + int length = 0; + for (int i = i1; i <= i2; i++) { + if (blocks[i].block == 0) { + length++; + } + } + length = i2 - length; + int[] xl = new int[length]; + int[] yl = new int[length]; + int[] zl = new int[length]; + int[] ids = new int[length]; + byte[] data = new byte[length]; + int count = 0; for (int i = i1; i <= i2; i++) { final short id = blocks[i].block; if (id == 0) { continue; } + count++; final int area = WIDTH * LENGTH; final int r = i % (area); @@ -353,13 +356,12 @@ public class SchematicHandler { final int y = i / area; final int z = r / WIDTH; - xl[i] = x; - yl[i] = y; - zl[i] = z; + xl[count] = x; + yl[count] = y; + zl[count] = z; - ids[i] = id; - data[i] = blocks[i].data; - + ids[count] = id; + data[count] = blocks[i].data; if (y > 256) { break; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java index 2cbd42342..e3ba7dba9 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java @@ -15,6 +15,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import com.intellectualcrafters.plot.object.Location; +import com.intellectualcrafters.plot.object.PlotBlock; import com.intellectualcrafters.plot.util.BlockManager; import com.intellectualcrafters.plot.util.SetBlockManager; import com.intellectualcrafters.plot.util.SetBlockSlow; @@ -37,6 +38,14 @@ public class BukkitUtil extends BlockManager { return world; } + public static int getMaxHeight(String world) { + return getWorld(world).getMaxHeight(); + } + + public static int getHeighestBlock(String world, int x, int z) { + return getWorld(world).getHighestBlockYAt(x, z); + } + public static Chunk getChunkAt(String worldname, int x, int z) { World world = getWorld(worldname); return world.getChunkAt(x, z); @@ -76,6 +85,15 @@ public class BukkitUtil extends BlockManager { SetBlockManager.setBlockManager.set(world, x, y, z, id, data); } } + + public static PlotBlock getBlock(Location loc) { + World world = getWorld(loc.getWorld()); + Block block = world.getBlockAt(loc.getX(), loc.getY(), loc.getZ()); + if (block == null) { + return new PlotBlock((short) 0 , (byte) 0); + } + return new PlotBlock((short) block.getTypeId(), block.getData()); + } public static Location getLocation(Entity entity) { org.bukkit.Location loc = entity.getLocation();