2011-07-23 21:00:47 +02:00
|
|
|
package com.Acrobot.ChestShop.Signs;
|
2011-06-23 23:25:34 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.BlockUtil;
|
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
|
|
|
import com.Acrobot.ChestShop.Config.Language;
|
2011-06-23 23:25:34 +02:00
|
|
|
import com.Acrobot.ChestShop.Permission;
|
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.BlockFace;
|
|
|
|
import org.bukkit.block.Sign;
|
|
|
|
import org.bukkit.entity.Player;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.event.block.SignChangeEvent;
|
2011-06-23 23:25:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2012-06-08 15:28:36 +02:00
|
|
|
public class RestrictedSign implements Listener {
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
|
|
|
public static void onSignChange(SignChangeEvent event) {
|
|
|
|
String[] lines = event.getLines();
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
|
|
|
if (isRestricted(lines)) {
|
|
|
|
if (!hasPermission(player, lines)) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
|
|
|
|
dropSignAndCancelEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Block connectedSign = event.getBlock().getRelative(BlockFace.DOWN);
|
|
|
|
|
|
|
|
if (!Permission.has(player, Permission.ADMIN) || !BlockUtil.isSign(connectedSign) || !ChestShopSign.isValid(connectedSign)) {
|
|
|
|
dropSignAndCancelEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sign sign = (Sign) connectedSign.getState();
|
|
|
|
|
|
|
|
if (!ChestShopSign.isValid(sign) || !ChestShopSign.canAccess(player, sign)) {
|
|
|
|
dropSignAndCancelEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-23 21:00:47 +02:00
|
|
|
public static boolean isRestrictedShop(Sign sign) {
|
|
|
|
Block blockUp = sign.getBlock().getRelative(BlockFace.UP);
|
2012-06-08 15:28:36 +02:00
|
|
|
return BlockUtil.isSign(blockUp) && isRestricted(((Sign) blockUp.getState()).getLines());
|
2011-06-23 23:25:34 +02:00
|
|
|
}
|
|
|
|
|
2011-07-02 20:34:14 +02:00
|
|
|
public static boolean isRestricted(String[] lines) {
|
2011-06-23 23:25:34 +02:00
|
|
|
return lines[0].equalsIgnoreCase("[restricted]");
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
public static boolean isRestricted(Sign sign) {
|
|
|
|
return sign.getLine(0).equalsIgnoreCase("[restricted]");
|
|
|
|
}
|
|
|
|
|
2011-07-02 20:34:14 +02:00
|
|
|
public static boolean canAccess(Sign sign, Player player) {
|
2011-07-23 21:00:47 +02:00
|
|
|
Block blockUp = sign.getBlock().getRelative(BlockFace.UP);
|
2012-06-08 15:28:36 +02:00
|
|
|
return !BlockUtil.isSign(blockUp) || hasPermission(player, ((Sign) blockUp.getState()).getLines());
|
2011-09-06 19:01:57 +02:00
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
}
|
2011-06-23 23:25:34 +02:00
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
public static boolean canDestroy(Player p, Sign sign) {
|
2012-01-09 22:39:38 +01:00
|
|
|
Sign shopSign = getAssociatedSign(sign);
|
2012-06-08 15:28:36 +02:00
|
|
|
return ChestShopSign.canAccess(p, shopSign);
|
2012-01-09 22:39:38 +01:00
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
public static Sign getAssociatedSign(Sign restricted) {
|
2012-01-09 22:39:38 +01:00
|
|
|
Block down = restricted.getBlock().getRelative(BlockFace.DOWN);
|
2012-06-08 15:28:36 +02:00
|
|
|
return BlockUtil.isSign(down) ? (Sign) down.getState() : null;
|
2012-01-09 22:39:38 +01:00
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
public static boolean hasPermission(Player p, String[] lines) {
|
2012-05-10 16:32:25 +02:00
|
|
|
if (Permission.has(p, Permission.ADMIN)) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-23 23:25:34 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
for (String line : lines) {
|
|
|
|
if (p.hasPermission(Permission.GROUP.toString() + line)) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-23 23:25:34 +02:00
|
|
|
}
|
2011-07-23 21:00:47 +02:00
|
|
|
return false;
|
2011-06-23 23:25:34 +02:00
|
|
|
}
|
2012-06-08 15:28:36 +02:00
|
|
|
|
|
|
|
private static void dropSignAndCancelEvent(SignChangeEvent event) {
|
|
|
|
event.getBlock().breakNaturally();
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
2011-06-23 23:25:34 +02:00
|
|
|
}
|