2011-06-26 13:31:13 +00:00
|
|
|
package com.earth2me.essentials.signs;
|
|
|
|
|
2012-09-08 13:55:37 +00:00
|
|
|
import com.earth2me.essentials.*;
|
2013-10-11 02:44:41 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2013-05-06 05:40:22 +00:00
|
|
|
import com.earth2me.essentials.Trade.OverflowType;
|
2013-06-08 21:31:19 +00:00
|
|
|
import com.earth2me.essentials.utils.FormatUtil;
|
2011-11-21 01:55:26 +00:00
|
|
|
import java.util.*;
|
2013-10-11 02:44:41 +00:00
|
|
|
import net.ess3.api.IEssentials;
|
2011-06-26 13:31:13 +00:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.BlockFace;
|
|
|
|
import org.bukkit.block.Sign;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
2013-05-06 05:40:22 +00:00
|
|
|
|
2012-10-06 02:31:34 +00:00
|
|
|
@Deprecated // This sign will be removed soon
|
2011-06-26 13:31:13 +00:00
|
|
|
public class SignProtection extends EssentialsSign
|
|
|
|
{
|
|
|
|
private final transient Set<Material> protectedBlocks = EnumSet.noneOf(Material.class);
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
public SignProtection()
|
|
|
|
{
|
|
|
|
super("Protection");
|
|
|
|
protectedBlocks.add(Material.CHEST);
|
|
|
|
protectedBlocks.add(Material.BURNING_FURNACE);
|
|
|
|
protectedBlocks.add(Material.FURNACE);
|
|
|
|
protectedBlocks.add(Material.DISPENSER);
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
@Override
|
|
|
|
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
|
|
|
|
{
|
|
|
|
sign.setLine(3, "§4" + username);
|
2011-11-04 00:23:50 +00:00
|
|
|
if (hasAdjacentBlock(sign.getBlock()))
|
2011-06-26 13:31:13 +00:00
|
|
|
{
|
2011-11-04 00:23:50 +00:00
|
|
|
final SignProtectionState state = isBlockProtected(sign.getBlock(), player, username, true);
|
|
|
|
if (state == SignProtectionState.NOSIGN || state == SignProtectionState.OWNER
|
|
|
|
|| player.isAuthorized("essentials.signs.protection.override"))
|
|
|
|
{
|
|
|
|
sign.setLine(3, "§1" + username);
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-26 13:31:13 +00:00
|
|
|
}
|
2011-11-21 01:55:26 +00:00
|
|
|
player.sendMessage(_("signProtectInvalidLocation"));
|
2011-06-30 23:33:35 +00:00
|
|
|
return false;
|
2011-06-26 13:31:13 +00:00
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
@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;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
public boolean hasAdjacentBlock(final Block block, final Block... ignoredBlocks)
|
|
|
|
{
|
|
|
|
final Block[] faces = getAdjacentBlocks(block);
|
|
|
|
for (Block b : faces)
|
|
|
|
{
|
|
|
|
for (Block ignoredBlock : ignoredBlocks)
|
|
|
|
{
|
|
|
|
if (b.getLocation().equals(ignoredBlock.getLocation()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (protectedBlocks.contains(b.getType()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
private void checkIfSignsAreBroken(final Block block, final User player, final String username, final IEssentials ess)
|
|
|
|
{
|
2011-06-30 23:33:35 +00:00
|
|
|
final Map<Location, SignProtectionState> signs = getConnectedSigns(block, player, username, false);
|
2011-06-26 13:31:13 +00:00
|
|
|
for (Map.Entry<Location, SignProtectionState> entry : signs.entrySet())
|
|
|
|
{
|
|
|
|
if (entry.getValue() != SignProtectionState.NOSIGN)
|
|
|
|
{
|
|
|
|
final Block sign = entry.getKey().getBlock();
|
|
|
|
if (!hasAdjacentBlock(sign, block))
|
|
|
|
{
|
|
|
|
block.setType(Material.AIR);
|
|
|
|
final Trade trade = new Trade(new ItemStack(Material.SIGN, 1), ess);
|
2013-05-06 05:40:22 +00:00
|
|
|
trade.pay(player, OverflowType.DROP);
|
2011-06-26 13:31:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-30 23:33:35 +00:00
|
|
|
private Map<Location, SignProtectionState> getConnectedSigns(final Block block, final User user, final String username, boolean secure)
|
2011-06-26 13:31:13 +00:00
|
|
|
{
|
|
|
|
final Map<Location, SignProtectionState> signs = new HashMap<Location, SignProtectionState>();
|
2011-06-30 23:33:35 +00:00
|
|
|
getConnectedSigns(block, signs, user, username, secure ? 4 : 2);
|
2011-06-26 13:31:13 +00:00
|
|
|
return signs;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
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);
|
|
|
|
for (Block b : faces)
|
|
|
|
{
|
|
|
|
final Location loc = b.getLocation();
|
|
|
|
if (signs.containsKey(loc))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final SignProtectionState check = checkProtectionSign(b, user, username);
|
|
|
|
signs.put(loc, check);
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
if (protectedBlocks.contains(b.getType()) && depth > 0)
|
|
|
|
{
|
|
|
|
getConnectedSigns(b, signs, user, username, depth - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
private SignProtectionState checkProtectionSign(final Block block, final User user, final String username)
|
|
|
|
{
|
|
|
|
if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN)
|
|
|
|
{
|
|
|
|
final BlockSign sign = new BlockSign(block);
|
2012-02-02 09:26:36 +00:00
|
|
|
if (sign.getLine(0).equals(this.getSuccessName()))
|
2011-06-26 13:31:13 +00:00
|
|
|
{
|
|
|
|
return checkProtectionSign(sign, user, username);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SignProtectionState.NOSIGN;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
private SignProtectionState checkProtectionSign(final ISign sign, final User user, final String username)
|
|
|
|
{
|
|
|
|
if (user == null || username == null)
|
|
|
|
{
|
|
|
|
return SignProtectionState.NOT_ALLOWED;
|
|
|
|
}
|
|
|
|
if (user.isAuthorized("essentials.signs.protection.override"))
|
|
|
|
{
|
|
|
|
return SignProtectionState.OWNER;
|
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
if (FormatUtil.stripFormat(sign.getLine(3)).equalsIgnoreCase(username))
|
2011-08-18 23:43:39 +00:00
|
|
|
{
|
|
|
|
return SignProtectionState.OWNER;
|
|
|
|
}
|
2011-06-26 13:31:13 +00:00
|
|
|
for (int i = 1; i <= 2; i++)
|
|
|
|
{
|
|
|
|
final String line = sign.getLine(i);
|
|
|
|
if (line.startsWith("(") && line.endsWith(")") && user.inGroup(line.substring(1, line.length() - 1)))
|
|
|
|
{
|
|
|
|
return SignProtectionState.ALLOWED;
|
|
|
|
}
|
2011-07-22 16:03:55 +00:00
|
|
|
else if (line.equalsIgnoreCase(username))
|
2011-06-26 13:31:13 +00:00
|
|
|
{
|
|
|
|
return SignProtectionState.ALLOWED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SignProtectionState.NOT_ALLOWED;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
private Block[] getAdjacentBlocks(final Block block)
|
|
|
|
{
|
|
|
|
return new Block[]
|
|
|
|
{
|
2011-07-17 23:17:24 +00:00
|
|
|
block.getRelative(BlockFace.NORTH),
|
|
|
|
block.getRelative(BlockFace.SOUTH),
|
|
|
|
block.getRelative(BlockFace.EAST),
|
|
|
|
block.getRelative(BlockFace.WEST),
|
|
|
|
block.getRelative(BlockFace.DOWN),
|
|
|
|
block.getRelative(BlockFace.UP)
|
2011-06-26 13:31:13 +00:00
|
|
|
};
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-30 23:33:35 +00:00
|
|
|
public SignProtectionState isBlockProtected(final Block block, final User user, final String username, boolean secure)
|
2011-06-26 13:31:13 +00:00
|
|
|
{
|
2011-06-30 23:33:35 +00:00
|
|
|
final Map<Location, SignProtectionState> signs = getConnectedSigns(block, user, username, secure);
|
2011-06-26 13:31:13 +00:00
|
|
|
SignProtectionState retstate = SignProtectionState.NOSIGN;
|
|
|
|
for (SignProtectionState state : signs.values())
|
|
|
|
{
|
2011-09-20 11:33:42 +00:00
|
|
|
if (state == SignProtectionState.ALLOWED)
|
2011-06-26 13:31:13 +00:00
|
|
|
{
|
|
|
|
retstate = state;
|
|
|
|
}
|
2011-09-20 11:33:42 +00:00
|
|
|
else if (state == SignProtectionState.NOT_ALLOWED && retstate != SignProtectionState.ALLOWED)
|
|
|
|
{
|
2011-09-20 12:53:26 +00:00
|
|
|
retstate = state;
|
2011-09-20 11:33:42 +00:00
|
|
|
}
|
2011-06-26 13:31:13 +00:00
|
|
|
}
|
2011-11-04 00:23:50 +00:00
|
|
|
if (!secure || retstate == SignProtectionState.NOSIGN)
|
|
|
|
{
|
|
|
|
for (SignProtectionState state : signs.values())
|
|
|
|
{
|
|
|
|
if (state == SignProtectionState.OWNER)
|
|
|
|
{
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-26 13:31:13 +00:00
|
|
|
return retstate;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
public boolean isBlockProtected(final Block block)
|
|
|
|
{
|
|
|
|
final Block[] faces = getAdjacentBlocks(block);
|
|
|
|
for (Block b : faces)
|
|
|
|
{
|
|
|
|
if (b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN)
|
|
|
|
{
|
2011-08-27 18:39:54 +00:00
|
|
|
final Sign sign = (Sign)b.getState();
|
2011-06-26 13:31:13 +00:00
|
|
|
if (sign.getLine(0).equalsIgnoreCase("§1[Protection]"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (protectedBlocks.contains(b.getType()))
|
|
|
|
{
|
|
|
|
final Block[] faceChest = getAdjacentBlocks(b);
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
for (Block a : faceChest)
|
|
|
|
{
|
|
|
|
if (a.getType() == Material.SIGN_POST || a.getType() == Material.WALL_SIGN)
|
|
|
|
{
|
2011-08-27 18:39:54 +00:00
|
|
|
final Sign sign = (Sign)a.getState();
|
2011-06-26 13:31:13 +00:00
|
|
|
if (sign.getLine(0).equalsIgnoreCase("§1[Protection]"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
@Override
|
|
|
|
public Set<Material> getBlocks()
|
|
|
|
{
|
|
|
|
return protectedBlocks;
|
|
|
|
}
|
2013-05-06 05:40:22 +00:00
|
|
|
|
2012-12-30 04:32:20 +00:00
|
|
|
@Override
|
|
|
|
public boolean areHeavyEventRequired()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 14:06:19 +00:00
|
|
|
@Override
|
|
|
|
protected boolean onBlockPlace(final Block block, final User player, final String username, final IEssentials ess) throws SignException
|
|
|
|
{
|
2011-06-30 23:33:35 +00:00
|
|
|
for (Block adjBlock : getAdjacentBlocks(block))
|
2011-06-26 14:06:19 +00:00
|
|
|
{
|
2011-06-30 23:33:35 +00:00
|
|
|
final SignProtectionState state = isBlockProtected(adjBlock, player, username, true);
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-30 23:33:35 +00:00
|
|
|
if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED)
|
|
|
|
&& !player.isAuthorized("essentials.signs.protection.override"))
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
player.sendMessage(_("noPlacePermission", block.getType().toString().toLowerCase(Locale.ENGLISH)));
|
2011-06-30 23:33:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-06-26 14:06:19 +00:00
|
|
|
}
|
2011-06-30 23:33:35 +00:00
|
|
|
return true;
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 14:06:19 +00:00
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 14:06:19 +00:00
|
|
|
@Override
|
|
|
|
protected boolean onBlockInteract(final Block block, final User player, final String username, final IEssentials ess) throws SignException
|
|
|
|
{
|
2011-06-30 23:33:35 +00:00
|
|
|
final SignProtectionState state = isBlockProtected(block, player, username, false);
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 14:06:19 +00:00
|
|
|
if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN || state == SignProtectionState.ALLOWED)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 14:06:19 +00:00
|
|
|
if (state == SignProtectionState.NOT_ALLOWED
|
|
|
|
&& player.isAuthorized("essentials.signs.protection.override"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
player.sendMessage(_("noAccessPermission", block.getType().toString().toLowerCase(Locale.ENGLISH)));
|
2011-06-26 14:06:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
@Override
|
|
|
|
protected boolean onBlockBreak(final Block block, final User player, final String username, final IEssentials ess) throws SignException
|
|
|
|
{
|
2011-06-30 23:33:35 +00:00
|
|
|
final SignProtectionState state = isBlockProtected(block, player, username, false);
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN)
|
|
|
|
{
|
|
|
|
checkIfSignsAreBroken(block, player, username, ess);
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED)
|
|
|
|
&& player.isAuthorized("essentials.signs.protection.override"))
|
|
|
|
{
|
|
|
|
checkIfSignsAreBroken(block, player, username, ess);
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
player.sendMessage(_("noDestroyPermission", block.getType().toString().toLowerCase(Locale.ENGLISH)));
|
2011-06-26 13:31:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-11-04 00:23:50 +00:00
|
|
|
|
2011-09-21 00:29:51 +00:00
|
|
|
@Override
|
|
|
|
public boolean onBlockBreak(final Block block, final IEssentials ess)
|
|
|
|
{
|
|
|
|
final SignProtectionState state = isBlockProtected(block, null, null, false);
|
|
|
|
|
|
|
|
return state == SignProtectionState.NOSIGN;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
@Override
|
|
|
|
public boolean onBlockExplode(final Block block, final IEssentials ess)
|
|
|
|
{
|
2011-06-30 23:33:35 +00:00
|
|
|
final SignProtectionState state = isBlockProtected(block, null, null, false);
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
return state == SignProtectionState.NOSIGN;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
@Override
|
|
|
|
public boolean onBlockBurn(final Block block, final IEssentials ess)
|
|
|
|
{
|
2011-06-30 23:33:35 +00:00
|
|
|
final SignProtectionState state = isBlockProtected(block, null, null, false);
|
2011-07-17 22:50:03 +00:00
|
|
|
|
|
|
|
return state == SignProtectionState.NOSIGN;
|
|
|
|
}
|
2011-11-04 00:23:50 +00:00
|
|
|
|
2011-08-25 15:37:41 +00:00
|
|
|
@Override
|
|
|
|
public boolean onBlockIgnite(final Block block, final IEssentials ess)
|
|
|
|
{
|
|
|
|
final SignProtectionState state = isBlockProtected(block, null, null, false);
|
|
|
|
|
|
|
|
return state == SignProtectionState.NOSIGN;
|
|
|
|
}
|
2011-07-17 22:50:03 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onBlockPush(final Block block, final IEssentials ess)
|
|
|
|
{
|
|
|
|
final SignProtectionState state = isBlockProtected(block, null, null, false);
|
|
|
|
|
2011-06-26 13:31:13 +00:00
|
|
|
return state == SignProtectionState.NOSIGN;
|
|
|
|
}
|
2013-11-07 02:22:32 +00:00
|
|
|
|
|
|
|
public enum SignProtectionState
|
|
|
|
{
|
|
|
|
NOT_ALLOWED, ALLOWED, NOSIGN, OWNER
|
|
|
|
}
|
2011-06-26 13:31:13 +00:00
|
|
|
}
|