summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/SpawnMob.java
blob: edf9b4fc5400e76729768dfa18ebf5cb55f2f354 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.earth2me.essentials;

import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.Mob.MobException;
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.StringUtil;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import net.ess3.api.IEssentials;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.entity.*;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;


public class SpawnMob
{
	public static String mobList(final User user)
	{
		final Set<String> mobList = Mob.getMobList();
		final Set<String> availableList = new HashSet<String>();
		for (String mob : mobList)
		{
			if (user.isAuthorized("essentials.spawnmob." + mob.toLowerCase(Locale.ENGLISH)))
			{
				availableList.add(mob);
			}
		}
		if (availableList.isEmpty())
		{
			availableList.add(tl("none"));
		}
		return StringUtil.joinList(availableList);
	}

	public static List<String> mobParts(final String mobString)
	{
		String[] mobParts = mobString.split(",");

		List<String> mobs = new ArrayList<String>();

		for (String mobPart : mobParts)
		{
			String[] mobDatas = mobPart.split(":");
			mobs.add(mobDatas[0]);
		}
		return mobs;
	}

	public static List<String> mobData(final String mobString)
	{
		String[] mobParts = mobString.split(",");

		List<String> mobData = new ArrayList<String>();

		for (String mobPart : mobParts)
		{
			String[] mobDatas = mobPart.split(":");
			if (mobDatas.length == 1)
			{
				if (mobPart.contains(":"))
				{
					mobData.add("");
				}
				else
				{
					mobData.add(null);
				}
			}
			else
			{
				mobData.add(mobDatas[1]);
			}
		}

		return mobData;
	}

	// This method spawns a mob where the user is looking, owned by user
	public static void spawnmob(final IEssentials ess, final Server server, final User user, final List<String> parts, final List<String> data, int mobCount) throws Exception
	{
		final Block block = LocationUtil.getTarget(user.getBase()).getBlock();
		if (block == null)
		{
			throw new Exception(tl("unableToSpawnMob"));
		}
		spawnmob(ess, server, user.getSource(), user, block.getLocation(), parts, data, mobCount);
	}

	// This method spawns a mob at target, owned by target
	public static void spawnmob(final IEssentials ess, final Server server, final CommandSource sender, final User target, final List<String> parts, final List<String> data, int mobCount) throws Exception
	{
		spawnmob(ess, server, sender, target, target.getLocation(), parts, data, mobCount);
	}

	// This method spawns a mob at loc, owned by target
	public static void spawnmob(final IEssentials ess, final Server server, final CommandSource sender, final User target, final Location loc, final List<String> parts, final List<String> data, int mobCount) throws Exception
	{
		final Location sloc = LocationUtil.getSafeDestination(loc);

		for (int i = 0; i < parts.size(); i++)
		{
			Mob mob = Mob.fromName(parts.get(i));
			checkSpawnable(ess, sender, mob);
		}

		final int serverLimit = ess.getSettings().getSpawnMobLimit();
		int effectiveLimit = serverLimit / parts.size();

		if (effectiveLimit < 1)
		{
			effectiveLimit = 1;
			while (parts.size() > serverLimit)
			{
				parts.remove(serverLimit);
			}
		}

		if (mobCount > effectiveLimit)
		{
			mobCount = effectiveLimit;
			sender.sendMessage(tl("mobSpawnLimit"));
		}

		Mob mob = Mob.fromName(parts.get(0)); // Get the first mob
		try
		{
			for (int i = 0; i < mobCount; i++)
			{
				spawnMob(ess, server, sender, target, sloc, parts, data);
			}
			sender.sendMessage(mobCount * parts.size() + " " + mob.name.toLowerCase(Locale.ENGLISH) + mob.suffix + " " + tl("spawned"));
		}
		catch (MobException e1)
		{
			throw new Exception(tl("unableToSpawnMob"), e1);
		}
		catch (NumberFormatException e2)
		{
			throw new Exception(tl("numberRequired"), e2);
		}
		catch (NullPointerException np)
		{
			throw new Exception(tl("soloMob"), np);
		}
	}

	private static void spawnMob(final IEssentials ess, final Server server, final CommandSource sender, final User target, final Location sloc, List<String> parts, List<String> data) throws Exception
	{
		Mob mob;
		Entity spawnedMob = null;
		Entity spawnedMount;

		for (int i = 0; i < parts.size(); i++)
		{
			if (i == 0)
			{
				mob = Mob.fromName(parts.get(i));
				spawnedMob = mob.spawn(sloc.getWorld(), server, sloc);
				defaultMobData(mob.getType(), spawnedMob);

				if (data.get(i) != null)
				{
					changeMobData(sender, mob.getType(), spawnedMob, data.get(i).toLowerCase(Locale.ENGLISH), target);
				}
			}

			int next = (i + 1);
			if (next < parts.size()) //If it's the last mob in the list, don't set the mount
			{
				Mob mMob = Mob.fromName(parts.get(next));
				spawnedMount = mMob.spawn(sloc.getWorld(), server, sloc);
				defaultMobData(mMob.getType(), spawnedMount);

				if (data.get(next) != null)
				{
					changeMobData(sender, mMob.getType(), spawnedMount, data.get(next).toLowerCase(Locale.ENGLISH), target);
				}

				spawnedMob.setPassenger(spawnedMount);

				spawnedMob = spawnedMount;
			}
		}
	}

