Merge branch 'SupaHam-custom-currency-formatting' into 2.x

This commit is contained in:
Trent Hensler 2016-07-06 15:44:37 -07:00
commit 785bd5bfcf
4 changed files with 78 additions and 4 deletions

View file

@ -3,11 +3,13 @@ package com.earth2me.essentials;
import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.signs.EssentialsSign;
import com.earth2me.essentials.textreader.IText;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.EventPriority;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -243,9 +245,9 @@ public interface ISettings extends IConf {
boolean isMilkBucketEasterEggEnabled();
boolean isSendFlyEnableOnJoin();
boolean isWorldTimePermissions();
boolean isSpawnOnJoin();
boolean isTeleportToCenterLocation();
@ -257,6 +259,8 @@ public interface ISettings extends IConf {
Entry<Pattern, Long> getCommandCooldownEntry(String label);
boolean isCommandCooldownPersistent(String label);
boolean isNpcsInBalanceRanking();
NumberFormat getCurrencyFormat();
}

View file

@ -6,6 +6,8 @@ import com.earth2me.essentials.signs.Signs;
import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.SimpleTextInput;
import com.earth2me.essentials.utils.FormatUtil;
import com.earth2me.essentials.utils.NumberUtil;
import net.ess3.api.IEssentials;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
@ -14,8 +16,15 @@ import org.bukkit.event.EventPriority;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.*;
import java.util.Locale.Category;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -24,6 +33,8 @@ import static com.earth2me.essentials.I18n.tl;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import com.google.common.base.Preconditions;
public class Settings implements net.ess3.api.ISettings {
private final transient EssentialsConf config;
@ -537,6 +548,7 @@ public class Settings implements net.ess3.api.ISettings {
muteCommands = _getMuteCommands();
commandCooldowns = _getCommandCooldowns();
npcsInBalanceRanking = _isNpcsInBalanceRanking();
currencyFormat = _getCurrencyFormat();
}
private List<Integer> itemSpawnBl = new ArrayList<Integer>();
@ -1168,7 +1180,7 @@ public class Settings implements net.ess3.api.ISettings {
public boolean isWorldTimePermissions() {
return config.getBoolean("world-time-permissions", false);
}
@Override
public boolean isSpawnOnJoin() {
return config.getBoolean("spawn-on-join", false);
@ -1274,4 +1286,46 @@ public class Settings implements net.ess3.api.ISettings {
public boolean isNpcsInBalanceRanking() {
return npcsInBalanceRanking;
}
private NumberFormat currencyFormat;
private NumberFormat _getCurrencyFormat() {
String currencyFormatString = config.getString("currency-format", "#,##0.00");
String symbolLocaleString = config.getString("currency-symbol-format-locale");
DecimalFormatSymbols decimalFormatSymbols;
if (symbolLocaleString != null) {
decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.forLanguageTag(symbolLocaleString));
} else {
// Fallback to the JVM's default locale
decimalFormatSymbols = DecimalFormatSymbols.getInstance();
}
DecimalFormat currencyFormat = new DecimalFormat(currencyFormatString, decimalFormatSymbols);
currencyFormat.setRoundingMode(RoundingMode.FLOOR);
// Updates NumberUtil#PRETTY_FORMAT field so that all of Essentials
// can follow a single format.
try {
Field field = NumberUtil.class.getDeclaredField("PRETTY_FORMAT");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, currencyFormat);
modifiersField.setAccessible(false);
field.setAccessible(false);
} catch (NoSuchFieldException | IllegalAccessException e) {
ess.getLogger().severe("Failed to apply custom currency format: " + e.getMessage());
if (isDebug()) {
e.printStackTrace();
}
}
return currencyFormat;
}
@Override
public NumberFormat getCurrencyFormat() {
return this.currencyFormat;
}
}

View file

@ -15,6 +15,9 @@ import static com.earth2me.essentials.I18n.tl;
public class NumberUtil {
static DecimalFormat twoDPlaces = new DecimalFormat("#,###.##");
static DecimalFormat currencyFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US));
// This field is likely to be modified in com.earth2me.essentials.Settings when loading currency format.
// This ensures that we can supply a constant formatting.
static final NumberFormat PRETTY_FORMAT = NumberFormat.getInstance(Locale.US);
static {

View file

@ -585,6 +585,19 @@ use-bukkit-permissions: false
# Minimum acceptable amount to be used in /pay.
minimum-pay-amount: 0.001
# The format of currency, excluding symbols. See currency-sumbol-format-locale for symbol configuration.
#
# "#,##0.00" is how the majority of countries display currency.
#currency-format: "#,##0.00"
# Format currency symbols. Some locales use , and . interchangeably.
# Some formats do not display properly in-game due to faulty Minecraft font rendering.
#
# For 1.234,50 use de-DE
# For 1,234.50 use en-US
# For 1'234,50 use fr-ch
#currency-symbol-format-locale: en-US
############################################################
# +------------------------------------------------------+ #
# | EssentialsHelp | #