diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Floor.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Floor.java index 5765e8f..ae0238f 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Floor.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Floor.java @@ -17,4 +17,17 @@ public class Floor { this.y = y; this.world = world; } + + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + Floor other = (Floor) obj; + if (getWorld() == null) { + if (other.getWorld() != null) return false; + } else if (!getWorld().equals(other.getWorld())) { + return false; + } + return getY() == other.getY(); + } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java index 99b8e80..e1e8cd6 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java @@ -67,4 +67,15 @@ public class LiftBlock implements Comparable { return ret; } + + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof LiftBlock)) { + if (!(obj instanceof LiftSign)) return false; + LiftSign other = (LiftSign) obj; + return getWorld().equals(other.getWorld()) && getX() == other.getX() && getY() == other.getY() && getZ() == other.getZ(); + } + LiftBlock other = (LiftBlock) obj; + return getWorld().equals(other.getWorld()) && getX() == other.getX() && getY() == other.getY() && getZ() == other.getZ(); + } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftRope.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftRope.java index b295a1a..0b7a625 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftRope.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftRope.java @@ -27,4 +27,17 @@ public class LiftRope { this.currently = minY; this.currentWorld = endWorld; } + + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof LiftRope)) return false; + LiftRope other = (LiftRope) obj; + return getStartWorld().equals(other.getStartWorld()) + && endWorld.equals(other.getEndWorld()) + && getX() == other.getX() + && getMinY() == other.getMinY() + && getMaxY() == other.getMaxY() + && getZ() == other.getZ(); + } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftSign.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftSign.java index 1d102f0..76c776b 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftSign.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftSign.java @@ -21,4 +21,15 @@ public class LiftSign { this.type = type; this.state = state; } + + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof LiftSign)) { + if (!(obj instanceof LiftBlock)) return false; + LiftBlock other = (LiftBlock) obj; + return getWorld().equals(other.getWorld()) && getX() == other.getX() && getY() == other.getY() && getZ() == other.getZ(); + } + LiftSign other = (LiftSign) obj; + return getWorld().equals(other.getWorld()) && getX() == other.getX() && getY() == other.getY() && getZ() == other.getZ(); + } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/V10Entity.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/V10Entity.java index 02be88f..31ee625 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/V10Entity.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/V10Entity.java @@ -5,29 +5,55 @@ import lombok.Setter; import org.bukkit.Location; import org.bukkit.entity.Entity; +import java.util.UUID; + @Getter public class V10Entity { - private final Entity e; + private final Entity entity; private final Location loc; private final int y; @Setter private short step; - public V10Entity(Entity e, Location loc, int y) { - this.e = e; + public V10Entity(Entity entity, Location loc, int y) { + this.entity = entity; this.loc = loc; this.y = y; this.step = 0; } public void moveUp() { - if (e == null || e.isDead()) return; + if (entity == null || entity.isDead()) return; loc.setY(y + step); - e.teleport(loc); + entity.teleport(loc); } public void moveDown() { - if (e == null || e.isDead()) return; + if (entity == null || entity.isDead()) return; loc.setY(y - step); - e.teleport(loc); + entity.teleport(loc); + } + + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + UUID uuid; + if (obj instanceof V10Entity) { + Entity ent = ((V10Entity) obj).getEntity(); + if (ent == null || ent.isDead()) { + return getEntity() == null || getEntity().isDead(); + } + uuid = ent.getUniqueId(); + } else if (obj instanceof Entity) { + Entity ent = (Entity) obj; + if (ent.isDead()) { + return getEntity() == null || getEntity().isDead(); + } + uuid = ent.getUniqueId(); + } else { + return false; + } + + if (getEntity() == null || getEntity().isDead()) return false; + return uuid == getEntity().getUniqueId(); } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java b/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java index e265c3f..2e5f5b0 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java @@ -1,9 +1,6 @@ package nl.SBDeveloper.V10Lift.API; -import nl.SBDeveloper.V10Lift.API.Objects.Floor; -import nl.SBDeveloper.V10Lift.API.Objects.Lift; -import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock; -import nl.SBDeveloper.V10Lift.API.Objects.LiftSign; +import nl.SBDeveloper.V10Lift.API.Objects.*; import nl.SBDeveloper.V10Lift.API.Runnables.DoorCloser; import nl.SBDeveloper.V10Lift.Managers.AntiCopyBlockManager; import nl.SBDeveloper.V10Lift.Managers.DataManager; @@ -576,6 +573,35 @@ public class V10LiftAPI { return 0; } + public boolean containsRope(String liftName, Block block) { + if (liftName == null || block == null || !DataManager.containsLift(liftName)) return false; + + Lift lift = DataManager.getLift(liftName); + if (lift.getRopes().isEmpty()) return false; + + String world = block.getWorld().getName(); + int x = block.getX(); + int y = block.getY(); + int z = block.getZ(); + + for (LiftRope rope : lift.getRopes()) { + if (x != rope.getX() || z != rope.getZ()) continue; + if (rope.getStartWorld().equals(rope.getEndWorld())) { + if (y >= rope.getMinY() && y <= rope.getMaxY()) { + return true; + } + } + } + return false; + } + + public boolean isRope(Block b) { + for (String lift : DataManager.getLifts().keySet()) { + if (containsRope(lift, b)) return true; + } + return false; + } + public void sendLiftInfo(Player p, String liftName) { if (p == null || liftName == null || !DataManager.containsLift(liftName)) return; diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Listeners/BlockBreakListener.java b/src/main/java/nl/SBDeveloper/V10Lift/Listeners/BlockBreakListener.java new file mode 100644 index 0000000..058caf9 --- /dev/null +++ b/src/main/java/nl/SBDeveloper/V10Lift/Listeners/BlockBreakListener.java @@ -0,0 +1,59 @@ +package nl.SBDeveloper.V10Lift.Listeners; + +import nl.SBDeveloper.V10Lift.API.Objects.Floor; +import nl.SBDeveloper.V10Lift.API.Objects.Lift; +import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock; +import nl.SBDeveloper.V10Lift.API.Objects.LiftSign; +import nl.SBDeveloper.V10Lift.Managers.DataManager; +import nl.SBDeveloper.V10Lift.V10LiftPlugin; +import org.bukkit.ChatColor; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; + +import java.util.Map; + +public class BlockBreakListener implements Listener { + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onBlockBreak(BlockBreakEvent e) { + Block b = e.getBlock(); + if (V10LiftPlugin.getAPI().isRope(b)) { + e.getPlayer().sendMessage(ChatColor.RED + "You can't do this! Remove the rope first."); + e.setCancelled(true); + return; + } + + LiftBlock tlb = new LiftBlock(b.getWorld().getName(), b.getX(), b.getY(), b.getZ(), (String) null); + for (Map.Entry entry : DataManager.getLifts().entrySet()) { + Lift lift = entry.getValue(); + if (lift.getBlocks().contains(tlb)) { + e.getPlayer().sendMessage(ChatColor.RED + "You can't do this! Remove the lift first."); + e.setCancelled(true); + return; + } + + for (Floor f : lift.getFloors().values()) { + if (f.getDoorBlocks().contains(tlb)) { + e.getPlayer().sendMessage(ChatColor.RED + "You can't do this! Remove the door first."); + e.setCancelled(true); + return; + } + } + + if (!(b.getState() instanceof Sign)) continue; + + if (!lift.getSigns().contains(tlb)) continue; + + if (!lift.getOwners().contains(e.getPlayer().getUniqueId()) && !e.getPlayer().hasPermission("v10lift.admin")) { + e.getPlayer().sendMessage(ChatColor.RED + "You can't do this!"); + e.setCancelled(true); + } else { + lift.getSigns().remove(tlb); + e.getPlayer().sendMessage(ChatColor.YELLOW + "Lift sign removed!"); + } + } + } +} diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Listeners/EntityDamageListener.java b/src/main/java/nl/SBDeveloper/V10Lift/Listeners/EntityDamageListener.java new file mode 100644 index 0000000..0296833 --- /dev/null +++ b/src/main/java/nl/SBDeveloper/V10Lift/Listeners/EntityDamageListener.java @@ -0,0 +1,43 @@ +package nl.SBDeveloper.V10Lift.Listeners; + +import nl.SBDeveloper.V10Lift.API.Objects.Lift; +import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock; +import nl.SBDeveloper.V10Lift.Managers.DataManager; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageEvent; + +public class EntityDamageListener implements Listener { + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityDamage(EntityDamageEvent e) { + if (e.getCause() != EntityDamageEvent.DamageCause.SUFFOCATION) return; + + Entity entity = e.getEntity(); + Location loc; + if (e instanceof LivingEntity) { + loc = ((LivingEntity) entity).getEyeLocation(); + } else { + loc = entity.getLocation(); + } + + if (loc.getWorld() == null) return; + + String world = loc.getWorld().getName(); + int x = loc.getBlockX(); + int y = loc.getBlockY(); + int z = loc.getBlockZ(); + + for (Lift lift : DataManager.getLifts().values()) { + for (LiftBlock lb : lift.getBlocks()) { + if (world.equals(lb.getWorld()) && x == lb.getX() && y == lb.getY() && z == lb.getZ()) { + e.setCancelled(true); + return; + } + } + } + } +} diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java b/src/main/java/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java new file mode 100644 index 0000000..1291c2d --- /dev/null +++ b/src/main/java/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java @@ -0,0 +1,20 @@ +package nl.SBDeveloper.V10Lift.Listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; + +public class PlayerInteractListener implements Listener { + //BUTTON CLICK + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPlayerInteractButton(PlayerInteractEvent e) { + + } + + //BLOCK ADD + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPlayerInteract(PlayerInteractEvent e) { + + } +} diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Listeners/SignChangeListener.java b/src/main/java/nl/SBDeveloper/V10Lift/Listeners/SignChangeListener.java new file mode 100644 index 0000000..2723d3e --- /dev/null +++ b/src/main/java/nl/SBDeveloper/V10Lift/Listeners/SignChangeListener.java @@ -0,0 +1,53 @@ +package nl.SBDeveloper.V10Lift.Listeners; + +import nl.SBDeveloper.V10Lift.API.Objects.Lift; +import nl.SBDeveloper.V10Lift.API.Objects.LiftSign; +import nl.SBDeveloper.V10Lift.Managers.DataManager; +import org.bukkit.ChatColor; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.SignChangeEvent; + +public class SignChangeListener implements Listener { + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onSignChange(SignChangeEvent e) { + String[] lines = e.getLines(); + if (!lines[0].equalsIgnoreCase("[v10lift]")) return; + + Player p = e.getPlayer(); + if (lines[1].isEmpty()) { + p.sendMessage(ChatColor.RED + "No lift name given!"); + return; + } + + if (!DataManager.containsLift(lines[1])) { + p.sendMessage(ChatColor.RED + "Lift " + lines[1] + " doesn't exists!"); + return; + } + + Lift lift = DataManager.getLift(lines[1]); + if (!lift.getOwners().contains(p.getUniqueId()) && !p.hasPermission("v10lift.admin")) { + p.sendMessage(ChatColor.RED + "You can't do this!"); + e.setCancelled(true); + return; + } + + byte type; + if (lift.getFloors().containsKey(lines[2])) { + type = 1; + e.setLine(3, ChatColor.GRAY + lines[2]); + } else { + type = 0; + } + e.setLine(2, ""); + + Block b = e.getBlock(); + lift.getSigns().add(new LiftSign(b.getWorld().getName(), b.getX(), b.getY(), b.getZ(), type, (byte) 0)); + p.sendMessage(ChatColor.GREEN + "Lift sign created!"); + } + +} diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DataManager.java b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DataManager.java index 308e049..ef26dbe 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DataManager.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DataManager.java @@ -3,10 +3,7 @@ package nl.SBDeveloper.V10Lift.Managers; import nl.SBDeveloper.V10Lift.API.Objects.Lift; import org.bukkit.block.Block; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.UUID; +import java.util.*; public class DataManager { /* A manager for general HashMaps */ @@ -43,6 +40,8 @@ public class DataManager { return lifts.get(liftName); } + public static LinkedHashMap getLifts() { return lifts; } + // // public static boolean containsPlayer(UUID player) { return builds.containsKey(player); diff --git a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java index bda8133..4994b8f 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java @@ -2,8 +2,14 @@ package nl.SBDeveloper.V10Lift; import nl.SBDeveloper.V10Lift.API.V10LiftAPI; import nl.SBDeveloper.V10Lift.Commands.V10LiftCommand; +import nl.SBDeveloper.V10Lift.Listeners.BlockBreakListener; +import nl.SBDeveloper.V10Lift.Listeners.EntityDamageListener; +import nl.SBDeveloper.V10Lift.Listeners.PlayerInteractListener; +import nl.SBDeveloper.V10Lift.Listeners.SignChangeListener; import nl.SBDeveloper.V10Lift.Utils.SBSQLiteDB; import nl.SBDeveloper.V10Lift.Utils.SBYamlFile; +import org.bukkit.Bukkit; +import org.bukkit.block.Sign; import org.bukkit.plugin.java.JavaPlugin; public class V10LiftPlugin extends JavaPlugin { @@ -24,6 +30,13 @@ public class V10LiftPlugin extends JavaPlugin { api = new V10LiftAPI(); getCommand("v10lift").setExecutor(new V10LiftCommand()); + + Bukkit.getPluginManager().registerEvents(new PlayerInteractListener(), this); + Bukkit.getPluginManager().registerEvents(new BlockBreakListener(), this); + Bukkit.getPluginManager().registerEvents(new SignChangeListener(), this); + Bukkit.getPluginManager().registerEvents(new EntityDamageListener(), this); + + getLogger().info("[V10Lift] Plugin loaded successfully!"); } @Override