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

import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.Mob;
import com.earth2me.essentials.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.bukkit.Chunk;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.*;

// This could be rewritten in a simpler form if we made a mapping of all Entity names to their types (which would also provide possible mod support)

public class Commandremove extends EssentialsCommand
{
	public Commandremove()
	{
		super("remove");
	}

	@Override
	protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
	{
		World world = user.getWorld();
		int radius = 0;
		if (args.length < 1)
		{
			throw new NotEnoughArgumentsException();
		}
		if (args.length >= 2)
		{
			try
			{
				radius = Integer.parseInt(args[1]);
			}
			catch (NumberFormatException e)
			{
				world = ess.getWorld(args[1]);
			}
		}
		if (args.length >= 3)
		{
			// This is to prevent breaking the old syntax
			radius = 0;
			world = ess.getWorld(args[2]);
		}
		parseCommand(server, user.getSource(), args, world, radius);

	}

	@Override
	protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length < 2)
		{
			throw new NotEnoughArgumentsException();
		}
		World world = ess.getWorld(args[1]);
		parseCommand(server, sender, args, world, 0);
	}

	private void parseCommand(Server server, CommandSource sender, String[] args, World world, int radius) throws Exception
	{
		List<String> types = new ArrayList<String>();
		List<String> customTypes = new ArrayList<String>();

		if (world == null)
		{
			throw new Exception(tl("invalidWorld"));
		}

		if (args[0].contentEquals("*") || args[0].contentEquals("all"))
		{
			types.add(0, "ALL");
		}
		else
		{
			for (String entityType : args[0].split(","))
			{
				ToRemove toRemove;
				try
				{
					toRemove = ToRemove.valueOf(entityType.toUpperCase(Locale.ENGLISH));
				}
				catch (Exception e)
				{
					try
					{
						toRemove = ToRemove.valueOf(entityType.concat("S").toUpperCase(Locale.ENGLISH));
					}
					catch (Exception ee)
					{
						toRemove = ToRemove.CUSTOM;
						customTypes.add(entityType);
					}
				}
				types.add(toRemove.toString());
			}
		}
		removeHandler(sender, types, customTypes, world, radius);
	}

	private void removeHandler(CommandSource sender, List<String> types, List<String> customTypes, World world, int radius)
	{
		int removed = 0;
		if (radius > 0)
		{
			radius *= radius;
		}

		ArrayList<ToRemove> removeTypes = new ArrayList<ToRemove>();
		ArrayList<Mob> customRemoveTypes = new ArrayList<Mob>();

		for (String s : types)
		{
			removeTypes.add(ToRemove.valueOf(s));
		}

		boolean warnUser = false;

		for (String s : customTypes)
		{
			Mob mobType = Mob.fromName(s);
			if (mobType == null)
			{
				warnUser = true;
			}
			else
			{
				customRemoveTypes.add(mobType);
			}
		}

		if (warnUser)
		{
			sender.sendMessage(tl("invalidMob"));
		}

		for (Chunk chunk : world.getLoadedChunks())
		{
			for (Entity e : chunk.getEntities())
			{
				if (radius > 0)
				{
					if (sender.getPlayer().getLocation().distanceSquared(e.getLocation()) > radius)
					{
						continue;
					}
				}
				if (e instanceof HumanEntity)
				{
					continue;
				}

				for (ToRemove toRemove : removeTypes)
				{

					// We should skip any TAMED animals unless we are specifially targetting them.
					if (e instanceof Tameable && ((Tameable)e).isTamed() 
						&& !removeTypes.contains(ToRemove.TAMED))
					{
						continue;
					}

					// We should skip any NAMED animals unless we are specifially targetting them.
					if (e instanceof LivingEntity && ((LivingEntity)e).getCustomName() != null 
						&& !removeTypes.contains(ToRemove.NAMED))
					{
						continue;
					}

					switch (toRemove)
					{
					case TAMED:
						if (e instanceof Tameable && ((Tameable)e).isTamed())
						{
							e.remove();
							removed++;
						}
						break;
					case NAMED:
						if (e instanceof LivingEntity && ((LivingEntity)e).getCustomName() != null)
						{
							e.remove();
							removed++;
						}
						break;
					case DROPS:
						if (e instanceof Item)
						{
							e.remove();
							removed++;
						}
						break;
					case ARROWS:
						if (e instanceof Projectile)
						{
							e.remove();
							removed++;
						}
						break;
					case BOATS:
						if (e instanceof Boat)
						{
							e.remove();
							removed++;
						}
						break;
					case MINECARTS:
						if (e instanceof Minecart)
						{
							e.remove();
							removed++;
						}
						break;
					case XP:
						if (e instanceof ExperienceOrb)
						{
							e.remove();
							removed++;
						}
						break;
					case PAINTINGS:
						if (e instanceof Painting)
						{
							e.remove();
							removed++;
						}
						break;
					case ITEMFRAMES:
						if (e instanceof ItemFrame)
						{
							e.remove();
							removed++;
						}
						break;
					case ENDERCRYSTALS:
						if (e instanceof EnderCrystal)
						{
							e.remove();
							removed++;
						}
						break;
					case AMBIENT:
						if (e instanceof Flying)
						{
							e.remove();
							removed++;
						}
						break;
					case HOSTILE:
					case MONSTERS:
						if (e instanceof Monster || e instanceof ComplexLivingEntity || e instanceof Flying || e instanceof Slime)
						{
							e.remove();
							removed++;
						}
						break;
					case PASSIVE:
					case ANIMALS:
						if (e instanceof Animals || e instanceof NPC || e instanceof Snowman || e instanceof WaterMob || e instanceof Ambient)
						{
							e.remove();
							removed++;
						}
						break;
					case MOBS:
						if (e instanceof Animals || e instanceof NPC || e instanceof Snowman || e instanceof WaterMob
							|| e instanceof Monster || e instanceof ComplexLivingEntity || e instanceof Flying || e instanceof Slime || e instanceof Ambient)
						{
							e.remove();
							removed++;
						}
						break;
					case ENTITIES:
					case ALL:
						if (e instanceof Entity)
						{
							e.remove();
							removed++;
						}
						break;
					case CUSTOM:
						for (Mob type : customRemoveTypes)
						{
							if (e.getType() == type.getType())
							{
								e.remove();
								removed++;
							}
						}
						break;
					}
				}
			}
		}
		sender.sendMessage(tl("removed", removed));
	}


	private enum ToRemove
	{
		DROPS,
		ARROWS,
		BOATS,
		MINECARTS,
		XP,
		PAINTINGS,
		ITEMFRAMES,
		ENDERCRYSTALS,
		HOSTILE,
		MONSTERS,
		PASSIVE,
		ANIMALS,
		AMBIENT,
		MOBS,
		ENTITIES,
		ALL,
		CUSTOM,
		TAMED,
		NAMED
	}
}