Fix Protection signs

This commit is contained in:
snowleo 2011-07-01 01:33:35 +02:00
parent e596f85953
commit ecaecc5e62

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,25 +30,27 @@ 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
{ {
sign.setLine(3, "§4" + username); sign.setLine(3, "§4" + username);
if (hasAdjacentBlock(sign.getBlock()) && isBlockProtected(sign.getBlock(), player, username) != SignProtectionState.NOT_ALLOWED) if (hasAdjacentBlock(sign.getBlock()) && isBlockProtected(sign.getBlock(), player, username, true) != SignProtectionState.NOT_ALLOWED)
{ {
sign.setLine(3, "§1" + username); sign.setLine(3, "§1" + username);
return true;
} }
return true; player.sendMessage("§4You are not allowed to create sign here.");
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);
@ -68,10 +70,10 @@ 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); final Map<Location, SignProtectionState> signs = getConnectedSigns(block, player, username, false);
for (Map.Entry<Location, SignProtectionState> entry : signs.entrySet()) for (Map.Entry<Location, SignProtectionState> entry : signs.entrySet())
{ {
if (entry.getValue() != SignProtectionState.NOSIGN) if (entry.getValue() != SignProtectionState.NOSIGN)
@ -86,14 +88,14 @@ public class SignProtection extends EssentialsSign
} }
} }
} }
private Map<Location, SignProtectionState> getConnectedSigns(final Block block, final User user, final String username) 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, 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);
@ -106,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)
@ -132,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)
@ -155,13 +157,13 @@ public class SignProtection extends EssentialsSign
return SignProtectionState.ALLOWED; return SignProtectionState.ALLOWED;
} }
} }
if (sign.getLine(3).equalsIgnoreCase(username)) if (sign.getLine(3).substring(2).equalsIgnoreCase(username))
{ {
return SignProtectionState.OWNER; return SignProtectionState.OWNER;
} }
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[]
@ -174,10 +176,10 @@ 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) public SignProtectionState isBlockProtected(final Block block, final User user, final String username, boolean secure)
{ {
final Map<Location, SignProtectionState> signs = getConnectedSigns(block, user, username); final Map<Location, SignProtectionState> signs = getConnectedSigns(block, user, username, secure);
SignProtectionState retstate = SignProtectionState.NOSIGN; SignProtectionState retstate = SignProtectionState.NOSIGN;
for (SignProtectionState state : signs.values()) for (SignProtectionState state : signs.values())
{ {
@ -192,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);
@ -209,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)
@ -225,7 +227,7 @@ public class SignProtection extends EssentialsSign
} }
return false; return false;
} }
@Override @Override
public Set<Material> getBlocks() public Set<Material> getBlocks()
{ {
@ -235,81 +237,78 @@ public class SignProtection extends EssentialsSign
@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
{ {
final SignProtectionState state = isBlockProtected(block, player, username); for (Block adjBlock : getAdjacentBlocks(block))
if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN)
{ {
return true; final SignProtectionState state = isBlockProtected(adjBlock, player, username, true);
if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED)
&& !player.isAuthorized("essentials.signs.protection.override"))
{
player.sendMessage(Util.format("noPlacePermission", block.getType().toString().toLowerCase()));
return false;
}
} }
return true;
if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED)
&& player.isAuthorized("essentials.signs.protection.override"))
{
return true;
}
player.sendMessage(Util.format("noPlacePermission", block.getType().toString().toLowerCase()));
return false;
} }
@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); 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); 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); 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); final SignProtectionState state = isBlockProtected(block, null, null, false);
return state == SignProtectionState.NOSIGN; return state == SignProtectionState.NOSIGN;
} }
} }