summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKHobbits <rob@khobbits.co.uk>2013-05-05 00:58:44 +0100
committerKHobbits <rob@khobbits.co.uk>2013-05-05 01:23:07 +0100
commit801acbb0047d36f13c21cea4afb551f6334ec318 (patch)
treea75ae7d2699660d4801cacd48be0809b85a77efd
parentaeb1b4601c96e79f7162efa7ab923300a22e2961 (diff)
downloadEssentials-801acbb0047d36f13c21cea4afb551f6334ec318.tar
Essentials-801acbb0047d36f13c21cea4afb551f6334ec318.tar.gz
Essentials-801acbb0047d36f13c21cea4afb551f6334ec318.tar.lz
Essentials-801acbb0047d36f13c21cea4afb551f6334ec318.tar.xz
Essentials-801acbb0047d36f13c21cea4afb551f6334ec318.zip
Trim stored precision to below that of a double, to prevent rounding issues.
-rw-r--r--Essentials/src/com/earth2me/essentials/UserData.java82
-rw-r--r--Essentials/src/com/earth2me/essentials/Util.java20
2 files changed, 55 insertions, 47 deletions
diff --git a/Essentials/src/com/earth2me/essentials/UserData.java b/Essentials/src/com/earth2me/essentials/UserData.java
index c4c273221..14f60d745 100644
--- a/Essentials/src/com/earth2me/essentials/UserData.java
+++ b/Essentials/src/com/earth2me/essentials/UserData.java
@@ -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();
diff --git a/Essentials/src/com/earth2me/essentials/Util.java b/Essentials/src/com/earth2me/essentials/Util.java
index 677aa230f..879816ce8 100644
--- a/Essentials/src/com/earth2me/essentials/Util.java
+++ b/Essentials/src/com/earth2me/essentials/Util.java
@@ -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();
}
}