Protection of signs against pistons

This commit is contained in:
snowleo 2011-07-18 00:50:03 +02:00
parent cc31fbed8e
commit 1ce6be5944
4 changed files with 118 additions and 50 deletions

View File

@ -197,6 +197,8 @@ public class Essentials extends JavaPlugin implements IEssentials
pm.registerEvent(Type.BLOCK_BREAK, signBlockListener, Priority.Highest, this); pm.registerEvent(Type.BLOCK_BREAK, signBlockListener, Priority.Highest, this);
pm.registerEvent(Type.BLOCK_IGNITE, signBlockListener, Priority.Low, this); pm.registerEvent(Type.BLOCK_IGNITE, signBlockListener, Priority.Low, this);
pm.registerEvent(Type.BLOCK_BURN, 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); final SignPlayerListener signPlayerListener = new SignPlayerListener(this);
pm.registerEvent(Type.PLAYER_INTERACT, signPlayerListener, Priority.Low, this); pm.registerEvent(Type.PLAYER_INTERACT, signPlayerListener, Priority.Low, this);

View File

@ -189,6 +189,11 @@ public class EssentialsSign
return true; return true;
} }
public boolean onBlockPush(Block block, IEssentials ess)
{
return true;
}
public static boolean checkIfBlockBreaksSigns(final Block block) public static boolean checkIfBlockBreaksSigns(final Block block)
{ {
if (block.getFace(BlockFace.UP).getType() == Material.SIGN_POST) 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 protected final Double getDoublePositive(final String line) throws SignException
{ {
final double quantity = getDouble(line); 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")); throw new SignException(Util.i18n("moreThanZero"));
} }
@ -425,7 +430,6 @@ public class EssentialsSign
{ {
return; return;
} }
} }
@ -454,7 +458,7 @@ public class EssentialsSign
{ {
return block; return block;
} }
public final void updateSign() public final void updateSign()
{ {
sign.update(); sign.update();
@ -469,7 +473,7 @@ public class EssentialsSign
void setLine(final int index, final String text); void setLine(final int index, final String text);
public Block getBlock(); public Block getBlock();
void updateSign(); void updateSign();
} }
} }

View File

@ -13,6 +13,8 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockBurnEvent; import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockIgniteEvent; import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockListener; 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.BlockPlaceEvent;
import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.block.SignChangeEvent;
@ -21,12 +23,12 @@ public class SignBlockListener extends BlockListener
{ {
private final transient IEssentials ess; private final transient IEssentials ess;
private final static Logger LOGGER = Logger.getLogger("Minecraft"); private final static Logger LOGGER = Logger.getLogger("Minecraft");
public SignBlockListener(IEssentials ess) public SignBlockListener(IEssentials ess)
{ {
this.ess = ess; this.ess = ess;
} }
@Override @Override
public void onBlockBreak(final BlockBreakEvent event) public void onBlockBreak(final BlockBreakEvent event)
{ {
@ -34,13 +36,13 @@ public class SignBlockListener extends BlockListener
{ {
return; return;
} }
if (protectSignsAndBlocks(event.getBlock(), event.getPlayer())) if (protectSignsAndBlocks(event.getBlock(), event.getPlayer()))
{ {
event.setCancelled(true); event.setCancelled(true);
} }
} }
public boolean protectSignsAndBlocks(final Block block, final Player player) public boolean protectSignsAndBlocks(final Block block, final Player player)
{ {
final int mat = block.getTypeId(); final int mat = block.getTypeId();
@ -78,7 +80,7 @@ public class SignBlockListener extends BlockListener
} }
return false; return false;
} }
@Override @Override
public void onSignChange(final SignChangeEvent event) public void onSignChange(final SignChangeEvent event)
{ {
@ -104,12 +106,13 @@ public class SignBlockListener extends BlockListener
User user = ess.getUser(event.getPlayer()); User user = ess.getUser(event.getPlayer());
if (user.isAuthorized("essentials.signs.color")) 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")); event.setLine(i, event.getLine(i).replaceAll("&([0-9a-f])", "§$1"));
} }
} }
} }
@Override @Override
public void onBlockPlace(final BlockPlaceEvent event) public void onBlockPlace(final BlockPlaceEvent event)
{ {
@ -117,7 +120,7 @@ public class SignBlockListener extends BlockListener
{ {
return; return;
} }
final Block against = event.getBlockAgainst(); final Block against = event.getBlockAgainst();
if (against.getType() == Material.WALL_SIGN if (against.getType() == Material.WALL_SIGN
|| against.getType() == Material.SIGN_POST) || against.getType() == Material.SIGN_POST)
@ -142,7 +145,7 @@ public class SignBlockListener extends BlockListener
} }
} }
} }
@Override @Override
public void onBlockBurn(final BlockBurnEvent event) public void onBlockBurn(final BlockBurnEvent event)
{ {
@ -150,7 +153,7 @@ public class SignBlockListener extends BlockListener
{ {
return; return;
} }
final Block block = event.getBlock(); final Block block = event.getBlock();
if ((block.getType() == Material.WALL_SIGN if ((block.getType() == Material.WALL_SIGN
|| block.getType() == Material.SIGN_POST || block.getType() == Material.SIGN_POST
@ -170,7 +173,7 @@ public class SignBlockListener extends BlockListener
} }
} }
} }
@Override @Override
public void onBlockIgnite(final BlockIgniteEvent event) public void onBlockIgnite(final BlockIgniteEvent event)
{ {
@ -178,10 +181,61 @@ public class SignBlockListener extends BlockListener
{ {
return; return;
} }
if (protectSignsAndBlocks(event.getBlock(), event.getPlayer())) if (protectSignsAndBlocks(event.getBlock(), event.getPlayer()))
{ {
event.setCancelled(true); 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;
}
}
}
}
} }

