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
302
303
304
|
package com.earth2me.essentials;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import com.earth2me.essentials.craftbukkit.SetExpFix;
import static com.earth2me.essentials.I18n._;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
public class Trade
{
private final transient String command;
private final transient Double money;
private final transient ItemStack itemStack;
private final transient Integer exp;
private final transient IEssentials ess;
public Trade(final String command, final IEssentials ess)
{
this(command, null, null, null, ess);
}
public Trade(final double money, final IEssentials ess)
{
this(null, money, null, null, ess);
}
public Trade(final ItemStack items, final IEssentials ess)
{
this(null, null, items, null, ess);
}
public Trade(final int exp, final IEssentials ess)
{
this(null, null, null, exp, ess);
}
private Trade(final String command, final Double money, final ItemStack item, final Integer exp, final IEssentials ess)
{
this.command = command;
this.money = money;
this.itemStack = item;
this.exp = exp;
this.ess = ess;
}
public void isAffordableFor(final IUser user) throws ChargeException
{
final double mon = user.getMoney();
if (getMoney() != null
&& mon < getMoney()
&& getMoney() > 0
&& !user.isAuthorized("essentials.eco.loan"))
{
throw new ChargeException(_("notEnoughMoney"));
}
if (getItemStack() != null
&& !InventoryWorkaround.containsItem(user.getInventory(), true, true, itemStack))
{
throw new ChargeException(_("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " ")));
}
if (command != null && !command.isEmpty()
&& !user.isAuthorized("essentials.nocommandcost.all")
&& !user.isAuthorized("essentials.nocommandcost." + command)
&& mon < ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command)
&& 0 < ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command)
&& !user.isAuthorized("essentials.eco.loan"))
{
throw new ChargeException(_("notEnoughMoney"));
}
if (exp != null && exp > 0
&& user.getTotalExperience() < exp) {
throw new ChargeException(_("notEnoughExperience"));
}
}
public void pay(final IUser user)
{
pay(user, true);
}
public boolean pay(final IUser user, final boolean dropItems)
{
boolean success = true;
if (getMoney() != null && getMoney() > 0)
{
user.giveMoney(getMoney());
}
if (getItemStack() != null)
{
if (dropItems)
{
final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack());
for (ItemStack itemStack : leftOver.values())
{
InventoryWorkaround.dropItem(user.getLocation(), itemStack);
}
}
else
{
success = InventoryWorkaround.addAllItems(user.getInventory(), true, getItemStack());
}
user.updateInventory();
}
if (getExperience() != null)
{
SetExpFix.setTotalExperience(user, user.getTotalExperience() + getExperience());
}
return success;
}
public void charge(final IUser user) throws ChargeException
{
if (getMoney() != null)
{
final double mon = user.getMoney();
if (mon < getMoney() && getMoney() > 0 && !user.isAuthorized("essentials.eco.loan"))
{
throw new ChargeException(_("notEnoughMoney"));
}
user.takeMoney(getMoney());
}
if (getItemStack() != null)
{
if (!InventoryWorkaround.containsItem(user.getInventory(), true, true, itemStack))
{
throw new ChargeException(_("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " ")));
}
InventoryWorkaround.removeItem(user.getInventory(), true, true, getItemStack());
user.updateInventory();
}
if (command != null && !command.isEmpty()
&& !user.isAuthorized("essentials.nocommandcost.all")
&& !user.isAuthorized("essentials.nocommandcost." + command))
{
final double mon = user.getMoney();
final double cost = ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command);
if (mon < cost && cost > 0 && !user.isAuthorized("essentials.eco.loan"))
{
throw new ChargeException(_("notEnoughMoney"));
}
user.takeMoney(cost);
}
if (getExperience() != null)
{
final int experience = user.getTotalExperience();
if (experience < getExperience() && getExperience() > 0)
{
throw new ChargeException(_("notEnoughExperience"));
}
SetExpFix.setTotalExperience(user, experience - getExperience());
}
}
public Double getMoney()
{
return money;
}
public ItemStack getItemStack()
{
return itemStack;
}
public Integer getExperience()
{
return exp;
}
private static FileWriter fw = null;
public static void log(String type, String subtype, String event, String sender, Trade charge, String receiver, Trade pay, Location loc, IEssentials ess)
{
if (!ess.getSettings().isEcoLogEnabled())
{
return;
}
if (fw == null)
{
try
{
fw = new FileWriter(new File(ess.getDataFolder(), "trade.log"), true);
}
catch (IOException ex)
{
Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
}
}
StringBuilder sb = new StringBuilder();
sb.append(type).append(",").append(subtype).append(",").append(event).append(",\"");
sb.append(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date()));
sb.append("\",\"");
if (sender != null)
{
sb.append(sender);
}
sb.append("\",");
if (charge == null)
{
sb.append("\"\",\"\",\"\"");
}
else
{
if (charge.getItemStack() != null)
{
sb.append(charge.getItemStack().getAmount()).append(",");
sb.append(charge.getItemStack().getType().toString()).append(",");
sb.append(charge.getItemStack().getDurability());
}
if (charge.getMoney() != null)
{
sb.append(charge.getMoney()).append(",");
sb.append("money").append(",");
sb.append(ess.getSettings().getCurrencySymbol());
}
if (charge.getExperience() != null)
{
sb.append(charge.getExperience()).append(",");
sb.append("exp").append(",");
sb.append("\"\"");
}
}
sb.append(",\"");
if (receiver != null)
{
sb.append(receiver);
}
sb.append("\",");
if (pay == null)
{
sb.append("\"\",\"\",\"\"");
}
else
{
if (pay.getItemStack() != null)
{
sb.append(pay.getItemStack().getAmount()).append(",");
sb.append(pay.getItemStack().getType().toString()).append(",");
sb.append(pay.getItemStack().getDurability());
}
if (pay.getMoney() != null)
{
sb.append(pay.getMoney()).append(",");
sb.append("money").append(",");
sb.append(ess.getSettings().getCurrencySymbol());
}
if (pay.getExperience() != null)
{
sb.append(pay.getExperience()).append(",");
sb.append("exp").append(",");
sb.append("\"\"");
}
}
if (loc == null)
{
sb.append(",\"\",\"\",\"\",\"\"");
}
else
{
sb.append(",\"");
sb.append(loc.getWorld().getName()).append("\",");
sb.append(loc.getBlockX()).append(",");
sb.append(loc.getBlockY()).append(",");
sb.append(loc.getBlockZ()).append(",");
}
sb.append("\n");
try
{
fw.write(sb.toString());
fw.flush();
}
catch (IOException ex)
{
Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
}
}
public static void closeLog()
{
if (fw != null)
{
try
{
fw.close();
}
catch (IOException ex)
{
Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
}
fw = null;
}
}
}
|