1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-22 14:23:09 +02:00

Further economy cleanup

This commit is contained in:
KHobbits
2013-05-05 08:03:06 +01:00
parent e3e184920b
commit 5c62ede70b
4 changed files with 27 additions and 35 deletions

View File

@@ -26,14 +26,15 @@ public class Trade
private final transient ItemStack itemStack; private final transient ItemStack itemStack;
private final transient Integer exp; private final transient Integer exp;
private final transient IEssentials ess; private final transient IEssentials ess;
public enum TradeType public enum TradeType
{ {
MONEY, MONEY,
EXP, EXP,
ITEM ITEM
} }
public Trade(final String command, final IEssentials ess) public Trade(final String command, final IEssentials ess)
{ {
this(command, null, null, null, null, ess); this(command, null, null, null, null, ess);
@@ -48,11 +49,6 @@ public class Trade
{ {
this(null, null, money, null, null, 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) public Trade(final ItemStack items, final IEssentials ess)
{ {
@@ -219,10 +215,11 @@ public class Trade
{ {
return exp; return exp;
} }
public TradeType getType() public TradeType getType()
{ {
if (getExperience() != null) { if (getExperience() != null)
{
return TradeType.EXP; return TradeType.EXP;
} }
@@ -231,7 +228,7 @@ public class Trade
return TradeType.ITEM; return TradeType.ITEM;
} }
return TradeType.MONEY; return TradeType.MONEY;
} }
public BigDecimal getCommandCost(final IUser user) public BigDecimal getCommandCost(final IUser user)
@@ -251,7 +248,7 @@ public class Trade
} }
} }
if (cost.compareTo(BigDecimal.ZERO) != 0 && (user.isAuthorized("essentials.nocommandcost.all") if (cost.compareTo(BigDecimal.ZERO) != 0 && (user.isAuthorized("essentials.nocommandcost.all")
|| user.isAuthorized("essentials.nocommandcost." + command))) || user.isAuthorized("essentials.nocommandcost." + command)))
{ {
return BigDecimal.ZERO; return BigDecimal.ZERO;
} }
@@ -263,7 +260,7 @@ public class Trade
{ {
//isEcoLogUpdateEnabled() - This refers to log entries with no location, ie API updates #EasterEgg //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. //isEcoLogEnabled() - This refers to log entries with with location, ie /pay /sell and eco signs.
if ((loc == null && !ess.getSettings().isEcoLogUpdateEnabled()) if ((loc == null && !ess.getSettings().isEcoLogUpdateEnabled())
|| (loc != null && !ess.getSettings().isEcoLogEnabled())) || (loc != null && !ess.getSettings().isEcoLogEnabled()))
{ {

View File

@@ -522,7 +522,6 @@ public class Util
} }
return is; return is;
} }
private static DecimalFormat dFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US)); private static DecimalFormat dFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US));
public static String formatAsCurrency(final BigDecimal value) public static String formatAsCurrency(final BigDecimal value)
@@ -540,16 +539,11 @@ public class Util
{ {
return _("currency", ess.getSettings().getCurrencySymbol(), formatAsCurrency(value)); return _("currency", ess.getSettings().getCurrencySymbol(), formatAsCurrency(value));
} }
public static String shortCurrency(final BigDecimal value, final IEssentials ess) public static String shortCurrency(final BigDecimal value, final IEssentials ess)
{ {
return ess.getSettings().getCurrencySymbol() + formatAsCurrency(value); 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) public static boolean isInt(final String sInt)
{ {

View File

@@ -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]+$"); 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 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); return new Trade(signName.toLowerCase(Locale.ENGLISH) + "sign", ess);
} }
final Double money = getMoney(line); final BigDecimal money = getMoney(line);
if (money == null) if (money == null)
{ {
final String[] split = line.split("[ :]+", 2); final String[] split = line.split("[ :]+", 2);

View File

@@ -8,12 +8,12 @@ import org.bukkit.inventory.ItemStack;
//TODO: TL exceptions //TODO: TL exceptions
public class SignTrade extends EssentialsSign public class SignTrade extends EssentialsSign
{ {
public SignTrade() public SignTrade()
{ {
super("Trade"); super("Trade");
} }
static final BigDecimal MINTRANSACTION = BigDecimal.valueOf(0.01);
@Override @Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException 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); validateTrade(sign, 2, true, ess);
final Trade trade = getTrade(sign, 2, true, true, ess); final Trade trade = getTrade(sign, 2, true, true, ess);
final Trade charge = getTrade(sign, 1, true, false, 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."); throw new SignException("You cannot trade for the same item type.");
} }
trade.isAffordableFor(player); trade.isAffordableFor(player);
@@ -138,7 +139,7 @@ public class SignTrade extends EssentialsSign
if (split.length == 1 && !amountNeeded) if (split.length == 1 && !amountNeeded)
{ {
final Double money = getMoney(split[0]); final BigDecimal money = getMoney(split[0]);
if (money != null) if (money != null)
{ {
if (Util.shortCurrency(money, ess).length() * 2 > 15) if (Util.shortCurrency(money, ess).length() * 2 > 15)
@@ -152,12 +153,12 @@ public class SignTrade extends EssentialsSign
if (split.length == 2 && amountNeeded) if (split.length == 2 && amountNeeded)
{ {
final Double money = getMoney(split[0]); final BigDecimal money = getMoney(split[0]);
Double amount = getDoublePositive(split[1]); BigDecimal amount = BigDecimal.valueOf(getDoublePositive(split[1]));
if (money != null && amount != null) if (money != null && amount != null)
{ {
amount -= amount % money; amount = amount.subtract(amount.remainder(money));
if (amount < 0.01 || money < 0.01) if (amount.compareTo(MINTRANSACTION) < 0 || money.compareTo(MINTRANSACTION) < 0)
{ {
throw new SignException(_("moreThanZero")); throw new SignException(_("moreThanZero"));
} }
@@ -221,8 +222,8 @@ public class SignTrade extends EssentialsSign
{ {
try try
{ {
final Double money = getMoney(split[0]); final BigDecimal money = getMoney(split[0]);
final Double amount = notEmpty ? getDoublePositive(split[1]) : getDouble(split[1]); final BigDecimal amount = BigDecimal.valueOf(notEmpty ? getDoublePositive(split[1]) : getDouble(split[1]));
if (money != null && amount != null) if (money != null && amount != null)
{ {
return new Trade(fullAmount ? amount : money, ess); return new Trade(fullAmount ? amount : money, ess);
@@ -315,11 +316,11 @@ public class SignTrade extends EssentialsSign
if (split.length == 2) if (split.length == 2)
{ {
final Double money = getMoney(split[0]); final BigDecimal money = getMoney(split[0]);
final Double amount = getDouble(split[1]); final BigDecimal amount = BigDecimal.valueOf(getDouble(split[1]));
if (money != null && amount != null) 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) if (newline.length() > 15)
{ {
throw new SignException("This sign is full: Line too long!"); throw new SignException("This sign is full: Line too long!");