summaryrefslogtreecommitdiffstats
path: root/EssentialsSigns/src/com/earth2me/essentials/signs/EssentialsSign.java
blob: 54266b3b93bda911f62083529dd7291493034ecb (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package com.earth2me.essentials.signs;

import com.earth2me.essentials.api.ChargeException;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.economy.Trade;
import com.earth2me.essentials.utils.Util;
import com.earth2me.essentials.api.IEssentials;
import com.earth2me.essentials.api.IUser;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.inventory.ItemStack;


public class EssentialsSign
{
	private static final Set<Material> EMPTY_SET = new HashSet<Material>();
	protected transient final String signName;

	public EssentialsSign(final String signName)
	{
		this.signName = signName;
	}

	public final boolean onSignCreate(final SignChangeEvent event, final IEssentials ess)
	{
		final ISign sign = new EventSign(event);
		final IUser user = ess.getUser(event.getPlayer());
		if (!SignsPermissions.getCreatePermission(signName).isAuthorized(user))
		{
			// Return true, so other plugins can use the same sign title, just hope
			// they won't change it to §1[Signname]
			return true;
		}
		sign.setLine(0, _("signFormatFail", this.signName));
		try
		{
			final boolean ret = onSignCreate(sign, user, getUsername(user), ess);
			if (ret)
			{
				sign.setLine(0, getSuccessName());
			}
			return ret;
		}
		catch (ChargeException ex)
		{
			ess.getCommandHandler().showCommandError(user, signName, ex);
		}
		catch (SignException ex)
		{
			ess.getCommandHandler().showCommandError(user, signName, ex);
		}
		// Return true, so the player sees the wrong sign.
		return true;
	}

	public String getSuccessName()
	{
		return _("signFormatSuccess", this.signName);
	}

	public String getTemplateName()
	{
		return _("signFormatTemplate", this.signName);
	}

	private String getUsername(final IUser user)
	{
		return user.getName().substring(0, user.getName().length() > 13 ? 13 : user.getName().length());
	}

	public final boolean onSignInteract(final Block block, final Player player, final IEssentials ess)
	{
		final ISign sign = new BlockSign(block);
		final IUser user = ess.getUser(player);
		try
		{
			return SignsPermissions.getUsePermission(signName).isAuthorized(user)
				   && onSignInteract(sign, user, getUsername(user), ess);
		}
		catch (ChargeException ex)
		{
			ess.getCommandHandler().showCommandError(user,signName, ex);
			return false;
		}
		catch (SignException ex)
		{
			ess.getCommandHandler().showCommandError(user, signName, ex);
			return false;
		}
	}

	public final boolean onSignBreak(final Block block, final Player player, final IEssentials ess)
	{
		final ISign sign = new BlockSign(block);
		final IUser user = ess.getUser(player);
		try
		{
			return SignsPermissions.getBreakPermission(signName).isAuthorized(user)
				   && onSignBreak(sign, user, getUsername(user), ess);
		}
		catch (SignException ex)
		{
			ess.getCommandHandler().showCommandError(user, signName, ex);
			return false;
		}
	}

	protected boolean onSignCreate(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException, ChargeException
	{
		return true;
	}

	protected boolean onSignInteract(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException, ChargeException
	{
		return true;
	}

	protected boolean onSignBreak(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException
	{
		return true;
	}

	public final boolean onBlockPlace(final Block block, final Player player, final IEssentials ess)
	{
		IUser user = ess.getUser(player);
		try
		{
			return onBlockPlace(block, user, getUsername(user), ess);
		}
		catch (ChargeException ex)
		{
			ess.getCommandHandler().showCommandError(user, signName, ex);
		}
		catch (SignException ex)
		{
			ess.getCommandHandler().showCommandError(user, signName, ex);
		}
		return false;
	}

	public final boolean onBlockInteract(final Block block, final Player player, final IEssentials ess)
	{
		IUser user = ess.getUser(player);
		try
		{
			return onBlockInteract(block, user, getUsername(user), ess);
		}
		catch (ChargeException ex)
		{
			ess.getCommandHandler().showCommandError(user, signName, ex);
		}
		catch (SignException ex)
		{
			ess.getCommandHandler().showCommandError(user, signName, ex);
		}
		return false;
	}

	public final boolean onBlockBreak(final Block block, final Player player, final IEssentials ess)
	{
		IUser user = ess.getUser(player);
		try
		{
			return onBlockBreak(block, user, getUsername(user), ess);
		}
		catch (SignException ex)
		{
			ess.getCommandHandler().showCommandError(user, signName, ex);
		}
		return false;
	}

	public boolean onBlockBreak(final Block block, final IEssentials ess)
	{
		return true;
	}

	public boolean onBlockExplode(final Block block, final IEssentials ess)
	{
		return true;
	}

	public boolean onBlockBurn(final Block block, final IEssentials ess)
	{
		return true;
	}

	public boolean onBlockIgnite(final Block block, final IEssentials ess)
	{
		return true;
	}

	public boolean onBlockPush(final Block block, final IEssentials ess)
	{
		return true;
	}

	public static boolean checkIfBlockBreaksSigns(final Block block)
	{
		final Block sign = block.getRelative(BlockFace.UP);
		if (sign.getType() == Material.SIGN_POST && isValidSign(new BlockSign(sign)))
		{
			return true;
		}
		final BlockFace[] directions = new BlockFace[]
		{
			BlockFace.NORTH,
			BlockFace.EAST,
			BlockFace.SOUTH,
			BlockFace.WEST
		};
		for (BlockFace blockFace : directions)
		{
			final Block signblock = block.getRelative(blockFace);
			if (signblock.getType() == Material.WALL_SIGN)
			{
				final org.bukkit.material.Sign signMat = (org.bukkit.material.Sign)signblock.getState().getData();
				if (signMat != null && signMat.getFacing() == blockFace && isValidSign(new BlockSign(signblock)))
				{
					return true;
				}
			}
		}
		return false;
	}

	public static boolean isValidSign(final ISign sign)
	{
		return sign.getLine(0).matches("§1\\[.*\\]");
	}

	protected boolean onBlockPlace(final Block block, final IUser player, final String username, final IEssentials ess) throws SignException, ChargeException
	{
		return true;
	}

	protected boolean onBlockInteract(final Block block, final IUser player, final String username, final IEssentials ess) throws SignException, ChargeException
	{
		return true;
	}

	protected boolean onBlockBreak(final Block block, final IUser player, final String username, final IEssentials ess) throws SignException
	{
		return true;
	}

	public Set<Material> getBlocks()
	{
		return EMPTY_SET;
	}

	protected final void validateTrade(final ISign sign, final int index, final IEssentials ess) throws SignException
	{
		final String line = sign.getLine(index).trim();
		if (line.isEmpty())
		{
			return;
		}
		final Trade trade = getTrade(sign, index, 0, ess);
		final Double money = trade.getMoney();
		if (money != null)
		{
			sign.setLine(index, Util.shortCurrency(money, ess));
		}
	}

	protected final void validateTrade(final ISign sign, final int amountIndex, final int itemIndex,
									   final IUser player, final IEssentials ess) throws SignException
	{
		if (sign.getLine(itemIndex).equalsIgnoreCase("exp") || sign.getLine(itemIndex).equalsIgnoreCase("xp"))
		{
			int amount = getIntegerPositive(sign.getLine(amountIndex));
			sign.setLine(amountIndex, Integer.toString(amount));
			sign.setLine(itemIndex, "exp");
			return;
		}
		final Trade trade = getTrade(sign, amountIndex, itemIndex, player, ess);
		final ItemStack item = trade.getItemStack();
		sign.setLine(amountIndex, Integer.toString(item.getAmount()));
		sign.setLine(itemIndex, sign.getLine(itemIndex).trim());
	}

	protected final Trade getTrade(final ISign sign, final int amountIndex, final int itemIndex,
								   final IUser player, final IEssentials ess) throws SignException
	{
		if (sign.getLine(itemIndex).equalsIgnoreCase("exp") || sign.getLine(itemIndex).equalsIgnoreCase("xp"))
		{
			final int amount = getIntegerPositive(sign.getLine(amountIndex));
			return new Trade(amount, ess);
		}
		final ItemStack item = getItemStack(sign.getLine(itemIndex), 1, ess);
		final int amount = Math.min(getIntegerPositive(sign.getLine(amountIndex)), item.getType().getMaxStackSize() * player.getInventory().getSize());
		if (item.getTypeId() == 0 || amount < 1)
		{
			throw new SignException(_("moreThanZero"));
		}
		item.setAmount(amount);
		return new Trade(item, ess);
	}

	protected final void validateInteger(final ISign sign, final int index) throws SignException
	{
		final String line = sign.getLine(index).trim();
		if (line.isEmpty())
		{
			throw new SignException("Empty line " + index);
		}
		final int quantity = getIntegerPositive(line);
		sign.setLine(index, Integer.toString(quantity));
	}

	protected final int getIntegerPositive(final String line) throws SignException
	{
		final int quantity = getInteger(line);
		if (quantity < 1)
		{
			throw new SignException(_("moreThanZero"));
		}
		return quantity;
	}

	protected final int getInteger(final String line) throws SignException
	{
		try
		{
			final int quantity = Integer.parseInt(line);

			return quantity;
		}
		catch (NumberFormatException ex)
		{
			throw new SignException("Invalid sign", ex);
		}
	}

	protected final ItemStack getItemStack(final String itemName, final int quantity, final IEssentials ess) throws SignException
	{
		try
		{
			final ItemStack item = ess.getItemDb().get(itemName);
			item.setAmount(quantity);
			return item;
		}
		catch (Exception ex)
		{
			throw new SignException(ex.getMessage(), ex);
		}
	}

	protected final Double getMoney(final String line) throws SignException
	{
		final boolean isMoney = line.matches("^[^0-9-\\.][\\.0-9]+$");
		return isMoney ? getDoublePositive(line.substring(1)) : null;
	}

	protected final Double getDoublePositive(final String line) throws SignException
	{
		final double quantity = getDouble(line);
		if (Math.round(quantity * 100.0) < 1.0)
		{
			throw new SignException(_("moreThanZero"));
		}
		return quantity;
	}

	protected final Double getDouble(final String line) throws SignException
	{
		try
		{
			return Double.parseDouble(line);
		}
		catch (NumberFormatException ex)
		{
			throw new SignException(ex.getMessage(), ex);
		}
	}

	protected final Trade getTrade(final ISign sign, final int index, final IEssentials ess) throws SignException
	{
		return getTrade(sign, index, 1, ess);
	}

	protected final Trade getTrade(final ISign sign, final int index, final int decrement, final IEssentials ess) throws SignException
	{
		final String line = sign.getLine(index).trim();
		if (line.isEmpty())
		{
			return new Trade(signName.toLowerCase(Locale.ENGLISH) + "sign", ess);
		}

		final Double money = getMoney(line);
		if (money == null)
		{
			final String[] split = line.split("[ :]+", 2);
			if (split.length != 2)
			{
				throw new SignException(_("invalidCharge"));
			}
			final int quantity = getIntegerPositive(split[0]);

			final String item = split[1].toLowerCase(Locale.ENGLISH);
			if (item.equalsIgnoreCase("times"))
			{
				sign.setLine(index, (quantity - decrement) + " times");
				return new Trade(signName.toLowerCase(Locale.ENGLISH) + "sign", ess);
			}
			else if (item.equalsIgnoreCase("exp") || item.equalsIgnoreCase("xp"))
			{
				sign.setLine(index, quantity + " exp");
				return new Trade(quantity, ess);
			}
			else
			{
				final ItemStack stack = getItemStack(item, quantity, ess);
				sign.setLine(index, quantity + " " + item);
				return new Trade(stack, ess);
			}
		}
		else
		{
			return new Trade(money, ess);
		}
	}


	static class EventSign implements ISign
	{
		private final transient SignChangeEvent event;
		private final transient Block block;

		public EventSign(final SignChangeEvent event)
		{
			this.event = event;
			this.block = event.getBlock();
		}

		@Override
		public final String getLine(final int index)
		{
			return event.getLine(index);
		}

		@Override
		public final void setLine(final int index, final String text)
		{
			event.setLine(index, text);
		}

		@Override
		public Block getBlock()
		{
			return block;
		}

		@Override
		public void updateSign()
		{
		}
	}


	static class BlockSign implements ISign
	{
		private final transient Sign sign;
		private final transient Block block;

		public BlockSign(final Block block)
		{
			this.block = block;
			this.sign = (Sign)block.getState();
		}

		@Override
		public final String getLine(final int index)
		{
			return sign.getLine(index);
		}

		@Override
		public final void setLine(final int index, final String text)
		{
			sign.setLine(index, text);
		}

		@Override
		public final Block getBlock()
		{
			return block;
		}

		@Override
		public final void updateSign()
		{
			sign.update();
		}
	}


	public interface ISign
	{
		String getLine(final int index);

		void setLine(final int index, final String text);

		public Block getBlock();

		void updateSign();
	}
}