From df763476970cafdccff59f1db1ed6c3eda0e16d2 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Wed, 5 Sep 2018 23:07:35 +0100 Subject: [PATCH] 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) --- .../com/Acrobot/ChestShop/Configuration/Properties.java | 3 +++ .../Listeners/PreShopCreation/PriceChecker.java | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java index ad5c16c..cfd89d7 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java @@ -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; diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/PriceChecker.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/PriceChecker.java index 78557d1..9a78527 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/PriceChecker.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/PriceChecker.java @@ -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(":");