16
16
import java .math .BigDecimal ;
17
17
import java .util .Map ;
18
18
19
- //TODO: TL exceptions
20
19
public class SignTrade extends EssentialsSign {
21
20
private static final int MAX_STOCK_LINE_LENGTH = 15 ;
22
21
@@ -47,14 +46,14 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri
47
46
final Trade stored ;
48
47
try {
49
48
stored = getTrade (sign , 1 , AmountType .TOTAL , true , true , ess );
50
- subtractAmount (sign , 1 , stored , ess );
49
+ subtractAmount (sign , 1 , stored , ess , false );
51
50
52
51
final Map <Integer , ItemStack > withdraw = stored .pay (player , OverflowType .RETURN );
53
52
54
53
if (withdraw == null ) {
55
54
Trade .log ("Sign" , "Trade" , "Withdraw" , username , store , username , null , sign .getBlock ().getLocation (), player .getMoney (), ess );
56
55
} else {
57
- setAmount (sign , 1 , BigDecimal .valueOf (withdraw .get (0 ).getAmount ()), ess );
56
+ setAmount (sign , 1 , BigDecimal .valueOf (withdraw .get (0 ).getAmount ()), ess , false );
58
57
Trade .log ("Sign" , "Trade" , "Withdraw" , username , stored , username , new Trade (withdraw .get (0 ), ess ), sign .getBlock ().getLocation (), player .getMoney (), ess );
59
58
}
60
59
} catch (final SignException e ) {
@@ -67,11 +66,16 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri
67
66
final Trade charge = getTrade (sign , 1 , AmountType .COST , false , true , ess );
68
67
final Trade trade = getTrade (sign , 2 , AmountType .COST , true , true , ess );
69
68
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 );
72
76
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 );
75
79
throw new ChargeException ("inventoryFull" );
76
80
}
77
81
charge .charge (player );
@@ -93,7 +97,7 @@ private Trade rechargeSign(final ISign sign, final IEssentials ess, final User p
93
97
stack = stack .clone ();
94
98
stack .setAmount (amount );
95
99
final Trade store = new Trade (stack , ess );
96
- addAmount (sign , 2 , store , ess );
100
+ addAmount (sign , 2 , store , ess , false );
97
101
store .charge (player );
98
102
return store ;
99
103
}
@@ -127,10 +131,10 @@ protected boolean onSignBreak(final ISign sign, final User player, final String
127
131
return true ;
128
132
}
129
133
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 );
131
135
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 );
132
136
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 );
134
138
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 );
135
139
136
140
sign .updateSign ();
@@ -267,38 +271,37 @@ protected final Trade getTrade(final ISign sign, final int index, final AmountTy
267
271
throw new SignException ("invalidSignLine" , index + 1 );
268
272
}
269
273
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 {
271
275
final BigDecimal money = trade .getMoney ();
272
276
if (money != null ) {
273
- changeAmount (sign , index , money .negate (), ess );
277
+ changeAmount (sign , index , money .negate (), ess , validationRun );
274
278
}
275
279
final ItemStack item = trade .getItemStack ();
276
280
if (item != null ) {
277
- changeAmount (sign , index , BigDecimal .valueOf (-item .getAmount ()), ess );
281
+ changeAmount (sign , index , BigDecimal .valueOf (-item .getAmount ()), ess , validationRun );
278
282
}
279
283
final Integer exp = trade .getExperience ();
280
284
if (exp != null ) {
281
- changeAmount (sign , index , BigDecimal .valueOf (-exp ), ess );
285
+ changeAmount (sign , index , BigDecimal .valueOf (-exp ), ess , validationRun );
282
286
}
283
287
}
284
288
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 {
286
290
final BigDecimal money = trade .getMoney ();
287
291
if (money != null ) {
288
- changeAmount (sign , index , money , ess );
292
+ changeAmount (sign , index , money , ess , validationRun );
289
293
}
290
294
final ItemStack item = trade .getItemStack ();
291
295
if (item != null ) {
292
- changeAmount (sign , index , BigDecimal .valueOf (item .getAmount ()), ess );
296
+ changeAmount (sign , index , BigDecimal .valueOf (item .getAmount ()), ess , validationRun );
293
297
}
294
298
final Integer exp = trade .getExperience ();
295
299
if (exp != null ) {
296
- changeAmount (sign , index , BigDecimal .valueOf (exp ), ess );
300
+ changeAmount (sign , index , BigDecimal .valueOf (exp ), ess , validationRun );
297
301
}
298
302
}
299
303
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 {
302
305
final String line = sign .getLine (index ).trim ();
303
306
if (line .isEmpty ()) {
304
307
throw new SignException ("emptySignLine" , index + 1 );
@@ -307,20 +310,18 @@ private void changeAmount(final ISign sign, final int index, final BigDecimal va
307
310
308
311
if (split .length == 2 ) {
309
312
final BigDecimal amount = getBigDecimal (split [1 ], ess ).add (value );
310
- setAmount (sign , index , amount , ess );
313
+ setAmount (sign , index , amount , ess , validationRun );
311
314
return ;
312
315
}
313
316
if (split .length == 3 ) {
314
317
final BigDecimal amount = getBigDecimal (split [2 ], ess ).add (value );
315
- setAmount (sign , index , amount , ess );
318
+ setAmount (sign , index , amount , ess , validationRun );
316
319
return ;
317
320
}
318
321
throw new SignException ("invalidSignLine" , index + 1 );
319
322
}
320
323
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 {
324
325
final String line = sign .getLine (index ).trim ();
325
326
if (line .isEmpty ()) {
326
327
throw new SignException ("emptySignLine" , index + 1 );
@@ -333,7 +334,9 @@ private void setAmount(final ISign sign, final int index, final BigDecimal value
333
334
if (money != null && amount != null ) {
334
335
final String newline = NumberUtil .shortCurrency (money , ess ) + ":" + NumberUtil .formatAsCurrency (value );
335
336
validateSignLength (newline );
336
- sign .setLine (index , newline );
337
+ if (!validationRun ) {
338
+ sign .setLine (index , newline );
339
+ }
337
340
return ;
338
341
}
339
342
}
@@ -343,12 +346,16 @@ private void setAmount(final ISign sign, final int index, final BigDecimal value
343
346
if (split [1 ].equalsIgnoreCase ("exp" ) || split [1 ].equalsIgnoreCase ("xp" )) {
344
347
final String newline = stackAmount + " " + split [1 ] + ":" + value .intValueExact ();
345
348
validateSignLength (newline );
346
- sign .setLine (index , newline );
349
+ if (!validationRun ) {
350
+ sign .setLine (index , newline );
351
+ }
347
352
} else {
348
353
getItemStack (split [1 ], stackAmount , ess );
349
354
final String newline = stackAmount + " " + split [1 ] + ":" + value .intValueExact ();
350
355
validateSignLength (newline );
351
- sign .setLine (index , newline );
356
+ if (!validationRun ) {
357
+ sign .setLine (index , newline );
358
+ }
352
359
}
353
360
return ;
354
361
}
0 commit comments