From 6044cca54fea75134d085847ce9871fa865e72f2 Mon Sep 17 00:00:00 2001 From: snowleo Date: Sun, 5 May 2013 11:41:19 +0200 Subject: =?UTF-8?q?ECONOMY=20MADNESS=3F=20THIS=20IS=20=E2=80=A6=20FIXED?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/earth2me/essentials/EssentialsConf.java | 34 ++++ .../src/com/earth2me/essentials/ISettings.java | 2 +- Essentials/src/com/earth2me/essentials/Kit.java | 2 +- .../src/com/earth2me/essentials/Settings.java | 41 ++--- Essentials/src/com/earth2me/essentials/Trade.java | 8 +- Essentials/src/com/earth2me/essentials/User.java | 4 +- .../src/com/earth2me/essentials/UserData.java | 5 +- Essentials/src/com/earth2me/essentials/Worth.java | 20 +-- .../src/com/earth2me/essentials/api/Economy.java | 187 ++++++++++++++++++--- .../earth2me/essentials/commands/Commandeco.java | 2 +- .../earth2me/essentials/signs/EssentialsSign.java | 30 ++-- .../com/earth2me/essentials/signs/SignTrade.java | 27 ++- .../test/com/earth2me/essentials/UserTest.java | 10 +- 13 files changed, 270 insertions(+), 102 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/EssentialsConf.java b/Essentials/src/com/earth2me/essentials/EssentialsConf.java index bf651e6ee..13b0d4556 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsConf.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsConf.java @@ -5,6 +5,9 @@ import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.api.InvalidWorldException; import com.google.common.io.Files; import java.io.*; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.MathContext; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; @@ -493,6 +496,11 @@ public class EssentialsConf extends YamlConfiguration { return get(path); } + + public void setProperty(final String path, final BigDecimal bigDecimal) + { + set(path, bigDecimal.toString()); + } public void setProperty(String path, Object object) { @@ -515,6 +523,32 @@ public class EssentialsConf extends YamlConfiguration { return super.get(path, def); } + + + public synchronized BigDecimal getBigDecimal(final String path, final BigDecimal def) + { + final String input = super.getString(path); + return toBigDecimal(input, def); + } + + public static BigDecimal toBigDecimal(final String input, final BigDecimal def) + { + if (input == null || input.isEmpty()) + { + return def; + } + else + { + try + { + return new BigDecimal(input, MathContext.DECIMAL128); + } + catch (ArithmeticException e) + { + return def; + } + } + } @Override public synchronized boolean getBoolean(String path) diff --git a/Essentials/src/com/earth2me/essentials/ISettings.java b/Essentials/src/com/earth2me/essentials/ISettings.java index 7de363c00..121d72c14 100644 --- a/Essentials/src/com/earth2me/essentials/ISettings.java +++ b/Essentials/src/com/earth2me/essentials/ISettings.java @@ -79,7 +79,7 @@ public interface ISettings extends IConf int getSpawnMobLimit(); - int getStartingBalance(); + BigDecimal getStartingBalance(); double getTeleportCooldown(); diff --git a/Essentials/src/com/earth2me/essentials/Kit.java b/Essentials/src/com/earth2me/essentials/Kit.java index 9305aa770..16cb35fc9 100644 --- a/Essentials/src/com/earth2me/essentials/Kit.java +++ b/Essentials/src/com/earth2me/essentials/Kit.java @@ -33,7 +33,7 @@ public class Kit { String cost = ""; BigDecimal costPrice = new Trade("kit-" + kitItem.toLowerCase(Locale.ENGLISH), ess).getCommandCost(user); - if (costPrice.compareTo(BigDecimal.ZERO) > 0) + if (costPrice.signum() > 0) { cost = _("kitCost", Util.displayCurrency(costPrice, ess)); } diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index 26aafb29c..2b0c9249f 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -113,9 +113,9 @@ public class Settings implements ISettings } @Override - public int getStartingBalance() + public BigDecimal getStartingBalance() { - return config.getInt("starting-balance", 0); + return config.getBigDecimal("starting-balance", BigDecimal.ZERO); } @Override @@ -234,7 +234,7 @@ public class Settings implements ISettings name = name.replace('.', '_').replace('/', '_'); if (commandCosts != null) { - return BigDecimal.valueOf(commandCosts.getDouble(name, 0.0)); + return EssentialsConf.toBigDecimal(commandCosts.getString(name), BigDecimal.ZERO); } return BigDecimal.ZERO; } @@ -690,42 +690,33 @@ public class Settings implements ISettings { return config.getBoolean(configName, def); } - private final static double MAXMONEY = 10000000000000.0; - private BigDecimal maxMoney = BigDecimal.valueOf(MAXMONEY); + private final static BigDecimal MAXMONEY = new BigDecimal("10000000000000"); + private BigDecimal maxMoney = MAXMONEY; private BigDecimal _getMaxMoney() { - double max = config.getDouble("max-money", MAXMONEY); - if (Math.abs(max) > MAXMONEY) - { - max = max < 0 ? -MAXMONEY : MAXMONEY; - } - return BigDecimal.valueOf(max); + return config.getBigDecimal("max-money", MAXMONEY); } - - @Override + + @Override public BigDecimal getMaxMoney() { return maxMoney; } - private final static double MINMONEY = -10000000000000.0; - private BigDecimal minMoney = BigDecimal.valueOf(MINMONEY); + private final static BigDecimal MINMONEY = new BigDecimal("-10000000000000"); + private BigDecimal minMoney = MINMONEY; private BigDecimal _getMinMoney() { - double min = config.getDouble("min-money", MINMONEY); - if (min > 0) + BigDecimal min = config.getBigDecimal("min-money", MINMONEY); + if (min.signum() > 0) { - min = -min; + min = min.negate(); } - if (min < MINMONEY) - { - min = MINMONEY; - } - return BigDecimal.valueOf(min); + return min; } - - @Override + + @Override public BigDecimal getMinMoney() { return minMoney; diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index 0c3e4a3c5..9506e72a9 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -79,7 +79,7 @@ public class Trade } if (getMoney() != null - && getMoney().compareTo(BigDecimal.ZERO) > 0 + && getMoney().signum() > 0 && !user.canAfford(getMoney())) { throw new ChargeException(_("notEnoughMoney")); @@ -93,7 +93,7 @@ public class Trade BigDecimal money; if (command != null && !command.isEmpty() - && (money = getCommandCost(user)).compareTo(BigDecimal.ZERO) > 0 + && (money = getCommandCost(user)).signum() > 0 && !user.canAfford(money)) { throw new ChargeException(_("notEnoughMoney")); @@ -114,7 +114,7 @@ public class Trade public boolean pay(final IUser user, final boolean dropItems) { boolean success = true; - if (getMoney() != null && getMoney().compareTo(BigDecimal.ZERO) > 0) + if (getMoney() != null && getMoney().signum() > 0) { user.giveMoney(getMoney()); } @@ -166,7 +166,7 @@ public class Trade if (getMoney() != null) { - if (!user.canAfford(getMoney()) && getMoney().compareTo(BigDecimal.ZERO) > 0) + if (!user.canAfford(getMoney()) && getMoney().signum() > 0) { throw new ChargeException(_("notEnoughMoney")); } diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index 876f8933c..690322d68 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -187,7 +187,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser public boolean canAfford(final BigDecimal cost, final boolean permcheck) { - if (cost.compareTo(BigDecimal.ZERO) <= 0) + if (cost.signum() <= 0) { return true; } @@ -196,7 +196,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser { return (remainingBalance.compareTo(ess.getSettings().getMinMoney()) >= 0); } - return (remainingBalance.compareTo(BigDecimal.ZERO) >= 0); + return (remainingBalance.signum() >= 0); } public void dispose() diff --git a/Essentials/src/com/earth2me/essentials/UserData.java b/Essentials/src/com/earth2me/essentials/UserData.java index 843ef8412..59d3849e5 100644 --- a/Essentials/src/com/earth2me/essentials/UserData.java +++ b/Essentials/src/com/earth2me/essentials/UserData.java @@ -71,15 +71,14 @@ public abstract class UserData extends PlayerExtension implements IConf private BigDecimal _getMoney() { - double bal = ess.getSettings().getStartingBalance(); + BigDecimal result = ess.getSettings().getStartingBalance(); BigDecimal maxMoney = ess.getSettings().getMaxMoney(); BigDecimal minMoney = ess.getSettings().getMinMoney(); if (config.hasProperty("money")) { - bal = config.getDouble("money", bal); + result = config.getBigDecimal("money", result); } - BigDecimal result = BigDecimal.valueOf(bal); if (result.compareTo(maxMoney) > 0) { result = maxMoney; diff --git a/Essentials/src/com/earth2me/essentials/Worth.java b/Essentials/src/com/earth2me/essentials/Worth.java index ee894fe3c..8cc65e24c 100644 --- a/Essentials/src/com/earth2me/essentials/Worth.java +++ b/Essentials/src/com/earth2me/essentials/Worth.java @@ -22,25 +22,25 @@ public class Worth implements IConf public BigDecimal getPrice(ItemStack itemStack) { String itemname = itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""); - double result; - result = config.getDouble("worth." + itemname + "." + itemStack.getDurability(), Double.NaN); - if (Double.isNaN(result)) + BigDecimal result; + result = config.getBigDecimal("worth." + itemname + "." + itemStack.getDurability(), BigDecimal.ONE.negate()); + if (result.signum() <= 0) { - result = config.getDouble("worth." + itemname + ".0", Double.NaN); + result = config.getBigDecimal("worth." + itemname + ".0", BigDecimal.ONE.negate()); } - if (Double.isNaN(result)) + if (result.signum() <= 0) { - result = config.getDouble("worth." + itemname, Double.NaN); + result = config.getBigDecimal("worth." + itemname, BigDecimal.ONE.negate()); } - if (Double.isNaN(result)) + if (result.signum() <= 0) { - result = config.getDouble("worth-" + itemStack.getTypeId(), Double.NaN); + result = config.getBigDecimal("worth-" + itemStack.getTypeId(), BigDecimal.ONE.negate()); } - if (Double.isNaN(result)) + if (result.signum() <= 0) { return null; } - return BigDecimal.valueOf(result); + return result; } public void setPrice(ItemStack itemStack, double price) diff --git a/Essentials/src/com/earth2me/essentials/api/Economy.java b/Essentials/src/com/earth2me/essentials/api/Economy.java index a0830d8d3..1a71e5b7d 100644 --- a/Essentials/src/com/earth2me/essentials/api/Economy.java +++ b/Essentials/src/com/earth2me/essentials/api/Economy.java @@ -7,13 +7,13 @@ import com.earth2me.essentials.User; import com.earth2me.essentials.Util; import java.io.File; import java.math.BigDecimal; +import java.math.MathContext; import java.util.logging.Level; import java.util.logging.Logger; /** - * Instead of using this api directly, we recommend to use the register plugin: - * http://bit.ly/RegisterMethod + * Instead of using this api directly, we recommend to use the register plugin: http://bit.ly/RegisterMethod */ public final class Economy { @@ -23,6 +23,7 @@ public final class Economy private static final Logger logger = Logger.getLogger("Minecraft"); private static IEssentials ess; private static final String noCallBeforeLoad = "Essentials API is called before Essentials is loaded."; + public static final MathContext MATH_CONTEXT = MathContext.DECIMAL128; /** * @param aEss the ess to set @@ -77,100 +78,177 @@ public final class Economy /** * Returns the balance of a user + * * @param name Name of the user * @return balance - * @throws UserDoesNotExistException + * @throws UserDoesNotExistException */ + @Deprecated public static double getMoney(String name) throws UserDoesNotExistException + { + return getMoneyExact(name).doubleValue(); + } + + public static BigDecimal getMoneyExact(String name) throws UserDoesNotExistException { User user = getUserByName(name); if (user == null) { throw new UserDoesNotExistException(name); } - return user.getMoney().doubleValue(); + return user.getMoney(); } /** * Sets the balance of a user + * * @param name Name of the user * @param balance The balance you want to set * @throws UserDoesNotExistException If a user by that name does not exists * @throws NoLoanPermittedException If the user is not allowed to have a negative balance */ + @Deprecated public static void setMoney(String name, double balance) throws UserDoesNotExistException, NoLoanPermittedException + { + try + { + setMoney(name, BigDecimal.valueOf(balance)); + } + catch (ArithmeticException e) + { + logger.log(Level.WARNING, "Failed to set balance of " + name + " to " + balance + ": " + e.getMessage(), e); + } + } + + public static void setMoney(String name, BigDecimal balance) throws UserDoesNotExistException, NoLoanPermittedException { User user = getUserByName(name); - BigDecimal newBalance = BigDecimal.valueOf(balance); if (user == null) { throw new UserDoesNotExistException(name); } - if (newBalance.compareTo(ess.getSettings().getMinMoney()) < 0) + if (balance.compareTo(ess.getSettings().getMinMoney()) < 0) { throw new NoLoanPermittedException(); } - if (newBalance.compareTo(BigDecimal.ZERO) < 0 && !user.isAuthorized("essentials.eco.loan")) + if (balance.signum() < 0 && !user.isAuthorized("essentials.eco.loan")) { throw new NoLoanPermittedException(); } - user.setMoney(newBalance); + user.setMoney(balance); } /** * Adds money to the balance of a user + * * @param name Name of the user * @param amount The money you want to add * @throws UserDoesNotExistException If a user by that name does not exists * @throws NoLoanPermittedException If the user is not allowed to have a negative balance */ + @Deprecated public static void add(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException { - double result = getMoney(name) + amount; + try + { + add(name, BigDecimal.valueOf(amount)); + } + catch (ArithmeticException e) + { + logger.log(Level.WARNING, "Failed to add " + amount + " to balance of " + name + ": " + e.getMessage(), e); + } + } + + public static void add(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException + { + BigDecimal result = getMoneyExact(name).add(amount, MATH_CONTEXT); setMoney(name, result); } /** * Substracts money from the balance of a user + * * @param name Name of the user * @param amount The money you want to substract * @throws UserDoesNotExistException If a user by that name does not exists * @throws NoLoanPermittedException If the user is not allowed to have a negative balance */ + @Deprecated public static void subtract(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException { - double result = getMoney(name) - amount; + try + { + substract(name, BigDecimal.valueOf(amount)); + } + catch (ArithmeticException e) + { + logger.log(Level.WARNING, "Failed to substract " + amount + " of balance of " + name + ": " + e.getMessage(), e); + } + } + + public static void substract(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException + { + BigDecimal result = getMoneyExact(name).subtract(amount, MATH_CONTEXT); setMoney(name, result); } /** * Divides the balance of a user by a value + * * @param name Name of the user * @param value The balance is divided by this value * @throws UserDoesNotExistException If a user by that name does not exists * @throws NoLoanPermittedException If the user is not allowed to have a negative balance */ - public static void divide(String name, double value) throws UserDoesNotExistException, NoLoanPermittedException + @Deprecated + public static void divide(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException + { + try + { + divide(name, BigDecimal.valueOf(amount)); + } + catch (ArithmeticException e) + { + logger.log(Level.WARNING, "Failed to divide balance of " + name + " by " + amount + ": " + e.getMessage(), e); + } + } + + public static void divide(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException { - double result = getMoney(name) / value; + BigDecimal result = getMoneyExact(name).divide(amount, MATH_CONTEXT); setMoney(name, result); } /** * Multiplies the balance of a user by a value + * * @param name Name of the user * @param value The balance is multiplied by this value * @throws UserDoesNotExistException If a user by that name does not exists * @throws NoLoanPermittedException If the user is not allowed to have a negative balance */ - public static void multiply(String name, double value) throws UserDoesNotExistException, NoLoanPermittedException + @Deprecated + public static void multiply(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException + { + try + { + multiply(name, BigDecimal.valueOf(amount)); + } + catch (ArithmeticException e) + { + logger.log(Level.WARNING, "Failed to multiply balance of " + name + " by " + amount + ": " + e.getMessage(), e); + } + } + + public static void multiply(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException { - double result = getMoney(name) * value; + BigDecimal result = getMoneyExact(name).multiply(amount, MATH_CONTEXT); setMoney(name, result); } /** * Resets the balance of a user to the starting balance + * * @param name Name of the user * @throws UserDoesNotExistException If a user by that name does not exists * @throws NoLoanPermittedException If the user is not allowed to have a negative balance @@ -190,9 +268,23 @@ public final class Economy * @return true, if the user has more or an equal amount of money * @throws UserDoesNotExistException If a user by that name does not exists */ + @Deprecated public static boolean hasEnough(String name, double amount) throws UserDoesNotExistException { - return amount <= getMoney(name); + try + { + return hasEnough(name, BigDecimal.valueOf(amount)); + } + catch (ArithmeticException e) + { + logger.log(Level.WARNING, "Failed to compare balance of " + name + " with " + amount + ": " + e.getMessage(), e); + return false; + } + } + + public static boolean hasEnough(String name, BigDecimal amount) throws UserDoesNotExistException, ArithmeticException + { + return amount.compareTo(getMoneyExact(name)) <= 0; } /** @@ -201,9 +293,23 @@ public final class Economy * @return true, if the user has more money * @throws UserDoesNotExistException If a user by that name does not exists */ + @Deprecated public static boolean hasMore(String name, double amount) throws UserDoesNotExistException { - return amount < getMoney(name); + try + { + return hasMore(name, BigDecimal.valueOf(amount)); + } + catch (ArithmeticException e) + { + logger.log(Level.WARNING, "Failed to compare balance of " + name + " with " + amount + ": " + e.getMessage(), e); + return false; + } + } + + public static boolean hasMore(String name, BigDecimal amount) throws UserDoesNotExistException, ArithmeticException + { + return amount.compareTo(getMoneyExact(name)) < 0; } /** @@ -212,39 +318,69 @@ public final class Economy * @return true, if the user has less money * @throws UserDoesNotExistException If a user by that name does not exists */ + @Deprecated public static boolean hasLess(String name, double amount) throws UserDoesNotExistException { - return amount > getMoney(name); + try + { + return hasLess(name, BigDecimal.valueOf(amount)); + } + catch (ArithmeticException e) + { + logger.log(Level.WARNING, "Failed to compare balance of " + name + " with " + amount + ": " + e.getMessage(), e); + return false; + } + } + + public static boolean hasLess(String name, BigDecimal amount) throws UserDoesNotExistException, ArithmeticException + { + return amount.compareTo(getMoneyExact(name)) > 0; } /** * Test if the user has a negative balance + * * @param name Name of the user * @return true, if the user has a negative balance * @throws UserDoesNotExistException If a user by that name does not exists */ public static boolean isNegative(String name) throws UserDoesNotExistException { - return getMoney(name) < 0.0; + return getMoneyExact(name).signum() < 0; } /** - * Formats the amount of money like all other Essentials functions. - * Example: $100000 or $12345.67 + * Formats the amount of money like all other Essentials functions. Example: $100000 or $12345.67 + * * @param amount The amount of money * @return Formatted money */ + @Deprecated public static String format(double amount) + { + try + { + return format(BigDecimal.valueOf(amount)); + } + catch (NumberFormatException e) + { + logger.log(Level.WARNING, "Failed to disply " + amount + ": " + e.getMessage(), e); + return "NaN"; + } + } + + public static String format(BigDecimal amount) { if (ess == null) { throw new RuntimeException(noCallBeforeLoad); } - return Util.displayCurrency(BigDecimal.valueOf(amount), ess); + return Util.displayCurrency(amount, ess); } /** * Test if a player exists to avoid the UserDoesNotExistException + * * @param name Name of the user * @return true, if the user exists */ @@ -255,9 +391,10 @@ public final class Economy /** * Test if a player is a npc + * * @param name Name of the player * @return true, if it's a npc - * @throws UserDoesNotExistException + * @throws UserDoesNotExistException */ public static boolean isNPC(String name) throws UserDoesNotExistException { @@ -271,6 +408,7 @@ public final class Economy /** * Creates dummy files for a npc, if there is no player yet with that name. + * * @param name Name of the player * @return true, if a new npc was created */ @@ -286,9 +424,10 @@ public final class Economy } /** - * Deletes a user, if it is marked as npc. + * Deletes a user, if it is marked as npc. + * * @param name Name of the player - * @throws UserDoesNotExistException + * @throws UserDoesNotExistException */ public static void removeNPC(String name) throws UserDoesNotExistException { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandeco.java b/Essentials/src/com/earth2me/essentials/commands/Commandeco.java index c7e1c47a5..ffb9ef645 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandeco.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandeco.java @@ -26,7 +26,7 @@ public class Commandeco extends EssentialsCommand } Commandeco.EcoCommands cmd; - BigDecimal startingBalance = new BigDecimal(ess.getSettings().getStartingBalance()); + BigDecimal startingBalance = ess.getSettings().getStartingBalance(); BigDecimal amount; BigDecimal broadcast = null; BigDecimal broadcastAll = null; diff --git a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java index bea17b808..8f948bc3c 100644 --- a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java +++ b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java @@ -18,7 +18,8 @@ import org.bukkit.inventory.ItemStack; public class EssentialsSign { private static final Set EMPTY_SET = new HashSet(); - + protected static final BigDecimal MINTRANSACTION = new BigDecimal("0.01"); + protected transient final String signName; public EssentialsSign(final String signName) @@ -68,8 +69,9 @@ public class EssentialsSign { return _("signFormatTemplate", this.signName); } - - public String getName() { + + public String getName() + { return this.signName; } @@ -89,7 +91,7 @@ public class EssentialsSign try { return (!user.isDead() && (user.isAuthorized("essentials.signs." + signName.toLowerCase(Locale.ENGLISH) + ".use") - || user.isAuthorized("essentials.signs.use." + signName.toLowerCase(Locale.ENGLISH)))) + || user.isAuthorized("essentials.signs.use." + signName.toLowerCase(Locale.ENGLISH)))) && onSignInteract(sign, user, getUsername(user), ess); } catch (ChargeException ex) @@ -264,7 +266,7 @@ public class EssentialsSign { return EMPTY_SET; } - + public boolean areHeavyEventRequired() { return false; @@ -371,24 +373,28 @@ public class EssentialsSign protected final BigDecimal getMoney(final String line) throws SignException { final boolean isMoney = line.matches("^[^0-9-\\.][\\.0-9]+$"); - return isMoney ? BigDecimal.valueOf(getDoublePositive(line.substring(1))) : null; + return isMoney ? getBigDecimalPositive(line.substring(1)) : null; } - protected final Double getDoublePositive(final String line) throws SignException + protected final BigDecimal getBigDecimalPositive(final String line) throws SignException { - final double quantity = getDouble(line); - if (Math.round(quantity * 100.0) < 1.0) + final BigDecimal quantity = getBigDecimal(line); + if (quantity.compareTo(MINTRANSACTION) < 0) { throw new SignException(_("moreThanZero")); } return quantity; } - protected final Double getDouble(final String line) throws SignException + protected final BigDecimal getBigDecimal(final String line) throws SignException { try { - return Double.parseDouble(line); + return new BigDecimal(line); + } + catch (ArithmeticException ex) + { + throw new SignException(ex.getMessage(), ex); } catch (NumberFormatException ex) { @@ -468,7 +474,7 @@ public class EssentialsSign public final void setLine(final int index, final String text) { event.setLine(index, text); - sign.setLine(index, text); + sign.setLine(index, text); updateSign(); } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java index 651e571b2..eb6cc60ee 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java @@ -13,7 +13,6 @@ public class SignTrade extends EssentialsSign { super("Trade"); } - static final BigDecimal MINTRANSACTION = BigDecimal.valueOf(0.01); @Override protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException @@ -154,7 +153,7 @@ public class SignTrade extends EssentialsSign if (split.length == 2 && amountNeeded) { final BigDecimal money = getMoney(split[0]); - BigDecimal amount = BigDecimal.valueOf(getDoublePositive(split[1])); + BigDecimal amount = getBigDecimalPositive(split[1]); if (money != null && amount != null) { amount = amount.subtract(amount.remainder(money)); @@ -223,7 +222,7 @@ public class SignTrade extends EssentialsSign try { final BigDecimal money = getMoney(split[0]); - final BigDecimal amount = BigDecimal.valueOf(notEmpty ? getDoublePositive(split[1]) : getDouble(split[1])); + final BigDecimal amount = notEmpty ? getBigDecimalPositive(split[1]) : getBigDecimal(split[1]); if (money != null && amount != null) { return new Trade(fullAmount ? amount : money, ess); @@ -270,17 +269,17 @@ public class SignTrade extends EssentialsSign final BigDecimal money = trade.getMoney(); if (money != null) { - changeAmount(sign, index, -money.doubleValue(), ess); + changeAmount(sign, index, money.negate(), ess); } final ItemStack item = trade.getItemStack(); if (item != null) { - changeAmount(sign, index, -item.getAmount(), ess); + changeAmount(sign, index, BigDecimal.valueOf(-item.getAmount()), ess); } final Integer exp = trade.getExperience(); if (exp != null) { - changeAmount(sign, index, -exp.intValue(), ess); + changeAmount(sign, index, BigDecimal.valueOf(-exp.intValue()), ess); } } @@ -289,22 +288,22 @@ public class SignTrade extends EssentialsSign final BigDecimal money = trade.getMoney(); if (money != null) { - changeAmount(sign, index, money.doubleValue(), ess); + changeAmount(sign, index, money, ess); } final ItemStack item = trade.getItemStack(); if (item != null) { - changeAmount(sign, index, item.getAmount(), ess); + changeAmount(sign, index, BigDecimal.valueOf(item.getAmount()), ess); } final Integer exp = trade.getExperience(); if (exp != null) { - changeAmount(sign, index, exp.intValue(), ess); + changeAmount(sign, index, BigDecimal.valueOf(exp.intValue()), ess); } } //TODO: Translate these exceptions. - private void changeAmount(final ISign sign, final int index, final double value, final IEssentials ess) throws SignException + private void changeAmount(final ISign sign, final int index, final BigDecimal value, final IEssentials ess) throws SignException { final String line = sign.getLine(index).trim(); @@ -317,10 +316,10 @@ public class SignTrade extends EssentialsSign if (split.length == 2) { final BigDecimal money = getMoney(split[0]); - final BigDecimal amount = BigDecimal.valueOf(getDouble(split[1])); + final BigDecimal amount = getBigDecimal(split[1]); if (money != null && amount != null) { - final String newline = Util.shortCurrency(money, ess) + ":" + Util.shortCurrency(amount.add(BigDecimal.valueOf(value)), ess).substring(1); + final String newline = Util.shortCurrency(money, ess) + ":" + Util.shortCurrency(amount.add(value), ess).substring(1); if (newline.length() > 15) { throw new SignException("This sign is full: Line too long!"); @@ -336,7 +335,7 @@ public class SignTrade extends EssentialsSign { final int stackamount = getIntegerPositive(split[0]); final int amount = getInteger(split[2]); - final String newline = stackamount + " " + split[1] + ":" + (amount + Math.round(value)); + final String newline = stackamount + " " + split[1] + ":" + (amount + value.intValueExact()); if (newline.length() > 15) { throw new SignException("This sign is full: Line too long!"); @@ -350,7 +349,7 @@ public class SignTrade extends EssentialsSign //TODO: Unused local variable final ItemStack item = getItemStack(split[1], stackamount, ess); final int amount = getInteger(split[2]); - final String newline = stackamount + " " + split[1] + ":" + (amount + Math.round(value)); + final String newline = stackamount + " " + split[1] + ":" + (amount + value.intValueExact()); if (newline.length() > 15) { throw new SignException("This sign is full: Line too long!"); diff --git a/Essentials/test/com/earth2me/essentials/UserTest.java b/Essentials/test/com/earth2me/essentials/UserTest.java index e7f2d1f48..150f9653d 100644 --- a/Essentials/test/com/earth2me/essentials/UserTest.java +++ b/Essentials/test/com/earth2me/essentials/UserTest.java @@ -70,13 +70,13 @@ public class UserTest extends TestCase { should("properly set, take, give, and get money"); User user = ess.getUser(base1); - double i = 100.5; - user.setMoney(BigDecimal.valueOf(i)); + BigDecimal i = new BigDecimal("100.5"); + user.setMoney(i); user.takeMoney(new BigDecimal(50)); - i -= 50; + i = i.subtract(BigDecimal.valueOf(50)); user.giveMoney(new BigDecimal(25)); - i += 25; - assertEquals(user.getMoney().doubleValue(), i); + i = i.add(BigDecimal.valueOf(25)); + assertEquals(user.getMoney(), i); } public void testGetGroup() -- cgit v1.2.3