Fix Trade signs

This commit is contained in:
snowleo 2011-07-01 01:33:09 +02:00
parent aef2ca3e5d
commit e596f85953
2 changed files with 24 additions and 19 deletions

View File

@ -290,7 +290,7 @@ public class EssentialsSign
protected final int getIntegerPositive(final String line) throws SignException protected final int getIntegerPositive(final String line) throws SignException
{ {
final int quantity = getInteger(line); final int quantity = getInteger(line);
if (quantity <= 1) if (quantity < 1)
{ {
throw new SignException(Util.i18n("moreThanZero")); throw new SignException(Util.i18n("moreThanZero"));
} }
@ -328,19 +328,24 @@ public class EssentialsSign
protected final Double getMoney(final String line) throws SignException protected final Double 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 ? getDouble(line.substring(1)) : null; return isMoney ? getDoublePositive(line.substring(1)) : null;
}
protected final Double getDoublePositive(final String line) throws SignException
{
final double quantity = getDouble(line);
if (quantity <= 0.0)
{
throw new SignException(Util.i18n("moreThanZero"));
}
return quantity;
} }
protected final Double getDouble(final String line) throws SignException protected final Double getDouble(final String line) throws SignException
{ {
try try
{ {
final double quantity = Double.parseDouble(line); return Double.parseDouble(line);
if (quantity <= 0.0)
{
throw new SignException(Util.i18n("moreThanZero"));
}
return quantity;
} }
catch (NumberFormatException ex) catch (NumberFormatException ex)
{ {

View File

@ -20,7 +20,7 @@ public class SignTrade extends EssentialsSign
{ {
validateTrade(sign, 1, false, ess); validateTrade(sign, 1, false, ess);
validateTrade(sign, 2, true, ess); validateTrade(sign, 2, true, ess);
final Trade charge = getTrade(sign, 2, true, ess); final Trade charge = getTrade(sign, 2, true, true, ess);
charge.isAffordableFor(player); charge.isAffordableFor(player);
sign.setLine(3, "§8" + username); sign.setLine(3, "§8" + username);
charge.charge(player); charge.charge(player);
@ -32,14 +32,14 @@ public class SignTrade extends EssentialsSign
{ {
if (sign.getLine(3).substring(2).equalsIgnoreCase(username)) if (sign.getLine(3).substring(2).equalsIgnoreCase(username))
{ {
final Trade stored = getTrade(sign, 1, true, ess); final Trade stored = getTrade(sign, 1, true, true, ess);
substractAmount(sign, 1, stored); substractAmount(sign, 1, stored);
stored.pay(player); stored.pay(player);
} }
else else
{ {
final Trade charge = getTrade(sign, 1, false, ess); final Trade charge = getTrade(sign, 1, false, false, ess);
final Trade trade = getTrade(sign, 2, false, ess); final Trade trade = getTrade(sign, 2, false, true, ess);
charge.isAffordableFor(player); charge.isAffordableFor(player);
substractAmount(sign, 2, trade); substractAmount(sign, 2, trade);
trade.pay(player); trade.pay(player);
@ -54,8 +54,8 @@ public class SignTrade extends EssentialsSign
{ {
if (sign.getLine(3).length() > 3 && sign.getLine(3).substring(2).equalsIgnoreCase(username)) if (sign.getLine(3).length() > 3 && sign.getLine(3).substring(2).equalsIgnoreCase(username))
{ {
final Trade stored1 = getTrade(sign, 1, true, ess); final Trade stored1 = getTrade(sign, 1, true, false, ess);
final Trade stored2 = getTrade(sign, 2, true, ess); final Trade stored2 = getTrade(sign, 2, true, false, ess);
stored1.pay(player); stored1.pay(player);
stored2.pay(player); stored2.pay(player);
return true; return true;
@ -88,7 +88,7 @@ public class SignTrade extends EssentialsSign
if (split.length == 2 && amountNeeded) if (split.length == 2 && amountNeeded)
{ {
final Double money = getMoney(split[0]); final Double money = getMoney(split[0]);
final Double amount = getDouble(split[1]); final Double amount = getDoublePositive(split[1]);
if (money != null && amount != null) if (money != null && amount != null)
{ {
sign.setLine(index, Util.formatCurrency(money) + ":" + Util.formatCurrency(amount).substring(1)); sign.setLine(index, Util.formatCurrency(money) + ":" + Util.formatCurrency(amount).substring(1));
@ -124,7 +124,7 @@ public class SignTrade extends EssentialsSign
throw new SignException(Util.format("invalidSignLine", index)); throw new SignException(Util.format("invalidSignLine", index));
} }
protected final Trade getTrade(final ISign sign, final int index, final boolean fullAmount, final IEssentials ess) throws SignException protected final Trade getTrade(final ISign sign, final int index, final boolean fullAmount, final boolean notEmpty, final IEssentials ess) throws SignException
{ {
final String line = sign.getLine(index).trim(); final String line = sign.getLine(index).trim();
if (line.isEmpty()) if (line.isEmpty())
@ -136,7 +136,7 @@ public class SignTrade extends EssentialsSign
if (split.length == 2) if (split.length == 2)
{ {
final Double money = getMoney(split[0]); final Double money = getMoney(split[0]);
final Double amount = getDouble(split[1]); final Double amount = 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);
@ -147,9 +147,9 @@ public class SignTrade extends EssentialsSign
{ {
final int stackamount = getIntegerPositive(split[0]); final int stackamount = getIntegerPositive(split[0]);
final ItemStack item = getItemStack(split[1], stackamount); final ItemStack item = getItemStack(split[1], stackamount);
int amount = getIntegerPositive(split[2]); int amount = getInteger(split[2]);
amount -= amount % stackamount; amount -= amount % stackamount;
if (amount < 1 || stackamount < 1 || item.getTypeId() == 0) if (notEmpty && (amount < 1 || stackamount < 1 || item.getTypeId() == 0))
{ {
throw new SignException(Util.i18n("moreThanZero")); throw new SignException(Util.i18n("moreThanZero"));
} }