From 1ce6be5944fd2b8db0804d588747340ad88751cc Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 18 Jul 2011 00:50:03 +0200 Subject: [PATCH] Protection of signs against pistons --- .../com/earth2me/essentials/Essentials.java | 2 + .../essentials/signs/EssentialsSign.java | 12 ++- .../essentials/signs/SignBlockListener.java | 78 ++++++++++++++++--- .../essentials/signs/SignProtection.java | 76 ++++++++++-------- 4 files changed, 118 insertions(+), 50 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 9bd0998f8..45a2aff60 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -197,6 +197,8 @@ public class Essentials extends JavaPlugin implements IEssentials pm.registerEvent(Type.BLOCK_BREAK, signBlockListener, Priority.Highest, this); pm.registerEvent(Type.BLOCK_IGNITE, signBlockListener, Priority.Low, this); pm.registerEvent(Type.BLOCK_BURN, signBlockListener, Priority.Low, this); + pm.registerEvent(Type.BLOCK_PISTON_EXTEND, signBlockListener, Priority.Low, this); + pm.registerEvent(Type.BLOCK_PISTON_RETRACT, signBlockListener, Priority.Low, this); final SignPlayerListener signPlayerListener = new SignPlayerListener(this); pm.registerEvent(Type.PLAYER_INTERACT, signPlayerListener, Priority.Low, this); diff --git a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java index c4853c31e..4884ce6f0 100644 --- a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java +++ b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java @@ -189,6 +189,11 @@ public class EssentialsSign return true; } + public boolean onBlockPush(Block block, IEssentials ess) + { + return true; + } + public static boolean checkIfBlockBreaksSigns(final Block block) { if (block.getFace(BlockFace.UP).getType() == Material.SIGN_POST) @@ -333,7 +338,7 @@ public class EssentialsSign protected final Double getDoublePositive(final String line) throws SignException { final double quantity = getDouble(line); - if (Math.round(quantity*100.0) < 1.0) + if (Math.round(quantity * 100.0) < 1.0) { throw new SignException(Util.i18n("moreThanZero")); } @@ -425,7 +430,6 @@ public class EssentialsSign { return; } - } @@ -454,7 +458,7 @@ public class EssentialsSign { return block; } - + public final void updateSign() { sign.update(); @@ -469,7 +473,7 @@ public class EssentialsSign void setLine(final int index, final String text); public Block getBlock(); - + void updateSign(); } } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignBlockListener.java b/Essentials/src/com/earth2me/essentials/signs/SignBlockListener.java index c452e1b63..7574b7691 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignBlockListener.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignBlockListener.java @@ -13,6 +13,8 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBurnEvent; import org.bukkit.event.block.BlockIgniteEvent; import org.bukkit.event.block.BlockListener; +import org.bukkit.event.block.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.SignChangeEvent; @@ -21,12 +23,12 @@ public class SignBlockListener extends BlockListener { private final transient IEssentials ess; private final static Logger LOGGER = Logger.getLogger("Minecraft"); - + public SignBlockListener(IEssentials ess) { this.ess = ess; } - + @Override public void onBlockBreak(final BlockBreakEvent event) { @@ -34,13 +36,13 @@ public class SignBlockListener extends BlockListener { return; } - + if (protectSignsAndBlocks(event.getBlock(), event.getPlayer())) { event.setCancelled(true); } } - + public boolean protectSignsAndBlocks(final Block block, final Player player) { final int mat = block.getTypeId(); @@ -78,7 +80,7 @@ public class SignBlockListener extends BlockListener } return false; } - + @Override public void onSignChange(final SignChangeEvent event) { @@ -104,12 +106,13 @@ public class SignBlockListener extends BlockListener User user = ess.getUser(event.getPlayer()); if (user.isAuthorized("essentials.signs.color")) { - for (int i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) + { event.setLine(i, event.getLine(i).replaceAll("&([0-9a-f])", "§$1")); } } } - + @Override public void onBlockPlace(final BlockPlaceEvent event) { @@ -117,7 +120,7 @@ public class SignBlockListener extends BlockListener { return; } - + final Block against = event.getBlockAgainst(); if (against.getType() == Material.WALL_SIGN || against.getType() == Material.SIGN_POST) @@ -142,7 +145,7 @@ public class SignBlockListener extends BlockListener } } } - + @Override public void onBlockBurn(final BlockBurnEvent event) { @@ -150,7 +153,7 @@ public class SignBlockListener extends BlockListener { return; } - + final Block block = event.getBlock(); if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST @@ -170,7 +173,7 @@ public class SignBlockListener extends BlockListener } } } - + @Override public void onBlockIgnite(final BlockIgniteEvent event) { @@ -178,10 +181,61 @@ public class SignBlockListener extends BlockListener { return; } - + if (protectSignsAndBlocks(event.getBlock(), event.getPlayer())) { event.setCancelled(true); } } + + @Override + public void onBlockPistonExtend(BlockPistonExtendEvent event) + { + for (Block block : event.getBlocks()) + { + if ((block.getType() == Material.WALL_SIGN + || block.getType() == Material.SIGN_POST + || EssentialsSign.checkIfBlockBreaksSigns(block))) + { + event.setCancelled(true); + return; + } + for (Signs signs : Signs.values()) + { + final EssentialsSign sign = signs.getSign(); + if (sign.getBlocks().contains(block.getType()) + && !sign.onBlockPush(block, ess)) + { + event.setCancelled(true); + return; + } + } + } + } + + @Override + public void onBlockPistonRetract(BlockPistonRetractEvent event) + { + if (event.isSticky()) + { + final Block block = event.getBlock(); + if ((block.getType() == Material.WALL_SIGN + || block.getType() == Material.SIGN_POST + || EssentialsSign.checkIfBlockBreaksSigns(block))) + { + event.setCancelled(true); + return; + } + for (Signs signs : Signs.values()) + { + final EssentialsSign sign = signs.getSign(); + if (sign.getBlocks().contains(block.getType()) + && !sign.onBlockPush(block, ess)) + { + event.setCancelled(true); + return; + } + } + } + } } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignProtection.java b/Essentials/src/com/earth2me/essentials/signs/SignProtection.java index 555bb0114..1e2813b5b 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignProtection.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignProtection.java @@ -21,7 +21,7 @@ import org.bukkit.inventory.ItemStack; public class SignProtection extends EssentialsSign { private final transient Set protectedBlocks = EnumSet.noneOf(Material.class); - + public SignProtection() { super("Protection"); @@ -30,7 +30,7 @@ public class SignProtection extends EssentialsSign protectedBlocks.add(Material.FURNACE); protectedBlocks.add(Material.DISPENSER); } - + @Override protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException { @@ -43,14 +43,14 @@ public class SignProtection extends EssentialsSign player.sendMessage("§4You are not allowed to create sign here."); return false; } - + @Override protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException { final SignProtectionState state = checkProtectionSign(sign, player, username); return state == SignProtectionState.OWNER; } - + public boolean hasAdjacentBlock(final Block block, final Block... ignoredBlocks) { final Block[] faces = getAdjacentBlocks(block); @@ -70,7 +70,7 @@ public class SignProtection extends EssentialsSign } return false; } - + private void checkIfSignsAreBroken(final Block block, final User player, final String username, final IEssentials ess) { final Map signs = getConnectedSigns(block, player, username, false); @@ -88,14 +88,14 @@ public class SignProtection extends EssentialsSign } } } - + private Map getConnectedSigns(final Block block, final User user, final String username, boolean secure) { final Map signs = new HashMap(); getConnectedSigns(block, signs, user, username, secure ? 4 : 2); return signs; } - + private void getConnectedSigns(final Block block, final Map signs, final User user, final String username, final int depth) { final Block[] faces = getAdjacentBlocks(block); @@ -108,20 +108,20 @@ public class SignProtection extends EssentialsSign } final SignProtectionState check = checkProtectionSign(b, user, username); signs.put(loc, check); - + if (protectedBlocks.contains(b.getType()) && depth > 0) { getConnectedSigns(b, signs, user, username, depth - 1); } } } - - + + public enum SignProtectionState { NOT_ALLOWED, ALLOWED, NOSIGN, OWNER } - + private SignProtectionState checkProtectionSign(final Block block, final User user, final String username) { if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN) @@ -134,7 +134,7 @@ public class SignProtection extends EssentialsSign } return SignProtectionState.NOSIGN; } - + private SignProtectionState checkProtectionSign(final ISign sign, final User user, final String username) { if (user == null || username == null) @@ -163,7 +163,7 @@ public class SignProtection extends EssentialsSign } return SignProtectionState.NOT_ALLOWED; } - + private Block[] getAdjacentBlocks(final Block block) { return new Block[] @@ -176,7 +176,7 @@ public class SignProtection extends EssentialsSign block.getFace(BlockFace.UP) }; } - + public SignProtectionState isBlockProtected(final Block block, final User user, final String username, boolean secure) { final Map signs = getConnectedSigns(block, user, username, secure); @@ -194,7 +194,7 @@ public class SignProtection extends EssentialsSign } return retstate; } - + public boolean isBlockProtected(final Block block) { final Block[] faces = getAdjacentBlocks(block); @@ -211,7 +211,7 @@ public class SignProtection extends EssentialsSign if (protectedBlocks.contains(b.getType())) { final Block[] faceChest = getAdjacentBlocks(b); - + for (Block a : faceChest) { if (a.getType() == Material.SIGN_POST || a.getType() == Material.WALL_SIGN) @@ -227,20 +227,20 @@ public class SignProtection extends EssentialsSign } return false; } - + @Override public Set getBlocks() { return protectedBlocks; } - + @Override protected boolean onBlockPlace(final Block block, final User player, final String username, final IEssentials ess) throws SignException { for (Block adjBlock : getAdjacentBlocks(block)) { final SignProtectionState state = isBlockProtected(adjBlock, player, username, true); - + if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED) && !player.isAuthorized("essentials.signs.protection.override")) { @@ -249,66 +249,74 @@ public class SignProtection extends EssentialsSign } } return true; - + } - + @Override protected boolean onBlockInteract(final Block block, final User player, final String username, final IEssentials ess) throws SignException { final SignProtectionState state = isBlockProtected(block, player, username, false); - + if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN || state == SignProtectionState.ALLOWED) { return true; } - + if (state == SignProtectionState.NOT_ALLOWED && player.isAuthorized("essentials.signs.protection.override")) { return true; } - - + + player.sendMessage(Util.format("noAccessPermission", block.getType().toString().toLowerCase())); return false; } - + @Override protected boolean onBlockBreak(final Block block, final User player, final String username, final IEssentials ess) throws SignException { final SignProtectionState state = isBlockProtected(block, player, username, false); - + if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN) { checkIfSignsAreBroken(block, player, username, ess); return true; } - + if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED) && player.isAuthorized("essentials.signs.protection.override")) { checkIfSignsAreBroken(block, player, username, ess); return true; } - - + + player.sendMessage(Util.format("noDestroyPermission", block.getType().toString().toLowerCase())); return false; } - + @Override public boolean onBlockExplode(final Block block, final IEssentials ess) { final SignProtectionState state = isBlockProtected(block, null, null, false); - + return state == SignProtectionState.NOSIGN; } - + @Override public boolean onBlockBurn(final Block block, final IEssentials ess) { final SignProtectionState state = isBlockProtected(block, null, null, false); - + + return state == SignProtectionState.NOSIGN; + } + + @Override + public boolean onBlockPush(final Block block, final IEssentials ess) + { + final SignProtectionState state = isBlockProtected(block, null, null, false); + return state == SignProtectionState.NOSIGN; } }