From 6d922f4f73e6bf66b6f06b47f79c4dff96efcd01 Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Wed, 26 Feb 2020 11:33:35 +0100 Subject: [PATCH] Added real door support --- .../V10Lift/API/Objects/Floor.java | 2 + .../SBDeveloper/V10Lift/API/V10LiftAPI.java | 28 +++++++ .../V10Lift/Commands/V10LiftCommand.java | 4 +- .../Listeners/PlayerInteractListener.java | 20 +++-- .../Managers/ForbiddenBlockManager.java | 4 +- .../SBDeveloper/V10Lift/Utils/DoorUtil.java | 80 +++++++++++++++++++ 6 files changed, 129 insertions(+), 9 deletions(-) create mode 100644 src/main/lombok/nl/SBDeveloper/V10Lift/Utils/DoorUtil.java diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/API/Objects/Floor.java b/src/main/lombok/nl/SBDeveloper/V10Lift/API/Objects/Floor.java index efaf4cf..70cc120 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/API/Objects/Floor.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/API/Objects/Floor.java @@ -13,6 +13,7 @@ public class Floor { private String world; private int y; private ArrayList doorBlocks = new ArrayList<>(); + private ArrayList realDoorBlocks = new ArrayList<>(); private HashSet userWhitelist = new HashSet<>(); private HashSet groupWhitelist = new HashSet<>(); @@ -48,6 +49,7 @@ public class Floor { "world='" + world + '\'' + ", y=" + y + ", doorBlocks=" + doorBlocks + + ", realDoorBlocks=" + realDoorBlocks + ", userWhitelist=" + userWhitelist + ", groupWhitelist=" + groupWhitelist + '}'; diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java b/src/main/lombok/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java index 7b67409..fb32a5c 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java @@ -8,6 +8,7 @@ import nl.SBDeveloper.V10Lift.Managers.DataManager; import nl.SBDeveloper.V10Lift.Managers.ForbiddenBlockManager; import nl.SBDeveloper.V10Lift.Utils.ConfigUtil; import nl.SBDeveloper.V10Lift.Utils.DirectionUtil; +import nl.SBDeveloper.V10Lift.Utils.DoorUtil; import nl.SBDeveloper.V10Lift.Utils.XMaterial; import nl.SBDeveloper.V10Lift.V10LiftPlugin; import nl.SBDevelopment.SBUtilities.Utils.LocationSerializer; @@ -387,6 +388,11 @@ public class V10LiftAPI { block.setType(Material.AIR); } + for (LiftBlock lb : f.getRealDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at openDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + DoorUtil.openDoor(block); + } + lift.setDoorOpen(f); if (lift.isRealistic()) { @@ -416,6 +422,11 @@ public class V10LiftAPI { block.setType(Material.AIR); } + for (LiftBlock lb : f.getRealDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at openDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + DoorUtil.openDoor(block); + } + lift.setDoorOpen(f); if (lift.isRealistic()) { @@ -456,6 +467,19 @@ public class V10LiftAPI { } if (blocked) break; } + + for (LiftBlock lb : lift.getDoorOpen().getRealDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at closeDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + for (Entity ent : block.getChunk().getEntities()) { + Location loc = ent.getLocation(); + + if (loc.getBlockX() == lb.getX() && loc.getBlockY() == lb.getY() && loc.getBlockZ() == lb.getZ()) { + blocked = true; + break; + } + } + if (blocked) break; + } } if (!blocked) { @@ -468,6 +492,10 @@ public class V10LiftAPI { } state.update(true); } + for (LiftBlock lb : lift.getDoorOpen().getRealDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at closeDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + DoorUtil.closeDoor(block); + } lift.setDoorOpen(null); if (lift.getDoorCloser() != null) lift.getDoorCloser().stop(); } diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java b/src/main/lombok/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java index 1d49bac..5b2f70d 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java @@ -571,7 +571,7 @@ public class V10LiftCommand implements CommandExecutor { String floor = null; if (args.length < 4) { Block b = p.getLocation().getBlock(); - Floor f = new Floor(b.getY() - 1, Objects.requireNonNull(b.getWorld(), "World was null at doorCommand").getName()); + Floor f = new Floor(b.getY() - 1, Objects.requireNonNull(b.getWorld(), "World was null at whitelistCommand").getName()); if (!lift.getFloors().containsValue(f)) { sender.sendMessage(ChatColor.RED + "Automatic floor detection failed!"); return true; @@ -667,7 +667,7 @@ public class V10LiftCommand implements CommandExecutor { } } DataManager.addDoorEditPlayer(p.getUniqueId(), floor); - sender.sendMessage(ChatColor.GREEN + "Now right-click on the door blocks!"); + sender.sendMessage(ChatColor.GREEN + "Now right-click on the door blocks! (If it are real blocks, click on the bottom block)"); sender.sendMessage(ChatColor.GREEN + "Then do /v10lift door to save it."); return true; } diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java b/src/main/lombok/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java index 644aa9d..cb88e82 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java @@ -6,6 +6,7 @@ import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock; import nl.SBDeveloper.V10Lift.Managers.DataManager; import nl.SBDeveloper.V10Lift.Managers.VaultManager; import nl.SBDeveloper.V10Lift.Utils.ConfigUtil; +import nl.SBDeveloper.V10Lift.Utils.DoorUtil; import nl.SBDeveloper.V10Lift.Utils.XMaterial; import nl.SBDeveloper.V10Lift.V10LiftPlugin; import org.bukkit.Bukkit; @@ -292,12 +293,21 @@ public class PlayerInteractListener implements Listener { } Lift lift = DataManager.getLift(DataManager.getEditPlayer(p.getUniqueId())); Floor floor = lift.getFloors().get(DataManager.getDoorEditPlayer(p.getUniqueId())); - if (floor.getDoorBlocks().contains(lb)) { - floor.getDoorBlocks().remove(lb); - p.sendMessage(ChatColor.GOLD + "Door removed."); - return; + if (DoorUtil.isOpenable(block)) { + if (floor.getRealDoorBlocks().contains(lb)) { + floor.getRealDoorBlocks().remove(lb); + p.sendMessage(ChatColor.GOLD + "Door removed."); + return; + } + floor.getRealDoorBlocks().add(lb); + } else { + if (floor.getDoorBlocks().contains(lb)) { + floor.getDoorBlocks().remove(lb); + p.sendMessage(ChatColor.GOLD + "Door removed."); + return; + } + floor.getDoorBlocks().add(lb); } - floor.getDoorBlocks().add(lb); p.sendMessage(ChatColor.GREEN + "Door created."); } else if (DataManager.containsWhoisREQPlayer(p.getUniqueId())) { if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return; diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/Managers/ForbiddenBlockManager.java b/src/main/lombok/nl/SBDeveloper/V10Lift/Managers/ForbiddenBlockManager.java index b459e7b..ed82a1b 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/Managers/ForbiddenBlockManager.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/Managers/ForbiddenBlockManager.java @@ -10,13 +10,13 @@ public class ForbiddenBlockManager { public ForbiddenBlockManager() { //TODO Add more forbidden materials - forbidden.add(XMaterial.ACACIA_DOOR); + /*forbidden.add(XMaterial.ACACIA_DOOR); forbidden.add(XMaterial.BIRCH_DOOR); forbidden.add(XMaterial.DARK_OAK_DOOR); forbidden.add(XMaterial.IRON_DOOR); forbidden.add(XMaterial.JUNGLE_DOOR); forbidden.add(XMaterial.OAK_DOOR); - forbidden.add(XMaterial.SPRUCE_DOOR); + forbidden.add(XMaterial.SPRUCE_DOOR);*/ forbidden.add(XMaterial.BLACK_BED); forbidden.add(XMaterial.BLUE_BED); forbidden.add(XMaterial.BROWN_BED); diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/Utils/DoorUtil.java b/src/main/lombok/nl/SBDeveloper/V10Lift/Utils/DoorUtil.java new file mode 100644 index 0000000..05eb41f --- /dev/null +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/Utils/DoorUtil.java @@ -0,0 +1,80 @@ +package nl.SBDeveloper.V10Lift.Utils; + +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; + +public class DoorUtil { + + /* Gate codes sponsored by MrWouter <3 */ + @SuppressWarnings("deprecation") + public static boolean openDoor(Block b) { + if (XMaterial.isNewVersion()) { + org.bukkit.block.data.BlockData blockData = b.getBlockData(); + if (isOpenable(b)) { + org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData; + if (op.isOpen()) { + return false; + } + op.setOpen(true); + b.setBlockData(blockData); + return true; + } + } else { + BlockState state = b.getState(); + if (isOpenable(b)) { + org.bukkit.material.Openable openable = (org.bukkit.material.Openable) state.getData(); + if (openable.isOpen()) { + return false; + } + openable.setOpen(true); + state.setData((org.bukkit.material.MaterialData) openable); + state.update(); + return true; + } + } + return false; + } + + /* Gate codes sponsored by MrWouter <3 */ + @SuppressWarnings("deprecation") + public static boolean closeDoor(Block b) { + if (XMaterial.isNewVersion()) { + org.bukkit.block.data.BlockData blockData = b.getBlockData(); + if (isOpenable(b)) { + org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData; + if (!op.isOpen()) { + return false; + } + op.setOpen(false); + b.setBlockData(blockData); + return true; + } + } else { + BlockState state = b.getState(); + if (isOpenable(b)) { + org.bukkit.material.Openable openable = (org.bukkit.material.Openable) state.getData(); + if (!openable.isOpen()) { + return false; + } + openable.setOpen(false); + state.setData((org.bukkit.material.MaterialData) openable); + state.update(); + return true; + } + } + return false; + } + + /* Gate codes sponsored by MrWouter <3 */ + public static boolean isOpenable(Block b) { + if (b == null) { + return false; + } + if (XMaterial.isNewVersion()) { + return b.getBlockData() instanceof org.bukkit.block.data.Openable; + } else { + return b.getState().getData() instanceof org.bukkit.material.Openable; + } + } + +} \ No newline at end of file