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

import net.minecraft.server.WorldServer;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.craftbukkit.entity.CraftEntity;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Mob;
import com.earth2me.essentials.Mob.MobException;
import com.earth2me.essentials.TargetBlock;
import net.minecraft.server.EntityWolf;
import net.minecraft.server.PathEntity;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.entity.CraftCreeper;
import org.bukkit.craftbukkit.entity.CraftSheep;
import org.bukkit.craftbukkit.entity.CraftSlime;
import org.bukkit.craftbukkit.entity.CraftWolf;


public class Commandspawnmob extends EssentialsCommand
{
	public Commandspawnmob()
	{
		super("spawnmob");
	}

	@Override
	public void run(Server server, User user, String commandLabel, String[] args) throws Exception
	{
		if (args.length < 1)
		{
			throw new NotEnoughArgumentsException();
			//TODO: user.sendMessage("§7Mobs: Zombie PigZombie Skeleton Slime Chicken Pig Monster Spider Creeper Ghast Squid Giant Cow Sheep Wolf");
		}


		String[] mountparts = args[0].split(",");
		String[] parts = mountparts[0].split(":");
		String mobType = parts[0];
		mobType = mobType.equalsIgnoreCase("PigZombie") ? "PigZombie" : capitalCase(mobType);
		String mobData = null;
		if (parts.length == 2)
		{
			mobData = parts[1];
		}
		String mountType = null;
		String mountData = null;
		if (mountparts.length > 1)
		{
			parts = mountparts[1].split(":");
			mountType = parts[0];
			mountType = mountType.equalsIgnoreCase("PigZombie") ? "PigZombie" : capitalCase(mountType);
			if (parts.length == 2)
			{
				mountData = parts[1];
			}
		}


		CraftEntity spawnedMob = null;
		Mob mob = null;
		CraftEntity spawnedMount = null;
		Mob mobMount = null;

		mob = Mob.fromName(mobType);
		if (mob == null)
		{
			user.sendMessage("Invalid mob type.");
			return;
		}
		charge(user);
		WorldServer world = ((CraftWorld)user.getWorld()).getHandle();
		try
		{
			spawnedMob = mob.spawn(user, server);
		}
		catch (MobException e)
		{
			user.sendMessage("Unable to spawn mob.");
			return;
		}
		int[] ignore =
		{
			8, 9
		};
		Location loc = (new TargetBlock(user, 300, 0.2, ignore)).getTargetBlock().getLocation();

		Block block = user.getWorld().getBlockAt(loc);
		while (!(block.getType() == Material.AIR || block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER))
		{
			loc.setY(loc.getY() + 1);
			block = user.getWorld().getBlockAt(loc);
		}
		spawnedMob.teleportTo(loc);
		world.addEntity(spawnedMob.getHandle());

		if (mountType != null)
		{
			mobMount = Mob.fromName(mountType);
			if (mobMount == null)
			{
				user.sendMessage("Invalid mob type.");
				return;
			}
			try
			{
				spawnedMount = mobMount.spawn(user, server);
			}
			catch (MobException e)
			{
				user.sendMessage("Unable to spawn mob.");
				return;
			}
			spawnedMount.teleportTo(spawnedMob);
			spawnedMount.getHandle().setPassengerOf(spawnedMob.getHandle());
			world.addEntity(spawnedMount.getHandle());
		}
		if (mobData != null)
		{
			changeMobData(mob.name, spawnedMob, mobData, user);
		}
		if (spawnedMount != null && mountData != null)
		{
			changeMobData(mobMount.name, spawnedMount, mountData, user);
		}
		if (args.length == 2)
		{
			int mobCount = Integer.parseInt(args[1]);
			int serverLimit = ess.getSettings().getSpawnMobLimit();
			if (mobCount > serverLimit)
			{
				mobCount = serverLimit;
				user.sendMessage("Mob quantity limited to server limit");
			}

			try
			{
				for (int i = 1; i < mobCount; i++)
				{
					spawnedMob = mob.spawn(user, server);
					spawnedMob.teleportTo(loc);
					world.addEntity(spawnedMob.getHandle());
					if (mobMount != null)
					{
						try
						{
							spawnedMount = mobMount.spawn(user, server);
						}
						catch (MobException e)
						{
							user.sendMessage("Unable to spawn mob.");
							return;
						}
						spawnedMount.teleportTo(spawnedMob);
						spawnedMount.getHandle().setPassengerOf(spawnedMob.getHandle());
						world.addEntity(spawnedMount.getHandle());
					}
					if (mobData != null)
					{
						changeMobData(mob.name, spawnedMob, mobData, user);
					}
					if (spawnedMount != null && mountData != null)
					{
						changeMobData(mobMount.name, spawnedMount, mountData, user);
					}
				}
				user.sendMessage(args[1] + " " + mob.name.toLowerCase() + mob.s + " spawned.");
			}
			catch (MobException e1)
			{
				throw new Exception("Unable to spawn mobs. Insert bad excuse here.");
			}
			catch (NumberFormatException e2)
			{
				throw new Exception("A number goes there, silly.");
			}
			catch (NullPointerException np)
			{
				throw new Exception("That mob likes to be alone");
			}
		}
		else
		{
			user.sendMessage(mob.name + " spawned.");
		}
	}

	private String capitalCase(String s)
	{
		return s.toUpperCase().charAt(0) + s.toLowerCase().substring(1);
	}

	private void changeMobData(String type, CraftEntity spawned, String data, User user) throws Exception
	{
		if ("Slime".equalsIgnoreCase(type))
		{
			try
			{
				((CraftSlime)spawned).setSize(Integer.parseInt(data));
			}
			catch (Exception e)
			{
				throw new Exception("Malformed size.");
			}
		}
		if ("Sheep".equalsIgnoreCase(type))
		{
			try
			{
				((CraftSheep)spawned).setColor(DyeColor.valueOf(data.toUpperCase()));
			}
			catch (Exception e)
			{
				throw new Exception("Malformed color.");
			}
		}
		if ("Wolf".equalsIgnoreCase(type) && data.equalsIgnoreCase("tamed"))
		{
			EntityWolf wolf = ((CraftWolf)spawned).getHandle();
			wolf.d(true);
			wolf.a((PathEntity)null);
			wolf.setSitting(true);
			wolf.health = 20;
			wolf.a(user.getName());
			wolf.world.a(wolf, (byte)7);
		}
		if ("Wolf".equalsIgnoreCase(type) && data.equalsIgnoreCase("angry"))
		{
			((CraftWolf)spawned).setAngry(true);
		}
		if ("Creeper".equalsIgnoreCase(type) && data.equalsIgnoreCase("powered")) {
			((CraftCreeper)spawned).setPowered(true);
		}
	}
}