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;
|
2012-01-09 22:39:38 +01:00
|
|
|
import com.Acrobot.ChestShop.Permission;
|
2011-09-23 14:07:20 +02:00
|
|
|
import com.palmergames.bukkit.towny.Towny;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.Sign;
|
2012-01-09 22:39:38 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
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(".+"),
|
2011-12-16 17:20:09 +01:00
|
|
|
Pattern.compile("[\\w : -]+")
|
2011-07-15 21:45:26 +02:00
|
|
|
};
|
2011-09-06 19:01:57 +02:00
|
|
|
|
2011-09-23 14:07:20 +02:00
|
|
|
public static Towny towny; //Moved this here - somehow, java fails at try/catch
|
|
|
|
|
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) {
|
2012-01-09 22:39:38 +01:00
|
|
|
return isValidPreparedSign(line) && (line[2].contains("B") || line[2].contains("S")) && !line[0].isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canAccess(Player p, Sign s) {
|
|
|
|
if (p == null) return false;
|
|
|
|
if (s == null) return true;
|
|
|
|
|
|
|
|
String line = s.getLine(0);
|
|
|
|
return uLongName.stripName(p.getName()).equals(line) || Permission.otherName(p, line);
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
|
2011-09-06 19:01:57 +02:00
|
|
|
public static boolean isValidPreparedSign(String[] lines) {
|
2012-01-09 22:39:38 +01:00
|
|
|
boolean toReturn = true;
|
|
|
|
for (int i = 0; i < 4 && toReturn; i++) toReturn = patterns[i].matcher(lines[i]).matches();
|
|
|
|
return toReturn && lines[2].indexOf(':') == lines[2].lastIndexOf(':');
|
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 float buyPrice(String text) {
|
2011-08-13 12:08:34 +02:00
|
|
|
return price(text, true);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static float sellPrice(String text) {
|
2011-08-13 12:08:34 +02:00
|
|
|
return price(text, false);
|
|
|
|
}
|
|
|
|
|
2011-09-06 19:01:57 +02:00
|
|
|
private static float price(String text, boolean buy) {
|
2011-08-13 12:08:34 +02:00
|
|
|
String toContain = buy ? "b" : "s";
|
2011-05-21 19:55:55 +02:00
|
|
|
text = text.replace(" ", "").toLowerCase();
|
|
|
|
|
|
|
|
String[] split = text.split(":");
|
2011-08-13 12:08:34 +02:00
|
|
|
int part = (text.contains(toContain) ? (split[0].contains(toContain) ? 0 : 1) : -1);
|
2011-09-06 19:01:57 +02:00
|
|
|
if (part == -1 || (part == 1 && split.length != 2)) return -1;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
split[part] = split[part].replace(toContain, "");
|
|
|
|
|
|
|
|
if (uNumber.isFloat(split[part])) {
|
|
|
|
Float price = Float.parseFloat(split[part]);
|
2011-08-26 23:12:32 +02:00
|
|
|
return (price > 0 ? price : -1);
|
2011-08-13 12:08:34 +02:00
|
|
|
} else if (split[part].equals("free")) return 0;
|
2011-05-21 19:55:55 +02:00
|
|
|
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-12-16 17:20:09 +01:00
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
public static String capitalizeFirst(String name) {
|
2011-12-16 17:20:09 +01:00
|
|
|
return capitalizeFirst(name, '_');
|
|
|
|
}
|
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
public static String capitalizeFirst(String name, char separator) {
|
2011-12-16 17:20:09 +01:00
|
|
|
name = name.toLowerCase();
|
|
|
|
String[] split = name.split(Character.toString(separator));
|
|
|
|
StringBuilder total = new StringBuilder(3);
|
|
|
|
for (String s : split) total.append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).append(' ');
|
|
|
|
|
|
|
|
return total.toString().trim();
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|