summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/SpawnMob.java
blob: 0b22569ca5b07f9384457b7d00f548f613e702b6 (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
package net.ess3;

import java.util.*;
import java.util.regex.Pattern;
import static net.ess3.I18n._;
import net.ess3.api.IEssentials;
import net.ess3.api.ISettings;
import net.ess3.api.IUser;
import net.ess3.bukkit.LivingEntities;
import net.ess3.bukkit.LivingEntities.MobException;
import net.ess3.commands.NotEnoughArgumentsException;
import net.ess3.permissions.Permissions;
import net.ess3.user.User;
import net.ess3.utils.LocationUtil;
import net.ess3.utils.Util;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.*;
import org.bukkit.material.Colorable;


public class SpawnMob
{
	private static Pattern colon = Pattern.compile(":");
	private static Pattern comma = Pattern.compile(",");

	public static String mobList(final IUser user) throws NotEnoughArgumentsException
	{
		final Set<String> mobList = LivingEntities.getLivingEntityList();
		final List<String> availableList = new ArrayList<String>();
		for (String mob : mobList)
		{
			if (Permissions.SPAWNMOB.isAuthorized(user, mob))
			{
				availableList.add(mob);
			}
		}
		if (availableList.isEmpty())
		{
			availableList.add(_("none"));
		}

		Collections.sort(availableList);
		return Util.joinList(availableList);
	}

	public static String[] mobData(final String mobString)
	{
		String[] returnString = new String[4];

		final String[] parts = comma.split(mobString);
		String[] mobParts = colon.split(parts[0]);

		returnString[0] = mobParts[0];
		if (mobParts.length == 2)
		{
			returnString[1] = mobParts[1];
		}

		if (parts.length > 1)
		{
			String[] mountParts = colon.split(parts[1]);
			returnString[2] = mountParts[0];
			if (mountParts.length == 2)
			{
				returnString[3] = mountParts[1];
			}
		}

		return returnString;
	}

	// This method spawns a mob where the user is looking, owned by user
	public static void spawnmob(final IEssentials ess, final Server server, final IUser user, final String[] Data, int mobCount) throws Exception
	{
		final Block block = LocationUtil.getTarget(user.getPlayer()).getBlock();
		if (block == null)
		{
			throw new Exception(_("unableToSpawnMob"));
		}
		spawnmob(ess, server, user, user, block.getLocation(), Data, mobCount);
	}

	// This method spawns a mob at loc, owned by noone
	public static void spawnmob(final IEssentials ess, final Server server, final CommandSender sender, final Location loc, final String[] Data, int mobCount) throws Exception
	{
		spawnmob(ess, server, sender, null, loc, Data, mobCount);
	}

	// This method spawns a mob at target, owned by target
	public static void spawnmob(final IEssentials ess, final Server server, final CommandSender sender, final IUser target, final String[] Data, int mobCount) throws Exception
	{
		spawnmob(ess, server, sender, target, target.getPlayer().getLocation(), Data, mobCount);
	}

	// This method spawns a mob at loc, owned by target
	public static void spawnmob(final IEssentials ess, final Server server, final CommandSender sender, final IUser target, final Location loc, final String[] Data, int mobCount) throws Exception
	{
		final Location sloc = LocationUtil.getSafeDestination(loc);
		final String mobType = Data[0];
		final String mobData = Data[1];
		final String mountType = Data[2];
		final String mountData = Data[3];

		EntityType mob = LivingEntities.fromName(mobType);
		EntityType mobMount = null;

		checkSpawnable(ess, sender, mob);

		if (mountType != null)
		{
			mobMount = LivingEntities.fromName(mountType);
			checkSpawnable(ess, sender, mobMount);
		}

		ISettings settings = ess.getSettings();
		int serverLimit = settings.getData().getCommands().getSpawnmob().getLimit();

		if (mobCount > serverLimit)
		{
			mobCount = serverLimit;
			sender.sendMessage(_("mobSpawnLimit"));
		}

		try
		{
			for (int i = 0; i < mobCount; i++)
			{
				spawnMob(ess, server, sender, target, sloc, mob, mobData, mobMount, mountData);
			}
			sender.sendMessage(mobCount + " " + mob.getName().toLowerCase(Locale.ENGLISH) + " " + _("spawned"));
		}
		catch (MobException e1)
		{
			throw new Exception(_("unableToSpawnMob"), e1);
		}
		catch (NumberFormatException e2)
		{
			throw new Exception(_("numberRequired"), e2);
		}
		catch (NullPointerException np)
		{
			throw new Exception(_("soloMob"), np);
		}
	}

	private static void spawnMob(final IEssentials ess, final Server server, final CommandSender sender, final IUser target, final Location sloc, EntityType mob, String mobData, EntityType mobMount, String mountData) throws Exception
	{

		final World spawningWorld = sloc.getWorld();
		final Entity spawnedMob = spawningWorld.spawn(sloc, (Class<? extends LivingEntity>)mob.getEntityClass());
		Entity spawnedMount = null;

		if (mobMount != null)
		{
			spawnedMount = spawningWorld.spawn(sloc, (Class<? extends LivingEntity>)mobMount.getEntityClass());
			spawnedMob.setPassenger(spawnedMount);
		}
		if (mobData != null)
		{
			changeMobData(mob, spawnedMob, mobData, target);
		}
		if (spawnedMount != null && mountData != null)
		{
			changeMobData(mobMount, spawnedMount, mountData, target);
		}
	}

	private static void checkSpawnable(IEssentials ess, CommandSender sender, EntityType mob) throws Exception
	{
		if (mob == null)
		{
			throw new Exception(_("invalidMob"));
		}

		if (!Permissions.SPAWNMOB.isAuthorized((User)sender, mob.getName()))
		{
			throw new Exception(_("noPermToSpawnMob"));
		}
	}

	private static void changeMobData(final EntityType type, final Entity spawned, String data, final IUser target) throws Exception
	{
		data = data.toLowerCase(Locale.ENGLISH);

		if (spawned instanceof Slime)
		{
			try
			{
				((Slime)spawned).setSize(Integer.parseInt(data));
			}
			catch (Exception e)
			{
				throw new Exception(_("slimeMalformedSize"), e);
			}
		}
		if (spawned instanceof Ageable && data.contains("baby"))
		{
			((Ageable)spawned).setBaby();
			return;
		}
		if (spawned instanceof Colorable)
		{
			final String color = data.toUpperCase(Locale.ENGLISH).replace("BABY", "");
			try
			{
				if (color.equals("RANDOM"))
				{
					final Random rand = new Random();
					((Colorable)spawned).setColor(DyeColor.values()[rand.nextInt(DyeColor.values().length)]);
				}
				else
				{
					((Colorable)spawned).setColor(DyeColor.valueOf(color));
				}
			}
			catch (Exception e)
			{
				throw new Exception(_("sheepMalformedColor"), e);
			}
		}
		if (spawned instanceof Tameable && data.contains("tamed") && target != null)
		{
			final Tameable tameable = ((Tameable)spawned);
			tameable.setTamed(true);
			tameable.setOwner(target.getPlayer());
		}
		if (type == EntityType.WOLF && data.contains("angry"))
		{
			((Wolf)spawned).setAngry(true);
		}
		if (type == EntityType.CREEPER && data.contains("powered"))
		{
			((Creeper)spawned).setPowered(true);
		}
		if (type == EntityType.OCELOT)
		{
			if (data.contains("siamese"))
			{
				((Ocelot)spawned).setCatType(Ocelot.Type.SIAMESE_CAT);
			}
			else if (data.contains("red"))
			{
				((Ocelot)spawned).setCatType(Ocelot.Type.RED_CAT);
			}
			else if (data.contains("black"))
			{
				((Ocelot)spawned).setCatType(Ocelot.Type.BLACK_CAT);
			}
		}
		if (type == EntityType.VILLAGER)
		{
			for (Villager.Profession prof : Villager.Profession.values())
			{
				if (data.contains(prof.toString().toLowerCase(Locale.ENGLISH)))
				{
					((Villager)spawned).setProfession(prof);
				}
			}
		}
	}
}