2013-01-15 21:33:00 +01:00
|
|
|
package com.Acrobot.ChestShop.Listeners.PreShopCreation;
|
|
|
|
|
|
|
|
import com.Acrobot.Breeze.Utils.PriceUtil;
|
2018-09-06 00:07:35 +02:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2013-01-15 21:33:00 +01:00
|
|
|
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
|
2020-01-29 16:35:04 +01:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2013-01-15 21:33:00 +01:00
|
|
|
import static com.Acrobot.Breeze.Utils.PriceUtil.isPrice;
|
|
|
|
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.INVALID_PRICE;
|
|
|
|
import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class PriceChecker implements Listener {
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.LOWEST)
|
|
|
|
public static void onPreShopCreation(PreShopCreationEvent event) {
|
2020-01-29 16:35:04 +01:00
|
|
|
String line = event.getSignLine(PRICE_LINE).toUpperCase(Locale.ROOT);
|
2018-09-06 00:07:35 +02:00
|
|
|
if (Properties.PRICE_PRECISION <= 0) {
|
|
|
|
line = line.replaceAll("\\.\\d*", ""); //remove too many decimal places
|
|
|
|
} else {
|
|
|
|
line = line.replaceAll("(\\.\\d{0," + Properties.PRICE_PRECISION + "})\\d*", "$1"); //remove too many decimal places
|
|
|
|
}
|
|
|
|
line = line.replaceAll("(\\.\\d*[1-9])0+", "$1"); //remove trailing zeroes
|
|
|
|
line = line.replaceAll("(\\d)\\.0+(\\D|$)", "$1$2"); //remove point and zeroes from strings that only have trailing zeros
|
2015-07-12 19:18:31 +02:00
|
|
|
|
2013-01-15 21:33:00 +01:00
|
|
|
String[] part = line.split(":");
|
|
|
|
|
2013-08-27 00:45:14 +02:00
|
|
|
if (part.length > 1 && (isInvalid(part[0]) ^ isInvalid(part[1]))) {
|
|
|
|
line = line.replace(':', ' ');
|
|
|
|
part = new String[]{line};
|
|
|
|
}
|
|
|
|
|
2013-11-01 13:48:54 +01:00
|
|
|
if (part[0].split(" ").length > 2) {
|
|
|
|
event.setOutcome(INVALID_PRICE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-12 12:50:39 +01:00
|
|
|
if (line.indexOf('B') != line.lastIndexOf('B') || line.indexOf('S') != line.lastIndexOf('S')) {
|
|
|
|
event.setOutcome(INVALID_PRICE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-15 21:33:00 +01:00
|
|
|
if (isPrice(part[0])) {
|
|
|
|
line = "B " + line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part.length > 1 && isPrice(part[1])) {
|
|
|
|
line += " S";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.length() > 15) {
|
|
|
|
line = line.replace(" ", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.length() > 15) {
|
|
|
|
event.setOutcome(INVALID_PRICE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.setSignLine(PRICE_LINE, line);
|
|
|
|
|
2013-02-01 19:29:07 +01:00
|
|
|
if (!PriceUtil.hasBuyPrice(line) && !PriceUtil.hasSellPrice(line)) {
|
2013-01-15 21:33:00 +01:00
|
|
|
event.setOutcome(INVALID_PRICE);
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 00:45:14 +02:00
|
|
|
|
|
|
|
private static boolean isInvalid(String part) {
|
|
|
|
char characters[] = {'B', 'S'};
|
|
|
|
|
|
|
|
for (char character : characters) {
|
|
|
|
if (part.contains(Character.toString(character))) {
|
|
|
|
return !PriceUtil.hasPrice(part, character);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-15 21:33:00 +01:00
|
|
|
}
|