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


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

	@Override
	public String[] getTriggers()
	{
		return new String[]
				{
					getName(), "mob"
				};
	}

	@Override
	public void run(Server server, Essentials parent, User user, String commandLabel, String[] args) throws Exception
	{
		if (args.length < 1)
		{
			user.sendMessage("§cUsage: /spawnmob [mob]<,mount><:size> <quantity>");
			user.sendMessage("§7Mobs: Zombie PigZombie Skeleton Slime Chicken Pig Monster Spider Creeper Ghast Squid Giant Cow Sheep");
			return;
		}

		String[] split1 = args[0].split(":");
		String[] split0 = null;
		CraftEntity spawned1 = null;
		Mob mob2 = null;
		if (split1.length == 1 && !split1[0].equalsIgnoreCase("Slime"))
		{
			split0 = args[0].split(",");
			split1[0] = split0[0];
		}
		if (split1.length == 2)
		{
			args[0] = split1[0] + "";
		}
		Mob mob = Mob.fromName(split1[0].equalsIgnoreCase("PigZombie") ? "PigZombie" : capitalCase(split1[0]));
		if (mob == null)
		{
			user.sendMessage("Invalid mob type.");
			return;
		}
		WorldServer world = ((org.bukkit.craftbukkit.CraftWorld)user.getWorld()).getHandle();
		CraftEntity spawned = null;
		try
		{
			spawned = 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();
		int blkId = user.getWorld().getBlockTypeIdAt(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
		while (!(blkId == 0 || blkId == 8 || blkId == 9))
		{
			loc.setY(loc.getY() + 1);
			blkId = user.getWorld().getBlockTypeIdAt(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
		}
		spawned.teleportTo(loc);
		world.a(spawned.getHandle());
		if (split0 != null && split0.length == 2)
		{
			mob2 = Mob.fromName(split0[1].equalsIgnoreCase("PigZombie") ? "PigZombie" : capitalCase(split0[1]));
			if (mob2 == null)
			{
				user.sendMessage("Invalid mob type.");
				return;
			}
			try
			{
				spawned1 = mob2.spawn(user, server);
			}
			catch (MobException e)
			{
				user.sendMessage("Unable to spawn mob.");
				return;
			}
			spawned1.teleportTo(spawned);
			spawned1.getHandle().setPassengerOf(spawned.getHandle());
			world.a(spawned1.getHandle());
		}
		if (split1.length == 2 && "Slime".equals(mob.name))
		{
			try
			{
				//((EntitySlime)spawned.getHandle()).a(Integer.parseInt(split1[1]));
			}
			catch (Exception e)
			{
				user.sendMessage("Malformed size.");
				return;
			}
		}
		if (args.length == 2)
		{
			int mobCount = Integer.parseInt(args[1]);
			int serverLimit =  Essentials.getSettings().getSpawnMobLimit();
			if(mobCount > serverLimit)
			{
				mobCount = serverLimit;
				user.sendMessage("Mob quantity limited to server limit");
			}
			user.charge(this);
			try
			{
				for (int i = 1; i < mobCount; i++)
				{
					spawned = mob.spawn(user, server);
					spawned.teleportTo(loc);
					if (split1.length > 1 && "Slime".equals("Slime"))
					{
						try
						{
							//((EntitySlime)spawned.getHandle()).a(Integer.parseInt(split1[1]));
						}
						catch (Exception e)
						{
							user.sendMessage("Malformed size.");
							return;
						}
					}
					world.a(spawned.getHandle());
					if (split0.length == 2)
					{
						if (mob2 == null)
						{
							user.sendMessage("Invalid mob mount.");
							return;
						}
						try
						{
							spawned1 = mob2.spawn(user, server);
						}
						catch (MobException e)
						{
							user.sendMessage("Unable to spawn mob.");
							return;
						}
						spawned1.teleportTo(spawned);
						spawned1.getHandle().setPassengerOf(spawned.getHandle());
						world.a(spawned1.getHandle());
					}
				}
				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);
	}
}