diff --git a/src/main/java/net/knarcraft/permissionsigns/listener/BlockListener.java b/src/main/java/net/knarcraft/permissionsigns/listener/BlockListener.java index 1445a90..4e0a7de 100644 --- a/src/main/java/net/knarcraft/permissionsigns/listener/BlockListener.java +++ b/src/main/java/net/knarcraft/permissionsigns/listener/BlockListener.java @@ -16,6 +16,8 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockExplodeEvent; import org.bukkit.event.block.BlockPhysicsEvent; +import org.bukkit.event.block.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; import org.bukkit.event.entity.EntityExplodeEvent; import java.util.ArrayList; @@ -36,14 +38,24 @@ public class BlockListener implements Listener { } } + @EventHandler + public void onPistonExtend(BlockPistonExtendEvent event) { + preventDestruction(event.getBlocks(), event); + } + + @EventHandler + public void onPistonRetract(BlockPistonRetractEvent event) { + preventDestruction(event.getBlocks(), event); + } + @EventHandler public void onBlockExplode(BlockExplodeEvent event) { - preventExplosion(event.blockList(), event); + preventDestruction(event.blockList(), event); } @EventHandler public void onEntityExplode(EntityExplodeEvent event) { - preventExplosion(event.blockList(), event); + preventDestruction(event.blockList(), event); } @EventHandler @@ -67,35 +79,15 @@ public class BlockListener implements Listener { } } - /** - * Prevents explosions from destroying permission signs - * - * @param blocks

The blocks affected by the explosion

- * @param event

The explosion event to cancel if a permission sign is found

- */ - private void preventExplosion(List blocks, Cancellable event) { - for (Block block : blocks) { - if (!Tag.SIGNS.isTagged(block.getBlockData().getMaterial())) { - continue; - } - Sign sign = (Sign) block.getState(); - boolean registered = SignManager.getSign(sign.getLocation()) != null; - if (registered) { - event.setCancelled(true); - break; - } - } - } - /** * Protects signs on falling blocks in the given direction * - * @param event

The triggered block break event

+ * @param event

The triggered event

* @param block

The broken block

* @param player

The player breaking the block

* @param direction

The direction to check for affected blocks

*/ - private void protectSignsInDirection(BlockBreakEvent event, Block block, Player player, BlockFace direction) { + private void protectSignsInDirection(Cancellable event, Block block, Player player, BlockFace direction) { Block directionBlock = block.getRelative(direction); while ((direction == BlockFace.DOWN && directionBlock.getBlockData().getMaterial() == Material.POINTED_DRIPSTONE) || (direction == BlockFace.UP && @@ -108,14 +100,37 @@ public class BlockListener implements Listener { } } + /** + * Prevents an event from destroying permission signs + * + * @param blocks

The blocks affected by the event

+ * @param event

The event to cancel if a permission sign is found

+ */ + private void preventDestruction(List blocks, Cancellable event) { + for (Block block : blocks) { + protectBlockIfPermissionSign(event, block, null); + if (event.isCancelled()) { + return; + } + + if (extensiveSignProtectionEnabled()) { + protectSignsInDirection(event, block, null, BlockFace.UP); + if (event.isCancelled()) { + return; + } + protectSignsInDirection(event, block, null, BlockFace.DOWN); + } + } + } + /** * Protects the given sign * - * @param event

The triggered block break event

+ * @param event

The triggered event

* @param block

The block to check if it's a sign

* @param player

The player breaking the block

*/ - private void protectBlockIfPermissionSign(BlockBreakEvent event, Block block, Player player) { + private void protectBlockIfPermissionSign(Cancellable event, Block block, Player player) { //Protect the permission sign itself Material material = block.getBlockData().getMaterial(); if (Tag.SIGNS.isTagged(material)) { @@ -131,11 +146,11 @@ public class BlockListener implements Listener { /** * Looks for permission signs adjacent to the current block * - * @param event

The triggered block break event

+ * @param event

The triggered event

* @param block

The block to check if it's a sign

* @param player

The player breaking the block

*/ - private void lookForAdjacentPermissionSigns(BlockBreakEvent event, Block block, Player player) { + private void lookForAdjacentPermissionSigns(Cancellable event, Block block, Player player) { Block aboveBlock = block.getRelative(BlockFace.UP); if (Tag.STANDING_SIGNS.isTagged(aboveBlock.getBlockData().getMaterial())) { checkIfBlockIsPermissionSign(aboveBlock, player, event); @@ -162,14 +177,18 @@ public class BlockListener implements Listener { * * @param block

The block to check

* @param player

The player that caused the event

- * @param event

The triggered block break event

+ * @param event

The triggered event

*/ - private void checkIfBlockIsPermissionSign(Block block, Player player, BlockBreakEvent event) { + private void checkIfBlockIsPermissionSign(Block block, Player player, Cancellable event) { Sign sign = (Sign) block.getState(); boolean registered = SignManager.getSign(sign.getLocation()) != null; if (!registered) { return; } + if (player == null) { + event.setCancelled(true); + return; + } if (!player.hasPermission("permissionsigns.admin.create")) { event.setCancelled(true); player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_DENY));