Skip to content

Commit 2bb4438

Browse files
authored
Fix further trade sign validation mismatches (#6116)
can never win with this fixes #6107
1 parent 06c886c commit 2bb4438

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

Essentials/src/main/java/com/earth2me/essentials/signs/SignTrade.java

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.math.BigDecimal;
1717
import java.util.Map;
1818

19-
//TODO: TL exceptions
2019
public class SignTrade extends EssentialsSign {
2120
private static final int MAX_STOCK_LINE_LENGTH = 15;
2221

@@ -47,14 +46,14 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri
4746
final Trade stored;
4847
try {
4948
stored = getTrade(sign, 1, AmountType.TOTAL, true, true, ess);
50-
subtractAmount(sign, 1, stored, ess);
49+
subtractAmount(sign, 1, stored, ess, false);
5150

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

5453
if (withdraw == null) {
5554
Trade.log("Sign", "Trade", "Withdraw", username, store, username, null, sign.getBlock().getLocation(), player.getMoney(), ess);
5655
} else {
57-
setAmount(sign, 1, BigDecimal.valueOf(withdraw.get(0).getAmount()), ess);
56+
setAmount(sign, 1, BigDecimal.valueOf(withdraw.get(0).getAmount()), ess, false);
5857
Trade.log("Sign", "Trade", "Withdraw", username, stored, username, new Trade(withdraw.get(0), ess), sign.getBlock().getLocation(), player.getMoney(), ess);
5958
}
6059
} catch (final SignException e) {
@@ -67,11 +66,16 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri
6766
final Trade charge = getTrade(sign, 1, AmountType.COST, false, true, ess);
6867
final Trade trade = getTrade(sign, 2, AmountType.COST, true, true, ess);
6968
charge.isAffordableFor(player);
70-
addAmount(sign, 1, charge, ess);
71-
subtractAmount(sign, 2, trade, ess);
69+
70+
// validate addAmount + subtractAmount first to ensure they both do not throw exceptions
71+
addAmount(sign, 1, charge, ess, true);
72+
subtractAmount(sign, 2, trade, ess, true);
73+
74+
addAmount(sign, 1, charge, ess, false);
75+
subtractAmount(sign, 2, trade, ess, false);
7276
if (!trade.pay(player)) {
73-
subtractAmount(sign, 1, charge, ess);
74-
addAmount(sign, 2, trade, ess);
77+
subtractAmount(sign, 1, charge, ess, false);
78+
addAmount(sign, 2, trade, ess, false);
7579
throw new ChargeException("inventoryFull");
7680
}
7781
charge.charge(player);
@@ -93,7 +97,7 @@ private Trade rechargeSign(final ISign sign, final IEssentials ess, final User p
9397
stack = stack.clone();
9498
stack.setAmount(amount);
9599
final Trade store = new Trade(stack, ess);
96-
addAmount(sign, 2, store, ess);
100+
addAmount(sign, 2, store, ess, false);
97101
store.charge(player);
98102
return store;
99103
}
@@ -127,10 +131,10 @@ protected boolean onSignBreak(final ISign sign, final User player, final String
127131
return true;
128132
}
129133

130-
setAmount(sign, 1, BigDecimal.valueOf(withdraw1 == null ? 0L : withdraw1.get(0).getAmount()), ess);
134+
setAmount(sign, 1, BigDecimal.valueOf(withdraw1 == null ? 0L : withdraw1.get(0).getAmount()), ess, false);
131135
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);
132136

133-
setAmount(sign, 2, BigDecimal.valueOf(withdraw2 == null ? 0L : withdraw2.get(0).getAmount()), ess);
137+
setAmount(sign, 2, BigDecimal.valueOf(withdraw2 == null ? 0L : withdraw2.get(0).getAmount()), ess, false);
134138
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);
135139

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

270-
protected final void subtractAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess) throws SignException {
274+
protected final void subtractAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess, final boolean validationRun) throws SignException {
271275
final BigDecimal money = trade.getMoney();
272276
if (money != null) {
273-
changeAmount(sign, index, money.negate(), ess);
277+
changeAmount(sign, index, money.negate(), ess, validationRun);
274278
}
275279
final ItemStack item = trade.getItemStack();
276280
if (item != null) {
277-
changeAmount(sign, index, BigDecimal.valueOf(-item.getAmount()), ess);
281+
changeAmount(sign, index, BigDecimal.valueOf(-item.getAmount()), ess, validationRun);
278282
}
279283
final Integer exp = trade.getExperience();
280284
if (exp != null) {
281-
changeAmount(sign, index, BigDecimal.valueOf(-exp), ess);
285+
changeAmount(sign, index, BigDecimal.valueOf(-exp), ess, validationRun);
282286
}
283287
}
284288

