summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/signs/SignTrade.java
blob: 544c3216e0926fc1759a9948364513a3deb4e19a (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
package com.earth2me.essentials.signs;

import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
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, 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 stored = getTrade(sign, 1, true, true, ess);
			substractAmount(sign, 1, stored, ess);
			stored.pay(player);
			Trade.log("Sign", "Trade", "OwnerInteract", username, null, username, stored, ess);
		}
		else
		{
			final Trade charge = getTrade(sign, 1, false, false, ess);
			final Trade trade = getTrade(sign, 2, false, true, ess);
			charge.isAffordableFor(player);
			substractAmount(sign, 2, trade, ess);
			trade.pay(player);
			addAmount(sign, 1, charge, ess);
			charge.charge(player);
			Trade.log("Sign", "Trade", "Interact", sign.getLine(3), charge, username, trade, ess);
		}
		sign.updateSign();
		return true;
	}

	@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"))
		{
			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, ess);
			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]);
			final Double amount = getDoublePositive(split[1]);
			if (money != null && amount != null)
			{
				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(Util.i18n("moreThanZero"));
			}
			String newline = amount + " " + split[1] + ":0";
			if ((newline + amount).length() > 16)
			{
				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(Util.i18n("moreThanZero"));
			}
			sign.setLine(index, stackamount + " " + split[1] + ":" + amount);
			return;
		}
		throw new SignException(Util.format("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)
		{
			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);
			}
		}

		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(Util.i18n("moreThanZero"));
			}
			item.setAmount(fullAmount ? amount : stackamount);
			return new Trade(item, ess);
		}
		throw new SignException(Util.format("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)
			{
				sign.setLine(index, Util.formatCurrency(money, ess) + ":" + Util.formatCurrency(amount + value, ess).substring(1));
				return;
			}
		}

		if (split.length == 3)
		{
			final int stackamount = getIntegerPositive(split[0]);
			final ItemStack item = getItemStack(split[1], stackamount, ess);
			final int amount = getInteger(split[2]);
			sign.setLine(index, stackamount + " " + split[1] + ":" + (amount + Math.round(value)));
			return;
		}
		throw new SignException(Util.format("invalidSignLine", index+1));
	}
}