Trim stored precision to below that of a double, to prevent rounding issues.

This commit is contained in:
KHobbits 2013-05-05 00:58:44 +01:00
parent aeb1b4601c
commit 801acbb004
2 changed files with 55 additions and 47 deletions

View File

@ -89,12 +89,12 @@ public abstract class UserData extends PlayerExtension implements IConf
public void setMoney(double value)
{
money = value;
money = Util.sanitizeMoney(value);
if (Math.abs(money) > ess.getSettings().getMaxMoney())
{
money = money < 0 ? -ess.getSettings().getMaxMoney() : ess.getSettings().getMaxMoney();
}
config.setProperty("money", value);
config.setProperty("money", money);
config.save();
}
private Map<String, Object> homes;
@ -122,11 +122,11 @@ public abstract class UserData extends PlayerExtension implements IConf
}
return search;
}
public Location getHome(String name) throws Exception
{
String search = getHomeName(name);
return config.getLocation("homes." + search, getServer());
return config.getLocation("homes." + search, getServer());
}
public Location getHome(final Location world)
@ -318,7 +318,6 @@ public abstract class UserData extends PlayerExtension implements IConf
config.setProperty("lastlocation", loc);
config.save();
}
private Location logoutLocation;
private Location _getLogoutLocation()
@ -337,7 +336,7 @@ public abstract class UserData extends PlayerExtension implements IConf
{
return logoutLocation;
}
public void setLogoutLocation(Location loc)
{
if (loc == null || loc.getWorld() == null)
@ -348,7 +347,6 @@ public abstract class UserData extends PlayerExtension implements IConf
config.setProperty("logoutlocation", loc);
config.save();
}
private long lastTeleportTimestamp;
private long _getLastTeleportTimestamp()
@ -549,7 +547,7 @@ public abstract class UserData extends PlayerExtension implements IConf
{
return config.getBoolean("muted", false);
}
public boolean getMuted()
{
return muted;
@ -825,10 +823,10 @@ public abstract class UserData extends PlayerExtension implements IConf
return config.getBoolean("powertoolsenabled", true);
}
private Map<String, Long> kitTimestamps;
private Map<String, Long> _getKitTimestamps()
{
if (config.isConfigurationSection("timestamps.kits"))
{
final ConfigurationSection section = config.getConfigurationSection("timestamps.kits");
@ -848,7 +846,7 @@ public abstract class UserData extends PlayerExtension implements IConf
}
return new HashMap<String, Long>();
}
public long getKitTimestamp(String name)
{
name = name.replace('.', '_').replace('/', '_');
@ -867,32 +865,32 @@ public abstract class UserData extends PlayerExtension implements IConf
}
public void setConfigProperty(String node, Object object)
{
final String prefix = "info.";
node = prefix+node;
if (object instanceof Map)
{
config.setProperty(node, (Map) object);
}
else if (object instanceof List)
{
config.setProperty(node, (List<String>) object);
}
else if (object instanceof Location)
{
config.setProperty(node, (Location) object);
}
else if (object instanceof ItemStack)
{
config.setProperty(node, (ItemStack) object);
}
else
{
config.setProperty(node, object);
}
config.save();
}
{
final String prefix = "info.";
node = prefix + node;
if (object instanceof Map)
{
config.setProperty(node, (Map)object);
}
else if (object instanceof List)
{
config.setProperty(node, (List<String>)object);
}
else if (object instanceof Location)
{
config.setProperty(node, (Location)object);
}
else if (object instanceof ItemStack)
{
config.setProperty(node, (ItemStack)object);
}
else
{
config.setProperty(node, object);
}
config.save();
}
public Set<String> getConfigKeys()
{
if (config.isConfigurationSection("info"))
@ -901,7 +899,7 @@ public abstract class UserData extends PlayerExtension implements IConf
}
return new HashSet<String>();
}
public Map<String, Object> getConfigMap()
{
if (config.isConfigurationSection("info"))
@ -910,16 +908,16 @@ public abstract class UserData extends PlayerExtension implements IConf
}
return new HashMap<String, Object>();
}
public Map<String, Object> getConfigMap(String node)
{
if (config.isConfigurationSection("info."+node))
if (config.isConfigurationSection("info." + node))
{
return config.getConfigurationSection("info."+node).getValues(true);
return config.getConfigurationSection("info." + node).getValues(true);
}
return new HashMap<String, Object>();
}
public void save()
{
config.save();

View File

@ -1,6 +1,8 @@
package com.earth2me.essentials;
import static com.earth2me.essentials.I18n._;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
@ -521,12 +523,19 @@ public class Util
}
return is;
}
public static double sanitizeMoney(final double value)
{
BigDecimal money = new BigDecimal(value, MathContext.DECIMAL128);
return money.doubleValue();
}
private static DecimalFormat dFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US));
public static String formatAsCurrency(final double value)
{
double fvalue = sanitizeMoney(value);
dFormat.setRoundingMode(RoundingMode.FLOOR);
String str = dFormat.format(value);
String str = dFormat.format(fvalue);
if (str.endsWith(".00"))
{
str = str.substring(0, str.length() - 3);
@ -714,10 +723,11 @@ public class Util
return pattern.matcher(input).replaceAll("\u00a7$1");
}
private static final Pattern IPPATTERN = Pattern.compile(
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
public static boolean validIP(String ipAddress) {
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
public static boolean validIP(String ipAddress)
{
return IPPATTERN.matcher(ipAddress).matches();
}
}