From c6de77130fd683fbf79c4a1816d3e6513aa1f656 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Wed, 30 Dec 2020 15:37:30 -0500 Subject: [PATCH] Rework Currency Symbol Parsing (#3628) * Made currency symbol standardized in Kit * Kits should be in a standardized format. * Having monetary rewards in kits should not break based off of a config value oriented around currency display (suffix). * Additionally, the dollar sign should be the standard money symbol which works on all servers and should not be tied to the server's individual currency symbol. * Note that the server's individual currency symbol will still work but probably should not be used due its volatility. * Reworked config currency symbol parsing in Settings * Simplifies the number of actions needed to sanitize the input. * Now just defaults to `$` if the currency-symbol cannot be parsed. * Removed symbol-suffixed parsing in NumberUtil#sanitizeCurrencyString --- .../main/java/com/earth2me/essentials/Kit.java | 4 ++-- .../java/com/earth2me/essentials/Settings.java | 18 ++++++++++++++---- .../earth2me/essentials/utils/NumberUtil.java | 7 +------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/Kit.java b/Essentials/src/main/java/com/earth2me/essentials/Kit.java index 4caa40634..d9e2995e5 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Kit.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Kit.java @@ -162,12 +162,12 @@ public class Kit { boolean spew = false; final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments(); - final boolean currencyIsSuffix = ess.getSettings().isCurrencySymbolSuffixed(); final List itemList = new ArrayList<>(); final List commandQueue = new ArrayList<>(); final List moneyQueue = new ArrayList<>(); + final String currencySymbol = ess.getSettings().getCurrencySymbol().isEmpty() ? "$" : ess.getSettings().getCurrencySymbol(); for (final String kitItem : output.getLines()) { - if (!currencyIsSuffix ? kitItem.startsWith(ess.getSettings().getCurrencySymbol()) : kitItem.endsWith(ess.getSettings().getCurrencySymbol())) { + if (kitItem.startsWith("$") || kitItem.startsWith(currencySymbol)) { moneyQueue.add(NumberUtil.sanitizeCurrencyString(kitItem, ess)); continue; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/Settings.java b/Essentials/src/main/java/com/earth2me/essentials/Settings.java index a78222edd..e01a97401 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Settings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Settings.java @@ -331,7 +331,7 @@ public class Settings implements net.ess3.api.ISettings { } else if (section.isString(command)) { final String costString = section.getString(command); try { - final double cost = Double.parseDouble(costString.trim().replace(getCurrencySymbol(), "").replaceAll("\\W", "")); + final double cost = Double.parseDouble(costString.trim().replace("$", "").replace(getCurrencySymbol(), "").replaceAll("\\W", "")); newSection.set(command.toLowerCase(Locale.ENGLISH), cost); } catch (final NumberFormatException ex) { ess.getLogger().warning("Invalid command cost for: " + command + " (" + costString + ")"); @@ -650,6 +650,7 @@ public class Settings implements net.ess3.api.ISettings { removeEffectsOnHeal = _isRemovingEffectsOnHeal(); vanishingItemPolicy = _getVanishingItemsPolicy(); bindingItemPolicy = _getBindingItemsPolicy(); + currencySymbol = _getCurrencySymbol(); } void _lateLoadItemSpawnBlacklist() { @@ -751,11 +752,20 @@ public class Settings implements net.ess3.api.ISettings { return config.getString("locale", ""); } - //This method should always only return one character due to the implementation of the calling methods - //If you need to use a string currency, for example "coins", use the translation key 'currency'. + private String currencySymbol = "$"; + + // A valid currency symbol value must be one non-integer character. + private String _getCurrencySymbol() { + String value = config.getString("currency-symbol", "$").trim(); + if (value.length() != 1 || value.matches("\\d")) { + value = "$"; + } + return value; + } + @Override public String getCurrencySymbol() { - return config.getString("currency-symbol", "$").concat("$").substring(0, 1).replaceAll("[0-9]", "$"); + return currencySymbol; } @Override diff --git a/Essentials/src/main/java/com/earth2me/essentials/utils/NumberUtil.java b/Essentials/src/main/java/com/earth2me/essentials/utils/NumberUtil.java index acfd29efc..a4d91e5a8 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/utils/NumberUtil.java +++ b/Essentials/src/main/java/com/earth2me/essentials/utils/NumberUtil.java @@ -92,12 +92,7 @@ public final class NumberUtil { } public static String sanitizeCurrencyString(final String input, final IEssentials ess) { - final String symbol = ess.getSettings().getCurrencySymbol(); - final boolean suffix = ess.getSettings().isCurrencySymbolSuffixed(); - if (input.contains(symbol)) { - return suffix ? input.substring(0, input.indexOf(symbol)) : input.substring(symbol.length()); - } - return input; + return input.replace(ess.getSettings().getCurrencySymbol(), ""); } public static boolean isInt(final String sInt) {