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 eventThe explosion event to cancel if a permission sign is found
- */ - private void preventExplosion(ListThe triggered block break event
+ * @param eventThe triggered event
* @param blockThe broken block
* @param playerThe player breaking the block
* @param directionThe 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 blocksThe blocks affected by the event
+ * @param eventThe event to cancel if a permission sign is found
+ */ + private void preventDestruction(ListThe triggered block break event
+ * @param eventThe triggered event
* @param blockThe block to check if it's a sign
* @param playerThe 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 eventThe triggered block break event
+ * @param eventThe triggered event
* @param blockThe block to check if it's a sign
* @param playerThe 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 blockThe block to check
* @param playerThe player that caused the event
- * @param eventThe triggered block break event
+ * @param eventThe 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));