Improve sign price corrections

This includes a new price_precision config entry to set the amount of decimal places to allow on a shop sign (set to 2 by default). Thanks to @andrewkm for this idea.
It also fixes an issue where a zero inside the decimal places was removed instead of it's end and also removes the point and zeros from prices that only have trailing zeros to avoid confusion (e.g. in languages that use the decimal point for thousands)
This commit is contained in:
Phoenix616 2018-09-05 23:07:35 +01:00
parent 06e4e1bf17
commit df76347697
2 changed files with 11 additions and 1 deletions

View File

@ -68,6 +68,9 @@ public class Properties {
@ConfigurationComment("How much money do you get back when destroying a sign?")
public static double SHOP_REFUND_PRICE = 0;
@ConfigurationComment("How many decimal places are allowed at a maximum for prices?")
public static int PRICE_PRECISION = 2;
@PrecededBySpace
@ConfigurationComment("Should we block shops that sell things for more than they buy? (This prevents newbies from creating shops that would be exploited)")
public static boolean BLOCK_SHOPS_WITH_SELL_PRICE_HIGHER_THAN_BUY_PRICE = true;

View File

@ -1,6 +1,7 @@
package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -18,7 +19,13 @@ public class PriceChecker implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public static void onPreShopCreation(PreShopCreationEvent event) {
String line = event.getSignLine(PRICE_LINE).toUpperCase();
line = line.replaceAll("(\\.\\d*?[1-9])0+", "$1"); //remove trailing zeroes
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
String[] part = line.split(":");