2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop.Utils;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
2011-06-11 17:36:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.Sign;
|
|
|
|
|
2011-07-15 21:45:26 +02:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2011-07-05 19:08:55 +02:00
|
|
|
public class uSign {
|
2011-07-23 21:00:47 +02:00
|
|
|
private static final Pattern[] patterns = {
|
2011-07-15 21:45:26 +02:00
|
|
|
Pattern.compile("^$|^\\w.+$"),
|
|
|
|
Pattern.compile("[0-9]+"),
|
|
|
|
Pattern.compile(".+"),
|
|
|
|
Pattern.compile("[\\w :]+")
|
|
|
|
};
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static boolean isSign(Block block) {
|
2011-07-15 21:45:26 +02:00
|
|
|
return block.getState() instanceof Sign;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static boolean isAdminShop(String owner) {
|
2011-06-11 17:36:55 +02:00
|
|
|
return owner.toLowerCase().replace(" ", "").equals(Config.getString(Property.ADMIN_SHOP_NAME).toLowerCase().replace(" ", ""));
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
public static boolean isValid(Sign sign) {
|
2011-05-15 18:16:25 +02:00
|
|
|
return isValid(sign.getLines());
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
public static boolean isValid(String[] line) {
|
|
|
|
try {
|
|
|
|
return isValidPreparedSign(line) && (line[2].contains("B") || line[2].contains("S"));
|
|
|
|
} catch (Exception e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 21:45:26 +02:00
|
|
|
public static boolean isValidPreparedSign(String[] lines){
|
|
|
|
try{
|
|
|
|
boolean toReturn = true;
|
|
|
|
for(int i = 0; i < 4; i++){
|
|
|
|
toReturn = toReturn && patterns[i].matcher(lines[i]).matches();
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
} catch (Exception e){
|
2011-05-15 18:16:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static float buyPrice(String text) {
|
2011-05-21 19:55:55 +02:00
|
|
|
text = text.replace(" ", "").toLowerCase();
|
|
|
|
|
|
|
|
String[] split = text.split(":");
|
2011-07-23 21:00:47 +02:00
|
|
|
int buyPart = (text.contains("b") ? (split[0].contains("b") ? 0 : 1) : -1);
|
|
|
|
if (buyPart == -1 || (buyPart == 1 && split.length != 2)) return -1;
|
|
|
|
|
|
|
|
split[buyPart] = split[buyPart].replace("b", "");
|
|
|
|
|
|
|
|
if (uNumber.isFloat(split[buyPart])) {
|
|
|
|
float buyPrice = Float.parseFloat(split[buyPart]);
|
2011-05-21 19:55:55 +02:00
|
|
|
return (buyPrice != 0 ? buyPrice : -1);
|
2011-07-23 21:00:47 +02:00
|
|
|
} else if (split[buyPart].equals("free")) {
|
2011-05-21 19:55:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-05-21 19:55:55 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static float sellPrice(String text) {
|
2011-05-21 19:55:55 +02:00
|
|
|
text = text.replace(" ", "").toLowerCase();
|
|
|
|
|
|
|
|
String[] split = text.split(":");
|
2011-07-23 21:00:47 +02:00
|
|
|
int sellPart = (text.contains("s") ? (split[0].contains("s") ? 0 : 1) : -1);
|
|
|
|
if(sellPart == -1 || (sellPart == 1 && split.length != 2)) return -1;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
split[sellPart] = split[sellPart].replace("s", "");
|
|
|
|
|
2011-07-05 19:08:55 +02:00
|
|
|
if (uNumber.isFloat(split[sellPart])) {
|
2011-05-21 19:55:55 +02:00
|
|
|
Float sellPrice = Float.parseFloat(split[sellPart]);
|
|
|
|
return (sellPrice != 0 ? sellPrice : -1);
|
2011-05-29 13:25:25 +02:00
|
|
|
} else if (split[sellPart].equals("free")) {
|
2011-05-21 19:55:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static int itemAmount(String text) {
|
2011-07-05 19:08:55 +02:00
|
|
|
if (uNumber.isInteger(text)) {
|
2011-06-19 23:52:36 +02:00
|
|
|
int amount = Integer.parseInt(text);
|
|
|
|
return (amount >= 1 ? amount : 1);
|
2011-07-23 21:00:47 +02:00
|
|
|
} else return 1;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|