	private static void checkSpawnable(IEssentials ess, CommandSource sender, Mob mob) throws Exception
	{
		if (mob == null)
		{
			throw new Exception(tl("invalidMob"));
		}

		if (ess.getSettings().getProtectPreventSpawn(mob.getType().toString().toLowerCase(Locale.ENGLISH)))
		{
			throw new Exception(tl("disabledToSpawnMob"));
		}

		if (sender.isPlayer() && !ess.getUser(sender.getPlayer()).isAuthorized("essentials.spawnmob." + mob.name.toLowerCase(Locale.ENGLISH)))
		{
			throw new Exception(tl("noPermToSpawnMob"));
		}
	}

	private static void changeMobData(final CommandSource sender, final EntityType type, final Entity spawned, final String inputData, final User target) throws Exception
	{
		String data = inputData;

		if (data.isEmpty())
		{
			sender.sendMessage(tl("mobDataList", StringUtil.joinList(MobData.getValidHelp(spawned))));
		}

		if (spawned instanceof Zombie || type == EntityType.SKELETON)
		{
			if (inputData.contains("armor") || inputData.contains("armour"))
			{
				final EntityEquipment invent = ((LivingEntity)spawned).getEquipment();
				if (inputData.contains("noarmor") || inputData.contains("noarmour"))
				{
					invent.clear();
				}
				else if (inputData.contains("diamond"))
				{
					invent.setBoots(new ItemStack(Material.DIAMOND_BOOTS, 1));
					invent.setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS, 1));
					invent.setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE, 1));
					invent.setHelmet(new ItemStack(Material.DIAMOND_HELMET, 1));
				}
				else if (inputData.contains("gold"))
				{
					invent.setBoots(new ItemStack(Material.GOLD_BOOTS, 1));
					invent.setLeggings(new ItemStack(Material.GOLD_LEGGINGS, 1));
					invent.setChestplate(new ItemStack(Material.GOLD_CHESTPLATE, 1));
					invent.setHelmet(new ItemStack(Material.GOLD_HELMET, 1));
				}
				else if (inputData.contains("leather"))
				{
					invent.setBoots(new ItemStack(Material.LEATHER_BOOTS, 1));
					invent.setLeggings(new ItemStack(Material.LEATHER_LEGGINGS, 1));
					invent.setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE, 1));
					invent.setHelmet(new ItemStack(Material.LEATHER_HELMET, 1));
				}
				else
				{
					invent.setBoots(new ItemStack(Material.IRON_BOOTS, 1));
					invent.setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
					invent.setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
					invent.setHelmet(new ItemStack(Material.IRON_HELMET, 1));
				}
				invent.setBootsDropChance(0f);
				invent.setLeggingsDropChance(0f);
				invent.setChestplateDropChance(0f);
				invent.setHelmetDropChance(0f);
			}

		}

		MobData newData = MobData.fromData(spawned, data);
		while (newData != null)
		{
			newData.setData(spawned, target.getBase(), data);
			data = data.replace(newData.getMatched(), "");
			newData = MobData.fromData(spawned, data);
		}
	}

	private static void defaultMobData(final EntityType type, final Entity spawned)
	{
		if (type == EntityType.SKELETON)
		{
			final EntityEquipment invent = ((LivingEntity)spawned).getEquipment();
			invent.setItemInHand(new ItemStack(Material.BOW, 1));
			invent.setItemInHandDropChance(0.1f);

			invent.setBoots(new ItemStack(Material.GOLD_BOOTS, 1));
			invent.setBootsDropChance(0.0f);
		}

		if (type == EntityType.PIG_ZOMBIE)
		{
			final EntityEquipment invent = ((LivingEntity)spawned).getEquipment();
			invent.setItemInHand(new ItemStack(Material.GOLD_SWORD, 1));
			invent.setItemInHandDropChance(0.1f);

			invent.setBoots(new ItemStack(Material.GOLD_BOOTS, 1));
			invent.setBootsDropChance(0.0f);
		}

		if (type == EntityType.ZOMBIE)
		{
			final EntityEquipment invent = ((LivingEntity)spawned).getEquipment();
			invent.setBoots(new ItemStack(Material.GOLD_BOOTS, 1));
			invent.setBootsDropChance(0.0f);
		}

		if (type == EntityType.HORSE)
		{
			((Horse)spawned).setJumpStrength(1.2);
		}
	}
}