From c03a7ee5b7bd1033b4f8050f7164f75fb3669ae8 Mon Sep 17 00:00:00 2001 From: Stijn Bannink Date: Sun, 13 Aug 2023 18:17:33 +0200 Subject: [PATCH] Improved input API, fixes #63 and closes #66 --- .../sbdevelopment/v10lift/api/V10LiftAPI.java | 8 +-- .../v10lift/api/objects/Lift.java | 4 +- .../v10lift/api/objects/LiftBlock.java | 33 +-------- .../v10lift/api/objects/LiftInput.java | 71 +++++++++++++++++++ .../v10lift/listeners/BlockBreakListener.java | 3 +- .../listeners/PlayerInteractListener.java | 61 ++++++++++------ 6 files changed, 121 insertions(+), 59 deletions(-) create mode 100644 src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftInput.java diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java b/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java index 749386b..b09ecd4 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java @@ -470,14 +470,14 @@ public class V10LiftAPI { lift.getFloors().remove(oldName); lift.getFloors().put(newName, f); sortFloors(lift); - Iterator liter = lift.getInputs().iterator(); - LiftBlock lb; - ArrayList newBlocks = new ArrayList<>(); + Iterator liter = lift.getInputs().iterator(); + LiftInput lb; + ArrayList newBlocks = new ArrayList<>(); while (liter.hasNext()) { lb = liter.next(); if (lb.getFloor().equals(oldName)) { liter.remove(); - newBlocks.add(new LiftBlock(lb.getWorld(), lb.getX(), lb.getY(), lb.getZ(), newName)); + newBlocks.add(new LiftInput(lb.getWorld(), lb.getX(), lb.getY(), lb.getZ(), newName)); } } newBlocks.forEach(nlb -> lift.getInputs().add(nlb)); diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/objects/Lift.java b/src/main/java/tech/sbdevelopment/v10lift/api/objects/Lift.java index dcd5ab7..e1d3874 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/objects/Lift.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/objects/Lift.java @@ -23,8 +23,8 @@ public class Lift { private final TreeSet blocks = new TreeSet<>(); private final LinkedHashMap floors = new LinkedHashMap<>(); private final HashSet signs = new HashSet<>(); - private final HashSet inputs = new HashSet<>(); - private final HashSet offlineInputs = new HashSet<>(); + private final HashSet inputs = new HashSet<>(); + private final HashSet offlineInputs = new HashSet<>(); @Setter private LinkedHashMap queue = null; private final HashSet ropes = new HashSet<>(); diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftBlock.java b/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftBlock.java index bbd7fd8..084b03e 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftBlock.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftBlock.java @@ -37,37 +37,8 @@ public class LiftBlock implements Comparable { private String[] signLines; @Setter private Boolean open; - - //Only used for inputs! - private String floor; - @Setter - private boolean active = false; - - //Only used for chests - public Map[] serializedItemStacks = null; - - /** - * Create a floor based liftblock, without material (no caching) - * - * @param world The world - * @param x The x-pos - * @param y The y-pos - * @param z The z-pos - * @param floor The floorname of the block - */ - public LiftBlock(String world, int x, int y, int z, String floor) { - this.world = world; - this.x = x; - this.y = y; - this.z = z; - this.mat = null; - this.face = null; - this.signLines = null; - this.floor = floor; - this.bisected = null; - this.slabType = null; - this.open = null; - } + //Used for chests + public Map[] serializedItemStacks; /** * Create a new liftblock from a block diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftInput.java b/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftInput.java new file mode 100644 index 0000000..573beba --- /dev/null +++ b/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftInput.java @@ -0,0 +1,71 @@ +package tech.sbdevelopment.v10lift.api.objects; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +import javax.annotation.Nonnull; + +/** + * A lift input object + */ +@Getter +@NoArgsConstructor +@ToString +public class LiftInput implements Comparable { + private String world; + private int x; + private int y; + private int z; + + private String floor; + + /** + * Create a new lift input + * + * @param world The world + * @param x The x-pos + * @param y The y-pos + * @param z The z-pos + * @param floor The floor + */ + public LiftInput(String world, int x, int y, int z, String floor) { + this.world = world; + this.x = x; + this.y = y; + this.z = z; + this.floor = floor; + } + + @Override + public int compareTo(@Nonnull LiftInput lb) { + int ret = Integer.compare(y, lb.y); + if (ret == 0) ret = Integer.compare(x, lb.x); + if (ret == 0) ret = Integer.compare(z, lb.z); + + return ret; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof LiftInput)) return false; + LiftInput other = (LiftInput) o; + return world.equals(other.world) && + x == other.x && + y == other.y && + z == other.z; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((world == null) ? 0 : world.hashCode()); + result = prime * result + x; + result = prime * result + y; + result = prime * result + z; + return result; + } +} diff --git a/src/main/java/tech/sbdevelopment/v10lift/listeners/BlockBreakListener.java b/src/main/java/tech/sbdevelopment/v10lift/listeners/BlockBreakListener.java index 6b68d73..c4c616a 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/listeners/BlockBreakListener.java +++ b/src/main/java/tech/sbdevelopment/v10lift/listeners/BlockBreakListener.java @@ -11,6 +11,7 @@ import tech.sbdevelopment.v10lift.api.V10LiftAPI; import tech.sbdevelopment.v10lift.api.objects.Floor; import tech.sbdevelopment.v10lift.api.objects.Lift; import tech.sbdevelopment.v10lift.api.objects.LiftBlock; +import tech.sbdevelopment.v10lift.api.objects.LiftInput; import tech.sbdevelopment.v10lift.managers.DataManager; import tech.sbdevelopment.v10lift.utils.ConfigUtil; import tech.sbdevelopment.v10lift.utils.DoorUtil; @@ -28,7 +29,7 @@ public class BlockBreakListener implements Listener { return; } - LiftBlock tlb = new LiftBlock(b.getWorld().getName(), b.getX(), b.getY(), b.getZ(), (String) null); + LiftBlock tlb = new LiftBlock(b); for (Map.Entry entry : DataManager.getLifts().entrySet()) { Lift lift = entry.getValue(); if (lift.getBlocks().contains(tlb)) { diff --git a/src/main/java/tech/sbdevelopment/v10lift/listeners/PlayerInteractListener.java b/src/main/java/tech/sbdevelopment/v10lift/listeners/PlayerInteractListener.java index 79d173b..f31adb7 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/listeners/PlayerInteractListener.java +++ b/src/main/java/tech/sbdevelopment/v10lift/listeners/PlayerInteractListener.java @@ -8,6 +8,8 @@ import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.Sign; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.Powerable; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -22,6 +24,7 @@ import tech.sbdevelopment.v10lift.api.V10LiftAPI; import tech.sbdevelopment.v10lift.api.objects.Floor; import tech.sbdevelopment.v10lift.api.objects.Lift; import tech.sbdevelopment.v10lift.api.objects.LiftBlock; +import tech.sbdevelopment.v10lift.api.objects.LiftInput; import tech.sbdevelopment.v10lift.managers.DataManager; import tech.sbdevelopment.v10lift.managers.ForbiddenBlockManager; import tech.sbdevelopment.v10lift.managers.VaultManager; @@ -42,31 +45,47 @@ public class PlayerInteractListener implements Listener { Material button = block.getType(); if (action == Action.RIGHT_CLICK_BLOCK - && e.getHand() != EquipmentSlot.OFF_HAND - && (button.toString().contains("BUTTON") || button == XMaterial.LEVER.parseMaterial())) { + && e.getHand() != EquipmentSlot.OFF_HAND) { String world = block.getWorld().getName(); int x = block.getX(); int y = block.getY(); int z = block.getZ(); + for (Map.Entry entry : DataManager.getLifts().entrySet()) { Lift lift = entry.getValue(); - for (LiftBlock lb : lift.getOfflineInputs()) { - if (world.equals(lb.getWorld()) && x == lb.getX() && y == lb.getY() && z == lb.getZ()) { - lb.setActive(!lb.isActive()); - V10LiftAPI.getInstance().setOffline(entry.getKey(), lb.isActive()); - return; + + for (LiftInput lbi : lift.getOfflineInputs()) { + if (world.equals(lbi.getWorld()) && x == lbi.getX() && y == lbi.getY() && z == lbi.getZ()) { + boolean newState = !lift.isOffline(); + V10LiftAPI.getInstance().setOffline(entry.getKey(), newState); + + //Update all offline inputs + for (LiftInput li : lift.getOfflineInputs()) { + Block b = Bukkit.getWorld(li.getWorld()).getBlockAt(li.getX(), li.getY(), li.getZ()); + BlockData bd = b.getBlockData(); + if (!(bd instanceof Powerable)) { + Bukkit.getLogger().warning("[V10Lift] Block at " + li.getX() + ", " + li.getY() + ", " + li.getZ() + " is not powerable, while it should be an offline input of " + entry.getKey() + "!"); + continue; + } + ((Powerable) bd).setPowered(newState); + b.setBlockData(bd); + } + + break; } } - if (lift.isOffline()) return; + if (lift.isOffline()) break; - for (LiftBlock lb : lift.getInputs()) { - if (world.equals(lb.getWorld()) && x == lb.getX() && y == lb.getY() && z == lb.getZ()) { - V10LiftAPI.getInstance().addToQueue(entry.getKey(), lift.getFloors().get(lb.getFloor()), lb.getFloor()); + for (LiftInput lbi : lift.getInputs()) { + if (world.equals(lbi.getWorld()) && x == lbi.getX() && y == lbi.getY() && z == lbi.getZ()) { + V10LiftAPI.getInstance().addToQueue(entry.getKey(), lift.getFloors().get(lbi.getFloor()), lbi.getFloor()); e.setCancelled(true); - return; + break; } } + + break; } } } @@ -104,7 +123,7 @@ public class PlayerInteractListener implements Listener { return; } - if (!lift.getBlocks().contains(new LiftBlock(sign.getWorld().getName(), sign.getX(), sign.getY(), sign.getZ(), (String) null))) + if (!lift.getBlocks().contains(new LiftBlock(sign.getBlock()))) return; if (DataManager.containsEditLift(liftName)) return; e.setCancelled(true); @@ -160,7 +179,7 @@ public class PlayerInteractListener implements Listener { } else if (DataManager.containsInputEditsPlayer(p.getUniqueId())) { if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return; Block block = e.getClickedBlock(); - LiftBlock tlb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), DataManager.getInputEditsPlayer(p.getUniqueId())); + LiftInput tlb = new LiftInput(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), DataManager.getInputEditsPlayer(p.getUniqueId())); Lift lift = DataManager.getLift(DataManager.getEditPlayer(p.getUniqueId())); e.setCancelled(true); if (lift.getInputs().contains(tlb)) { @@ -173,7 +192,7 @@ public class PlayerInteractListener implements Listener { } else if (DataManager.containsOfflineEditsPlayer(p.getUniqueId())) { if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return; Block block = e.getClickedBlock(); - LiftBlock tlb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), (String) null); + LiftInput tlb = new LiftInput(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), null); Lift lift = DataManager.getLift(DataManager.getEditPlayer(p.getUniqueId())); e.setCancelled(true); if (lift.getOfflineInputs().contains(tlb)) { @@ -186,7 +205,7 @@ public class PlayerInteractListener implements Listener { } else if (DataManager.containsInputRemovesPlayer(p.getUniqueId())) { if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return; Block block = e.getClickedBlock(); - LiftBlock tlb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), (String) null); + LiftInput tlb = new LiftInput(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), null); Lift lift = DataManager.getLift(DataManager.getEditPlayer(p.getUniqueId())); e.setCancelled(true); if (lift.getInputs().contains(tlb)) { @@ -199,7 +218,7 @@ public class PlayerInteractListener implements Listener { } else if (DataManager.containsOfflineRemovesPlayer(p.getUniqueId())) { if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return; Block block = e.getClickedBlock(); - LiftBlock tlb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), (String) null); + LiftInput tlb = new LiftInput(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), null); Lift lift = DataManager.getLift(DataManager.getEditPlayer(p.getUniqueId())); e.setCancelled(true); if (lift.getOfflineInputs().contains(tlb)) { @@ -234,8 +253,8 @@ public class PlayerInteractListener implements Listener { Block now = e.getClickedBlock(); if (start == null) { ConfigUtil.sendMessage(e.getPlayer(), "Rope.ClickOnEnd"); - DataManager.addRopeEditPlayer(p.getUniqueId(), new LiftBlock(now.getWorld().getName(), now.getX(), now.getY(), now.getZ(), (String) null)); - } else if (start.equals(new LiftBlock(now.getWorld().getName(), now.getX(), now.getY(), now.getZ(), (String) null))) { + DataManager.addRopeEditPlayer(p.getUniqueId(), new LiftBlock(now)); + } else if (start.equals(new LiftBlock(now))) { DataManager.addRopeEditPlayer(p.getUniqueId(), null); ConfigUtil.sendMessage(e.getPlayer(), "Rope.PartRemoved"); } else { @@ -306,7 +325,7 @@ public class PlayerInteractListener implements Listener { if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return; e.setCancelled(true); Block block = e.getClickedBlock(); - LiftBlock lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), (String) null); + LiftBlock lb = new LiftBlock(block); DataManager.removeWhoisREQPlayer(p.getUniqueId()); for (Map.Entry entry : DataManager.getLifts().entrySet()) { Lift lift = entry.getValue(); @@ -363,7 +382,7 @@ public class PlayerInteractListener implements Listener { return; } - if (!lift.getBlocks().contains(new LiftBlock(sign.getWorld().getName(), sign.getX(), sign.getY(), sign.getZ(), (String) null))) + if (!lift.getBlocks().contains(new LiftBlock(sign.getBlock()))) return; if (DataManager.containsEditLift(liftName)) return; e.setCancelled(true);