From 9148e26635ccac500f969dc540ba59ff0083d813 Mon Sep 17 00:00:00 2001 From: Tobias de Bruijn Date: Fri, 4 Jun 2021 17:25:55 +0200 Subject: [PATCH] Fixed #11 & Other fixes - Fixed #8 using PlayerInteractEvent for regular trapdoor interactions and BlockPhysicsEvent for redstone trapdoor interactions - Burnable blocks with a torch attached/ontop can no longer burn - When a block is broken, and a torch is attached to it on the sides, it can now no longer be broken. However this causes an interesting issue, mostly due to my lazyness, I only check the blocks on all 4 sides, but I dont check if the torch is actually dependent on the block being broken. We'll see if people complain --- .../nl/thedutchmc/harotorch/HaroTorch.java | 4 ++ .../events/BlockBreakEventListener.java | 15 ++++++- .../events/BlockBurnEventListener.java | 31 ++++++++++++++ .../events/BlockExplodeEventListener.java | 13 ++++-- .../events/BlockPhysicsEventListener.java | 42 +++++++++++++++++++ .../BlockPistonExtendEventListener.java | 18 +++++--- .../BlockPistonRetractEventListener.java | 17 +++++--- .../events/EntityExplodeEventListener.java | 11 ++++- .../events/PlayerInteractEventListener.java | 29 +++++++++++++ 9 files changed, 165 insertions(+), 15 deletions(-) create mode 100644 src/main/java/nl/thedutchmc/harotorch/events/BlockBurnEventListener.java create mode 100644 src/main/java/nl/thedutchmc/harotorch/events/BlockPhysicsEventListener.java create mode 100644 src/main/java/nl/thedutchmc/harotorch/events/PlayerInteractEventListener.java diff --git a/src/main/java/nl/thedutchmc/harotorch/HaroTorch.java b/src/main/java/nl/thedutchmc/harotorch/HaroTorch.java index 48c61be..3f0148c 100644 --- a/src/main/java/nl/thedutchmc/harotorch/HaroTorch.java +++ b/src/main/java/nl/thedutchmc/harotorch/HaroTorch.java @@ -61,6 +61,10 @@ public void run() { Bukkit.getPluginManager().registerEvents(new EntityExplodeEventListener(), this); Bukkit.getPluginManager().registerEvents(new BlockPistonExtendEventListener(), this); Bukkit.getPluginManager().registerEvents(new BlockPistonRetractEventListener(), this); + Bukkit.getPluginManager().registerEvents(new PlayerInteractEventListener(), this); + Bukkit.getPluginManager().registerEvents(new BlockPhysicsEventListener(), this); + Bukkit.getPluginManager().registerEvents(new BlockBurnEventListener(), this); + //Commands this.getCommand("torch").setExecutor(new TorchCommandExecutor(this)); diff --git a/src/main/java/nl/thedutchmc/harotorch/events/BlockBreakEventListener.java b/src/main/java/nl/thedutchmc/harotorch/events/BlockBreakEventListener.java index 7f5227f..684bc4d 100644 --- a/src/main/java/nl/thedutchmc/harotorch/events/BlockBreakEventListener.java +++ b/src/main/java/nl/thedutchmc/harotorch/events/BlockBreakEventListener.java @@ -20,9 +20,22 @@ public class BlockBreakEventListener implements Listener { public void onBlockBreakEvent(BlockBreakEvent event) { Location loc = event.getBlock().getLocation(); + Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ()); + Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ()); + Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ()); + Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1); + Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1); - if(TorchHandler.isTorch(loc_y_plus_1)) { + if( + ( + TorchHandler.isTorch(loc_y_plus_1) + || TorchHandler.isTorch(loc_x_plus_1) + || TorchHandler.isTorch(loc_x_minus_1) + || TorchHandler.isTorch(loc_z_plus_1) + || TorchHandler.isTorch(loc_z_minus_1) + ) + && !TorchHandler.isTorch(loc)) { event.getPlayer().sendMessage(HaroTorch.getMessagePrefix() + ChatColor.GOLD + LangHandler.activeLang.getLangMessages().get("blockBreakNotAllowedTorchOntop")); event.setCancelled(true); diff --git a/src/main/java/nl/thedutchmc/harotorch/events/BlockBurnEventListener.java b/src/main/java/nl/thedutchmc/harotorch/events/BlockBurnEventListener.java new file mode 100644 index 0000000..1a4ecb8 --- /dev/null +++ b/src/main/java/nl/thedutchmc/harotorch/events/BlockBurnEventListener.java @@ -0,0 +1,31 @@ +package nl.thedutchmc.harotorch.events; + +import org.bukkit.Location; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBurnEvent; + +import nl.thedutchmc.harotorch.torch.TorchHandler; + +public class BlockBurnEventListener implements Listener { + + @EventHandler + public void onBlockBurnEvent(BlockBurnEvent event) { + Location loc = event.getBlock().getLocation(); + + Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ()); + Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ()); + Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ()); + Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1); + Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1); + + if(TorchHandler.isTorch(loc_y_plus_1) + || TorchHandler.isTorch(loc_x_plus_1) + || TorchHandler.isTorch(loc_x_minus_1) + || TorchHandler.isTorch(loc_z_plus_1) + || TorchHandler.isTorch(loc_z_minus_1)) { + + event.setCancelled(true); + } + } +} diff --git a/src/main/java/nl/thedutchmc/harotorch/events/BlockExplodeEventListener.java b/src/main/java/nl/thedutchmc/harotorch/events/BlockExplodeEventListener.java index 2033641..2b70a35 100644 --- a/src/main/java/nl/thedutchmc/harotorch/events/BlockExplodeEventListener.java +++ b/src/main/java/nl/thedutchmc/harotorch/events/BlockExplodeEventListener.java @@ -22,10 +22,17 @@ public void onBlockExplodeEvent(BlockExplodeEvent event) { continue; } - - //Check if the block above the block being exploded is a torch Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ()); - if(TorchHandler.isTorch(loc_y_plus_1)) { + Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ()); + Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ()); + Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1); + Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1); + if(TorchHandler.isTorch(loc_y_plus_1) + || TorchHandler.isTorch(loc_x_plus_1) + || TorchHandler.isTorch(loc_x_minus_1) + || TorchHandler.isTorch(loc_z_plus_1) + || TorchHandler.isTorch(loc_z_minus_1)) { + event.setCancelled(true); } } diff --git a/src/main/java/nl/thedutchmc/harotorch/events/BlockPhysicsEventListener.java b/src/main/java/nl/thedutchmc/harotorch/events/BlockPhysicsEventListener.java new file mode 100644 index 0000000..1b5257d --- /dev/null +++ b/src/main/java/nl/thedutchmc/harotorch/events/BlockPhysicsEventListener.java @@ -0,0 +1,42 @@ +package nl.thedutchmc.harotorch.events; + +import org.bukkit.Location; +import org.bukkit.block.data.Openable; +import org.bukkit.block.data.type.TrapDoor; +import org.bukkit.event.EventHandler; +import org.bukkit.event.block.BlockPhysicsEvent; + +import nl.thedutchmc.harotorch.torch.TorchHandler; + +import org.bukkit.event.Listener; + +public class BlockPhysicsEventListener implements Listener { + + @EventHandler + public void onBlockPhysicsEvent(BlockPhysicsEvent event) { + if(event.getBlock().getType().data != TrapDoor.class) { + return; + } + + Openable blockState = (Openable) event.getBlock().getBlockData(); + if(blockState.isOpen()) { + Location loc = event.getBlock().getLocation(); + + Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ()); + Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ()); + Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ()); + Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1); + Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1); + + if(TorchHandler.isTorch(loc_y_plus_1) + || TorchHandler.isTorch(loc_x_plus_1) + || TorchHandler.isTorch(loc_x_minus_1) + || TorchHandler.isTorch(loc_z_plus_1) + || TorchHandler.isTorch(loc_z_minus_1)) { + + blockState.setOpen(false); + event.getBlock().setBlockData(blockState); + } + } + } +} diff --git a/src/main/java/nl/thedutchmc/harotorch/events/BlockPistonExtendEventListener.java b/src/main/java/nl/thedutchmc/harotorch/events/BlockPistonExtendEventListener.java index 41e78b4..12c89f6 100644 --- a/src/main/java/nl/thedutchmc/harotorch/events/BlockPistonExtendEventListener.java +++ b/src/main/java/nl/thedutchmc/harotorch/events/BlockPistonExtendEventListener.java @@ -14,17 +14,25 @@ public class BlockPistonExtendEventListener implements Listener { public void onBlockPistonExtendEvent(BlockPistonExtendEvent event) { for(Block b : event.getBlocks()) { //The block being moved - Location movedBlockLocation = b.getLocation(); + Location loc = b.getLocation(); //The block above the block being moved. If this is a torch we don't want to move the block below that torch, as it'd break the torch. - Location movedBlockLocationAbove = b.getLocation(); - movedBlockLocationAbove.setY(b.getLocation().getY() +1); + Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ()); + Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ()); + Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ()); + Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1); + Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1); - if(TorchHandler.isTorch(movedBlockLocation)) { + if(TorchHandler.isTorch(loc)) { event.setCancelled(true); } - if(TorchHandler.isTorch(movedBlockLocationAbove)) { + if(TorchHandler.isTorch(loc_y_plus_1) + || TorchHandler.isTorch(loc_x_plus_1) + || TorchHandler.isTorch(loc_x_minus_1) + || TorchHandler.isTorch(loc_z_plus_1) + || TorchHandler.isTorch(loc_z_minus_1)) { + event.setCancelled(true); } } diff --git a/src/main/java/nl/thedutchmc/harotorch/events/BlockPistonRetractEventListener.java b/src/main/java/nl/thedutchmc/harotorch/events/BlockPistonRetractEventListener.java index 32e2738..6caaef8 100644 --- a/src/main/java/nl/thedutchmc/harotorch/events/BlockPistonRetractEventListener.java +++ b/src/main/java/nl/thedutchmc/harotorch/events/BlockPistonRetractEventListener.java @@ -14,17 +14,24 @@ public class BlockPistonRetractEventListener implements Listener { public void onBlockPistonRetractEvent(BlockPistonRetractEvent event) { for(Block b : event.getBlocks()) { //The block being moved - Location movedBlockLocation = b.getLocation(); + Location loc = b.getLocation(); //The block above the block being moved. If this is a torch we don't want to move the block below that torch, as it'd break the torch. - Location movedBlockLocationAbove = b.getLocation(); - movedBlockLocationAbove.setY(b.getLocation().getY() +1); + Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ()); + Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ()); + Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ()); + Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1); + Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1); - if(TorchHandler.isTorch(movedBlockLocation)) { + if(TorchHandler.isTorch(loc)) { event.setCancelled(true); } - if(TorchHandler.isTorch(movedBlockLocationAbove)) { + if(TorchHandler.isTorch(loc_y_plus_1) + || TorchHandler.isTorch(loc_x_plus_1) + || TorchHandler.isTorch(loc_x_minus_1) + || TorchHandler.isTorch(loc_z_plus_1) + || TorchHandler.isTorch(loc_z_minus_1)) { event.setCancelled(true); } } diff --git a/src/main/java/nl/thedutchmc/harotorch/events/EntityExplodeEventListener.java b/src/main/java/nl/thedutchmc/harotorch/events/EntityExplodeEventListener.java index 25c7cfb..4e29c12 100644 --- a/src/main/java/nl/thedutchmc/harotorch/events/EntityExplodeEventListener.java +++ b/src/main/java/nl/thedutchmc/harotorch/events/EntityExplodeEventListener.java @@ -29,7 +29,16 @@ public void onEntityExplodeEvent(EntityExplodeEvent event) { //Check if the block above the block being exploded is a torch Location loc_y_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY() + 1, loc.getZ()); - if(TorchHandler.isTorch(loc_y_plus_1)) { + Location loc_x_plus_1 = new Location(loc.getWorld(), loc.getX() +1, loc.getY(), loc.getZ()); + Location loc_x_minus_1 = new Location(loc.getWorld(), loc.getX() -1, loc.getY(), loc.getZ()); + Location loc_z_plus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() +1); + Location loc_z_minus_1 = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ() -1); + + if(TorchHandler.isTorch(loc_y_plus_1) + || TorchHandler.isTorch(loc_x_plus_1) + || TorchHandler.isTorch(loc_x_minus_1) + || TorchHandler.isTorch(loc_z_plus_1) + || TorchHandler.isTorch(loc_z_minus_1)) { blocksDontRemove.add(b); } } diff --git a/src/main/java/nl/thedutchmc/harotorch/events/PlayerInteractEventListener.java b/src/main/java/nl/thedutchmc/harotorch/events/PlayerInteractEventListener.java new file mode 100644 index 0000000..7a66c93 --- /dev/null +++ b/src/main/java/nl/thedutchmc/harotorch/events/PlayerInteractEventListener.java @@ -0,0 +1,29 @@ +package nl.thedutchmc.harotorch.events; + +import org.bukkit.Location; +import org.bukkit.block.data.type.TrapDoor; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; + +import nl.thedutchmc.harotorch.torch.TorchHandler; + + +public class PlayerInteractEventListener implements Listener { + + @EventHandler + public void onPlayerInteractEvent(PlayerInteractEvent event) { + if(event.getClickedBlock() == null) { + return; + } + + if(event.getClickedBlock().getType().data == TrapDoor.class) { + Location clickedBlockLoc = event.getClickedBlock().getLocation(); + Location possibleTorchLoc = new Location(clickedBlockLoc.getWorld(), clickedBlockLoc.getX(), clickedBlockLoc.getY() + 1d, clickedBlockLoc.getZ()); + + if(TorchHandler.isTorch(possibleTorchLoc)) { + event.setCancelled(true); + } + } + } +}