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

import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import java.util.List;
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");
	}

	//TODO: Cleanup
	@Override
	public void run(Server server, User user, String commandLabel, String[] args) throws Exception
	{
		if (args.length > 0 && user.isAuthorized("essentials.clearinventory.others"))
		{
			if (args[0].contentEquals("*") && user.isAuthorized("essentials.clearinventory.all"))
			{
				cleanInventoryAll(server, user, args);
			}
			else if (args[0].trim().length() < 2)
			{
				cleanInventorySelf(server, user, args);
			}
			else
			{
				cleanInventoryOthers(server, user, args);
			}
		}
		else
		{
			cleanInventorySelf(server, user, args);
		}
	}

	@Override
	protected void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception
	{
		if (args.length > 0)
		{
			if (args[0].contentEquals("*"))
			{
				cleanInventoryAll(server, sender, args);
			}
			else if (args[0].trim().length() < 2)
			{
				throw new Exception(_("playerNotFound"));
			}
			else
			{
				cleanInventoryOthers(server, sender, args);
			}
		}
		else
		{
			throw new NotEnoughArgumentsException();
		}
	}

	private void cleanInventoryAll(Server server, CommandSender sender, String[] args) throws Exception
	{
		if (args.length > 1)
		{
			for (Player onlinePlayer : server.getOnlinePlayers())
			{
				clearInventory(onlinePlayer, args[1]);
			}
			sender.sendMessage(_("inventoryClearedAll"));
		}
		else
		{
			throw new NotEnoughArgumentsException();
		}
	}

	private void cleanInventoryOthers(Server server, CommandSender sender, String[] args) throws Exception
	{
		List<Player> online = server.matchPlayer(args[0]);

		if (!online.isEmpty())
		{
			for (Player p : online)
			{
				if (args.length > 1)
				{
					clearInventory(p, args[1]);
				}
				else
				{
					p.getInventory().clear();
				}
				sender.sendMessage(_("inventoryClearedOthers", p.getDisplayName()));
			}
		}
		else
		{
			throw new Exception(_("playerNotFound"));
		}
	}

	private void cleanInventorySelf(Server server, User user, String[] args) throws Exception
	{
		if (args.length > 0)
		{
			clearInventory(user, args[0]);
		}
		else
		{
			user.getInventory().clear();
		}
		user.sendMessage(_("inventoryCleared"));
	}

	private void clearInventory(Player player, String arg) throws Exception
	{
		if (arg.equalsIgnoreCase("*"))
		{
			player.getInventory().clear();
		}
		else if (arg.equalsIgnoreCase("**"))
		{
			player.getInventory().clear();
			player.getInventory().setArmorContents(null);
		}
		else
		{
			final String[] split = arg.split(":");
			final ItemStack item = ess.getItemDb().get(split[0]);
			final int type = item.getTypeId();

			if (split.length > 1 && Util.isInt(split[1]))
			{
				player.getInventory().clear(type, Integer.parseInt(split[1]));
			}
			else if (split.length > 1 && split[1].equalsIgnoreCase("*"))
			{
				player.getInventory().clear(type, -1);
			}
			else
			{
				if (Util.isInt(split[0]))
				{
					player.getInventory().clear(type, -1);
				}
				else
				{
					player.getInventory().clear(type, item.getDurability());
				}
			}
		}
	}
}