Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.math.BigDecimal;
import java.util.Map;

//TODO: TL exceptions
public class SignTrade extends EssentialsSign {
private static final int MAX_STOCK_LINE_LENGTH = 15;

Expand Down Expand Up @@ -47,14 +46,14 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri
final Trade stored;
try {
stored = getTrade(sign, 1, AmountType.TOTAL, true, true, ess);
subtractAmount(sign, 1, stored, ess);
subtractAmount(sign, 1, stored, ess, false);

final Map<Integer, ItemStack> withdraw = stored.pay(player, OverflowType.RETURN);

if (withdraw == null) {
Trade.log("Sign", "Trade", "Withdraw", username, store, username, null, sign.getBlock().getLocation(), player.getMoney(), ess);
} else {
setAmount(sign, 1, BigDecimal.valueOf(withdraw.get(0).getAmount()), ess);
setAmount(sign, 1, BigDecimal.valueOf(withdraw.get(0).getAmount()), ess, false);
Trade.log("Sign", "Trade", "Withdraw", username, stored, username, new Trade(withdraw.get(0), ess), sign.getBlock().getLocation(), player.getMoney(), ess);
}
} catch (final SignException e) {
Expand All @@ -67,11 +66,16 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri
final Trade charge = getTrade(sign, 1, AmountType.COST, false, true, ess);
final Trade trade = getTrade(sign, 2, AmountType.COST, true, true, ess);
charge.isAffordableFor(player);
addAmount(sign, 1, charge, ess);
subtractAmount(sign, 2, trade, ess);

// validate addAmount + subtractAmount first to ensure they both do not throw exceptions
addAmount(sign, 1, charge, ess, true);
subtractAmount(sign, 2, trade, ess, true);

addAmount(sign, 1, charge, ess, false);
subtractAmount(sign, 2, trade, ess, false);
if (!trade.pay(player)) {
subtractAmount(sign, 1, charge, ess);
addAmount(sign, 2, trade, ess);
subtractAmount(sign, 1, charge, ess, false);
addAmount(sign, 2, trade, ess, false);
throw new ChargeException("inventoryFull");
}
charge.charge(player);
Expand All @@ -93,7 +97,7 @@ private Trade rechargeSign(final ISign sign, final IEssentials ess, final User p
stack = stack.clone();
stack.setAmount(amount);
final Trade store = new Trade(stack, ess);
addAmount(sign, 2, store, ess);
addAmount(sign, 2, store, ess, false);
store.charge(player);
return store;
}
Expand Down Expand Up @@ -127,10 +131,10 @@ protected boolean onSignBreak(final ISign sign, final User player, final String
return true;
}

setAmount(sign, 1, BigDecimal.valueOf(withdraw1 == null ? 0L : withdraw1.get(0).getAmount()), ess);
setAmount(sign, 1, BigDecimal.valueOf(withdraw1 == null ? 0L : withdraw1.get(0).getAmount()), ess, false);
Trade.log("Sign", "Trade", "Withdraw", signOwner.substring(2), stored1, username, withdraw1 == null ? null : new Trade(withdraw1.get(0), ess), sign.getBlock().getLocation(), player.getMoney(), ess);

setAmount(sign, 2, BigDecimal.valueOf(withdraw2 == null ? 0L : withdraw2.get(0).getAmount()), ess);
setAmount(sign, 2, BigDecimal.valueOf(withdraw2 == null ? 0L : withdraw2.get(0).getAmount()), ess, false);
Trade.log("Sign", "Trade", "Withdraw", signOwner.substring(2), stored2, username, withdraw2 == null ? null : new Trade(withdraw2.get(0), ess), sign.getBlock().getLocation(), player.getMoney(), ess);

sign.updateSign();
Expand Down Expand Up @@ -267,38 +271,37 @@ protected final Trade getTrade(final ISign sign, final int index, final AmountTy
throw new SignException("invalidSignLine", index + 1);
}

protected final void subtractAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess) throws SignException {
protected final void subtractAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess, final boolean validationRun) throws SignException {
final BigDecimal money = trade.getMoney();
if (money != null) {
changeAmount(sign, index, money.negate(), ess);
changeAmount(sign, index, money.negate(), ess, validationRun);
}
final ItemStack item = trade.getItemStack();
if (item != null) {
changeAmount(sign, index, BigDecimal.valueOf(-item.getAmount()), ess);
changeAmount(sign, index, BigDecimal.valueOf(-item.getAmount()), ess, validationRun);
}
final Integer exp = trade.getExperience();
if (exp != null) {
changeAmount(sign, index, BigDecimal.valueOf(-exp), ess);
changeAmount(sign, index, BigDecimal.valueOf(-exp), ess, validationRun);
}
}

protected final void addAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess) throws SignException {
protected final void addAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess, final boolean validationRun) throws SignException {
final BigDecimal money = trade.getMoney();
if (money != null) {
changeAmount(sign, index, money, ess);
changeAmount(sign, index, money, ess, validationRun);
}
final ItemStack item = trade.getItemStack();
if (item != null) {
changeAmount(sign, index, BigDecimal.valueOf(item.getAmount()), ess);
changeAmount(sign, index, BigDecimal.valueOf(item.getAmount()), ess, validationRun);
}
final Integer exp = trade.getExperience();
if (exp != null) {
changeAmount(sign, index, BigDecimal.valueOf(exp), ess);
changeAmount(sign, index, BigDecimal.valueOf(exp), ess, validationRun);
}
}

