summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandmore.java
blob: b03d185343bc36e073d700f13eab5ca7469cf589 (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
package net.ess3.commands;

import java.util.Locale;
import static net.ess3.I18n._;
import net.ess3.api.ChargeException;
import net.ess3.api.ISettings;
import net.ess3.api.IUser;
import net.ess3.economy.Trade;
import net.ess3.permissions.Permissions;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;


public class Commandmore extends EssentialsCommand
{
	@Override
	public void run(final IUser user, final String commandLabel, final String[] args) throws Exception
	{
		ItemStack[] stacks;
		final Player player = user.getPlayer();
		if (args.length > 0 && args[0].equalsIgnoreCase("all"))
		{
			stacks = player.getInventory().getContents();
		}
		else
		{
			stacks = new ItemStack[]{
					player.getItemInHand()
			};
		}
		for (ItemStack stack : stacks)
		{
			if (stack == null)
			{
				if (stacks.length == 1)
				{
					throw new Exception(_("You are not allowed to spawn the item {0}.", "Air"));
				}
				else
				{
					continue;
				}
			}
			ISettings settings = ess.getSettings();

			final int defaultStackSize = settings.getData().getGeneral().getDefaultStacksize();
			final int oversizedStackSize = settings.getData().getGeneral().getOversizedStacksize();

			int newAmount = Permissions.OVERSIZEDSTACKS.isAuthorized(
					user) ? oversizedStackSize : defaultStackSize > 0 ? defaultStackSize : stack.getMaxStackSize();
			if (stack.getAmount() >= newAmount)
			{
				if (stacks.length == 1)
				{
					throw new NoChargeException();
				}
				else
				{
					continue;
				}
			}
			final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
			if (!Permissions.ITEMSPAWN.isAuthorized(user, stack))
			{
				if (stacks.length == 1)
				{
					throw new Exception(_("You are not allowed to spawn the item {0}.", itemname));
				}
				else
				{
					continue;
				}
			}
			if (stack.getAmount() < newAmount && stacks.length > 1)
			{
				Trade trade = new Trade("more", ess);
				try
				{
					trade.charge(user);
				}
				catch (ChargeException ex)
				{
					user.sendMessage(ex.getMessage());
					break;
				}
			}
			stack.setAmount(newAmount);
		}
		if (stacks.length > 1)
		{
			player.getInventory().setContents(stacks);
		}
		player.updateInventory();
		if (stacks.length > 1)
		{
			throw new NoChargeException();
		}
	}
}