summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/Kit.java
blob: 6152e83961cd2f9d0799afb848e7c809b732b48c (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
package com.earth2me.essentials;

import static com.earth2me.essentials.I18n.tl;
import static com.earth2me.essentials.I18n.capitalCase;
import com.earth2me.essentials.Trade.OverflowType;
import com.earth2me.essentials.commands.NoChargeException;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer;
import com.earth2me.essentials.textreader.SimpleTextInput;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.NumberUtil;
import java.math.BigDecimal;
import java.util.*;
import java.util.logging.Level;
import net.ess3.api.IEssentials;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;


public class Kit
{
	final IEssentials ess;
	final String kitName;
	final Map<String, Object> kit;
	final Trade charge;

	public Kit(final String kitName, final IEssentials ess) throws Exception
	{
		this.kitName = kitName;
		this.ess = ess;
		this.kit = ess.getSettings().getKit(kitName);
		this.charge = new Trade("kit-" + kitName, new Trade("kit-kit", ess), ess);

		if (kit == null)
		{
			throw new Exception(tl("kitNotFound"));
		}
	}

	//TODO: Convert this to use one of the new text classes?
	public static String listKits(final IEssentials ess, final User user) throws Exception
	{
		try
		{
			final ConfigurationSection kits = ess.getSettings().getKits();
			final StringBuilder list = new StringBuilder();
			for (String kitItem : kits.getKeys(false))
			{
				if (user == null)
				{
					list.append(" ").append(capitalCase(kitItem));
				}
				else if (user.isAuthorized("essentials.kits." + kitItem.toLowerCase(Locale.ENGLISH)))
				{
					String cost = "";
					String name = capitalCase(kitItem);
					BigDecimal costPrice = new Trade("kit-" + kitItem.toLowerCase(Locale.ENGLISH), ess).getCommandCost(user);
					if (costPrice.signum() > 0)
					{
						cost = tl("kitCost", NumberUtil.displayCurrency(costPrice, ess));
					}

					Kit kit = new Kit(kitItem, ess);
					if (kit.getNextUse(user) != 0)
					{
						name = tl("kitDelay", name);
					}

					list.append(" ").append(name).append(cost);
				}
			}
			return list.toString().trim();
		}
		catch (Exception ex)
		{
			throw new Exception(tl("kitError"), ex);
		}

	}

	public String getName()
	{
		return kitName;
	}

	public void checkPerms(final User user) throws Exception
	{
		if (!user.isAuthorized("essentials.kits." + kitName))
		{
			throw new Exception(tl("noKitPermission", "essentials.kits." + kitName));
		}
	}

	public void checkDelay(final User user) throws Exception
	{
		long nextUse = getNextUse(user);

		if (nextUse == 0L)
		{
			return;
		}
		else if (nextUse < 0L)
		{
			user.sendMessage(tl("kitOnce"));
			throw new NoChargeException();
		}
		else
		{
			user.sendMessage(tl("kitTimed", DateUtil.formatDateDiff(nextUse)));
			throw new NoChargeException();
		}
	}

	public void checkAffordable(final User user) throws Exception
	{
		charge.isAffordableFor(user);
	}

	public void setTime(final User user) throws Exception
	{
		final Calendar time = new GregorianCalendar();
		user.setKitTimestamp(kitName, time.getTimeInMillis());
	}

	public void chargeUser(final User user) throws Exception
	{
		charge.charge(user);
	}

	public long getNextUse(final User user) throws Exception
	{
		if (user.isAuthorized("essentials.kit.exemptdelay"))
		{
			return 0L;
		}

		final Calendar time = new GregorianCalendar();

		double delay = 0;
		try
		{
			// Make sure delay is valid
			delay = kit.containsKey("delay") ? ((Number)kit.get("delay")).doubleValue() : 0.0d;
		}
		catch (Exception e)
		{
			throw new Exception(tl("kitError2"));
		}

		// When was the last kit used?
		final long lastTime = user.getKitTimestamp(kitName);

		// When can be use the kit again?
		final Calendar delayTime = new GregorianCalendar();
		delayTime.setTimeInMillis(lastTime);
		delayTime.add(Calendar.SECOND, (int)delay);
		delayTime.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0));

		if (lastTime == 0L || lastTime > time.getTimeInMillis())
		{
			// If we have no record of kit use, or its corrupted, give them benifit of the doubt.
			return 0L;
		}
		else if (delay < 0d)
		{
			// If the kit has a negative kit time, it can only be used once.
			return -1;
		}
		else if (delayTime.before(time))
		{
			// If the kit was used in the past, but outside the delay time, it can be used.
			return 0L;
		}
		else
		{
			// If the kit has been used recently, return the next time it can be used.
			return delayTime.getTimeInMillis();
		}
	}

	public List<String> getItems(final User user) throws Exception
	{
		if (kit == null)
		{
			throw new Exception(tl("kitNotFound"));
		}
		try
		{
			final List<String> itemList = new ArrayList<String>();
			final Object kitItems = kit.get("items");
			if (kitItems instanceof List)
			{
				for (Object item : (List)kitItems)
				{
					if (item instanceof String)
					{
						itemList.add(item.toString());
						continue;
					}
					throw new Exception("Invalid kit item: " + item.toString());
				}
				return itemList;
			}
			throw new Exception("Invalid item list");
		}
		catch (Exception e)
		{
			ess.getLogger().log(Level.WARNING, "Error parsing kit " + kitName + ": " + e.getMessage());
			throw new Exception(tl("kitError2"), e);
		}
	}

	public void expandItems(final User user) throws Exception
	{
		expandItems(user, getItems(user));
	}

	public void expandItems(final User user, final List<String> items) throws Exception
	{
		try
		{
			IText input = new SimpleTextInput(items);
			IText output = new KeywordReplacer(input, user.getSource(), ess);

			boolean spew = false;
			final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
			for (String kitItem : output.getLines())
			{
				if (kitItem.startsWith(ess.getSettings().getCurrencySymbol()))
				{
					BigDecimal value = new BigDecimal(kitItem.substring(ess.getSettings().getCurrencySymbol().length()).trim());
					Trade t = new Trade(value, ess);
					t.pay(user, OverflowType.DROP);
					continue;
				}

				final String[] parts = kitItem.split(" +");
				final ItemStack parseStack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1);

				if (parseStack.getType() == Material.AIR)
				{
					continue;
				}

				final MetaItemStack metaStack = new MetaItemStack(parseStack);

				if (parts.length > 2)
				{
					// We pass a null sender here because kits should not do perm checks
					metaStack.parseStringMeta(null, allowUnsafe, parts, 2, ess);
				}

				final Map<Integer, ItemStack> overfilled;
				final boolean allowOversizedStacks = user.isAuthorized("essentials.oversizedstacks");
				if (allowOversizedStacks)
				{
					overfilled = InventoryWorkaround.addOversizedItems(user.getBase().getInventory(), ess.getSettings().getOversizedStackSize(), metaStack.getItemStack());
				}
				else
				{
					overfilled = InventoryWorkaround.addItems(user.getBase().getInventory(), metaStack.getItemStack());
				}
				for (ItemStack itemStack : overfilled.values())
				{
					int spillAmount = itemStack.getAmount();
					if (!allowOversizedStacks)
					{
						itemStack.setAmount(spillAmount < itemStack.getMaxStackSize() ? spillAmount : itemStack.getMaxStackSize());
					}
					while (spillAmount > 0)
					{
						user.getWorld().dropItemNaturally(user.getLocation(), itemStack);
						spillAmount -= itemStack.getAmount();
					}
					spew = true;
				}
			}
			user.getBase().updateInventory();
			if (spew)
			{
				user.sendMessage(tl("kitInvFull"));
			}
		}
		catch (Exception e)
		{
			user.getBase().updateInventory();
			ess.getLogger().log(Level.WARNING, e.getMessage());
			throw new Exception(tl("kitError2"), e);
		}
	}
}