//TODO: Translate these exceptions.
private void changeAmount(final ISign sign, final int index, final BigDecimal value, final IEssentials ess) throws SignException {
private void changeAmount(final ISign sign, final int index, final BigDecimal value, final IEssentials ess, final boolean validationRun) throws SignException {
final String line = sign.getLine(index).trim();
if (line.isEmpty()) {
throw new SignException("emptySignLine", index + 1);
Expand All @@ -307,20 +310,18 @@ private void changeAmount(final ISign sign, final int index, final BigDecimal va

if (split.length == 2) {
final BigDecimal amount = getBigDecimal(split[1], ess).add(value);
setAmount(sign, index, amount, ess);
setAmount(sign, index, amount, ess, validationRun);
return;
}
if (split.length == 3) {
final BigDecimal amount = getBigDecimal(split[2], ess).add(value);
setAmount(sign, index, amount, ess);
setAmount(sign, index, amount, ess, validationRun);
return;
}
throw new SignException("invalidSignLine", index + 1);
}

//TODO: Translate these exceptions.
private void setAmount(final ISign sign, final int index, final BigDecimal value, final IEssentials ess) throws SignException {

private void setAmount(final ISign sign, final int index, final BigDecimal value, final IEssentials ess, final boolean validationRun) throws SignException {
final String line = sign.getLine(index).trim();
if (line.isEmpty()) {
throw new SignException("emptySignLine", index + 1);
Expand All @@ -333,7 +334,9 @@ private void setAmount(final ISign sign, final int index, final BigDecimal value
if (money != null && amount != null) {
final String newline = NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.formatAsCurrency(value);
validateSignLength(newline);
sign.setLine(index, newline);
if (!validationRun) {
sign.setLine(index, newline);
}
return;
}
}
Expand All @@ -343,12 +346,16 @@ private void setAmount(final ISign sign, final int index, final BigDecimal value
if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) {
final String newline = stackAmount + " " + split[1] + ":" + value.intValueExact();
validateSignLength(newline);
sign.setLine(index, newline);
if (!validationRun) {
sign.setLine(index, newline);
}
} else {
getItemStack(split[1], stackAmount, ess);
final String newline = stackAmount + " " + split[1] + ":" + value.intValueExact();
validateSignLength(newline);
sign.setLine(index, newline);
if (!validationRun) {
sign.setLine(index, newline);
}
}
return;
}
Expand Down
Loading