2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop.Protection;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2011-10-11 13:15:53 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.blockBreak;
|
2011-08-13 12:08:34 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uLongName;
|
2011-10-11 13:15:53 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uSign;
|
2012-03-17 15:00:25 +01:00
|
|
|
import org.bukkit.Material;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.block.Block;
|
2011-10-11 13:15:53 +02:00
|
|
|
import org.bukkit.block.BlockFace;
|
2011-05-29 13:25:25 +02:00
|
|
|
import org.bukkit.block.Sign;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2011-12-01 14:02:58 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class Security {
|
2012-03-17 15:00:25 +01:00
|
|
|
private static final BlockFace[] faces = {BlockFace.UP, BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH};
|
|
|
|
private static final BlockFace[] blockFaces = {BlockFace.UP, BlockFace.DOWN, BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH};
|
2011-12-01 14:02:58 +01:00
|
|
|
public static ArrayList<Protection> protections = new ArrayList<Protection>();
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static boolean protect(String name, Block block) {
|
2011-12-01 14:02:58 +01:00
|
|
|
boolean works = false;
|
|
|
|
for (int i = 0; i < protections.size() && !works; i++) works = protections.get(i).protect(name, block);
|
|
|
|
return works;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static boolean canAccess(Player player, Block block) {
|
2011-12-01 14:02:58 +01:00
|
|
|
boolean works = true;
|
|
|
|
for (int i = 0; i < protections.size() && works; i++) works = protections.get(i).canAccess(player, block);
|
|
|
|
return works;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static boolean isProtected(Block block) {
|
2012-01-09 22:39:38 +01:00
|
|
|
boolean isProt = false;
|
|
|
|
for (int i = 0; i < protections.size() && !isProt; i++) isProt = protections.get(i).isProtected(block);
|
|
|
|
return isProt;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-10-11 13:15:53 +02:00
|
|
|
public static boolean canPlaceSign(Player p, Sign sign) {
|
2012-03-17 15:00:25 +01:00
|
|
|
return !anotherShopFound(blockBreak.getAttachedFace(sign), sign.getBlock(), p) && canBePlaced(p, sign.getBlock());
|
2011-10-11 13:15:53 +02:00
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
private static boolean canBePlaced(Player p, Block signBlock) {
|
|
|
|
for (BlockFace bf : blockFaces) {
|
|
|
|
Block block = signBlock.getRelative(bf);
|
|
|
|
|
|
|
|
if (block.getType() != Material.CHEST) continue;
|
|
|
|
if (isProtected(block) && !canAccess(p, block)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean anotherShopFound(Block baseBlock, Block signBlock, Player p) {
|
|
|
|
String shortName = uLongName.stripName(p.getName());
|
|
|
|
if (Config.getBoolean(Property.ALLOW_MULTIPLE_SHOPS_AT_ONE_BLOCK)) return false;
|
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
for (BlockFace bf : faces) {
|
2011-10-11 13:15:53 +02:00
|
|
|
Block block = baseBlock.getRelative(bf);
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
if (!uSign.isSign(block)) continue;
|
|
|
|
|
|
|
|
Sign s = (Sign) block.getState();
|
|
|
|
if (uSign.isValid(s) && !block.equals(signBlock) && blockBreak.getAttachedFace(s).equals(baseBlock) && !s.getLine(0).equals(shortName))
|
2012-01-09 22:39:38 +01:00
|
|
|
return true;
|
2011-10-11 13:15:53 +02:00
|
|
|
}
|
|
|
|
return false;
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|