diff options
21 files changed, 216 insertions, 157 deletions
diff --git a/Essentials/src/com/earth2me/essentials/ISettings.java b/Essentials/src/com/earth2me/essentials/ISettings.java index be75ef50d..7de363c00 100644 --- a/Essentials/src/com/earth2me/essentials/ISettings.java +++ b/Essentials/src/com/earth2me/essentials/ISettings.java @@ -3,6 +3,7 @@ package com.earth2me.essentials; import com.earth2me.essentials.commands.IEssentialsCommand; import com.earth2me.essentials.signs.EssentialsSign; import com.earth2me.essentials.textreader.IText; +import java.math.BigDecimal; import java.text.MessageFormat; import java.util.List; import java.util.Map; @@ -30,9 +31,9 @@ public interface ISettings extends IConf int getChatRadius(); - double getCommandCost(IEssentialsCommand cmd); + BigDecimal getCommandCost(IEssentialsCommand cmd); - double getCommandCost(String label); + BigDecimal getCommandCost(String label); String getCurrencySymbol(); @@ -110,9 +111,9 @@ public interface ISettings extends IConf boolean warnOnSmite(); - double getMaxMoney(); + BigDecimal getMaxMoney(); - double getMinMoney(); + BigDecimal getMinMoney(); boolean isEcoLogEnabled(); diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index 00c4f328f..227cfa480 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -1,6 +1,7 @@ package com.earth2me.essentials; import com.earth2me.essentials.commands.IEssentialsCommand; +import java.math.BigDecimal; import java.util.Map; import java.util.Set; import org.bukkit.Location; @@ -23,13 +24,13 @@ public interface IUser extends Player Player getBase(); - double getMoney(); + BigDecimal getMoney(); - void takeMoney(double value); + void takeMoney(BigDecimal value); - void giveMoney(double value); + void giveMoney(BigDecimal value); - boolean canAfford(double value); + boolean canAfford(BigDecimal value); String getGroup(); diff --git a/Essentials/src/com/earth2me/essentials/Kit.java b/Essentials/src/com/earth2me/essentials/Kit.java index e018832d0..9305aa770 100644 --- a/Essentials/src/com/earth2me/essentials/Kit.java +++ b/Essentials/src/com/earth2me/essentials/Kit.java @@ -7,6 +7,7 @@ import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import com.earth2me.essentials.textreader.IText; import com.earth2me.essentials.textreader.KeywordReplacer; import com.earth2me.essentials.textreader.SimpleTextInput; +import java.math.BigDecimal; import java.util.*; import java.util.logging.Level; import org.bukkit.configuration.ConfigurationSection; @@ -31,8 +32,8 @@ public class Kit else if (user.isAuthorized("essentials.kits." + kitItem.toLowerCase(Locale.ENGLISH))) { String cost = ""; - Double costPrice = new Trade("kit-" + kitItem.toLowerCase(Locale.ENGLISH), ess).getCommandCost(user); - if (costPrice > 0d) + BigDecimal costPrice = new Trade("kit-" + kitItem.toLowerCase(Locale.ENGLISH), ess).getCommandCost(user); + if (costPrice.compareTo(BigDecimal.ZERO) > 0) { cost = _("kitCost", Util.displayCurrency(costPrice, ess)); } @@ -134,7 +135,7 @@ public class Kit { if (kitItem.startsWith(ess.getSettings().getCurrencySymbol())) { - Double value = Double.parseDouble(kitItem.substring(ess.getSettings().getCurrencySymbol().length()).trim()); + BigDecimal value = new BigDecimal(kitItem.substring(ess.getSettings().getCurrencySymbol().length()).trim()); Trade t = new Trade(value, ess); t.pay(user); continue; diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index 3327849c1..26aafb29c 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -7,6 +7,7 @@ import com.earth2me.essentials.signs.Signs; import com.earth2me.essentials.textreader.IText; import com.earth2me.essentials.textreader.SimpleTextInput; import java.io.File; +import java.math.BigDecimal; import java.text.MessageFormat; import java.util.*; import java.util.logging.Level; @@ -177,12 +178,12 @@ public class Settings implements ISettings private ConfigurationSection commandCosts; @Override - public double getCommandCost(IEssentialsCommand cmd) + public BigDecimal getCommandCost(IEssentialsCommand cmd) { return getCommandCost(cmd.getName()); } - public ConfigurationSection _getCommandCosts() + private ConfigurationSection _getCommandCosts() { if (config.isConfigurationSection("command-costs")) { @@ -228,18 +229,18 @@ public class Settings implements ISettings } @Override - public double getCommandCost(String name) + public BigDecimal getCommandCost(String name) { name = name.replace('.', '_').replace('/', '_'); if (commandCosts != null) { - return commandCosts.getDouble(name, 0.0); + return BigDecimal.valueOf(commandCosts.getDouble(name, 0.0)); } - return 0.0; + return BigDecimal.ZERO; } private Set<String> socialSpyCommands = new HashSet<String>(); - public Set<String> _getSocialSpyCommands() + private Set<String> _getSocialSpyCommands() { Set<String> socialspyCommands = new HashSet<String>(); @@ -289,7 +290,7 @@ public class Settings implements ISettings } private ConfigurationSection kits; - public ConfigurationSection _getKits() + private ConfigurationSection _getKits() { if (config.isConfigurationSection("kits")) { @@ -507,6 +508,8 @@ public class Settings implements ISettings socialSpyCommands = _getSocialSpyCommands(); warnOnBuildDisallow = _warnOnBuildDisallow(); mailsPerMinute = _getMailsPerMinute(); + maxMoney = _getMaxMoney(); + minMoney = _getMinMoney(); } private List<Integer> itemSpawnBl = new ArrayList<Integer>(); @@ -688,21 +691,27 @@ public class Settings implements ISettings return config.getBoolean(configName, def); } private final static double MAXMONEY = 10000000000000.0; + private BigDecimal maxMoney = BigDecimal.valueOf(MAXMONEY); - @Override - public double getMaxMoney() + private BigDecimal _getMaxMoney() { double max = config.getDouble("max-money", MAXMONEY); if (Math.abs(max) > MAXMONEY) { max = max < 0 ? -MAXMONEY : MAXMONEY; } - return max; + return BigDecimal.valueOf(max); + } + + @Override + public BigDecimal getMaxMoney() + { + return maxMoney; } private final static double MINMONEY = -10000000000000.0; + private BigDecimal minMoney = BigDecimal.valueOf(MINMONEY); - @Override - public double getMinMoney() + private BigDecimal _getMinMoney() { double min = config.getDouble("min-money", MINMONEY); if (min > 0) @@ -713,7 +722,13 @@ public class Settings implements ISettings { min = MINMONEY; } - return min; + return BigDecimal.valueOf(min); + } + + @Override + public BigDecimal getMinMoney() + { + return minMoney; } @Override @@ -974,7 +989,7 @@ public class Settings implements ISettings } private long teleportInvulnerabilityTime; - public long _getTeleportInvulnerability() + private long _getTeleportInvulnerability() { return config.getLong("teleport-invulnerability", 0) * 1000; } diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index 457691bbf..db53b0e7f 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -6,6 +6,7 @@ import com.earth2me.essentials.craftbukkit.SetExpFix; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.math.BigDecimal; import java.text.DateFormat; import java.util.Date; import java.util.Locale; @@ -21,7 +22,7 @@ public class Trade { private final transient String command; private final transient Trade fallbackTrade; - private final transient Double money; + private final transient BigDecimal money; private final transient ItemStack itemStack; private final transient Integer exp; private final transient IEssentials ess; @@ -43,10 +44,15 @@ public class Trade this(command, fallback, null, null, null, ess); } - public Trade(final double money, final IEssentials ess) + public Trade(final BigDecimal money, final IEssentials ess) { this(null, null, money, null, null, ess); } + + public Trade(final double money, final IEssentials ess) + { + this(null, null, BigDecimal.valueOf(money), null, null, ess); + } public Trade(final ItemStack items, final IEssentials ess) { @@ -58,7 +64,7 @@ public class Trade this(null, null, null, null, exp, ess); } - private Trade(final String command, final Trade fallback, final Double money, final ItemStack item, final Integer exp, final IEssentials ess) + private Trade(final String command, final Trade fallback, final BigDecimal money, final ItemStack item, final Integer exp, final IEssentials ess) { this.command = command; this.fallbackTrade = fallback; @@ -77,7 +83,7 @@ public class Trade } if (getMoney() != null - && getMoney() > 0 + && getMoney().compareTo(BigDecimal.ZERO) > 0 && !user.canAfford(getMoney())) { throw new ChargeException(_("notEnoughMoney")); @@ -89,9 +95,9 @@ public class Trade throw new ChargeException(_("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " "))); } - double money; + BigDecimal money; if (command != null && !command.isEmpty() - && 0 < (money = getCommandCost(user)) + && (money = getCommandCost(user)).compareTo(BigDecimal.ZERO) > 0 && !user.canAfford(money)) { throw new ChargeException(_("notEnoughMoney")); @@ -112,7 +118,7 @@ public class Trade public boolean pay(final IUser user, final boolean dropItems) { boolean success = true; - if (getMoney() != null && getMoney() > 0) + if (getMoney() != null && getMoney().compareTo(BigDecimal.ZERO) > 0) { user.giveMoney(getMoney()); } @@ -164,7 +170,7 @@ public class Trade if (getMoney() != null) { - if (!user.canAfford(getMoney()) && getMoney() > 0.0d) + if (!user.canAfford(getMoney()) && getMoney().compareTo(BigDecimal.ZERO) > 0) { throw new ChargeException(_("notEnoughMoney")); } @@ -181,8 +187,8 @@ public class Trade } if (command != null) { - final double cost = getCommandCost(user); - if (!user.canAfford(cost) && cost > 0.0d) + final BigDecimal cost = getCommandCost(user); + if (!user.canAfford(cost) && cost.compareTo(BigDecimal.ZERO) > 0) { throw new ChargeException(_("notEnoughMoney")); } @@ -199,7 +205,7 @@ public class Trade } } - public Double getMoney() + public BigDecimal getMoney() { return money; } @@ -228,13 +234,13 @@ public class Trade return TradeType.MONEY; } - public Double getCommandCost(final IUser user) + public BigDecimal getCommandCost(final IUser user) { - double cost = 0.0d; + BigDecimal cost = BigDecimal.ZERO; if (command != null && !command.isEmpty()) { cost = ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command); - if (cost == 0.0d && fallbackTrade != null) + if (cost.compareTo(BigDecimal.ZERO) == 0 && fallbackTrade != null) { cost = fallbackTrade.getCommandCost(user); } @@ -244,10 +250,10 @@ public class Trade ess.getLogger().log(Level.INFO, "calculated command (" + command + ") cost for " + user.getName() + " as " + cost); } } - if (cost != 0.0d && (user.isAuthorized("essentials.nocommandcost.all") + if (cost.compareTo(BigDecimal.ZERO) != 0 && (user.isAuthorized("essentials.nocommandcost.all") || user.isAuthorized("essentials.nocommandcost." + command))) { - return 0.0d; + return BigDecimal.ZERO; } return cost; } diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index ea66a63a0..68afe4a68 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -3,6 +3,7 @@ package com.earth2me.essentials; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.commands.IEssentialsCommand; import com.earth2me.essentials.register.payment.Method; +import java.math.BigDecimal; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.logging.Level; @@ -120,18 +121,18 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser } @Override - public void giveMoney(final double value) + public void giveMoney(final BigDecimal value) { giveMoney(value, null); } - public void giveMoney(final double value, final CommandSender initiator) + public void giveMoney(final BigDecimal value, final CommandSender initiator) { - if (value == 0.0d) + if (value.compareTo(BigDecimal.ZERO) == 0) { return; } - setMoney(getMoney() + value); + setMoney(getMoney().add(value)); sendMessage(_("addedToAccount", Util.displayCurrency(value, ess))); if (initiator != null) { @@ -139,16 +140,16 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser } } - public void payUser(final User reciever, final double value) throws Exception + public void payUser(final User reciever, final BigDecimal value) throws Exception { - if (value == 0.0d) + if (value.compareTo(BigDecimal.ZERO) == 0) { return; } if (canAfford(value)) { - setMoney(getMoney() - value); - reciever.setMoney(reciever.getMoney() + value); + setMoney(getMoney().subtract(value)); + reciever.setMoney(reciever.getMoney().add(value)); sendMessage(_("moneySentTo", Util.displayCurrency(value, ess), reciever.getDisplayName())); reciever.sendMessage(_("moneyRecievedFrom", Util.displayCurrency(value, ess), getDisplayName())); } @@ -159,18 +160,18 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser } @Override - public void takeMoney(final double value) + public void takeMoney(final BigDecimal value) { takeMoney(value, null); } - public void takeMoney(final double value, final CommandSender initiator) + public void takeMoney(final BigDecimal value, final CommandSender initiator) { - if (value == 0.0d) + if (value.compareTo(BigDecimal.ZERO) == 0) { return; } - setMoney(getMoney() - value); + setMoney(getMoney().subtract(value)); sendMessage(_("takenFromAccount", Util.displayCurrency(value, ess))); if (initiator != null) { @@ -179,23 +180,23 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser } @Override - public boolean canAfford(final double cost) + public boolean canAfford(final BigDecimal cost) { return canAfford(cost, true); } - public boolean canAfford(final double cost, final boolean permcheck) + public boolean canAfford(final BigDecimal cost, final boolean permcheck) { - if (cost <= 0.0d) + if (cost.compareTo(BigDecimal.ZERO) <= 0) { return true; } - final double mon = getMoney(); + final BigDecimal remainingBalance = getMoney().subtract(cost); if (!permcheck || isAuthorized("essentials.eco.loan")) { - return (mon - cost) >= ess.getSettings().getMinMoney(); + return (remainingBalance.compareTo(ess.getSettings().getMinMoney()) >= 0); } - return cost <= mon; + return (remainingBalance.compareTo(BigDecimal.ZERO) >= 0); } public void dispose() @@ -391,7 +392,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser } @Override - public double getMoney() + public BigDecimal getMoney() { if (ess.getPaymentMethod().hasMethod()) { @@ -403,7 +404,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser throw new Exception(); } final Method.MethodAccount account = ess.getPaymentMethod().getMethod().getAccount(this.getName()); - return account.balance(); + return BigDecimal.valueOf(account.balance()); } catch (Throwable ex) { @@ -413,7 +414,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser } @Override - public void setMoney(final double value) + public void setMoney(final BigDecimal value) { if (ess.getPaymentMethod().hasMethod()) { @@ -425,7 +426,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser throw new Exception(); } final Method.MethodAccount account = ess.getPaymentMethod().getMethod().getAccount(this.getName()); - account.set(value); + account.set(value.doubleValue()); } catch (Throwable ex) { @@ -435,7 +436,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser Trade.log("Update", "Set", "API", getName(), new Trade(value, ess), null, null, null, ess); } - public void updateMoneyCache(final double value) + public void updateMoneyCache(final BigDecimal value) { if (ess.getPaymentMethod().hasMethod() && super.getMoney() != value) { diff --git a/Essentials/src/com/earth2me/essentials/UserData.java b/Essentials/src/com/earth2me/essentials/UserData.java index 14f60d745..843ef8412 100644 --- a/Essentials/src/com/earth2me/essentials/UserData.java +++ b/Essentials/src/com/earth2me/essentials/UserData.java @@ -2,6 +2,7 @@ package com.earth2me.essentials; import static com.earth2me.essentials.I18n._; import java.io.File; +import java.math.BigDecimal; import java.util.*; import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; @@ -66,33 +67,47 @@ public abstract class UserData extends PlayerExtension implements IConf ignoredPlayers = _getIgnoredPlayers(); logoutLocation = _getLogoutLocation(); } - private double money; + private BigDecimal money; - private double _getMoney() + private BigDecimal _getMoney() { - double money = ess.getSettings().getStartingBalance(); + double bal = ess.getSettings().getStartingBalance(); + BigDecimal maxMoney = ess.getSettings().getMaxMoney(); + BigDecimal minMoney = ess.getSettings().getMinMoney(); + if (config.hasProperty("money")) { - money = config.getDouble("money", money); + bal = config.getDouble("money", bal); } - if (Math.abs(money) > ess.getSettings().getMaxMoney()) + BigDecimal result = BigDecimal.valueOf(bal); + if (result.compareTo(maxMoney) > 0) { - money = money < 0 ? -ess.getSettings().getMaxMoney() : ess.getSettings().getMaxMoney(); + result = maxMoney; } - return money; + if (result.compareTo(minMoney) < 0) + { + result = minMoney; + } + return result; } - public double getMoney() + public BigDecimal getMoney() { return money; } - public void setMoney(double value) + public void setMoney(BigDecimal value) { - money = Util.sanitizeMoney(value); - if (Math.abs(money) > ess.getSettings().getMaxMoney()) + money = value; + BigDecimal maxMoney = ess.getSettings().getMaxMoney(); + BigDecimal minMoney = ess.getSettings().getMinMoney(); + if (money.compareTo(maxMoney) > 0) + { + money = maxMoney; + } + if (money.compareTo(minMoney) < 0) { - money = money < 0 ? -ess.getSettings().getMaxMoney() : ess.getSettings().getMaxMoney(); + money = minMoney; } config.setProperty("money", money); config.save(); diff --git a/Essentials/src/com/earth2me/essentials/Util.java b/Essentials/src/com/earth2me/essentials/Util.java index 879816ce8..c7d8fcc2f 100644 --- a/Essentials/src/com/earth2me/essentials/Util.java +++ b/Essentials/src/com/earth2me/essentials/Util.java @@ -2,7 +2,6 @@ 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; @@ -524,18 +523,12 @@ 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) + public static String formatAsCurrency(final BigDecimal value) { - double fvalue = sanitizeMoney(value); dFormat.setRoundingMode(RoundingMode.FLOOR); - String str = dFormat.format(fvalue); + String str = dFormat.format(value); if (str.endsWith(".00")) { str = str.substring(0, str.length() - 3); @@ -543,15 +536,20 @@ public class Util return str; } - public static String displayCurrency(final double value, final IEssentials ess) + public static String displayCurrency(final BigDecimal value, final IEssentials ess) { return _("currency", ess.getSettings().getCurrencySymbol(), formatAsCurrency(value)); } - - public static String shortCurrency(final double value, final IEssentials ess) + + public static String shortCurrency(final BigDecimal value, final IEssentials ess) { return ess.getSettings().getCurrencySymbol() + formatAsCurrency(value); } + + public static String shortCurrency(final double value, final IEssentials ess) + { + return shortCurrency(BigDecimal.valueOf(value), ess); + } public static boolean isInt(final String sInt) { diff --git a/Essentials/src/com/earth2me/essentials/Worth.java b/Essentials/src/com/earth2me/essentials/Worth.java index b597bd3e8..ee894fe3c 100644 --- a/Essentials/src/com/earth2me/essentials/Worth.java +++ b/Essentials/src/com/earth2me/essentials/Worth.java @@ -1,6 +1,7 @@ package com.earth2me.essentials; import java.io.File; +import java.math.BigDecimal; import java.util.Locale; import java.util.logging.Logger; import org.bukkit.inventory.ItemStack; @@ -18,7 +19,7 @@ public class Worth implements IConf config.load(); } - public double getPrice(ItemStack itemStack) + public BigDecimal getPrice(ItemStack itemStack) { String itemname = itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""); double result; @@ -35,7 +36,11 @@ public class Worth implements IConf { result = config.getDouble("worth-" + itemStack.getTypeId(), Double.NaN); } - return result; + if (Double.isNaN(result)) + { + return null; + } + return BigDecimal.valueOf(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 c22145a13..a0830d8d3 100644 --- a/Essentials/src/com/earth2me/essentials/api/Economy.java +++ b/Essentials/src/com/earth2me/essentials/api/Economy.java @@ -6,6 +6,7 @@ import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; import java.io.File; +import java.math.BigDecimal; import java.util.logging.Level; import java.util.logging.Logger; @@ -87,7 +88,7 @@ public final class Economy { throw new UserDoesNotExistException(name); } - return user.getMoney(); + return user.getMoney().doubleValue(); } /** @@ -100,19 +101,20 @@ public final class Economy public static void setMoney(String name, double balance) throws UserDoesNotExistException, NoLoanPermittedException { User user = getUserByName(name); + BigDecimal newBalance = BigDecimal.valueOf(balance); if (user == null) { throw new UserDoesNotExistException(name); } - if (balance < ess.getSettings().getMinMoney()) + if (newBalance.compareTo(ess.getSettings().getMinMoney()) < 0) { throw new NoLoanPermittedException(); } - if (balance < 0.0 && !user.isAuthorized("essentials.eco.loan")) + if (newBalance.compareTo(BigDecimal.ZERO) < 0 && !user.isAuthorized("essentials.eco.loan")) { throw new NoLoanPermittedException(); } - user.setMoney(balance); + user.setMoney(newBalance); } /** @@ -238,7 +240,7 @@ public final class Economy { throw new RuntimeException(noCallBeforeLoad); } - return Util.displayCurrency(amount, ess); + return Util.displayCurrency(BigDecimal.valueOf(amount), ess); } /** diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandbalance.java b/Essentials/src/com/earth2me/essentials/commands/Commandbalance.java index 062709bd4..270b497ae 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandbalance.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandbalance.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; +import java.math.BigDecimal; import org.bukkit.Server; import org.bukkit.command.CommandSender; @@ -32,13 +33,13 @@ public class Commandbalance extends EssentialsCommand if (args.length < 1 || !user.isAuthorized("essentials.balance.others")) { - final double bal = user.getMoney(); + final BigDecimal bal = user.getMoney(); user.sendMessage(_("balance", Util.displayCurrency(bal, ess))); } else { final User target = getPlayer(server, args, 0, true, true); - final double bal = target.getMoney(); + final BigDecimal bal = target.getMoney(); user.sendMessage(_("balanceOther", target.getDisplayName(), Util.displayCurrency(bal, ess))); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java b/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java index b15071805..6ca916f11 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java @@ -5,6 +5,7 @@ import com.earth2me.essentials.User; import com.earth2me.essentials.Util; import com.earth2me.essentials.textreader.SimpleTextInput; import com.earth2me.essentials.textreader.TextPager; +import java.math.BigDecimal; import java.text.DateFormat; import java.util.*; import java.util.Map.Entry; @@ -106,33 +107,33 @@ public class Commandbalancetop extends EssentialsCommand if (force || cacheage <= System.currentTimeMillis() - CACHETIME) { cache.getLines().clear(); - final Map<String, Double> balances = new HashMap<String, Double>(); - double totalMoney = 0d; + final Map<String, BigDecimal> balances = new HashMap<String, BigDecimal>(); + BigDecimal totalMoney = BigDecimal.ZERO; for (String u : ess.getUserMap().getAllUniqueUsers()) { final User user = ess.getUserMap().getUser(u); if (user != null) { - final double userMoney = user.getMoney(); + final BigDecimal userMoney = user.getMoney(); user.updateMoneyCache(userMoney); - totalMoney += userMoney; + totalMoney = totalMoney.add(userMoney); balances.put(user.getDisplayName(), userMoney); } } - final List<Map.Entry<String, Double>> sortedEntries = new ArrayList<Map.Entry<String, Double>>(balances.entrySet()); - Collections.sort(sortedEntries, new Comparator<Map.Entry<String, Double>>() + final List<Map.Entry<String, BigDecimal>> sortedEntries = new ArrayList<Map.Entry<String, BigDecimal>>(balances.entrySet()); + Collections.sort(sortedEntries, new Comparator<Map.Entry<String, BigDecimal>>() { @Override - public int compare(final Entry<String, Double> entry1, final Entry<String, Double> entry2) + public int compare(final Entry<String, BigDecimal> entry1, final Entry<String, BigDecimal> entry2) { - return -entry1.getValue().compareTo(entry2.getValue()); + return entry2.getValue().compareTo(entry1.getValue()); } }); cache.getLines().add(_("serverTotal", Util.displayCurrency(totalMoney, ess))); int pos = 1; - for (Map.Entry<String, Double> entry : sortedEntries) + for (Map.Entry<String, BigDecimal> entry : sortedEntries) { cache.getLines().add(pos + ". " + entry.getKey() + ", " + Util.displayCurrency(entry.getValue(), ess)); pos++; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandeco.java b/Essentials/src/com/earth2me/essentials/commands/Commandeco.java index 5cad0e7be..c7e1c47a5 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandeco.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandeco.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; +import java.math.BigDecimal; import java.util.Locale; import org.bukkit.Server; import org.bukkit.command.CommandSender; @@ -25,14 +26,14 @@ public class Commandeco extends EssentialsCommand } Commandeco.EcoCommands cmd; - double startingBalance = (double)ess.getSettings().getStartingBalance(); - double amount; - Double broadcast = null; - Double broadcastAll = null; + BigDecimal startingBalance = new BigDecimal(ess.getSettings().getStartingBalance()); + BigDecimal amount; + BigDecimal broadcast = null; + BigDecimal broadcastAll = null; try { cmd = Commandeco.EcoCommands.valueOf(args[0].toUpperCase(Locale.ENGLISH)); - amount = (cmd == Commandeco.EcoCommands.RESET) ? startingBalance : Double.parseDouble(args[2].replaceAll("[^0-9\\.]", "")); + amount = (cmd == Commandeco.EcoCommands.RESET) ? startingBalance : new BigDecimal(args[2].replaceAll("[^0-9\\.]", "")); } catch (Exception ex) { @@ -107,26 +108,26 @@ public class Commandeco extends EssentialsCommand if (broadcast != null) { - server.broadcastMessage(_("resetBal", Util.formatAsCurrency(broadcast))); + server.broadcastMessage(_("resetBal", Util.displayCurrency(broadcast, ess))); } if (broadcastAll != null) { - server.broadcastMessage(_("resetBalAll", Util.formatAsCurrency(broadcastAll))); + server.broadcastMessage(_("resetBalAll", Util.displayCurrency(broadcastAll, ess))); } } - private void take(double amount, final User player, final CommandSender sender) throws Exception + private void take(BigDecimal amount, final User player, final CommandSender sender) throws Exception { - double money = player.getMoney(); - double minBalance = ess.getSettings().getMinMoney(); - if (money - amount > minBalance) + BigDecimal money = player.getMoney(); + BigDecimal minBalance = ess.getSettings().getMinMoney(); + if (money.subtract(amount).compareTo(minBalance) > 0) { player.takeMoney(amount, sender); } else if (sender == null) { player.setMoney(minBalance); - player.sendMessage(_("takenFromAccount", Util.displayCurrency(money - minBalance, ess))); + player.sendMessage(_("takenFromAccount", Util.displayCurrency(player.getMoney(), ess))); } else { @@ -134,10 +135,10 @@ public class Commandeco extends EssentialsCommand } } - private void set(double amount, final User player, final CommandSender sender) + private void set(BigDecimal amount, final User player, final CommandSender sender) { - double minBalance = ess.getSettings().getMinMoney(); - boolean underMinimum = amount < minBalance; + BigDecimal minBalance = ess.getSettings().getMinMoney(); + boolean underMinimum = (amount.compareTo(minBalance) < 0); player.setMoney(underMinimum ? minBalance : amount); player.sendMessage(_("setBal", Util.displayCurrency(player.getMoney(), ess))); if (sender != null) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java b/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java index 5bb7e6ac0..d900b276a 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java @@ -250,7 +250,7 @@ public class Commandessentials extends EssentialsCommand long timeDiff = System.currentTimeMillis() - lastLog; long milliDays = daysArg * 24L * 60L * 60L * 1000L; int homeCount = user.getHomes().size(); - double moneyCount = user.getMoney(); + double moneyCount = user.getMoney().doubleValue(); if ((lastLog == 0) || (ban > bansArg) || (timeDiff < milliDays) || (homeCount > homesArg) || (moneyCount > moneyArg)) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandpay.java b/Essentials/src/com/earth2me/essentials/commands/Commandpay.java index 60b9baa66..ae56b3cea 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandpay.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandpay.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; +import java.math.BigDecimal; import java.util.List; import org.bukkit.Server; import org.bukkit.entity.Player; @@ -29,7 +30,7 @@ public class Commandpay extends EssentialsCommand throw new NotEnoughArgumentsException("You need to specify a player to pay."); } - double amount = Double.parseDouble(args[1].replaceAll("[^0-9\\.]", "")); + BigDecimal amount = new BigDecimal(args[1].replaceAll("[^0-9\\.]", "")); boolean skipHidden = !user.isAuthorized("essentials.vanish.interact"); boolean foundUser = false; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java index a7b1ae32d..a2d18fd7e 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java @@ -4,6 +4,7 @@ import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; +import java.math.BigDecimal; import java.util.Locale; import java.util.logging.Level; import org.bukkit.Material; @@ -21,7 +22,7 @@ public class Commandsell extends EssentialsCommand @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - double totalWorth = 0.0; + BigDecimal totalWorth = BigDecimal.ZERO; String type = ""; if (args.length < 1) { @@ -42,13 +43,13 @@ public class Commandsell extends EssentialsCommand } try { - totalWorth += sellItem(user, stack, args, true); + totalWorth = totalWorth.add(sellItem(user, stack, args, true)); } catch (Exception e) { } } - if (totalWorth > 0) + if (totalWorth.compareTo(BigDecimal.ZERO) > 0) { user.sendMessage(_("totalWorthAll", type, Util.displayCurrency(totalWorth, ess))); } @@ -64,13 +65,13 @@ public class Commandsell extends EssentialsCommand } try { - totalWorth += sellItem(user, stack, args, true); + totalWorth = totalWorth.add(sellItem(user, stack, args, true)); } catch (Exception e) { } } - if (totalWorth > 0) + if (totalWorth.compareTo(BigDecimal.ZERO) > 0) { user.sendMessage(_("totalWorthBlocks", type, Util.displayCurrency(totalWorth, ess))); } @@ -83,7 +84,7 @@ public class Commandsell extends EssentialsCommand sellItem(user, is, args, false); } - private double sellItem(User user, ItemStack is, String[] args, boolean isBulkSell) throws Exception + private BigDecimal sellItem(User user, ItemStack is, String[] args, boolean isBulkSell) throws Exception { if (is == null || is.getType() == Material.AIR) { @@ -99,11 +100,11 @@ public class Commandsell extends EssentialsCommand amount = -amount; } } - double worth = ess.getWorth().getPrice(is); + BigDecimal worth = ess.getWorth().getPrice(is); boolean stack = args.length > 1 && args[1].endsWith("s"); boolean requireStack = ess.getSettings().isTradeInStacks(id); - if (Double.isNaN(worth)) + if (worth == null) { throw new Exception(_("itemCannotBeSold")); } @@ -146,10 +147,11 @@ public class Commandsell extends EssentialsCommand } else { - return worth * amount; + return worth.multiply(new BigDecimal(amount)); } } + BigDecimal result = worth.multiply(new BigDecimal(amount)); //TODO: Prices for Enchantments final ItemStack ris = is.clone(); ris.setAmount(amount); @@ -159,10 +161,10 @@ public class Commandsell extends EssentialsCommand } user.getInventory().removeItem(ris); user.updateInventory(); - Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(worth * amount, ess), user.getLocation(), ess); - user.giveMoney(worth * amount); - user.sendMessage(_("itemSold", Util.displayCurrency(worth * amount, ess), amount, is.getType().toString().toLowerCase(Locale.ENGLISH), Util.displayCurrency(worth, ess))); - logger.log(Level.INFO, _("itemSoldConsole", user.getDisplayName(), is.getType().toString().toLowerCase(Locale.ENGLISH), Util.displayCurrency(worth * amount, ess), amount, Util.displayCurrency(worth, ess))); - return worth * amount; + Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(result, ess), user.getLocation(), ess); + user.giveMoney(result); + user.sendMessage(_("itemSold", Util.displayCurrency(result, ess), amount, is.getType().toString().toLowerCase(Locale.ENGLISH), Util.displayCurrency(worth, ess))); + logger.log(Level.INFO, _("itemSoldConsole", user.getDisplayName(), is.getType().toString().toLowerCase(Locale.ENGLISH), Util.displayCurrency(result, ess), amount, Util.displayCurrency(worth, ess))); + return result; } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java b/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java index dab972b26..509b2cd08 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java @@ -5,6 +5,7 @@ import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; import com.earth2me.essentials.Warps; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -110,7 +111,7 @@ public class Commandwarp extends EssentialsCommand { final Trade chargeWarp = new Trade("warp-" + name.toLowerCase(Locale.ENGLISH).replace('_', '-'), ess); final Trade chargeCmd = new Trade(this.getName(), ess); - final double fullCharge = chargeWarp.getCommandCost(user) + chargeCmd.getCommandCost(user); + final BigDecimal fullCharge = chargeWarp.getCommandCost(user).add(chargeCmd.getCommandCost(user)); final Trade charge = new Trade(fullCharge, ess); charge.isAffordableFor(owner); if (ess.getSettings().getPerWarpPermission() && !owner.isAuthorized("essentials.warps." + name)) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandworth.java b/Essentials/src/com/earth2me/essentials/commands/Commandworth.java index c8573ba25..72d08d4fa 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandworth.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandworth.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; +import java.math.BigDecimal; import java.util.Locale; import org.bukkit.Server; import org.bukkit.command.CommandSender; @@ -41,22 +42,24 @@ public class Commandworth extends EssentialsCommand } iStack.setAmount(amount); - final double worth = ess.getWorth().getPrice(iStack); - if (Double.isNaN(worth)) + final BigDecimal worth = ess.getWorth().getPrice(iStack); + if (worth == null) { throw new Exception(_("itemCannotBeSold")); } + + final BigDecimal result = worth.multiply(new BigDecimal(amount)); user.sendMessage(iStack.getDurability() != 0 ? _("worthMeta", iStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""), iStack.getDurability(), - Util.displayCurrency(worth * amount, ess), + Util.displayCurrency(result, ess), amount, Util.displayCurrency(worth, ess)) : _("worth", iStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""), - Util.displayCurrency(worth * amount, ess), + Util.displayCurrency(result, ess), amount, Util.displayCurrency(worth, ess))); } @@ -85,24 +88,25 @@ public class Commandworth extends EssentialsCommand } iStack.setAmount(amount); - final double worth = ess.getWorth().getPrice(iStack); - if (Double.isNaN(worth)) + final BigDecimal worth = ess.getWorth().getPrice(iStack); + if (worth == null) { throw new Exception(_("itemCannotBeSold")); } + + final BigDecimal result = worth.multiply(new BigDecimal(amount)); sender.sendMessage(iStack.getDurability() != 0 ? _("worthMeta", iStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""), iStack.getDurability(), - Util.displayCurrency(worth * amount, ess), + Util.displayCurrency(result, ess), amount, Util.displayCurrency(worth, ess)) : _("worth", iStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""), - Util.displayCurrency(worth * amount, ess), + Util.displayCurrency(result, ess), amount, Util.displayCurrency(worth, ess))); - } } diff --git a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java index 3e3e095c5..e02df26a7 100644 --- a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java +++ b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java @@ -2,6 +2,7 @@ package com.earth2me.essentials.signs; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.*; +import java.math.BigDecimal; import java.util.HashSet; import java.util.Locale; import java.util.Set; @@ -277,7 +278,7 @@ public class EssentialsSign return; } final Trade trade = getTrade(sign, index, 0, ess); - final Double money = trade.getMoney(); + final BigDecimal money = trade.getMoney(); if (money != null) { sign.setLine(index, Util.shortCurrency(money, ess)); diff --git a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java index 58d62e5cc..5c3195d46 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.signs; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.Trade.TradeType; import com.earth2me.essentials.*; +import java.math.BigDecimal; import org.bukkit.inventory.ItemStack; //TODO: TL exceptions @@ -265,10 +266,10 @@ public class SignTrade extends EssentialsSign protected final void subtractAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess) throws SignException { - final Double money = trade.getMoney(); + final BigDecimal money = trade.getMoney(); if (money != null) { - changeAmount(sign, index, -money, ess); + changeAmount(sign, index, -money.doubleValue(), ess); } final ItemStack item = trade.getItemStack(); if (item != null) @@ -284,10 +285,10 @@ public class SignTrade extends EssentialsSign protected final void addAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess) throws SignException { - final Double money = trade.getMoney(); + final BigDecimal money = trade.getMoney(); if (money != null) { - changeAmount(sign, index, money, ess); + changeAmount(sign, index, money.doubleValue(), ess); } final ItemStack item = trade.getItemStack(); if (item != null) diff --git a/Essentials/test/com/earth2me/essentials/UserTest.java b/Essentials/test/com/earth2me/essentials/UserTest.java index 5b096431f..e7f2d1f48 100644 --- a/Essentials/test/com/earth2me/essentials/UserTest.java +++ b/Essentials/test/com/earth2me/essentials/UserTest.java @@ -1,6 +1,7 @@ package com.earth2me.essentials; import java.io.IOException; +import java.math.BigDecimal; import junit.framework.TestCase; import org.bukkit.Location; import org.bukkit.World.Environment; @@ -69,13 +70,13 @@ public class UserTest extends TestCase { should("properly set, take, give, and get money"); User user = ess.getUser(base1); - double i; - user.setMoney(i = 100.5); - user.takeMoney(50); + double i = 100.5; + user.setMoney(BigDecimal.valueOf(i)); + user.takeMoney(new BigDecimal(50)); i -= 50; - user.giveMoney(25); + user.giveMoney(new BigDecimal(25)); i += 25; - assertEquals(user.getMoney(), i); + assertEquals(user.getMoney().doubleValue(), i); } public void testGetGroup() |