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
|
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Mob;
import com.earth2me.essentials.Mob.MobException;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import java.util.Random;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.entity.*;
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("CaveSpider") ? "CaveSpider" : mobType.equalsIgnoreCase("PigZombie") ? "PigZombie" : Util.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("CaveSpider") ? "CaveSpider" : mountType.equalsIgnoreCase("PigZombie") ? "PigZombie" : Util.capitalCase(mountType);
if (parts.length == 2)
{
mountData = parts[1];
}
}
Entity spawnedMob = null;
Mob mob = null;
Entity spawnedMount = null;
Mob mobMount = null;
mob = Mob.fromName(mobType);
if (mob == null)
{
throw new Exception(Util.i18n("invalidMob"));
}
if (ess.getSettings().getProtectPreventSpawn(mob.getType().toString().toLowerCase()))
{
throw new Exception(Util.i18n("unableToSpawnMob"));
}
final Block block = Util.getTarget(user).getBlock();
if (block == null)
{
throw new Exception(Util.i18n("unableToSpawnMob"));
}
Location loc = block.getLocation();
Location sloc = Util.getSafeDestination(loc);
try
{
spawnedMob = mob.spawn(user, server, sloc);
}
catch (MobException e)
{
throw new Exception(Util.i18n("unableToSpawnMob"));
}
if (mountType != null)
{
mobMount = Mob.fromName(mountType);
if (mobMount == null)
{
user.sendMessage(Util.i18n("invalidMob"));
return;
}
if (ess.getSettings().getProtectPreventSpawn(mobMount.getType().toString().toLowerCase()))
{
throw new Exception(Util.i18n("unableToSpawnMob"));
}
try
{
spawnedMount = mobMount.spawn(user, server, loc);
}
catch (MobException e)
{
throw new Exception(Util.i18n("unableToSpawnMob"));
}
spawnedMob.setPassenger(spawnedMount);
}
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(Util.i18n("mobSpawnLimit"));
}
try
{
for (int i = 1; i < mobCount; i++)
{
spawnedMob = mob.spawn(user, server, loc);
if (mobMount != null)
{
try
{
spawnedMount = mobMount.spawn(user, server, loc);
}
catch (MobException e)
{
throw new Exception(Util.i18n("unableToSpawnMob"));
}
spawnedMob.setPassenger(spawnedMount);
}
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.suffix + " " + Util.i18n("spawned"));
}
catch (MobException e1)
{
throw new Exception(Util.i18n("unableToSpawnMob"), e1);
}
catch (NumberFormatException e2)
{
throw new Exception(Util.i18n("numberRequired"), e2);
}
catch (NullPointerException np)
{
throw new Exception(Util.i18n("soloMob"), np);
}
}
else
{
user.sendMessage(mob.name + " " + Util.i18n("spawned"));
}
}
private void changeMobData(String type, Entity spawned, String data, User user) throws Exception
{
if ("Slime".equalsIgnoreCase(type))
{
try
{
((Slime)spawned).setSize(Integer.parseInt(data));
}
catch (Exception e)
{
throw new Exception(Util.i18n("slimeMalformedSize"), e);
}
}
if ("Sheep".equalsIgnoreCase(type))
{
try
{
if (data.equalsIgnoreCase("random"))
{
Random rand = new Random();
((Sheep)spawned).setColor(DyeColor.values()[rand.nextInt(DyeColor.values().length)]);
}
else
{
((Sheep)spawned).setColor(DyeColor.valueOf(data.toUpperCase()));
}
}
catch (Exception e)
{
throw new Exception(Util.i18n("sheepMalformedColor"), e);
}
}
if ("Wolf".equalsIgnoreCase(type) && data.equalsIgnoreCase("tamed"))
{
final Wolf wolf = ((Wolf)spawned);
wolf.setTamed(true);
wolf.setOwner(user);
wolf.setSitting(true);
}
if ("Wolf".equalsIgnoreCase(type) && data.equalsIgnoreCase("angry"))
{
((Wolf)spawned).setAngry(true);
}
if ("Creeper".equalsIgnoreCase(type) && data.equalsIgnoreCase("powered"))
{
((Creeper)spawned).setPowered(true);
}
}
}
|