summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/signs/SignTrade.java
blob: 05b488d29afc585d1e709fa421d7aaaabcd60241 (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package com.earth2me.essentials.signs;

import net.ess3.api.IEssentials;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Trade.TradeType;
import com.earth2me.essentials.*;
import com.earth2me.essentials.Trade.OverflowType;
import com.earth2me.essentials.utils.NumberUtil;
import java.math.BigDecimal;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

//TODO: TL exceptions
public class SignTrade extends EssentialsSign
{
	public SignTrade()
	{
		super("Trade");
	}


	public enum AmountType
	{
		TOTAL,
		ROUNDED,
		COST
	}

	@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 trade = getTrade(sign, 2, AmountType.ROUNDED, true, ess);
		final Trade charge = getTrade(sign, 1, AmountType.ROUNDED, false, ess);
		if (trade.getType() == charge.getType()
			&& (trade.getType() != TradeType.ITEM || trade.getItemStack().isSimilar(charge.getItemStack())))
		{
			throw new SignException("You cannot trade for the same item type.");
		}
		trade.isAffordableFor(player);
		sign.setLine(3, "§8" + username);
		trade.charge(player);
		Trade.log("Sign", "Trade", "Create", username, trade, 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, AmountType.TOTAL, true, ess);
				subtractAmount(sign, 1, stored, ess);

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

				if (withdraw == null)
				{
					Trade.log("Sign", "Trade", "Withdraw", username, store, username, null, sign.getBlock().getLocation(), ess);
				}
				else
				{
					setAmount(sign, 1, BigDecimal.valueOf(withdraw.get(0).getAmount()), ess);
					Trade.log("Sign", "Trade", "Withdraw", username, stored, username, new Trade(withdraw.get(0), ess), sign.getBlock().getLocation(), ess);
				}
			}
			catch (SignException e)
			{
				if (store == null)
				{
					throw new SignException(_("tradeSignEmptyOwner"), e);
				}
			}
			Trade.log("Sign", "Trade", "Deposit", username, store, username, null, sign.getBlock().getLocation(), ess);
		}
		else
		{
			final Trade charge = getTrade(sign, 1, AmountType.COST, false, ess);
			final Trade trade = getTrade(sign, 2, AmountType.COST, true, ess);
			charge.isAffordableFor(player);
			addAmount(sign, 1, charge, ess);
			subtractAmount(sign, 2, trade, ess);
			if (!trade.pay(player))
			{
				subtractAmount(sign, 1, charge, ess);
				addAmount(sign, 2, trade, ess);
				throw new ChargeException("Full inventory");
			}
			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, AmountType.COST, false, ess);
		if (trade.getItemStack() != null && player.getItemInHand() != null
			&& trade.getItemStack().getType() == player.getItemInHand().getType()
			&& trade.getItemStack().getDurability() == player.getItemInHand().getDurability()
			&& trade.getItemStack().getEnchantments().equals(player.getItemInHand().getEnchantments()))
		{
			int amount = player.getItemInHand().getAmount();
			amount -= amount % trade.getItemStack().getAmount();
			if (amount > 0)
			{
				final ItemStack stack = player.getItemInHand().clone();
				stack.setAmount(amount);
				final Trade store = new Trade(stack, 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, AmountType.TOTAL, false, ess);
				final Trade stored2 = getTrade(sign, 2, AmountType.TOTAL, false, ess);
				Map<Integer, ItemStack> withdraw1 = stored1.pay(player, OverflowType.RETURN);
				Map<Integer, ItemStack> withdraw2 = stored2.pay(player, OverflowType.RETURN);

				if (withdraw1 == null && withdraw2 == null)
				{
					Trade.log("Sign", "Trade", "Break", username, stored2, username, stored1, sign.getBlock().getLocation(), ess);
					return true;
				}

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

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

				sign.updateSign();
			}
			catch (SignException e)
			{
				if (player.isAuthorized("essentials.signs.trade.override"))
				{
					return true;
				}
				throw e;
			}
			return false;
		}
		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 BigDecimal money = getMoney(split[0]);
			if (money != null)
			{
				if (NumberUtil.shortCurrency(money, ess).length() * 2 > 15)
				{
					throw new SignException("Line can be too long!");
				}
				sign.setLine(index, NumberUtil.shortCurrency(money, ess) + ":0");
				return;
			}
		}

		if (split.length == 2 && amountNeeded)
		{
			final BigDecimal money = getMoney(split[0]);
			BigDecimal amount = getBigDecimalPositive(split[1]);
			if (money != null && amount != null)
			{
				amount = amount.subtract(amount.remainder(money));
				if (amount.compareTo(MINTRANSACTION) < 0 || money.compareTo(MINTRANSACTION) < 0)
				{
					throw new SignException(_("moreThanZero"));
				}
				sign.setLine(index, NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.shortCurrency(amount, ess).substring(1));
				return;
			}
		}

		if (split.length == 2 && !amountNeeded)
		{
			final int amount = getIntegerPositive(split[0]);

			if (amount < 1)
			{
				throw new SignException(_("moreThanZero"));
			}
			if (!(split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp"))
				&& getItemStack(split[1], amount, ess).getType() == Material.AIR)
			{
				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]);
			int amount = getIntegerPositive(split[2]);
			amount -= amount % stackamount;
			if (amount < 1 || stackamount < 1)
			{
				throw new SignException(_("moreThanZero"));
			}
			if (!(split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp"))
				&& getItemStack(split[1], stackamount, ess).getType() == Material.AIR)
			{
				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 AmountType amountType, 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 BigDecimal money = getMoney(split[0]);
				final BigDecimal amount = notEmpty ? getBigDecimalPositive(split[1]) : getBigDecimal(split[1]);
				if (money != null && amount != null)
				{
					return new Trade(amountType == AmountType.COST ? money : amount, ess);
				}
			}
			catch (SignException e)
			{
				throw new SignException(_("tradeSignEmpty"), e);
			}
		}

		if (split.length == 3)
		{
			if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp"))
			{
				final int stackamount = getIntegerPositive(split[0]);
				int amount = getInteger(split[2]);
				if (amountType == AmountType.ROUNDED)
				{
					amount -= amount % stackamount;
				}
				if (notEmpty && (amount < 1 || stackamount < 1))
				{
					throw new SignException(_("tradeSignEmpty"));
				}
				return new Trade((amountType == AmountType.COST ? stackamount : amount), ess);
			}
			else
			{
				final int stackamount = getIntegerPositive(split[0]);
				final ItemStack item = getItemStack(split[1], stackamount, ess);
				int amount = getInteger(split[2]);
				if (amountType == AmountType.ROUNDED)
				{
					amount -= amount % stackamount;
				}
				if (notEmpty && (amount < 1 || stackamount < 1 || item.getType() == Material.AIR || amount < stackamount))
				{
					throw new SignException(_("tradeSignEmpty"));
				}
				item.setAmount(amountType == AmountType.COST ? stackamount : amount);
				return new Trade(item, ess);
			}
		}
		throw new SignException(_("invalidSignLine", index + 1));
	}

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

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

	//TODO: Translate these exceptions.
	private void changeAmount(final ISign sign, final int index, final BigDecimal 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 BigDecimal amount = getBigDecimal(split[1]).add(value);
			setAmount(sign, index, amount, ess);
			return;
		}
		if (split.length == 3)
		{
			final BigDecimal amount = getBigDecimal(split[2]).add(value);
			setAmount(sign, index, amount, ess);
			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
	{

		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 BigDecimal money = getMoney(split[0]);
			final BigDecimal amount = getBigDecimal(split[1]);
			if (money != null && amount != null)
			{
				final String newline = NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.shortCurrency(value, ess).substring(1);
				if (newline.length() > 15)
				{
					throw new SignException("This sign is full: Line too long!");
				}
				sign.setLine(index, newline);
				return;
			}
		}

		if (split.length == 3)
		{
			if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp"))
			{
				final int stackamount = getIntegerPositive(split[0]);
				final String newline = stackamount + " " + split[1] + ":" + (value.intValueExact());
				if (newline.length() > 15)
				{
					throw new SignException("This sign is full: Line too long!");
				}
				sign.setLine(index, newline);
				return;
			}
			else
			{
				final int stackamount = getIntegerPositive(split[0]);
				getItemStack(split[1], stackamount, ess);
				final String newline = stackamount + " " + split[1] + ":" + (value.intValueExact());
				if (newline.length() > 15)
				{
					throw new SignException("This sign is full: Line too long!");
				}
				sign.setLine(index, newline);
				return;
			}
		}
		throw new SignException(_("invalidSignLine", index + 1));
	}
}