From 7bcc4c4ce3dd95913869eb1f3dee18ff19fcc35e Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Mon, 31 Dec 2018 17:15:47 +0000 Subject: [PATCH] Fix plot move/swap block rotation issue (switch to BaseBlock). Also fix container inventory pasting. Some cleanup. Fixes #2209 --- .../bukkit/object/schematic/StateWrapper.java | 32 +++-- .../bukkit/util/BukkitChunkManager.java | 119 +++++++++--------- .../plotsquared/plot/commands/Move.java | 6 +- .../plotsquared/plot/object/Plot.java | 105 +++++++--------- 4 files changed, 127 insertions(+), 135 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/schematic/StateWrapper.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/schematic/StateWrapper.java index 2f4d33d6e..3557f33af 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/schematic/StateWrapper.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/schematic/StateWrapper.java @@ -1,12 +1,14 @@ package com.github.intellectualsites.plotsquared.bukkit.object.schematic; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitUtil; +import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.config.C; import com.sk89q.jnbt.*; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockState; +import org.bukkit.block.Container; import org.bukkit.block.Sign; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.Inventory; @@ -152,6 +154,13 @@ public class StateWrapper { BlockState state = block.getState(); switch (tileid) { case "chest": + case "beacon": + case "brewingstand": + case "dispenser": + case "dropper": + case "furnace": + case "hopper": + case "shulkerbox": List itemsTag = this.tag.getListTag("Items").getValue(); int length = itemsTag.size(); String[] ids = new String[length]; @@ -160,20 +169,27 @@ public class StateWrapper { for (int i = 0; i < length; i++) { Tag itemTag = itemsTag.get(i); CompoundTag itemComp = (CompoundTag) itemTag; - String id = itemComp.getString("Id"); + String id = itemComp.getString("id"); + if (id.startsWith("minecraft:")) { + id = id.replace("minecraft:", ""); + } ids[i] = id; amounts[i] = itemComp.getByte("Count"); slots[i] = itemComp.getByte("Slot"); } - if (state instanceof InventoryHolder) { - InventoryHolder holder = (InventoryHolder) state; - Inventory inv = holder.getInventory(); + if (state instanceof Container) { + Container container = (Container) state; + Inventory inv = container.getSnapshotInventory(); for (int i = 0; i < ids.length; i++) { - ItemStack item = - new ItemStack(Material.getMaterial(ids[i]), (int) amounts[i]); - inv.addItem(item); + Material mat = Material.getMaterial(ids[i].toUpperCase()); + if (mat != null) { + ItemStack item = new ItemStack(mat, (int) amounts[i]); + inv.setItem(slots[i], item); + PlotSquared.log(mat.name() + " " + slots[i]); + } } - state.update(true); + PlotSquared.log(inv.getStorageContents()); + container.update(true, true); return true; } return false; diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java index 5f666091b..3e04f38b3 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java @@ -10,12 +10,14 @@ import com.github.intellectualsites.plotsquared.plot.util.TaskManager; import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue; import com.github.intellectualsites.plotsquared.plot.util.block.LocalBlockQueue; import com.github.intellectualsites.plotsquared.plot.util.block.ScopedLocalBlockQueue; +import com.sk89q.worldedit.bukkit.BukkitWorld; +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.world.block.BaseBlock; import org.bukkit.*; -import org.bukkit.block.*; +import org.bukkit.block.Block; import org.bukkit.block.banner.Pattern; import org.bukkit.block.data.BlockData; import org.bukkit.entity.*; -import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import java.util.*; @@ -51,43 +53,43 @@ public class BukkitChunkManager extends ChunkManager { String worldName1 = world1.getName(); String worldName2 = world2.getName(); + BukkitWorld bukkitWorld1 = new BukkitWorld(world1); + BukkitWorld bukkitWorld2 = new BukkitWorld(world2); + LocalBlockQueue queue1 = GlobalBlockQueue.IMP.getNewQueue(worldName1, false); LocalBlockQueue queue2 = GlobalBlockQueue.IMP.getNewQueue(worldName2, false); for (int x = Math.max(r1.minX, sx); x <= Math.min(r1.maxX, sx + 15); x++) { for (int z = Math.max(r1.minZ, sz); z <= Math.min(r1.maxZ, sz + 15); z++) { - map.saveBlocks(world1, 256, sx, sz, relX, relZ, false); + map.saveBlocks(bukkitWorld1, 256, sx, sz, relX, relZ); for (int y = 0; y < 256; y++) { Block block1 = world1.getBlockAt(x, y, z); - // int id1 = block1.getTypeId(); - Material id1 = block1.getType(); + BaseBlock baseBlock1 = bukkitWorld2.getFullBlock(BlockVector3.at(x, y, z)); BlockData data1 = block1.getBlockData(); - // byte data1 = block1.getData(); + int xx = x + relX; int zz = z + relZ; + Block block2 = world2.getBlockAt(xx, y, zz); - // int id2 = block2.getTypeId(); - Material id2 = block2.getType(); + BaseBlock baseBlock2 = bukkitWorld2.getFullBlock(BlockVector3.at(xx, y, zz)); BlockData data2 = block2.getBlockData(); - // byte data2 = block2.getData(); - if (id1 == Material.AIR) { - if (id2 != Material.AIR) { - queue1.setBlock(x, y, z, PlotBlock.get(id2)); + + if (block1.isEmpty()) { + if (block2.isEmpty()) { + queue1.setBlock(x, y, z, baseBlock2); queue2.setBlock(xx, y, zz, PlotBlock.get("air")); } - } else if (id2 == Material.AIR) { + } else if (block2.isEmpty()) { queue1.setBlock(x, y, z, PlotBlock.get("air")); - queue2.setBlock(xx, y, zz, PlotBlock.get(id1)); - } else if (id1 == id2) { + queue2.setBlock(xx, y, zz, baseBlock1); + } else if (block1.equals(block2)) { if (data1 != data2) { block1.setBlockData(data2); block2.setBlockData(data1); } } else { - queue1.setBlock(x, y, z, PlotBlock.get(id2)); - queue2.setBlock(xx, y, zz, PlotBlock.get(id1)); - // queue1.setBlock(x, y, z, (short) id2, data2); - // queue2.setBlock(xx, y, zz, (short) id1, data1); + queue1.setBlock(x, y, z, baseBlock2); + queue2.setBlock(xx, y, zz, baseBlock1); } } } @@ -115,13 +117,11 @@ public class BukkitChunkManager extends ChunkManager { final Runnable whenDone) { final int relX = newPos.getX() - pos1.getX(); final int relZ = newPos.getZ() - pos1.getZ(); - com.github.intellectualsites.plotsquared.plot.object.Location pos4 = - new com.github.intellectualsites.plotsquared.plot.object.Location(newPos.getWorld(), - newPos.getX() + relX, 256, newPos.getZ() + relZ); final RegionWrapper region = new RegionWrapper(pos1.getX(), pos2.getX(), pos1.getZ(), pos2.getZ()); final World oldWorld = Bukkit.getWorld(pos1.getWorld()); + final BukkitWorld oldBukkitWorld = new BukkitWorld(oldWorld); final World newWorld = Bukkit.getWorld(newPos.getWorld()); final String newWorldName = newWorld.getName(); final ContentMap map = new ContentMap(); @@ -139,24 +139,24 @@ public class BukkitChunkManager extends ChunkManager { map.saveEntitiesIn(chunk, region); for (int x = bx & 15; x <= (tx & 15); x++) { for (int z = bz & 15; z <= (tz & 15); z++) { - map.saveBlocks(oldWorld, 256, cxx + x, czz + z, relX, relZ, true); + map.saveBlocks(oldBukkitWorld, 256, cxx + x, czz + z, relX, relZ); } } } }, () -> { - for (Entry entry : map.allBlocks.entrySet()) { + for (Entry entry : map.allBlocks.entrySet()) { PlotLoc loc = entry.getKey(); - PlotBlock[] blocks = entry.getValue(); + BaseBlock[] blocks = entry.getValue(); for (int y = 0; y < blocks.length; y++) { if (blocks[y] != null) { - PlotBlock block = blocks[y]; + BaseBlock block = blocks[y]; queue.setBlock(loc.x, y, loc.z, block); } } } queue.enqueue(); GlobalBlockQueue.IMP.addTask(() -> { - map.restoreBlocks(newWorld, 0, 0); + //map.restoreBlocks(newWorld, 0, 0); map.restoreEntities(newWorld, relX, relZ); TaskManager.runTask(whenDone); }); @@ -187,6 +187,7 @@ public class BukkitChunkManager extends ChunkManager { } } final World worldObj = Bukkit.getWorld(world); + final BukkitWorld bukkitWorldObj = new BukkitWorld(worldObj); TaskManager.runTask(new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); @@ -246,28 +247,28 @@ public class BukkitChunkManager extends ChunkManager { } final ContentMap map = new ContentMap(); if (checkX1) { - map.saveRegion(worldObj, xxb, xxb2, zzb2, zzt2); // + map.saveRegion(bukkitWorldObj, xxb, xxb2, zzb2, zzt2); // } if (checkX2) { - map.saveRegion(worldObj, xxt2, xxt, zzb2, zzt2); // + map.saveRegion(bukkitWorldObj, xxt2, xxt, zzb2, zzt2); // } if (checkZ1) { - map.saveRegion(worldObj, xxb2, xxt2, zzb, zzb2); // + map.saveRegion(bukkitWorldObj, xxb2, xxt2, zzb, zzb2); // } if (checkZ2) { - map.saveRegion(worldObj, xxb2, xxt2, zzt2, zzt); // + map.saveRegion(bukkitWorldObj, xxb2, xxt2, zzt2, zzt); // } if (checkX1 && checkZ1) { - map.saveRegion(worldObj, xxb, xxb2, zzb, zzb2); // + map.saveRegion(bukkitWorldObj, xxb, xxb2, zzb, zzb2); // } if (checkX2 && checkZ1) { - map.saveRegion(worldObj, xxt2, xxt, zzb, zzb2); // ? + map.saveRegion(bukkitWorldObj, xxt2, xxt, zzb, zzb2); // ? } if (checkX1 && checkZ2) { - map.saveRegion(worldObj, xxb, xxb2, zzt2, zzt); // ? + map.saveRegion(bukkitWorldObj, xxb, xxb2, zzt2, zzt); // ? } if (checkX2 && checkZ2) { - map.saveRegion(worldObj, xxt2, xxt, zzt2, zzt); // + map.saveRegion(bukkitWorldObj, xxt2, xxt, zzt2, zzt); // } map.saveEntitiesOut(chunkObj, currentPlotClear); AugmentedUtils.bypass(ignoreAugment, @@ -280,10 +281,10 @@ public class BukkitChunkManager extends ChunkManager { for (int x1 = 0; x1 < 16; x1++) { for (int z1 = 0; z1 < 16; z1++) { PlotLoc loc = new PlotLoc(bx + x1, bz + z1); - PlotBlock[] ids = map.allBlocks.get(loc); + BaseBlock[] ids = map.allBlocks.get(loc); if (ids != null) { for (int y = 0; y < Math.min(128, ids.length); y++) { - PlotBlock id = ids[y]; + BaseBlock id = ids[y]; if (id != null) { value.setBlock(x1, y, z1, id); } else { @@ -292,7 +293,7 @@ public class BukkitChunkManager extends ChunkManager { } for (int y = Math.min(128, ids.length); y < ids.length; y++) { - PlotBlock id = ids[y]; + BaseBlock id = ids[y]; if (id != null) { value.setBlock(x1, y, z1, id); } @@ -302,7 +303,7 @@ public class BukkitChunkManager extends ChunkManager { } } }, world, chunk)); - map.restoreBlocks(worldObj, 0, 0); + //map.restoreBlocks(worldObj, 0, 0); map.restoreEntities(worldObj, 0, 0); } if (!chunks.isEmpty()) { @@ -379,7 +380,7 @@ public class BukkitChunkManager extends ChunkManager { } GlobalBlockQueue.IMP.addTask(() -> { for (ContentMap map : maps) { - map.restoreBlocks(world1, 0, 0); + //map.restoreBlocks(world1, 0, 0); map.restoreEntities(world1, 0, 0); TaskManager.runTaskLater(whenDone, 1); } @@ -606,7 +607,7 @@ public class BukkitChunkManager extends ChunkManager { final Map> bannerPatterns; final Map bannerBase; final Set entities; - final Map allBlocks; + final Map allBlocks; ContentMap() { this.chestContents = new HashMap<>(); @@ -630,7 +631,7 @@ public class BukkitChunkManager extends ChunkManager { this.allBlocks = new HashMap<>(); } - public void saveRegion(World world, int x1, int x2, int z1, int z2) { + public void saveRegion(BukkitWorld world, int x1, int x2, int z1, int z2) { if (z1 > z2) { int tmp = z1; z1 = z2; @@ -643,7 +644,7 @@ public class BukkitChunkManager extends ChunkManager { } for (int x = x1; x <= x2; x++) { for (int z = z1; z <= z2; z++) { - saveBlocks(world, 256, x, z, 0, 0, true); + saveBlocks(world, 256, x, z, 0, 0); } } } @@ -708,7 +709,7 @@ public class BukkitChunkManager extends ChunkManager { this.entities.clear(); } - public void restoreBlocks(World world, int xOffset, int zOffset) { +/* public void restoreBlocks(World world, int xOffset, int zOffset) { for (Entry blockLocEntry : this.chestContents.entrySet()) { try { Block block = world @@ -1065,25 +1066,23 @@ public class BukkitChunkManager extends ChunkManager { + ',' + (blockLocByteEntry.getKey().z + zOffset)); } } - } + }*/ - public void saveBlocks(World world, int maxY, int x, int z, int offsetX, int offsetZ, - boolean storeNormal) { + public void saveBlocks(BukkitWorld world, int maxY, int x, int z, int offsetX, + int offsetZ) { maxY = Math.min(255, maxY); - PlotBlock[] ids; - if (storeNormal) { - ids = new PlotBlock[maxY + 1]; - } else { - ids = null; - } + BaseBlock[] ids; + ids = new BaseBlock[maxY + 1]; for (short y = 0; y <= maxY; y++) { - Block block = world.getBlockAt(x, y, z); - Material blockType = block.getType(); - if (storeNormal) { - if (blockType.name().contains("AIR")) { - ids[y] = StringPlotBlock.EVERYTHING; + BaseBlock block = world.getFullBlock(BlockVector3.at(x, y, z)); + ids[y] = block; +/* if (storeNormal) { + if (block.isEmpty()) { + BukkitAdapter. + ids[y] = + BukkitAdapter.adapt(Material.AIR.createBlockData()).toBaseBlock(); } else { - ids[y] = PlotBlock.get(blockType.name()); + ids[y] = BukkitAdapter.adapt(block.getBlockData()).toBaseBlock(); } } if (!blockType.name().contains("AIR")) { @@ -1169,7 +1168,7 @@ public class BukkitChunkManager extends ChunkManager { e.printStackTrace(); PlotSquared.debug("------------ but we caught it ^ --------"); } - } + }*/ } PlotLoc loc = new PlotLoc(x + offsetX, z + offsetZ); this.allBlocks.put(loc, ids); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Move.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Move.java index 346dea7b9..289a3c4a6 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Move.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Move.java @@ -54,11 +54,7 @@ import com.github.intellectualsites.plotsquared.plot.util.Permissions; C.PLOTWORLD_INCOMPATIBLE.send(player); return false; } - if (plot1.move(plot2, new Runnable() { - @Override public void run() { - MainUtil.sendMessage(player, C.MOVE_SUCCESS); - } - }, false)) { + if (plot1.move(plot2, () -> MainUtil.sendMessage(player, C.MOVE_SUCCESS), false)) { return true; } else { MainUtil.sendMessage(player, C.REQUIRES_UNOWNED); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index fe5db5e8f..994557f1c 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -806,14 +806,12 @@ public class Plot { Runnable run = new Runnable() { @Override public void run() { if (queue.isEmpty()) { - Runnable run = new Runnable() { - @Override public void run() { - for (RegionWrapper region : regions) { - Location[] corners = region.getCorners(getWorldName()); - ChunkManager.manager.clearAllEntities(corners[0], corners[1]); - } - TaskManager.runTask(whenDone); + Runnable run = () -> { + for (RegionWrapper region : regions) { + Location[] corners = region.getCorners(getWorldName()); + ChunkManager.manager.clearAllEntities(corners[0], corners[1]); } + TaskManager.runTask(whenDone); }; for (Plot current : plots) { if (isDelete || current.owner == null) { @@ -928,11 +926,9 @@ public class Plot { current.setMerged(merged); } if (createSign) { - GlobalBlockQueue.IMP.addTask(new Runnable() { - @Override public void run() { - for (Plot current : plots) { - current.setSign(MainUtil.getName(current.owner)); - } + GlobalBlockQueue.IMP.addTask(() -> { + for (Plot current : plots) { + current.setSign(MainUtil.getName(current.owner)); } }); } @@ -951,11 +947,7 @@ public class Plot { if (!isLoaded()) return; if (!PlotSquared.get().isMainThread(Thread.currentThread())) { - TaskManager.runTask(new Runnable() { - @Override public void run() { - Plot.this.setSign(name); - } - }); + TaskManager.runTask(() -> Plot.this.setSign(name)); return; } PlotManager manager = this.area.getPlotManager(); @@ -1045,13 +1037,11 @@ public class Plot { return false; } final Set plots = this.getConnectedPlots(); - this.clear(false, true, new Runnable() { - @Override public void run() { - for (Plot current : plots) { - current.unclaim(); - } - TaskManager.runTask(whenDone); + this.clear(false, true, () -> { + for (Plot current : plots) { + current.unclaim(); } + TaskManager.runTask(whenDone); }); return true; } @@ -1259,7 +1249,9 @@ public class Plot { x = bot.getX() + loc.x; z = bot.getZ() + loc.z; } - int y = loc.y < 1 ? (isLoaded() ? WorldUtil.IMP.getHighestBlock(plot.getWorldName(), x, z) + 1 : 63) : loc.y; + int y = loc.y < 1 ? + (isLoaded() ? WorldUtil.IMP.getHighestBlock(plot.getWorldName(), x, z) + 1 : 63) : + loc.y; PlotSquared.log("Getting home with Y " + y); return new Location(plot.getWorldName(), x, y, z); } @@ -1485,12 +1477,10 @@ public class Plot { this.getDenied().clear(); this.settings = new PlotSettings(); if (this.area.addPlot(this)) { - DBFunc.createPlotAndSettings(this, new Runnable() { - @Override public void run() { - PlotArea plotworld = Plot.this.area; - if (notify && plotworld.AUTO_MERGE) { - Plot.this.autoMerge(-1, Integer.MAX_VALUE, uuid, true); - } + DBFunc.createPlotAndSettings(this, () -> { + PlotArea plotworld = Plot.this.area; + if (notify && plotworld.AUTO_MERGE) { + Plot.this.autoMerge(-1, Integer.MAX_VALUE, uuid, true); } }); return true; @@ -1839,16 +1829,14 @@ public class Plot { TaskManager.runTask(whenDone); } } else { - TaskManager.runTaskAsync(new Runnable() { - @Override public void run() { - String name = Plot.this.id + "," + Plot.this.area + ',' + MainUtil - .getName(Plot.this.owner); - boolean result = SchematicHandler.manager.save(value, - Settings.Paths.SCHEMATICS + File.separator + name + ".schematic"); - if (whenDone != null) { - whenDone.value = result; - TaskManager.runTask(whenDone); - } + TaskManager.runTaskAsync(() -> { + String name = Plot.this.id + "," + Plot.this.area + ',' + MainUtil + .getName(Plot.this.owner); + boolean result = SchematicHandler.manager.save(value, + Settings.Paths.SCHEMATICS + File.separator + name + ".schematic"); + if (whenDone != null) { + whenDone.value = result; + TaskManager.runTask(whenDone); } }); } @@ -2627,12 +2615,10 @@ public class Plot { * - Usually called when the plot state changes (unclaimed/claimed/flag change etc) */ public void reEnter() { - TaskManager.runTaskLater(new Runnable() { - @Override public void run() { - for (PlotPlayer pp : Plot.this.getPlayersInPlot()) { - PlotListener.plotExit(pp, Plot.this); - PlotListener.plotEntry(pp, Plot.this); - } + TaskManager.runTaskLater(() -> { + for (PlotPlayer pp : Plot.this.getPlayersInPlot()) { + PlotListener.plotExit(pp, Plot.this); + PlotListener.plotEntry(pp, Plot.this); } }, 1); } @@ -2688,17 +2674,15 @@ public class Plot { MainUtil.sendMessage(player, C.TELEPORT_IN_SECONDS, Settings.Teleport.DELAY + ""); final String name = player.getName(); TaskManager.TELEPORT_QUEUE.add(name); - TaskManager.runTaskLater(new Runnable() { - @Override public void run() { - if (!TaskManager.TELEPORT_QUEUE.contains(name)) { - MainUtil.sendMessage(player, C.TELEPORT_FAILED); - return; - } - TaskManager.TELEPORT_QUEUE.remove(name); - if (player.isOnline()) { - MainUtil.sendMessage(player, C.TELEPORTED_TO_PLOT); - player.teleport(location); - } + TaskManager.runTaskLater(() -> { + if (!TaskManager.TELEPORT_QUEUE.contains(name)) { + MainUtil.sendMessage(player, C.TELEPORT_FAILED); + return; + } + TaskManager.TELEPORT_QUEUE.remove(name); + if (player.isOnline()) { + MainUtil.sendMessage(player, C.TELEPORTED_TO_PLOT); + player.teleport(location); } }, Settings.Teleport.DELAY * 20); return true; @@ -2885,11 +2869,8 @@ public class Plot { final Location pos2 = corners[1]; Location newPos = pos1.clone().add(offsetX, 0, offsetZ); newPos.setWorld(destination.getWorldName()); - ChunkManager.manager.copyRegion(pos1, pos2, newPos, new Runnable() { - @Override public void run() { - ChunkManager.manager.regenerateRegion(pos1, pos2, false, task); - } - }); + ChunkManager.manager.copyRegion(pos1, pos2, newPos, + () -> ChunkManager.manager.regenerateRegion(pos1, pos2, false, task)); } }; Runnable swap = new Runnable() {