2012-05-10 16:32:25 +02:00
|
|
|
package com.Acrobot.ChestShop;
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.BlockUtil;
|
2012-05-10 16:32:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.Protection.ProtectBlockEvent;
|
|
|
|
import com.Acrobot.ChestShop.Events.Protection.ProtectionCheckEvent;
|
|
|
|
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
2012-05-10 16:32:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
|
|
|
import com.Acrobot.ChestShop.Utils.uName;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.BlockFace;
|
|
|
|
import org.bukkit.block.Sign;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.Event;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class Security {
|
2012-06-08 15:28:36 +02:00
|
|
|
private static final BlockFace[] SIGN_CONNECTION_FACES = {BlockFace.UP, BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH};
|
|
|
|
private static final BlockFace[] BLOCKS_AROUND = {BlockFace.UP, BlockFace.DOWN, BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH};
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
public static boolean protect(String playerName, Block block) {
|
|
|
|
ProtectBlockEvent event = new ProtectBlockEvent(block, playerName);
|
|
|
|
ChestShop.callEvent(event);
|
|
|
|
|
|
|
|
return event.isProtected();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canAccess(Player player, Block block) {
|
2012-09-12 12:35:48 +02:00
|
|
|
return canAccess(player, block, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canAccess(Player player, Block block, boolean ignoreDefault) {
|
|
|
|
ProtectionCheckEvent event = new ProtectionCheckEvent(block, player, ignoreDefault);
|
2012-05-10 16:32:25 +02:00
|
|
|
ChestShop.callEvent(event);
|
|
|
|
|
|
|
|
return event.getResult() != Event.Result.DENY;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canPlaceSign(Player p, Sign sign) {
|
|
|
|
return !anotherShopFound(uBlock.getAttachedFace(sign), sign.getBlock(), p) && canBePlaced(p, sign.getBlock());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean canBePlaced(Player player, Block signBlock) {
|
2012-06-08 15:28:36 +02:00
|
|
|
for (BlockFace face : BLOCKS_AROUND) {
|
|
|
|
Block block = signBlock.getRelative(face);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
if (block.getType() != Material.CHEST) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!canAccess(player, block)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean anotherShopFound(Block baseBlock, Block signBlock, Player p) {
|
|
|
|
String shortName = uName.stripName(p.getName());
|
|
|
|
if (Config.getBoolean(Property.ALLOW_MULTIPLE_SHOPS_AT_ONE_BLOCK)) return false;
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
for (BlockFace bf : SIGN_CONNECTION_FACES) {
|
2012-05-10 16:32:25 +02:00
|
|
|
Block block = baseBlock.getRelative(bf);
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (!BlockUtil.isSign(block)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
Sign s = (Sign) block.getState();
|
2012-06-08 15:28:36 +02:00
|
|
|
if (ChestShopSign.isValid(s) && !block.equals(signBlock) && uBlock.getAttachedFace(s).equals(baseBlock) && !s.getLine(0).equals(shortName))
|
2012-05-10 16:32:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|