mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2025-01-05 23:08:23 +00:00
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:
parent
02ba924f33
commit
c6de77130f
3 changed files with 17 additions and 12 deletions
|
@ -162,12 +162,12 @@ public class Kit {
|
||||||
|
|
||||||
boolean spew = false;
|
boolean spew = false;
|
||||||
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
|
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
|
||||||
final boolean currencyIsSuffix = ess.getSettings().isCurrencySymbolSuffixed();
|
|
||||||
final List<ItemStack> itemList = new ArrayList<>();
|
final List<ItemStack> itemList = new ArrayList<>();
|
||||||
final List<String> commandQueue = new ArrayList<>();
|
final List<String> commandQueue = new ArrayList<>();
|
||||||
final List<String> moneyQueue = new ArrayList<>();
|
final List<String> moneyQueue = new ArrayList<>();
|
||||||
|
final String currencySymbol = ess.getSettings().getCurrencySymbol().isEmpty() ? "$" : ess.getSettings().getCurrencySymbol();
|
||||||
for (final String kitItem : output.getLines()) {
|
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));
|
moneyQueue.add(NumberUtil.sanitizeCurrencyString(kitItem, ess));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -331,7 +331,7 @@ public class Settings implements net.ess3.api.ISettings {
|
||||||
} else if (section.isString(command)) {
|
} else if (section.isString(command)) {
|
||||||
final String costString = section.getString(command);
|
final String costString = section.getString(command);
|
||||||
try {
|
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);
|
newSection.set(command.toLowerCase(Locale.ENGLISH), cost);
|
||||||
} catch (final NumberFormatException ex) {
|
} catch (final NumberFormatException ex) {
|
||||||
ess.getLogger().warning("Invalid command cost for: " + command + " (" + costString + ")");
|
ess.getLogger().warning("Invalid command cost for: " + command + " (" + costString + ")");
|
||||||
|
@ -650,6 +650,7 @@ public class Settings implements net.ess3.api.ISettings {
|
||||||
removeEffectsOnHeal = _isRemovingEffectsOnHeal();
|
removeEffectsOnHeal = _isRemovingEffectsOnHeal();
|
||||||
vanishingItemPolicy = _getVanishingItemsPolicy();
|
vanishingItemPolicy = _getVanishingItemsPolicy();
|
||||||
bindingItemPolicy = _getBindingItemsPolicy();
|
bindingItemPolicy = _getBindingItemsPolicy();
|
||||||
|
currencySymbol = _getCurrencySymbol();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _lateLoadItemSpawnBlacklist() {
|
void _lateLoadItemSpawnBlacklist() {
|
||||||
|
@ -751,11 +752,20 @@ public class Settings implements net.ess3.api.ISettings {
|
||||||
return config.getString("locale", "");
|
return config.getString("locale", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
//This method should always only return one character due to the implementation of the calling methods
|
private String currencySymbol = "$";
|
||||||
//If you need to use a string currency, for example "coins", use the translation key 'currency'.
|
|
||||||
|
// 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
|
@Override
|
||||||
public String getCurrencySymbol() {
|
public String getCurrencySymbol() {
|
||||||
return config.getString("currency-symbol", "$").concat("$").substring(0, 1).replaceAll("[0-9]", "$");
|
return currencySymbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -92,12 +92,7 @@ public final class NumberUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String sanitizeCurrencyString(final String input, final IEssentials ess) {
|
public static String sanitizeCurrencyString(final String input, final IEssentials ess) {
|
||||||
final String symbol = ess.getSettings().getCurrencySymbol();
|
return input.replace(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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isInt(final String sInt) {
|
public static boolean isInt(final String sInt) {
|
||||||
|
|
Loading…
Reference in a new issue