1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
package com.earth2me.essentials.signs;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.*;
import org.bukkit.inventory.ItemStack;
public class SignTrade extends EssentialsSign
{
public SignTrade()
{
super("Trade");
}
@Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{
validateTrade(sign, 1, false, ess);
validateTrade(sign, 2, true, ess);
final Trade charge = getTrade(sign, 2, true, true, ess);
charge.isAffordableFor(player);
sign.setLine(3, "§8" + username);
charge.charge(player);
Trade.log("Sign", "Trade", "Create", username, charge, username, null, sign.getBlock().getLocation(), ess);
return true;
}
@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{
if (sign.getLine(3).substring(2).equalsIgnoreCase(username))
{
final Trade store = rechargeSign(sign, ess, player);
Trade stored = null;
try
{
stored = getTrade(sign, 1, true, true, ess);
substractAmount(sign, 1, stored, ess);
stored.pay(player);
}
catch (SignException e)
{
if (store == null)
{
throw new SignException(_("tradeSignEmptyOwner"), e);
}
}
Trade.log("Sign", "Trade", "OwnerInteract", username, store, username, stored, sign.getBlock().getLocation(), ess);
}
else
{
final Trade charge = getTrade(sign, 1, false, false, ess);
final Trade trade = getTrade(sign, 2, false, true, ess);
charge.isAffordableFor(player);
if (!trade.pay(player, false))
{
throw new ChargeException("Full inventory");
}
substractAmount(sign, 2, trade, ess);
addAmount(sign, 1, charge, ess);
charge.charge(player);
Trade.log("Sign", "Trade", "Interact", sign.getLine(3), charge, username, trade, sign.getBlock().getLocation(), ess);
}
sign.updateSign();
return true;
}
private Trade rechargeSign(final ISign sign, final IEssentials ess, final User player) throws SignException, ChargeException
{
final Trade trade = getTrade(sign, 2, false, false, ess);
if (trade.getItemStack() != null && player.getItemInHand() != null
&& trade.getItemStack().getTypeId() == player.getItemInHand().getTypeId()
&& trade.getItemStack().getDurability() == player.getItemInHand().getDurability())
{
int amount = player.getItemInHand().getAmount();
amount -= amount % trade.getItemStack().getAmount();
if (amount > 0)
{
final Trade store = new Trade(new ItemStack(player.getItemInHand().getTypeId(), amount, player.getItemInHand().getDurability()), ess);
addAmount(sign, 2, store, ess);
store.charge(player);
return store;
}
}
return null;
}
@Override
protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{
if ((sign.getLine(3).length() > 3 && sign.getLine(3).substring(2).equalsIgnoreCase(username))
|| player.isAuthorized("essentials.signs.trade.override"))
{
try
{
final Trade stored1 = getTrade(sign, 1, true, false, ess);
final Trade stored2 = getTrade(sign, 2, true, false, ess);
stored1.pay(player);
stored2.pay(player);
Trade.log("Sign", "Trade", "Break", username, stored2, username, stored1, sign.getBlock().getLocation(), ess);
}
catch (SignException e)
{
if (player.isAuthorized("essentials.signs.trade.override"))
{
return true;
}
throw e;
}
return true;
}
else
{
return false;
}
}
protected final void validateTrade(final ISign sign, final int index, final boolean amountNeeded, final IEssentials ess) throws SignException
{
final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
throw new SignException("Empty line");
}
final String[] split = line.split("[ :]+");
if (split.length == 1 && !amountNeeded)
{
final Double money = getMoney(split[0]);
if (money != null)
{
if (Util.formatCurrency(money, ess).length() * 2 > 15)
{
throw new SignException("Line can be too long!");
}
sign.setLine(index, Util.formatCurrency(money, ess) + ":0");
return;
}
}
if (split.length == 2 && amountNeeded)
{
final Double money = getMoney(split[0]);
Double amount = getDoublePositive(split[1]);
if (money != null && amount != null)
{
amount -= amount % money;
if (amount < 0.01 || money < 0.01)
{
throw new SignException(_("moreThanZero"));
}
sign.setLine(index, Util.formatCurrency(money, ess) + ":" + Util.formatCurrency(amount, ess).substring(1));
return;
}
}
if (split.length == 2 && !amountNeeded)
{
final int amount = getIntegerPositive(split[0]);
final ItemStack item = getItemStack(split[1], amount, ess);
if (amount < 1 || item.getTypeId() == 0)
{
throw new SignException(_("moreThanZero"));
}
String newline = amount + " " + split[1] + ":0";
if ((newline + amount).length() > 15)
{
throw new SignException("Line can be too long!");
}
sign.setLine(index, newline);
return;
}
if (split.length == 3 && amountNeeded)
{
final int stackamount = getIntegerPositive(split[0]);
final ItemStack item = getItemStack(split[1], stackamount, ess);
int amount = getIntegerPositive(split[2]);
amount -= amount % stackamount;
if (amount < 1 || stackamount < 1 || item.getTypeId() == 0)
{
throw new SignException(_("moreThanZero"));
}
sign.setLine(index, stackamount + " " + split[1] + ":" + amount);
return;
}
throw new SignException(_("invalidSignLine", index + 1));
}
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();
if (line.isEmpty())
{
throw new SignException("Empty line");
}
final String[] split = line.split("[ :]+");
if (split.length == 2)
{
try
{
final Double money = getMoney(split[0]);
final Double amount = notEmpty ? getDoublePositive(split[1]) : getDouble(split[1]);
if (money != null && amount != null)
{
return new Trade(fullAmount ? amount : money, ess);
}
}
catch (SignException e)
{
throw new SignException(_("tradeSignEmpty"));
}
}
if (split.length == 3)
{
final int stackamount = getIntegerPositive(split[0]);
final ItemStack item = getItemStack(split[1], stackamount, ess);
int amount = getInteger(split[2]);
amount -= amount % stackamount;
if (notEmpty && (amount < 1 || stackamount < 1 || item.getTypeId() == 0))
{
throw new SignException(_("tradeSignEmpty"));
}
item.setAmount(fullAmount ? amount : stackamount);
return new Trade(item, ess);
}
throw new SignException(_("invalidSignLine", index + 1));
}
protected final void substractAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess) throws SignException
{
final Double money = trade.getMoney();
if (money != null)
{
changeAmount(sign, index, -money, ess);
}
final ItemStack item = trade.getItemStack();
if (item != null)
{
changeAmount(sign, index, -item.getAmount(), ess);
}
}
protected final void addAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess) throws SignException
{
final Double money = trade.getMoney();
if (money != null)
{
changeAmount(sign, index, money, ess);
}
final ItemStack item = trade.getItemStack();
if (item != null)
{
changeAmount(sign, index, item.getAmount(), ess);
}
}
private void changeAmount(final ISign sign, final int index, final double value, final IEssentials ess) throws SignException
{
final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
throw new SignException("Empty line");
}
final String[] split = line.split("[ :]+");
if (split.length == 2)
{
final Double money = getMoney(split[0]);
final Double amount = getDouble(split[1]);
if (money != null && amount != null)
{
final String newline = Util.formatCurrency(money, ess) + ":" + Util.formatCurrency(amount + value, ess).substring(1);
if (newline.length() > 15)
{
throw new SignException("Line too long!");
}
sign.setLine(index, newline);
return;
}
}
if (split.length == 3)
{
final int stackamount = getIntegerPositive(split[0]);
//TODO: Unused local variable
final ItemStack item = getItemStack(split[1], stackamount, ess);
final int amount = getInteger(split[2]);
final String newline = stackamount + " " + split[1] + ":" + (amount + Math.round(value));
if (newline.length() > 15)
{
throw new SignException("Line too long!");
}
sign.setLine(index, newline);
return;
}
throw new SignException(_("invalidSignLine", index + 1));
}
}
|