diff options
author | KHobbits <rob@khobbits.co.uk> | 2013-05-05 08:03:06 +0100 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2013-05-05 08:03:06 +0100 |
commit | c4ac744d35a33961b7169b4f65cdbb6a013947bc (patch) | |
tree | 0734d7da144e2d6209adeff55001c87390e6f325 | |
parent | 162b67aaa66a9aef2c41ec63aad7f71a1b8730a4 (diff) | |
download | Essentials-c4ac744d35a33961b7169b4f65cdbb6a013947bc.tar Essentials-c4ac744d35a33961b7169b4f65cdbb6a013947bc.tar.gz Essentials-c4ac744d35a33961b7169b4f65cdbb6a013947bc.tar.lz Essentials-c4ac744d35a33961b7169b4f65cdbb6a013947bc.tar.xz Essentials-c4ac744d35a33961b7169b4f65cdbb6a013947bc.zip |
Further economy cleanup
4 files changed, 27 insertions, 35 deletions
diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index db53b0e7f..0c3e4a3c5 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -26,14 +26,15 @@ public class Trade private final transient ItemStack itemStack; private final transient Integer exp; private final transient IEssentials ess; - + + public enum TradeType { MONEY, EXP, ITEM } - + public Trade(final String command, final IEssentials ess) { this(command, null, null, null, null, ess); @@ -48,11 +49,6 @@ public class Trade { 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) { @@ -219,10 +215,11 @@ public class Trade { return exp; } - + public TradeType getType() { - if (getExperience() != null) { + if (getExperience() != null) + { return TradeType.EXP; } @@ -231,7 +228,7 @@ public class Trade return TradeType.ITEM; } - return TradeType.MONEY; + return TradeType.MONEY; } public BigDecimal getCommandCost(final IUser user) @@ -251,7 +248,7 @@ public class Trade } } if (cost.compareTo(BigDecimal.ZERO) != 0 && (user.isAuthorized("essentials.nocommandcost.all") - || user.isAuthorized("essentials.nocommandcost." + command))) + || user.isAuthorized("essentials.nocommandcost." + command))) { return BigDecimal.ZERO; } @@ -263,7 +260,7 @@ public class Trade { //isEcoLogUpdateEnabled() - This refers to log entries with no location, ie API updates #EasterEgg //isEcoLogEnabled() - This refers to log entries with with location, ie /pay /sell and eco signs. - + if ((loc == null && !ess.getSettings().isEcoLogUpdateEnabled()) || (loc != null && !ess.getSettings().isEcoLogEnabled())) { diff --git a/Essentials/src/com/earth2me/essentials/Util.java b/Essentials/src/com/earth2me/essentials/Util.java index c7d8fcc2f..fc67fe21b 100644 --- a/Essentials/src/com/earth2me/essentials/Util.java +++ b/Essentials/src/com/earth2me/essentials/Util.java @@ -522,7 +522,6 @@ public class Util } return is; } - private static DecimalFormat dFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US)); public static String formatAsCurrency(final BigDecimal value) @@ -540,16 +539,11 @@ public class Util { return _("currency", ess.getSettings().getCurrencySymbol(), formatAsCurrency(value)); } - + 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/signs/EssentialsSign.java b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java index e02df26a7..bea17b808 100644 --- a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java +++ b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java @@ -368,10 +368,10 @@ public class EssentialsSign } } - protected final Double getMoney(final String line) throws SignException + protected final BigDecimal getMoney(final String line) throws SignException { final boolean isMoney = line.matches("^[^0-9-\\.][\\.0-9]+$"); - return isMoney ? getDoublePositive(line.substring(1)) : null; + return isMoney ? BigDecimal.valueOf(getDoublePositive(line.substring(1))) : null; } protected final Double getDoublePositive(final String line) throws SignException @@ -409,7 +409,7 @@ public class EssentialsSign return new Trade(signName.toLowerCase(Locale.ENGLISH) + "sign", ess); } - final Double money = getMoney(line); + final BigDecimal money = getMoney(line); if (money == null) { final String[] split = line.split("[ :]+", 2); diff --git a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java index 5c3195d46..651e571b2 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java @@ -8,12 +8,12 @@ import org.bukkit.inventory.ItemStack; //TODO: TL exceptions public class SignTrade extends EssentialsSign -{ - +{ public SignTrade() { 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 @@ -22,7 +22,8 @@ public class SignTrade extends EssentialsSign validateTrade(sign, 2, true, ess); final Trade trade = getTrade(sign, 2, true, true, ess); final Trade charge = getTrade(sign, 1, true, false, ess); - if (trade.getType() == charge.getType() && (trade.getType() != TradeType.ITEM || trade.getItemStack().getType().equals(charge.getItemStack().getType()))) { + if (trade.getType() == charge.getType() && (trade.getType() != TradeType.ITEM || trade.getItemStack().getType().equals(charge.getItemStack().getType()))) + { throw new SignException("You cannot trade for the same item type."); } trade.isAffordableFor(player); @@ -138,7 +139,7 @@ public class SignTrade extends EssentialsSign if (split.length == 1 && !amountNeeded) { - final Double money = getMoney(split[0]); + final BigDecimal money = getMoney(split[0]); if (money != null) { if (Util.shortCurrency(money, ess).length() * 2 > 15) @@ -152,12 +153,12 @@ public class SignTrade extends EssentialsSign if (split.length == 2 && amountNeeded) { - final Double money = getMoney(split[0]); - Double amount = getDoublePositive(split[1]); + final BigDecimal money = getMoney(split[0]); + BigDecimal amount = BigDecimal.valueOf(getDoublePositive(split[1])); if (money != null && amount != null) { - amount -= amount % money; - if (amount < 0.01 || money < 0.01) + amount = amount.subtract(amount.remainder(money)); + if (amount.compareTo(MINTRANSACTION) < 0 || money.compareTo(MINTRANSACTION) < 0) { throw new SignException(_("moreThanZero")); } @@ -221,8 +222,8 @@ public class SignTrade extends EssentialsSign { try { - final Double money = getMoney(split[0]); - final Double amount = notEmpty ? getDoublePositive(split[1]) : getDouble(split[1]); + final BigDecimal money = getMoney(split[0]); + final BigDecimal amount = BigDecimal.valueOf(notEmpty ? getDoublePositive(split[1]) : getDouble(split[1])); if (money != null && amount != null) { return new Trade(fullAmount ? amount : money, ess); @@ -315,11 +316,11 @@ public class SignTrade extends EssentialsSign if (split.length == 2) { - final Double money = getMoney(split[0]); - final Double amount = getDouble(split[1]); + final BigDecimal money = getMoney(split[0]); + final BigDecimal amount = BigDecimal.valueOf(getDouble(split[1])); if (money != null && amount != null) { - final String newline = Util.shortCurrency(money, ess) + ":" + Util.shortCurrency(amount + value, ess).substring(1); + final String newline = Util.shortCurrency(money, ess) + ":" + Util.shortCurrency(amount.add(BigDecimal.valueOf(value)), ess).substring(1); if (newline.length() > 15) { throw new SignException("This sign is full: Line too long!"); |