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
This commit is contained in:
Josh Roy 2020-12-30 15:37:30 -05:00 committed by GitHub
parent 02ba924f33
commit c6de77130f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 12 deletions

View File

@ -162,12 +162,12 @@ public class Kit {
boolean spew = false;
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
final boolean currencyIsSuffix = ess.getSettings().isCurrencySymbolSuffixed();
final List<ItemStack> itemList = new ArrayList<>();
final List<String> commandQueue = new ArrayList<>();
final List<String> 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;
}

View File

@ -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

View File

@ -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) {