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

import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.NumberUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;


public class Commandclearinventory extends EssentialsCommand
{
	public Commandclearinventory()
	{
		super("clearinventory");
	}
	static int BASE_AMOUNT = 100000;
	static int EXTENDED_CAP = 8;

	@Override
	public void run(Server server, User user, String commandLabel, String[] args) throws Exception
	{
		parseCommand(server, user, args, user.isAuthorized("essentials.clearinventory.others"), user.isAuthorized("essentials.clearinventory.all"));
	}

	@Override
	protected void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception
	{
		parseCommand(server, sender, args, true, true);
	}

	private void parseCommand(Server server, CommandSender sender, String[] args, boolean allowOthers, boolean allowAll) throws Exception
	{
		List<Player> players = new ArrayList<Player>();
		int offset = 0;

		if (sender instanceof User)
		{
			players.add(((User)sender).getBase());
		}

		if (allowAll && args.length > 0 && args[0].contentEquals("*"))
		{
			sender.sendMessage(_("inventoryClearingFromAll"));
			offset = 1;
			players = Arrays.asList(server.getOnlinePlayers());
		}
		else if (allowOthers && args.length > 0 && args[0].trim().length() > 2)
		{
			offset = 1;
			players = server.matchPlayer(args[0].trim());
		}

		if (players.size() < 1)
		{
			throw new PlayerNotFoundException();
		}
		for (Player player : players)
		{
			clearHandler(sender, player, args, offset, players.size() < EXTENDED_CAP);		
		}
	}

	protected void clearHandler(CommandSender sender, Player player, String[] args, int offset, boolean showExtended) throws Exception
	{
		short data = -1;
		int type = -1;
		int amount = -1;

		if (args.length > (offset + 1) && NumberUtil.isInt(args[(offset + 1)]))
		{
			amount = Integer.parseInt(args[(offset + 1)]);
		}
		if (args.length > offset)
		{
			if (args[offset].equalsIgnoreCase("**"))
			{
				type = -2;
			}
			else if (!args[offset].equalsIgnoreCase("*"))
			{
				final String[] split = args[offset].split(":");
				final ItemStack item = ess.getItemDb().get(split[0]);
				type = item.getTypeId();

				if (split.length > 1 && NumberUtil.isInt(split[1]))
				{
					data = Short.parseShort(split[1]);
				}
				else
				{
					data = item.getDurability();
				}
			}
		}

		if (type == -1) // type -1 represents wildcard or all items
		{
			if (showExtended)
			{
				sender.sendMessage(_("inventoryClearingAllItems", player.getDisplayName()));
			}
			player.getInventory().clear();
		}
		else if (type == -2) // type -2 represents double wildcard or all items and armor
		{
			if (showExtended)
			{
				sender.sendMessage(_("inventoryClearingAllArmor", player.getDisplayName()));
			}
			player.getInventory().clear();
			player.getInventory().setArmorContents(null);
		}
		else
		{
			if (data == -1) // data -1 means that all subtypes will be cleared
			{
				ItemStack stack = new ItemStack(type);
				if (showExtended) {
					sender.sendMessage(_("inventoryClearingAllStack", stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName()));
				}
				player.getInventory().clear(type, data);
			}
			else if (amount == -1) // amount -1 means all items will be cleared
			{
				ItemStack stack = new ItemStack(type, BASE_AMOUNT, data);
				ItemStack removedStack = player.getInventory().removeItem(stack).get(0);
				final int removedAmount = (BASE_AMOUNT - removedStack.getAmount());
				if (removedAmount > 0 || showExtended)
				{
					sender.sendMessage(_("inventoryClearingStack", removedAmount, stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName()));
				}
			}
			else
			{
				ItemStack stack = new ItemStack(type, amount, data);
				if (player.getInventory().containsAtLeast(stack, amount))
				{
					sender.sendMessage(_("inventoryClearingStack", amount, stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName()));
					player.getInventory().removeItem(stack);
				}
				else
				{
					if (showExtended)
					{
						sender.sendMessage(_("inventoryClearFail", player.getDisplayName(), amount, stack.getType().toString().toLowerCase(Locale.ENGLISH)));
					}
				}
			}
		}
	}
}