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

import java.util.Collections;
import java.util.Locale;
import static net.ess3.I18n._;
import net.ess3.bukkit.LivingEntities;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.*;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;


public class Commandkillall extends EssentialsCommand
{
	//TODO: Tidy - missed this during command cleanup
	@Override
	protected void run(final CommandSender sender, final String commandLabel, final String[] args) throws Exception
	{
		String type = "all";
		int radius = -1;
		World world;
		if (isUser(sender))
		{
			world = getPlayer(sender).getWorld();
			if (args.length == 1)
			{
				try
				{
					radius = Integer.parseInt(args[0]);
				}
				catch (NumberFormatException e1)
				{
					type = args[0];
				}
			}
			else if (args.length > 1)
			{
				type = args[0];
				try
				{
					radius = Integer.parseInt(args[1]);
				}
				catch (NumberFormatException e)
				{
					throw new Exception(_("A number goes there, silly."), e);
				}
			}
		}
		else
		{
			if (args.length == 0)
			{
				throw new NotEnoughArgumentsException();
			}
			else if (args.length == 1)
			{
				world = ess.getWorld(args[0]);
			}
			else
			{
				type = args[0];
				world = ess.getWorld(args[1]);
			}
		}
		if (radius >= 0)
		{
			radius *= radius;
		}
		final String killType = type.toLowerCase(Locale.ENGLISH);
		final boolean animals = killType.startsWith("animal");
		final boolean monster = killType.startsWith("monster") || killType.startsWith("mob");
		final boolean all = killType.equals("all");
		Class<? extends Entity> entityClass = null;
		if (!animals && !monster && !all)
		{
			if (LivingEntities.fromName(killType) == null)
			{
				throw new Exception(_("Invalid mob type."));
			}
			entityClass = LivingEntities.fromName(killType).getEntityClass();
		}
		int numKills = 0;
		final Location loc = isUser(sender) ? getPlayer(sender).getLocation() : null;
		for (Chunk chunk : world.getLoadedChunks())
		{
			for (Entity entity : chunk.getEntities())
			{
				if (loc != null)
				{
					if (radius >= 0 && loc.distanceSquared(entity.getLocation()) > radius)
					{
						continue;
					}
				}
				if (!(entity instanceof LivingEntity) || entity instanceof HumanEntity)
				{
					continue;
				}
				if (entity instanceof Wolf)
				{
					if (((Wolf)entity).isTamed())
					{
						continue;
					}
				}
				if (entity instanceof Ocelot)
				{
					if (((Ocelot)entity).isTamed())
					{
						continue;
					}
				}
				if (animals)
				{
					if (entity instanceof Animals || entity instanceof NPC || entity instanceof Snowman || entity instanceof WaterMob)
					{
						EntityDeathEvent event = new EntityDeathEvent((LivingEntity)entity, Collections.<ItemStack>emptyList());
						ess.getServer().getPluginManager().callEvent(event);
						entity.remove();
						numKills++;
					}
				}
				else if (monster)
				{
					if (entity instanceof Monster || entity instanceof ComplexLivingEntity || entity instanceof Flying || entity instanceof Slime)
					{
						EntityDeathEvent event = new EntityDeathEvent((LivingEntity)entity, Collections.<ItemStack>emptyList());
						ess.getServer().getPluginManager().callEvent(event);
						entity.remove();
						numKills++;
					}
				}
				else if (all)
				{
					EntityDeathEvent event = new EntityDeathEvent((LivingEntity)entity, Collections.<ItemStack>emptyList());
					ess.getServer().getPluginManager().callEvent(event);
					entity.remove();
					numKills++;
				}
				else if (entityClass != null && entityClass.isAssignableFrom(entity.getClass()))
				{
					EntityDeathEvent event = new EntityDeathEvent((LivingEntity)entity, Collections.<ItemStack>emptyList());
					ess.getServer().getPluginManager().callEvent(event);
					entity.remove();
					numKills++;
				}
			}
		}
		sender.sendMessage(_("Killed {0}.", numKills));
	}
}