285-
protected final void addAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess) throws SignException {
289+
protected final void addAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess, final boolean validationRun) throws SignException {
286290
final BigDecimal money = trade.getMoney();
287291
if (money != null) {
288-
changeAmount(sign, index, money, ess);
292+
changeAmount(sign, index, money, ess, validationRun);
289293
}
290294
final ItemStack item = trade.getItemStack();
291295
if (item != null) {
292-
changeAmount(sign, index, BigDecimal.valueOf(item.getAmount()), ess);
296+
changeAmount(sign, index, BigDecimal.valueOf(item.getAmount()), ess, validationRun);
293297
}
294298
final Integer exp = trade.getExperience();
295299
if (exp != null) {
296-
changeAmount(sign, index, BigDecimal.valueOf(exp), ess);
300+
changeAmount(sign, index, BigDecimal.valueOf(exp), ess, validationRun);
297301
}
298302
}
299303

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

308311
if (split.length == 2) {
309312
final BigDecimal amount = getBigDecimal(split[1], ess).add(value);
310-
setAmount(sign, index, amount, ess);
313+
setAmount(sign, index, amount, ess, validationRun);
311314
return;
312315
}
313316
if (split.length == 3) {
314317
final BigDecimal amount = getBigDecimal(split[2], ess).add(value);
315-
setAmount(sign, index, amount, ess);
318+
setAmount(sign, index, amount, ess, validationRun);
316319
return;
317320
}
318321
throw new SignException("invalidSignLine", index + 1);
319322
}
320323

321-
//TODO: Translate these exceptions.
322-
private void setAmount(final ISign sign, final int index, final BigDecimal value, final IEssentials ess) throws SignException {
323-
324+
private void setAmount(final ISign sign, final int index, final BigDecimal value, final IEssentials ess, final boolean validationRun) throws SignException {
324325
final String line = sign.getLine(index).trim();
325326
if (line.isEmpty()) {
326327
throw new SignException("emptySignLine", index + 1);
@@ -333,7 +334,9 @@ private void setAmount(final ISign sign, final int index, final BigDecimal value
333334
if (money != null && amount != null) {
334335
final String newline = NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.formatAsCurrency(value);
335336
validateSignLength(newline);
336-
sign.setLine(index, newline);
337+
if (!validationRun) {
338+
sign.setLine(index, newline);
339+
}
337340
return;
338341
}
339342
}
@@ -343,12 +346,16 @@ private void setAmount(final ISign sign, final int index, final BigDecimal value
343346
if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) {
344347
final String newline = stackAmount + " " + split[1] + ":" + value.intValueExact();
345348
validateSignLength(newline);
346-
sign.setLine(index, newline);
349+
if (!validationRun) {
350+
sign.setLine(index, newline);
351+
}
347352
} else {
348353
getItemStack(split[1], stackAmount, ess);
349354
final String newline = stackAmount + " " + split[1] + ":" + value.intValueExact();
350355
validateSignLength(newline);
351-
sign.setLine(index, newline);
356+
if (!validationRun) {
357+
sign.setLine(index, newline);
358+
}
352359
}
353360
return;
354361
}

0 commit comments

Comments
 (0)