2012-06-08 15:28:36 +02:00
|
|
|
package com.Acrobot.ChestShop.Signs;
|
|
|
|
|
|
|
|
import com.Acrobot.Breeze.Utils.BlockUtil;
|
2012-11-23 21:04:13 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2012-09-15 20:32:22 +02:00
|
|
|
import com.Acrobot.ChestShop.Containers.AdminInventory;
|
2012-10-16 17:03:45 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uName;
|
2012-10-16 17:03:45 +02:00
|
|
|
import org.bukkit.Material;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.block.Block;
|
2012-10-16 17:03:45 +02:00
|
|
|
import org.bukkit.block.Chest;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.block.Sign;
|
|
|
|
import org.bukkit.entity.Player;
|
2012-09-15 20:32:22 +02:00
|
|
|
import org.bukkit.inventory.Inventory;
|
2012-06-08 15:28:36 +02:00
|
|
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class ChestShopSign {
|
|
|
|
public static final byte NAME_LINE = 0;
|
|
|
|
public static final byte QUANTITY_LINE = 1;
|
|
|
|
public static final byte PRICE_LINE = 2;
|
|
|
|
public static final byte ITEM_LINE = 3;
|
|
|
|
|
|
|
|
public static final Pattern[] SHOP_SIGN_PATTERN = {
|
2013-01-15 21:33:00 +01:00
|
|
|
Pattern.compile("^[\\w -.]*$"),
|
2012-10-16 17:03:45 +02:00
|
|
|
Pattern.compile("^[1-9][0-9]*$"),
|
2012-09-12 12:35:48 +02:00
|
|
|
Pattern.compile("(?i)^[\\d.bs(free) :]+$"),
|
2013-01-24 22:35:28 +01:00
|
|
|
Pattern.compile("^[\\w #:-]+$")
|
2012-06-08 15:28:36 +02:00
|
|
|
};
|
|
|
|
|
2012-09-15 20:32:22 +02:00
|
|
|
public static boolean isAdminShop(Inventory ownerInventory) {
|
|
|
|
return ownerInventory instanceof AdminInventory;
|
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public static boolean isAdminShop(String owner) {
|
2012-11-23 21:04:13 +01:00
|
|
|
return owner.replace(" ", "").equalsIgnoreCase(Properties.ADMIN_SHOP_NAME.replace(" ", ""));
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isAdminShop(Sign sign) {
|
|
|
|
return isAdminShop(sign.getLine(NAME_LINE));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isValid(Sign sign) {
|
|
|
|
return isValid(sign.getLines());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isValid(String[] line) {
|
2012-11-25 23:34:21 +01:00
|
|
|
return isValidPreparedSign(line) && (line[PRICE_LINE].toUpperCase().contains("B") || line[PRICE_LINE].toUpperCase().contains("S")) && !line[NAME_LINE].isEmpty();
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isValid(Block sign) {
|
|
|
|
return BlockUtil.isSign(sign) && isValid((Sign) sign.getState());
|
|
|
|
}
|
|
|
|
|
2012-10-16 17:03:45 +02:00
|
|
|
public static boolean isShopChest(Block chest) {
|
|
|
|
if (chest.getType() != Material.CHEST) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uBlock.getConnectedSign((Chest) chest.getState()) != null;
|
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public static boolean canAccess(Player player, Sign sign) {
|
|
|
|
if (player == null) return false;
|
|
|
|
if (sign == null) return true;
|
|
|
|
|
|
|
|
return uName.canUseName(player, sign.getLine(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isValidPreparedSign(String[] lines) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (!SHOP_SIGN_PATTERN[i].matcher(lines[i]).matches()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-25 23:34:21 +01:00
|
|
|
return lines[PRICE_LINE].indexOf(':') == lines[PRICE_LINE].lastIndexOf(':');
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
|
|
|
}
|