View File

@ -21,7 +21,7 @@ import org.bukkit.inventory.ItemStack;
public class SignProtection extends EssentialsSign public class SignProtection extends EssentialsSign
{ {
private final transient Set<Material> protectedBlocks = EnumSet.noneOf(Material.class); private final transient Set<Material> protectedBlocks = EnumSet.noneOf(Material.class);
public SignProtection() public SignProtection()
{ {
super("Protection"); super("Protection");
@ -30,7 +30,7 @@ public class SignProtection extends EssentialsSign
protectedBlocks.add(Material.FURNACE); protectedBlocks.add(Material.FURNACE);
protectedBlocks.add(Material.DISPENSER); protectedBlocks.add(Material.DISPENSER);
} }
@Override @Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException 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."); player.sendMessage("§4You are not allowed to create sign here.");
return false; return false;
} }
@Override @Override
protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{ {
final SignProtectionState state = checkProtectionSign(sign, player, username); final SignProtectionState state = checkProtectionSign(sign, player, username);
return state == SignProtectionState.OWNER; return state == SignProtectionState.OWNER;
} }
public boolean hasAdjacentBlock(final Block block, final Block... ignoredBlocks) public boolean hasAdjacentBlock(final Block block, final Block... ignoredBlocks)
{ {
final Block[] faces = getAdjacentBlocks(block); final Block[] faces = getAdjacentBlocks(block);
@ -70,7 +70,7 @@ public class SignProtection extends EssentialsSign
} }
return false; return false;
} }
private void checkIfSignsAreBroken(final Block block, final User player, final String username, final IEssentials ess) private void checkIfSignsAreBroken(final Block block, final User player, final String username, final IEssentials ess)
{ {
final Map<Location, SignProtectionState> signs = getConnectedSigns(block, player, username, false); final Map<Location, SignProtectionState> signs = getConnectedSigns(block, player, username, false);
@ -88,14 +88,14 @@ public class SignProtection extends EssentialsSign
} }
} }
} }
private Map<Location, SignProtectionState> getConnectedSigns(final Block block, final User user, final String username, boolean secure) private Map<Location, SignProtectionState> getConnectedSigns(final Block block, final User user, final String username, boolean secure)
{ {
final Map<Location, SignProtectionState> signs = new HashMap<Location, SignProtectionState>(); final Map<Location, SignProtectionState> signs = new HashMap<Location, SignProtectionState>();
getConnectedSigns(block, signs, user, username, secure ? 4 : 2); getConnectedSigns(block, signs, user, username, secure ? 4 : 2);
return signs; return signs;
} }
private void getConnectedSigns(final Block block, final Map<Location, SignProtectionState> signs, final User user, final String username, final int depth) private void getConnectedSigns(final Block block, final Map<Location, SignProtectionState> signs, final User user, final String username, final int depth)
{ {
final Block[] faces = getAdjacentBlocks(block); final Block[] faces = getAdjacentBlocks(block);
@ -108,20 +108,20 @@ public class SignProtection extends EssentialsSign
} }
final SignProtectionState check = checkProtectionSign(b, user, username); final SignProtectionState check = checkProtectionSign(b, user, username);
signs.put(loc, check); signs.put(loc, check);
if (protectedBlocks.contains(b.getType()) && depth > 0) if (protectedBlocks.contains(b.getType()) && depth > 0)
{ {
getConnectedSigns(b, signs, user, username, depth - 1); getConnectedSigns(b, signs, user, username, depth - 1);
} }
} }
} }
public enum SignProtectionState public enum SignProtectionState
{ {
NOT_ALLOWED, ALLOWED, NOSIGN, OWNER NOT_ALLOWED, ALLOWED, NOSIGN, OWNER
} }
private SignProtectionState checkProtectionSign(final Block block, final User user, final String username) private SignProtectionState checkProtectionSign(final Block block, final User user, final String username)
{ {
if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN) if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN)
@ -134,7 +134,7 @@ public class SignProtection extends EssentialsSign
} }
return SignProtectionState.NOSIGN; return SignProtectionState.NOSIGN;
} }
private SignProtectionState checkProtectionSign(final ISign sign, final User user, final String username) private SignProtectionState checkProtectionSign(final ISign sign, final User user, final String username)
{ {
if (user == null || username == null) if (user == null || username == null)
@ -163,7 +163,7 @@ public class SignProtection extends EssentialsSign
} }
return SignProtectionState.NOT_ALLOWED; return SignProtectionState.NOT_ALLOWED;
} }
private Block[] getAdjacentBlocks(final Block block) private Block[] getAdjacentBlocks(final Block block)
{ {
return new Block[] return new Block[]
@ -176,7 +176,7 @@ public class SignProtection extends EssentialsSign
block.getFace(BlockFace.UP) block.getFace(BlockFace.UP)
}; };
} }
public SignProtectionState isBlockProtected(final Block block, final User user, final String username, boolean secure) public SignProtectionState isBlockProtected(final Block block, final User user, final String username, boolean secure)
{ {
final Map<Location, SignProtectionState> signs = getConnectedSigns(block, user, username, secure); final Map<Location, SignProtectionState> signs = getConnectedSigns(block, user, username, secure);
@ -194,7 +194,7 @@ public class SignProtection extends EssentialsSign
} }
return retstate; return retstate;
} }
public boolean isBlockProtected(final Block block) public boolean isBlockProtected(final Block block)
{ {
final Block[] faces = getAdjacentBlocks(block); final Block[] faces = getAdjacentBlocks(block);
@ -211,7 +211,7 @@ public class SignProtection extends EssentialsSign
if (protectedBlocks.contains(b.getType())) if (protectedBlocks.contains(b.getType()))
{ {
final Block[] faceChest = getAdjacentBlocks(b); final Block[] faceChest = getAdjacentBlocks(b);
for (Block a : faceChest) for (Block a : faceChest)
{ {
if (a.getType() == Material.SIGN_POST || a.getType() == Material.WALL_SIGN) if (a.getType() == Material.SIGN_POST || a.getType() == Material.WALL_SIGN)
@ -227,20 +227,20 @@ public class SignProtection extends EssentialsSign
} }
return false; return false;
} }
@Override @Override
public Set<Material> getBlocks() public Set<Material> getBlocks()
{ {
return protectedBlocks; return protectedBlocks;
} }
@Override @Override
protected boolean onBlockPlace(final Block block, final User player, final String username, final IEssentials ess) throws SignException protected boolean onBlockPlace(final Block block, final User player, final String username, final IEssentials ess) throws SignException
{ {
for (Block adjBlock : getAdjacentBlocks(block)) for (Block adjBlock : getAdjacentBlocks(block))
{ {
final SignProtectionState state = isBlockProtected(adjBlock, player, username, true); final SignProtectionState state = isBlockProtected(adjBlock, player, username, true);
if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED) if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED)
&& !player.isAuthorized("essentials.signs.protection.override")) && !player.isAuthorized("essentials.signs.protection.override"))
{ {
@ -249,66 +249,74 @@ public class SignProtection extends EssentialsSign
} }
} }
return true; return true;
} }
@Override @Override
protected boolean onBlockInteract(final Block block, final User player, final String username, final IEssentials ess) throws SignException 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); final SignProtectionState state = isBlockProtected(block, player, username, false);
if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN || state == SignProtectionState.ALLOWED) if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN || state == SignProtectionState.ALLOWED)
{ {
return true; return true;
} }
if (state == SignProtectionState.NOT_ALLOWED if (state == SignProtectionState.NOT_ALLOWED
&& player.isAuthorized("essentials.signs.protection.override")) && player.isAuthorized("essentials.signs.protection.override"))
{ {
return true; return true;
} }
player.sendMessage(Util.format("noAccessPermission", block.getType().toString().toLowerCase())); player.sendMessage(Util.format("noAccessPermission", block.getType().toString().toLowerCase()));
return false; return false;
} }
@Override @Override
protected boolean onBlockBreak(final Block block, final User player, final String username, final IEssentials ess) throws SignException 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); final SignProtectionState state = isBlockProtected(block, player, username, false);
if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN) if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN)
{ {
checkIfSignsAreBroken(block, player, username, ess); checkIfSignsAreBroken(block, player, username, ess);
return true; return true;
} }
if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED) if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED)
&& player.isAuthorized("essentials.signs.protection.override")) && player.isAuthorized("essentials.signs.protection.override"))
{ {
checkIfSignsAreBroken(block, player, username, ess); checkIfSignsAreBroken(block, player, username, ess);
return true; return true;
} }
player.sendMessage(Util.format("noDestroyPermission", block.getType().toString().toLowerCase())); player.sendMessage(Util.format("noDestroyPermission", block.getType().toString().toLowerCase()));
return false; return false;
} }
@Override @Override
public boolean onBlockExplode(final Block block, final IEssentials ess) public boolean onBlockExplode(final Block block, final IEssentials ess)
{ {
final SignProtectionState state = isBlockProtected(block, null, null, false); final SignProtectionState state = isBlockProtected(block, null, null, false);
return state == SignProtectionState.NOSIGN; return state == SignProtectionState.NOSIGN;
} }
@Override @Override
public boolean onBlockBurn(final Block block, final IEssentials ess) public boolean onBlockBurn(final Block block, final IEssentials ess)
{ {
final SignProtectionState state = isBlockProtected(block, null, null, false); 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; return state == SignProtectionState.NOSIGN;
} }
} }