From 2359e546dfdc9e390e8dd62761a455de5fa8be67 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Thu, 12 Feb 2015 02:37:52 +1100 Subject: [PATCH] block updating --- .../intellectualcrafters/plot/PlotMain.java | 24 ++- .../plot/commands/Claim.java | 5 +- .../plot/commands/CreateRoadSchematic.java | 5 +- .../plot/commands/Merge.java | 6 +- .../plot/commands/Schematic.java | 5 +- .../plot/commands/Swap.java | 8 +- .../plot/commands/Unlink.java | 5 +- .../plot/generator/AugmentedPopulator.java | 4 +- .../plot/generator/HybridPlotManager.java | 64 ++---- .../plot/generator/HybridPlotWorld.java | 13 +- .../plot/object/PlotSelection.java | 28 +-- .../plot/util/AbstractSetBlock.java | 13 ++ .../plot/util/PlotHelper.java | 200 +++++++----------- .../plot/util/SchematicHandler.java | 14 +- .../plot/util/SetBlockFast.java | 34 +-- .../plot/util/SetBlockFast_1_8.java | 121 +++++++++++ .../plot/util/SetBlockSlow.java | 35 +++ 17 files changed, 318 insertions(+), 266 deletions(-) create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/util/AbstractSetBlock.java create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockFast_1_8.java create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockSlow.java diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java index 3bda9ba8f..596047fab 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java @@ -108,11 +108,14 @@ import com.intellectualcrafters.plot.util.ExpireManager; import com.intellectualcrafters.plot.util.Lag; import com.intellectualcrafters.plot.util.Logger; import com.intellectualcrafters.plot.util.Logger.LogLevel; +import com.intellectualcrafters.plot.util.AbstractSetBlock; import com.intellectualcrafters.plot.util.Metrics; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.SendChunk; import com.intellectualcrafters.plot.util.SetBlockFast; +import com.intellectualcrafters.plot.util.SetBlockFast_1_8; +import com.intellectualcrafters.plot.util.SetBlockSlow; import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.uuid.DefaultUUIDWrapper; @@ -1587,13 +1590,22 @@ public class PlotMain extends JavaPlugin implements Listener { } // Test for SetBlockFast { - try { - new SetBlockFast(); - PlotHelper.canSetFast = true; - } catch (final Throwable e) { - PlotHelper.canSetFast = false; + if (checkVersion(1, 8, 0)) { + try { + AbstractSetBlock.setBlockManager = new SetBlockFast_1_8(); + } + catch (Exception e) { + e.printStackTrace(); + AbstractSetBlock.setBlockManager = new SetBlockSlow(); + } + } + else { + try { + AbstractSetBlock.setBlockManager = new SetBlockFast(); + } catch (NoSuchMethodException e) { + AbstractSetBlock.setBlockManager = new SetBlockSlow(); + } } - try { new SendChunk(); PlotHelper.canSendChunk = true; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Claim.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Claim.java index aa5e4729d..a8913aeae 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Claim.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Claim.java @@ -32,6 +32,7 @@ import com.intellectualcrafters.plot.events.PlayerClaimPlotEvent; import com.intellectualcrafters.plot.generator.HybridPlotWorld; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotWorld; +import com.intellectualcrafters.plot.util.AbstractSetBlock; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.SchematicHandler; @@ -83,9 +84,7 @@ public class Claim extends SubCommand { final HybridPlotWorld pW = (HybridPlotWorld) world; if (!(pW.CLAIMED_WALL_BLOCK.equals(pW.WALL_BLOCK))) { PlotMain.getPlotManager(plot.getWorld()).setWall(plot.getWorld(), world, plot.getId(), pW.CLAIMED_WALL_BLOCK); - if (PlotHelper.canSetFast) { - SetBlockFast.update(player); - } + PlotHelper.update(player); } } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/CreateRoadSchematic.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/CreateRoadSchematic.java index b601748d1..1e63b3f69 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/CreateRoadSchematic.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/CreateRoadSchematic.java @@ -28,7 +28,9 @@ import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.generator.HybridPlotManager; import com.intellectualcrafters.plot.generator.HybridPlotWorld; import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.util.AbstractSetBlock; import com.intellectualcrafters.plot.util.PlayerFunctions; +import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.SetBlockFast; public class CreateRoadSchematic extends SubCommand { @@ -54,8 +56,7 @@ public class CreateRoadSchematic extends SubCommand { HybridPlotManager manager = (HybridPlotManager) PlotMain.getPlotManager(player.getWorld()); manager.setupRoadSchematic(plot); - SetBlockFast.update(player); - + PlotHelper.update(player); PlayerFunctions.sendMessage(player, "&6Saved new road schematic"); return true; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Merge.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Merge.java index 3a6b86282..e27aa84dd 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Merge.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Merge.java @@ -36,6 +36,7 @@ import com.intellectualcrafters.plot.events.PlotMergeEvent; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotWorld; +import com.intellectualcrafters.plot.util.AbstractSetBlock; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.SetBlockFast; @@ -178,10 +179,7 @@ public class Merge extends SubCommand { PlotHelper.mergePlots(world, plots); PlotHelper.setSign(world, UUIDHandler.getName(plot.owner), plot); - - if (PlotHelper.canSetFast) { - SetBlockFast.update(plr); - } + PlotHelper.update(plr); return true; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Schematic.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Schematic.java index cddf9ecca..966b1a623 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Schematic.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Schematic.java @@ -36,6 +36,7 @@ import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; +import com.intellectualcrafters.plot.util.AbstractSetBlock; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.SchematicHandler; @@ -140,9 +141,7 @@ public class Schematic extends SubCommand { final int start = Schematic.this.counter * 5000; if (start > blen) { sendMessage(plr, C.SCHEMATIC_PASTE_SUCCESS); - if (PlotHelper.canSetFast) { - SetBlockFast.update(plr); - } + PlotHelper.update(plr); Schematic.this.running = false; Bukkit.getScheduler().cancelTask(Schematic.this.task); return; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java index 635dc8c31..14b3ba80a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java @@ -27,10 +27,12 @@ import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotSelection; +import com.intellectualcrafters.plot.util.AbstractSetBlock; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.SetBlockFast; import com.intellectualcrafters.plot.util.UUIDHandler; + import org.bukkit.World; import org.bukkit.entity.Player; @@ -93,11 +95,7 @@ public class Swap extends SubCommand { // TODO Requires testing!! PlayerFunctions.sendMessage(plr, C.SWAP_SUCCESS); - - if (PlotHelper.canSetFast) { - SetBlockFast.update(plr); - } - + PlotHelper.update(plr); return true; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Unlink.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Unlink.java index c6e73695c..54110ad63 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Unlink.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Unlink.java @@ -35,6 +35,7 @@ import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotWorld; +import com.intellectualcrafters.plot.util.AbstractSetBlock; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.SetBlockFast; @@ -70,9 +71,7 @@ public class Unlink extends SubCommand { return false; } try { - if (PlotHelper.canSetFast) { - SetBlockFast.update(plr); - } + PlotHelper.update(plr); } catch (final Exception e) { // execute(final Player plr, final String... args) { try { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java index 62c984d0d..d4f55cb3a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java @@ -177,9 +177,7 @@ public class AugmentedPopulator extends BlockPopulator { if (p && manager.getPlotIdAbs(plotworld, new Location(world, xx, 0, zz)) != null) { continue; } - PlotBlock plotblock = new PlotBlock((short) blockInfo.id, (byte) 0 ); - Block block = world.getBlockAt(xx, blockInfo.y, zz); - PlotHelper.setBlock(block, plotblock); + PlotHelper.setBlock(world, xx, blockInfo.y, zz, blockInfo.id, (byte) 0); } } for (BlockPopulator populator : generator.getDefaultPopulators(world)) { 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 936fc77b8..004ffebdf 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java @@ -42,6 +42,7 @@ import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotBlock; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotWorld; +import com.intellectualcrafters.plot.util.AbstractSetBlock; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.SchematicHandler; @@ -191,13 +192,15 @@ import com.intellectualcrafters.plot.util.SendChunk; chunks.add(chunk); } } - + ArrayList chunks2 = new ArrayList<>(); for (int x = sx; x < sx + 16; x++) { for (int z = sz; z < sz + 16; z++) { Chunk chunk = world.getChunkAt(x, z); + chunks2.add(chunk); regenerateRoad(chunk); } } + AbstractSetBlock.setBlockManager.update(chunks2); } public boolean scheduleRoadUpdate(final World world) { @@ -343,17 +346,13 @@ import com.intellectualcrafters.plot.util.SendChunk; ChunkLoc loc = new ChunkLoc(absX, absZ); HashMap blocks = plotworld.G_SCH.get(loc); for (short y = (short) (plotworld.ROAD_HEIGHT + 1); y <= plotworld.ROAD_HEIGHT + plotworld.SCHEMATIC_HEIGHT; y++) { - Block block = world.getBlockAt(x + X, y, z + Z); - PlotBlock plotBlock = new PlotBlock((short) 0, (byte) 0); - PlotHelper.setBlock(block, plotBlock); + PlotHelper.setBlock(world, x + X, sy + y, z + Z, 0, (byte) 0); } if (blocks != null) { HashMap datas = plotworld.G_SCH_DATA.get(loc); if (datas == null) { for (Short y : blocks.keySet()) { - Block block = world.getBlockAt(x + X, sy + y, z + Z); - PlotBlock plotBlock = new PlotBlock(blocks.get(y), (byte) 0); - PlotHelper.setBlock(block, plotBlock); + PlotHelper.setBlock(world, x + X, sy + y, z + Z, blocks.get(y), (byte) 0); } } else { @@ -362,26 +361,13 @@ import com.intellectualcrafters.plot.util.SendChunk; if (data == null) { data = 0; } - Block block = world.getBlockAt(x + X, sy + y, z + Z); - PlotBlock plotBlock = new PlotBlock(blocks.get(y), data); - PlotHelper.setBlock(block, plotBlock); + PlotHelper.setBlock(world, x + X, sy + y, z + Z, blocks.get(y), data); } } } } } } - - if (PlotHelper.canSetFast) { - if (PlotHelper.canSendChunk) { - ArrayList chunks = new ArrayList<>(); - chunks.add(chunk); - SendChunk.sendChunk(chunks); - } - else { - world.refreshChunk(chunk.getX(), chunk.getZ()); - } - } return true; } } @@ -963,37 +949,30 @@ import com.intellectualcrafters.plot.util.SendChunk; final Location top = PlotHelper.getPlotTopLoc(w, plotid); int x, z; - - Block block; - z = bottom.getBlockZ(); for (x = bottom.getBlockX(); x < (top.getBlockX() + 1); x++) { for (int y = 1; y <= dpw.WALL_HEIGHT; y++) { - block = w.getBlockAt(x, y, z); - PlotHelper.setBlock(block, plotblock); + PlotHelper.setBlock(w, x, y, z, plotblock.id, plotblock.data); } } x = top.getBlockX() + 1; for (z = bottom.getBlockZ(); z < (top.getBlockZ() + 1); z++) { for (int y = 1; y <= dpw.WALL_HEIGHT; y++) { - block = w.getBlockAt(x, y, z); - PlotHelper.setBlock(block, plotblock); + PlotHelper.setBlock(w, x, y, z, plotblock.id, plotblock.data); } } z = top.getBlockZ() + 1; for (x = top.getBlockX() + 1; x > (bottom.getBlockX() - 1); x--) { for (int y = 1; y <= dpw.WALL_HEIGHT; y++) { - block = w.getBlockAt(x, y, z); - PlotHelper.setBlock(block, plotblock); + PlotHelper.setBlock(w, x, y, z, plotblock.id, plotblock.data); } } x = bottom.getBlockX(); for (z = top.getBlockZ() + 1; z > (bottom.getBlockZ() - 1); z--) { for (int y = 1; y <= dpw.WALL_HEIGHT; y++) { - block = w.getBlockAt(x, y, z); - PlotHelper.setBlock(block, plotblock); + PlotHelper.setBlock(w, x, y, z, plotblock.id, plotblock.data); } } return true; @@ -1010,26 +989,21 @@ import com.intellectualcrafters.plot.util.SendChunk; int x, z; - Block block; z = bottom.getBlockZ(); for (x = bottom.getBlockX(); x < (top.getBlockX() + 1); x++) { - block = w.getBlockAt(x, dpw.WALL_HEIGHT + 1, z); - PlotHelper.setBlock(block, plotblock); + PlotHelper.setBlock(w, x, dpw.WALL_HEIGHT + 1, z, plotblock.id, plotblock.data); } x = top.getBlockX() + 1; for (z = bottom.getBlockZ(); z < (top.getBlockZ() + 1); z++) { - block = w.getBlockAt(x, dpw.WALL_HEIGHT + 1, z); - PlotHelper.setBlock(block, plotblock); + PlotHelper.setBlock(w, x, dpw.WALL_HEIGHT + 1, z, plotblock.id, plotblock.data); } z = top.getBlockZ() + 1; for (x = top.getBlockX() + 1; x > (bottom.getBlockX() - 1); x--) { - block = w.getBlockAt(x, dpw.WALL_HEIGHT + 1, z); - PlotHelper.setBlock(block, plotblock); + PlotHelper.setBlock(w, x, dpw.WALL_HEIGHT + 1, z, plotblock.id, plotblock.data); } x = bottom.getBlockX(); for (z = top.getBlockZ() + 1; z > (bottom.getBlockZ() - 1); z--) { - block = w.getBlockAt(x, dpw.WALL_HEIGHT + 1, z); - PlotHelper.setBlock(block, plotblock); + PlotHelper.setBlock(w, x, dpw.WALL_HEIGHT + 1, z, plotblock.id, plotblock.data); } return true; } @@ -1246,9 +1220,7 @@ import com.intellectualcrafters.plot.util.SendChunk; HashMap datas = hpw.G_SCH_DATA.get(loc); if (datas == null) { for (Short y : blocks.keySet()) { - Block block = world.getBlockAt(sx + loc.x, sy + y, sz + loc.z); - PlotBlock plotBlock = new PlotBlock(blocks.get(y), (byte) 0); - PlotHelper.setBlock(block, plotBlock); + PlotHelper.setBlock(world, sx + loc.x, sy + y, sz + loc.z, blocks.get(y), (byte) 0); } } else { @@ -1257,9 +1229,7 @@ import com.intellectualcrafters.plot.util.SendChunk; if (data == null) { data = 0; } - Block block = world.getBlockAt(sx + loc.x, sy + y, sz + loc.z); - PlotBlock plotBlock = new PlotBlock(blocks.get(y), data); - PlotHelper.setBlock(block, plotBlock); + PlotHelper.setBlock(world, sx + loc.x, sy + y, sz + loc.z, blocks.get(y), data); } } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java index cac4e60ae..ff1cd918a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java @@ -300,7 +300,7 @@ public class HybridPlotWorld extends PlotWorld { this.ROAD_SCHEMATIC_ENABLED = true; } - public boolean isRotate(short id) { + public static boolean isRotate(short id) { switch (id) { case 23: return true; @@ -424,17 +424,18 @@ public class HybridPlotWorld extends PlotWorld { if (x < 0) { x += this.SIZE; } - if (rotate && isRotate(id)) { - data = (byte) ((data + 2) % 4); - } ChunkLoc loc = new ChunkLoc(x, z); if (!this.G_SCH.containsKey(loc)) { this.G_SCH.put(loc, new HashMap()); } this.G_SCH.get(loc).put(y, id); - - if (data == 0) { + if (isRotate(id)) { + if (rotate) { + data = (byte) ((data + 2) % 4); + } + } + else if (data == 0) { return; } if (!this.G_SCH_DATA.containsKey(loc)) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotSelection.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotSelection.java index 45b93488b..8f3aec2e2 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotSelection.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotSelection.java @@ -78,13 +78,7 @@ import com.intellectualcrafters.plot.util.SetBlockFast; final Location bot2 = PlotHelper.getPlotBottomLocAbs(world, id2).add(1, 0, 1); final Location bot1 = PlotHelper.getPlotBottomLocAbs(world, id1).add(1, 0, 1); final Location top1 = PlotHelper.getPlotTopLocAbs(world, id1); - - final int minX = bot1.getBlockX(), maxX = top1.getBlockX(), minZ = bot1.getBlockZ(), maxZ = top1.getBlockZ(), - - minX2 = bot2.getBlockX(), minZ2 = bot2.getBlockZ(); - - final boolean canSetFast = PlotHelper.canSetFast; - + final int minX = bot1.getBlockX(), maxX = top1.getBlockX(), minZ = bot1.getBlockZ(), maxZ = top1.getBlockZ(), minX2 = bot2.getBlockX(), minZ2 = bot2.getBlockZ(); for (int x = 0; x <= (maxX - minX); x++) { for (int z = 0; z <= (maxZ - minZ); z++) { for (int y = 1; y <= world.getMaxHeight(); y++) { @@ -96,25 +90,7 @@ import com.intellectualcrafters.plot.util.SetBlockFast; final BlockWrapper b2 = wrapBlock(block2); if ((b1.id != b2.id) || (b1.data != b2.data)) { - if (canSetFast) { - try { - SetBlockFast.set(world, b1.x, b1.y, b1.z, b2.id, b2.data); - SetBlockFast.set(world, b2.x, b2.y, b2.z, b1.id, b1.data); - } catch (final NoSuchMethodException e) { - PlotHelper.canSetFast = false; - } - } else { - if ((b1.id != b2.id) && (b1.data != b2.data)) { - block1.setTypeIdAndData(b2.id, b2.data, false); - block2.setTypeIdAndData(b1.id, b1.data, false); - } else if (b1.id != b2.id) { - block1.setTypeId(b2.id); - block2.setTypeId(b1.id); - } else { - block1.setData(b2.data); - block2.setData(b1.data); - } - } + PlotHelper.setBlock(world, b1.x, b1.y, b1.z, b2.id, b2.data); } } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/AbstractSetBlock.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/AbstractSetBlock.java new file mode 100644 index 000000000..b80d4eef7 --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/AbstractSetBlock.java @@ -0,0 +1,13 @@ +package com.intellectualcrafters.plot.util; + +import java.util.ArrayList; + +import org.bukkit.Chunk; + +public abstract class AbstractSetBlock { + public static AbstractSetBlock setBlockManager = null; + + public abstract boolean set(final org.bukkit.World world, final int x, final int y, final int z, final int blockId, final byte data); + + public abstract void update(ArrayList chunks); +} diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java index 562663d8f..fc8c1961a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java @@ -60,7 +60,6 @@ import com.intellectualcrafters.plot.object.PlotWorld; */ @SuppressWarnings({"unused", "javadoc", "deprecation"}) public class PlotHelper { public final static HashMap runners = new HashMap<>(); - public static boolean canSetFast = false; public static boolean canSendChunk = false; public static ArrayList runners_p = new ArrayList<>(); static long state = 1; @@ -339,38 +338,41 @@ import com.intellectualcrafters.plot.object.PlotWorld; return string; } - /** - * Set a block quickly, attempts to use NMS if possible - * - * @param block - * @param plotblock - */ - public static boolean setBlock(final Block block, final PlotBlock plotblock) { - - if (canSetFast) { - if ((block.getTypeId() != plotblock.id) || (plotblock.data != block.getData())) { - try { - SetBlockFast.set(block.getWorld(), block.getX(), block.getY(), block.getZ(), plotblock.id, plotblock.data); - return true; - } catch (final Throwable e) { - canSetFast = false; - } - } - return false; + public static boolean setBlock(World world, int x, int y, int z, int id, byte data) { + try { + return AbstractSetBlock.setBlockManager.set(world, x, y, z, id, data); } - if (block.getData() == plotblock.data) { - if (block.getTypeId() != plotblock.id) { - block.setTypeId(plotblock.id); - } - } else { - if (block.getTypeId() == plotblock.id) { - block.setData(plotblock.data); - } else { - block.setTypeIdAndData(plotblock.id, plotblock.data, false); - } + catch (Throwable e) { + AbstractSetBlock.setBlockManager = new SetBlockSlow(); + return AbstractSetBlock.setBlockManager.set(world, x, y, z, id, data); } - return false; } + +// public static boolean setBlock(final Block block, final PlotBlock plotblock) { +// if (canSetFast) { +// if ((block.getTypeId() != plotblock.id) || (plotblock.data != block.getData())) { +// try { +// SetBlockFast.set(block.getWorld(), block.getX(), block.getY(), block.getZ(), plotblock.id, plotblock.data); +// return true; +// } catch (final Throwable e) { +// canSetFast = false; +// } +// } +// return false; +// } +// if (block.getData() == plotblock.data) { +// if (block.getTypeId() != plotblock.id) { +// block.setTypeId(plotblock.id); +// } +// } else { +// if (block.getTypeId() == plotblock.id) { +// block.setData(plotblock.data); +// } else { +// block.setTypeIdAndData(plotblock.id, plotblock.data, false); +// } +// } +// return false; +// } /** * Adjusts a plot wall @@ -385,9 +387,19 @@ import com.intellectualcrafters.plot.object.PlotWorld; final PlotWorld plotworld = PlotMain.getWorldSettings(world); manager.setWall(world, plotworld, plot.id, block); - if (canSetFast) { - SetBlockFast.update(player); + update(player); + } + + public static void update(Player player) { + ArrayList chunks = new ArrayList<>(); + final int distance = Bukkit.getViewDistance(); + for (int cx = -distance; cx < distance; cx++) { + for (int cz = -distance; cz < distance; cz++) { + final Chunk chunk = player.getWorld().getChunkAt(player.getLocation().getChunk().getX() + cx, player.getLocation().getChunk().getZ() + cz); + chunks.add(chunk); + } } + AbstractSetBlock.setBlockManager.update(chunks); } public static void autoMerge(final World world, final Plot plot, final Player player) { @@ -445,9 +457,7 @@ import com.intellectualcrafters.plot.object.PlotWorld; } merge = false; } - if (canSetFast) { - SetBlockFast.update(player); - } + update(player); } private static boolean ownsPlots(final World world, final ArrayList plots, final Player player, final int dir) { @@ -543,9 +553,7 @@ import com.intellectualcrafters.plot.object.PlotWorld; final PlotWorld plotworld = PlotMain.getWorldSettings(world); manager.setWallFilling(world, plotworld, plot.id, block); PlayerFunctions.sendMessage(requester, C.SET_BLOCK_ACTION_FINISHED); - if (canSetFast) { - SetBlockFast.update(requester); - } + update(requester); } public static void setFloor(final Player requester, final Plot plot, final PlotBlock[] blocks) { @@ -559,9 +567,7 @@ import com.intellectualcrafters.plot.object.PlotWorld; final PlotWorld plotworld = PlotMain.getWorldSettings(world); PlayerFunctions.sendMessage(requester, C.SET_BLOCK_ACTION_FINISHED); manager.setFloor(world, plotworld, plot.id, blocks); - if (canSetFast) { - SetBlockFast.update(requester); - } + update(requester); } public static int square(final int x) { @@ -635,16 +641,17 @@ import com.intellectualcrafters.plot.object.PlotWorld; manager.clearPlot(world, plot, isDelete); - if (canSetFast) { - final Plugin plugin = PlotMain.getMain(); - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { - @Override - public void run() { - PlotHelper.setBiome(world, plot, Biome.FOREST); - refreshPlotChunks(world, plot); + final Plugin plugin = PlotMain.getMain(); + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + @Override + public void run() { + PlotHelper.setBiome(world, plot, Biome.FOREST); + if (player != null && player.isOnline()) { + PlayerFunctions.sendMessage(player, C.CLEARING_DONE.s().replaceAll("%time%", "" + ((System.currentTimeMillis() - start)))); } - }, 90L); - } + update(player); + } + }, 90L); } /** @@ -676,32 +683,15 @@ import com.intellectualcrafters.plot.object.PlotWorld; } public static void setCuboid(final World world, final Location pos1, final Location pos2, final PlotBlock newblock) { - if (!canSetFast) { - for (int y = pos1.getBlockY(); y < pos2.getBlockY(); y++) { - for (int x = pos1.getBlockX(); x < pos2.getBlockX(); x++) { - for (int z = pos1.getBlockZ(); z < pos2.getBlockZ(); z++) { - final Block block = world.getBlockAt(x, y, z); - if (!((block.getTypeId() == newblock.id) && (block.getData() == newblock.data))) { - block.setTypeIdAndData(newblock.id, newblock.data, false); - } + for (int y = pos1.getBlockY(); y < pos2.getBlockY(); y++) { + for (int x = pos1.getBlockX(); x < pos2.getBlockX(); x++) { + for (int z = pos1.getBlockZ(); z < pos2.getBlockZ(); z++) { + final Block block = world.getBlockAt(x, y, z); + if (!((block.getTypeId() == newblock.id) && (block.getData() == newblock.data))) { + setBlock(world, x, y, z, newblock.id, newblock.data); } } } - } else { - try { - for (int y = pos1.getBlockY(); y < pos2.getBlockY(); y++) { - for (int x = pos1.getBlockX(); x < pos2.getBlockX(); x++) { - for (int z = pos1.getBlockZ(); z < pos2.getBlockZ(); z++) { - final Block block = world.getBlockAt(x, y, z); - if (!((block.getTypeId() == newblock.id) && (block.getData() == newblock.data))) { - SetBlockFast.set(world, x, y, z, newblock.id, newblock.data); - } - } - } - } - } catch (final Exception e) { - // - } } } @@ -710,66 +700,30 @@ import com.intellectualcrafters.plot.object.PlotWorld; setCuboid(world, pos1, pos2, blocks[0]); return; } - if (!canSetFast) { - for (int y = pos1.getBlockY(); y < pos2.getBlockY(); y++) { - for (int x = pos1.getBlockX(); x < pos2.getBlockX(); x++) { - for (int z = pos1.getBlockZ(); z < pos2.getBlockZ(); z++) { - final int i = random(blocks.length); - final PlotBlock newblock = blocks[i]; - final Block block = world.getBlockAt(x, y, z); - if (!((block.getTypeId() == newblock.id) && (block.getData() == newblock.data))) { - block.setTypeIdAndData(newblock.id, newblock.data, false); - } + for (int y = pos1.getBlockY(); y < pos2.getBlockY(); y++) { + for (int x = pos1.getBlockX(); x < pos2.getBlockX(); x++) { + for (int z = pos1.getBlockZ(); z < pos2.getBlockZ(); z++) { + final int i = random(blocks.length); + final PlotBlock newblock = blocks[i]; + final Block block = world.getBlockAt(x, y, z); + if (!((block.getTypeId() == newblock.id) && (block.getData() == newblock.data))) { + setBlock(world, x, y, z, newblock.id, newblock.data); } } } - } else { - try { - for (int y = pos1.getBlockY(); y < pos2.getBlockY(); y++) { - for (int x = pos1.getBlockX(); x < pos2.getBlockX(); x++) { - for (int z = pos1.getBlockZ(); z < pos2.getBlockZ(); z++) { - final int i = random(blocks.length); - final PlotBlock newblock = blocks[i]; - final Block block = world.getBlockAt(x, y, z); - if (!((block.getTypeId() == newblock.id) && (block.getData() == newblock.data))) { - SetBlockFast.set(world, x, y, z, newblock.id, newblock.data); - } - } - } - } - } catch (final Exception e) { - // - } } } public static void setSimpleCuboid(final World world, final Location pos1, final Location pos2, final PlotBlock newblock) { - if (!canSetFast) { - for (int y = pos1.getBlockY(); y < pos2.getBlockY(); y++) { - for (int x = pos1.getBlockX(); x < pos2.getBlockX(); x++) { - for (int z = pos1.getBlockZ(); z < pos2.getBlockZ(); z++) { - final Block block = world.getBlockAt(x, y, z); - if (!((block.getTypeId() == newblock.id))) { - block.setTypeId(newblock.id, false); - } + for (int y = pos1.getBlockY(); y < pos2.getBlockY(); y++) { + for (int x = pos1.getBlockX(); x < pos2.getBlockX(); x++) { + for (int z = pos1.getBlockZ(); z < pos2.getBlockZ(); z++) { + final Block block = world.getBlockAt(x, y, z); + if (!((block.getTypeId() == newblock.id))) { + setBlock(world, x, y, z, newblock.id, (byte) 0); } } } - } else { - try { - for (int y = pos1.getBlockY(); y < pos2.getBlockY(); y++) { - for (int x = pos1.getBlockX(); x < pos2.getBlockX(); x++) { - for (int z = pos1.getBlockZ(); z < pos2.getBlockZ(); z++) { - final Block block = world.getBlockAt(x, y, z); - if (!((block.getTypeId() == newblock.id))) { - SetBlockFast.set(world, x, y, z, newblock.id, (byte) 0); - } - } - } - } - } catch (final Exception e) { - // - } } } 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 578be208c..168ec56b3 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java @@ -113,11 +113,7 @@ public class SchematicHandler { // z_offset, block.tag); // } // else { - final Block bukkitBlock = world.getBlockAt(l1.getBlockX() + x + x_offset, y + y_offset, l1.getBlockZ() + z + z_offset); - - final PlotBlock plotblock = new PlotBlock(id, data); - - PlotHelper.setBlock(bukkitBlock, plotblock); + PlotHelper.setBlock(world, l1.getBlockX() + x + x_offset, y + y_offset, l1.getBlockZ() + z + z_offset, id, data); // } } } @@ -350,7 +346,6 @@ public class SchematicHandler { final int area = WIDTH * LENGTH; final int r = i % (area); - final int x = r % WIDTH; final int y = i / area; final int z = r / WIDTH; @@ -358,12 +353,7 @@ public class SchematicHandler { if (y > 256) { break; } - - final Block block = world.getBlockAt(l1.getBlockX() + x + x_offset, l1.getBlockY() + y, l1.getBlockZ() + z + z_offset); - - final PlotBlock plotblock = new PlotBlock(id, data); - - final boolean set = PlotHelper.setBlock(block, plotblock); + final boolean set = PlotHelper.setBlock(world, l1.getBlockX() + x + x_offset, l1.getBlockY() + y, l1.getBlockZ() + z + z_offset, id, data); if (!result && set) { result = true; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockFast.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockFast.java index 86b95b112..25b425cb8 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockFast.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockFast.java @@ -27,6 +27,8 @@ import java.util.ArrayList; import org.bukkit.Bukkit; import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.World; import com.intellectualcrafters.plot.util.ReflectionUtils.RefClass; import com.intellectualcrafters.plot.util.ReflectionUtils.RefMethod; @@ -36,7 +38,7 @@ import com.intellectualcrafters.plot.util.ReflectionUtils.RefMethod; * * @author Empire92 */ -public class SetBlockFast { +public class SetBlockFast extends AbstractSetBlock { private static final RefClass classBlock = getRefClass("{nms}.Block"); private static final RefClass classChunk = getRefClass("{nms}.Chunk"); @@ -71,10 +73,9 @@ public class SetBlockFast { * @param data Block Data Value * * @return true - * - * @throws NoSuchMethodException */ - public static boolean set(final org.bukkit.World world, final int x, final int y, final int z, final int blockId, final byte data) throws NoSuchMethodException { + @Override + public boolean set(final org.bukkit.World world, final int x, final int y, final int z, final int blockId, final byte data) { final Object w = methodGetHandle.of(world).call(); final Object chunk = methodGetChunkAt.of(w).call(x >> 4, z >> 4); @@ -88,31 +89,18 @@ public class SetBlockFast { * * @param player Player whose chunks we're updating */ - public static void update(final org.bukkit.entity.Player player) { - if (!PlotHelper.canSetFast) { + @Override + public void update(ArrayList chunks) { + if (chunks.size() == 0) { return; } if (!PlotHelper.canSendChunk) { - - final int distance = Bukkit.getViewDistance(); - for (int cx = -distance; cx < distance; cx++) { - for (int cz = -distance; cz < distance; cz++) { - player.getWorld().refreshChunk(player.getLocation().getChunk().getX() + cx, player.getLocation().getChunk().getZ() + cz); - } + World world = chunks.get(0).getWorld(); + for (Chunk chunk : chunks) { + world.refreshChunk(chunk.getX(), chunk.getZ()); } - return; } - final ArrayList chunks = new ArrayList<>(); - - final int distance = Bukkit.getViewDistance(); - for (int cx = -distance; cx < distance; cx++) { - for (int cz = -distance; cz < distance; cz++) { - final Chunk chunk = player.getWorld().getChunkAt(player.getLocation().getChunk().getX() + cx, player.getLocation().getChunk().getZ() + cz); - chunks.add(chunk); - } - } - try { SendChunk.sendChunk(chunks); } catch (final Throwable e) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockFast_1_8.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockFast_1_8.java new file mode 100644 index 000000000..977f34b21 --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockFast_1_8.java @@ -0,0 +1,121 @@ +//////////////////////////////////////////////////////////////////////////////////////////////////// +// PlotSquared - A plot manager and world generator for the Bukkit API / +// Copyright (c) 2014 IntellectualSites/IntellectualCrafters / +// / +// This program is free software; you can redistribute it and/or modify / +// it under the terms of the GNU General Public License as published by / +// the Free Software Foundation; either version 3 of the License, or / +// (at your option) any later version. / +// / +// This program is distributed in the hope that it will be useful, / +// but WITHOUT ANY WARRANTY; without even the implied warranty of / +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the / +// GNU General Public License for more details. / +// / +// You should have received a copy of the GNU General Public License / +// along with this program; if not, write to the Free Software Foundation, / +// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA / +// / +// You can contact us via: support@intellectualsites.com / +//////////////////////////////////////////////////////////////////////////////////////////////////// + +package com.intellectualcrafters.plot.util; + +import static com.intellectualcrafters.plot.util.ReflectionUtils.getRefClass; + +import java.util.ArrayList; + +import org.bukkit.Bukkit; +import org.bukkit.Chunk; +import org.bukkit.World; + +import com.intellectualcrafters.plot.util.ReflectionUtils.RefClass; +import com.intellectualcrafters.plot.util.ReflectionUtils.RefConstructor; +import com.intellectualcrafters.plot.util.ReflectionUtils.RefMethod; + +/** + * SetBlockFast class
Used to do fast world editing + * + * @author Empire92 + */ +public class SetBlockFast_1_8 extends AbstractSetBlock { + + private static final RefClass classBlock = getRefClass("{nms}.Block"); + private static final RefClass classBlockPosition = getRefClass("{nms}.BlockPosition"); + private static final RefClass classIBlockData = getRefClass("{nms}.IBlockData"); + + + + + private static final RefClass classChunk = getRefClass("{nms}.Chunk"); + private static final RefClass classWorld = getRefClass("{nms}.World"); + private static final RefClass classCraftWorld = getRefClass("{cb}.CraftWorld"); + + private static RefMethod methodGetHandle; + private static RefMethod methodGetChunkAt; + private static RefMethod methodA; + private static RefMethod methodGetByCombinedId; + + private static RefConstructor constructorBlockPosition; + + /** + * Constructor + * + * @throws NoSuchMethodException + */ + public SetBlockFast_1_8() throws NoSuchMethodException { + + constructorBlockPosition = classBlockPosition.getConstructor(int.class, int.class, int.class); + methodGetByCombinedId = classBlock.getMethod("getByCombinedId", int.class); + + methodGetHandle = classCraftWorld.getMethod("getHandle"); + methodGetChunkAt = classWorld.getMethod("getChunkAt", int.class, int.class); + methodA = classChunk.getMethod("a", classBlockPosition, classIBlockData); + } + + /** + * Set the block at the location + * + * @param world World in which the block should be set + * @param x X Coordinate + * @param y Y Coordinate + * @param z Z Coordinate + * @param blockId Block ID + * @param data Block Data Value + * + * @return true + */ + @Override + public boolean set(final World world, final int x, final int y, final int z, final int blockId, final byte data) { + final Object w = methodGetHandle.of(world).call(); + final Object chunk = methodGetChunkAt.of(w).call(x >> 4, z >> 4); + final Object pos = constructorBlockPosition.create((int) (x & 0x0f), y, (int) (z & 0x0f)); + final Object id = methodGetByCombinedId.of(null).call(blockId + (data << 12)); + methodA.of(chunk).call(pos, id); + return true; + } + + /** + * Update chunks + * + * @param player Player whose chunks we're updating + */ + @Override + public void update(ArrayList chunks) { + if (chunks.size() == 0) { + return; + } + if (!PlotHelper.canSendChunk) { + World world = chunks.get(0).getWorld(); + for (Chunk chunk : chunks) { + world.refreshChunk(chunk.getX(), chunk.getZ()); + } + return; + } + try { + SendChunk.sendChunk(chunks); + } catch (final Throwable e) { + PlotHelper.canSendChunk = false; + } + } +} diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockSlow.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockSlow.java new file mode 100644 index 000000000..1a5fa319a --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SetBlockSlow.java @@ -0,0 +1,35 @@ +package com.intellectualcrafters.plot.util; + +import java.util.ArrayList; + +import org.bukkit.Chunk; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; + +public class SetBlockSlow extends AbstractSetBlock { + + @Override + public boolean set(World world, int x, int y, int z, int id, byte data) { + Block block = world.getBlockAt(x, y, z); + if (block.getData() == data) { + if (block.getTypeId() != id) { + block.setTypeId(id, false); + } + } else { + if (block.getTypeId() == id) { + block.setData(data, false); + } else { + block.setTypeIdAndData(id, data, false); + } + } + return false; + } + + @Override + public void update(ArrayList chunks) { + // TODO Auto-generated method stub + + } + +}