2012-11-11 18:55:02 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2014-03-20 15:54:07 +00:00
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
2012-11-11 18:55:02 +00:00
|
|
|
import com.earth2me.essentials.Mob.MobException;
|
2013-06-08 21:31:19 +00:00
|
|
|
import com.earth2me.essentials.utils.LocationUtil;
|
2013-10-11 02:44:41 +00:00
|
|
|
import com.earth2me.essentials.utils.StringUtil;
|
2013-03-22 21:24:11 +00:00
|
|
|
import java.util.ArrayList;
|
2012-11-11 18:55:02 +00:00
|
|
|
import java.util.HashSet;
|
2013-03-22 21:24:11 +00:00
|
|
|
import java.util.List;
|
2012-11-11 18:55:02 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Set;
|
2013-10-11 02:44:41 +00:00
|
|
|
import net.ess3.api.IEssentials;
|
2012-11-11 18:55:02 +00:00
|
|
|
import org.bukkit.Location;
|
2013-06-01 22:33:31 +00:00
|
|
|
import org.bukkit.Material;
|
2012-11-11 18:55:02 +00:00
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.block.Block;
|
2013-03-16 09:16:33 +00:00
|
|
|
import org.bukkit.entity.*;
|
2013-06-01 22:33:31 +00:00
|
|
|
import org.bukkit.inventory.EntityEquipment;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2012-11-11 18:55:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2013-03-10 02:04:11 +00:00
|
|
|
if (user.isAuthorized("essentials.spawnmob." + mob.toLowerCase(Locale.ENGLISH)))
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
|
|
|
availableList.add(mob);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (availableList.isEmpty())
|
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
availableList.add(tl("none"));
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
return StringUtil.joinList(availableList);
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
2012-12-16 21:39:31 +00:00
|
|
|
|
2013-03-22 21:24:11 +00:00
|
|
|
public static List<String> mobParts(final String mobString)
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-03-22 21:24:11 +00:00
|
|
|
String[] mobParts = mobString.split(",");
|
2012-12-16 21:39:31 +00:00
|
|
|
|
2013-03-22 21:24:11 +00:00
|
|
|
List<String> mobs = new ArrayList<String>();
|
2012-12-16 21:39:31 +00:00
|
|
|
|
2013-03-22 21:24:11 +00:00
|
|
|
for (String mobPart : mobParts)
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-03-22 21:24:11 +00:00
|
|
|
String[] mobDatas = mobPart.split(":");
|
|
|
|
mobs.add(mobDatas[0]);
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
2013-03-22 21:24:11 +00:00
|
|
|
return mobs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<String> mobData(final String mobString)
|
|
|
|
{
|
|
|
|
String[] mobParts = mobString.split(",");
|
|
|
|
|
|
|
|
List<String> mobData = new ArrayList<String>();
|
2012-12-16 21:39:31 +00:00
|
|
|
|
2013-03-22 21:24:11 +00:00
|
|
|
for (String mobPart : mobParts)
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-03-22 21:24:11 +00:00
|
|
|
String[] mobDatas = mobPart.split(":");
|
|
|
|
if (mobDatas.length == 1)
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-07-20 20:27:47 +00:00
|
|
|
if (mobPart.contains(":"))
|
|
|
|
{
|
|
|
|
mobData.add("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mobData.add(null);
|
|
|
|
}
|
2013-03-22 21:24:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mobData.add(mobDatas[1]);
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-16 21:39:31 +00:00
|
|
|
|
2013-03-22 21:24:11 +00:00
|
|
|
return mobData;
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method spawns a mob where the user is looking, owned by user
|
2013-03-22 21:24:11 +00:00
|
|
|
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
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-07-07 11:11:57 +00:00
|
|
|
final Block block = LocationUtil.getTarget(user.getBase()).getBlock();
|
2012-11-11 18:55:02 +00:00
|
|
|
if (block == null)
|
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
throw new Exception(tl("unableToSpawnMob"));
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
2013-10-16 19:59:39 +00:00
|
|
|
spawnmob(ess, server, user.getSource(), user, block.getLocation(), parts, data, mobCount);
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
2012-12-16 21:39:31 +00:00
|
|
|
|
2012-11-11 18:55:02 +00:00
|
|
|
// This method spawns a mob at target, owned by target
|
2013-10-16 19:59:39 +00:00
|
|
|
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
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-03-22 21:24:11 +00:00
|
|
|
spawnmob(ess, server, sender, target, target.getLocation(), parts, data, mobCount);
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
2012-12-16 21:39:31 +00:00
|
|
|
|
2012-11-11 18:55:02 +00:00
|
|
|
// This method spawns a mob at loc, owned by target
|
2013-10-16 19:59:39 +00:00
|
|
|
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
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-06-08 21:31:19 +00:00
|
|
|
final Location sloc = LocationUtil.getSafeDestination(loc);
|
2012-11-11 18:55:02 +00:00
|
|
|
|
2013-04-30 00:03:37 +00:00
|
|
|
for (int i = 0; i < parts.size(); i++)
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-03-22 21:24:11 +00:00
|
|
|
Mob mob = Mob.fromName(parts.get(i));
|
|
|
|
checkSpawnable(ess, sender, mob);
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
|
2013-04-30 00:03:37 +00:00
|
|
|
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)
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-04-30 00:03:37 +00:00
|
|
|
mobCount = effectiveLimit;
|
2014-03-20 15:54:07 +00:00
|
|
|
sender.sendMessage(tl("mobSpawnLimit"));
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 21:24:11 +00:00
|
|
|
Mob mob = Mob.fromName(parts.get(0)); // Get the first mob
|
2012-11-11 18:55:02 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
for (int i = 0; i < mobCount; i++)
|
|
|
|
{
|
2013-03-22 21:24:11 +00:00
|
|
|
spawnMob(ess, server, sender, target, sloc, parts, data);
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
2014-03-20 15:54:07 +00:00
|
|
|
sender.sendMessage(mobCount * parts.size() + " " + mob.name.toLowerCase(Locale.ENGLISH) + mob.suffix + " " + tl("spawned"));
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
catch (MobException e1)
|
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
throw new Exception(tl("unableToSpawnMob"), e1);
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
catch (NumberFormatException e2)
|
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
throw new Exception(tl("numberRequired"), e2);
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
catch (NullPointerException np)
|
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
throw new Exception(tl("soloMob"), np);
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 19:59:39 +00:00
|
|
|
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
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-03-22 21:24:11 +00:00
|
|
|
Mob mob;
|
|
|
|
Entity spawnedMob = null;
|
|
|
|
Entity spawnedMount;
|
2012-12-16 21:39:31 +00:00
|
|
|
|
2013-03-22 21:24:11 +00:00
|
|
|
for (int i = 0; i < parts.size(); i++)
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-03-22 21:24:11 +00:00
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
mob = Mob.fromName(parts.get(i));
|
|
|
|
spawnedMob = mob.spawn(sloc.getWorld(), server, sloc);
|
2013-06-01 22:33:31 +00:00
|
|
|
defaultMobData(mob.getType(), spawnedMob);
|
2013-03-22 21:24:11 +00:00
|
|
|
|
|
|
|
if (data.get(i) != null)
|
|
|
|
{
|
2013-07-20 20:27:47 +00:00
|
|
|
changeMobData(sender, mob.getType(), spawnedMob, data.get(i).toLowerCase(Locale.ENGLISH), target);
|
2013-03-22 21:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-30 00:03:37 +00:00
|
|
|
|
2013-03-22 21:24:11 +00:00
|
|
|
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);
|
2013-06-01 22:33:31 +00:00
|
|
|
defaultMobData(mMob.getType(), spawnedMount);
|
2013-03-22 21:24:11 +00:00
|
|
|
|
|
|
|
if (data.get(next) != null)
|
|
|
|
{
|
2013-07-20 20:27:47 +00:00
|
|
|
changeMobData(sender, mMob.getType(), spawnedMount, data.get(next).toLowerCase(Locale.ENGLISH), target);
|
2013-03-22 21:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spawnedMob.setPassenger(spawnedMount);
|
|
|
|
|
|
|
|
spawnedMob = spawnedMount;
|
|
|
|
}
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 19:59:39 +00:00
|
|
|
private static void checkSpawnable(IEssentials ess, CommandSource sender, Mob mob) throws Exception
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
|
|
|
if (mob == null)
|
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
throw new Exception(tl("invalidMob"));
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ess.getSettings().getProtectPreventSpawn(mob.getType().toString().toLowerCase(Locale.ENGLISH)))
|
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
throw new Exception(tl("disabledToSpawnMob"));
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 19:59:39 +00:00
|
|
|
if (sender.isPlayer() && !ess.getUser(sender.getPlayer()).isAuthorized("essentials.spawnmob." + mob.name.toLowerCase(Locale.ENGLISH)))
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
throw new Exception(tl("noPermToSpawnMob"));
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 19:59:39 +00:00
|
|
|
private static void changeMobData(final CommandSource sender, final EntityType type, final Entity spawned, final String inputData, final User target) throws Exception
|
2012-11-11 18:55:02 +00:00
|
|
|
{
|
2013-07-20 20:27:47 +00:00
|
|
|
String data = inputData;
|
2012-11-11 18:55:02 +00:00
|
|
|
|
2013-11-07 02:22:32 +00:00
|
|
|
if (data.isEmpty())
|
2013-07-20 20:27:47 +00:00
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
sender.sendMessage(tl("mobDataList", StringUtil.joinList(MobData.getValidHelp(spawned))));
|
2013-07-20 20:27:47 +00:00
|
|
|
}
|
2014-07-06 17:25:00 +00:00
|
|
|
|
|
|
|
if (spawned instanceof Zombie)
|
|
|
|
{
|
|
|
|
((Zombie)spawned).setBaby(false);
|
|
|
|
}
|
|
|
|
else if(spawned instanceof Ageable)
|
|
|
|
{
|
|
|
|
((Ageable)spawned).setAdult();
|
|
|
|
}
|
2013-10-16 19:59:39 +00:00
|
|
|
|
2013-06-05 02:15:12 +00:00
|
|
|
if (spawned instanceof Zombie || type == EntityType.SKELETON)
|
2013-06-01 22:59:21 +00:00
|
|
|
{
|
2013-07-20 20:27:47 +00:00
|
|
|
if (inputData.contains("armor") || inputData.contains("armour"))
|
2013-06-01 22:59:21 +00:00
|
|
|
{
|
|
|
|
final EntityEquipment invent = ((LivingEntity)spawned).getEquipment();
|
2013-12-22 00:12:22 +00:00
|
|
|
if (inputData.contains("noarmor") || inputData.contains("noarmour"))
|
|
|
|
{
|
|
|
|
invent.clear();
|
|
|
|
}
|
|
|
|
else if (inputData.contains("diamond"))
|
2013-06-01 22:59:21 +00:00
|
|
|
{
|
|
|
|
invent.setBoots(new ItemStack(Material.DIAMOND_BOOTS, 1));
|
2013-07-20 20:27:47 +00:00
|
|
|
invent.setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS, 1));
|
|
|
|
invent.setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE, 1));
|
|
|
|
invent.setHelmet(new ItemStack(Material.DIAMOND_HELMET, 1));
|
2013-06-01 22:59:21 +00:00
|
|
|
}
|
2013-07-20 20:27:47 +00:00
|
|
|
else if (inputData.contains("gold"))
|
2013-06-01 22:59:21 +00:00
|
|
|
{
|
|
|
|
invent.setBoots(new ItemStack(Material.GOLD_BOOTS, 1));
|
2013-07-20 20:27:47 +00:00
|
|
|
invent.setLeggings(new ItemStack(Material.GOLD_LEGGINGS, 1));
|
|
|
|
invent.setChestplate(new ItemStack(Material.GOLD_CHESTPLATE, 1));
|
|
|
|
invent.setHelmet(new ItemStack(Material.GOLD_HELMET, 1));
|
2013-06-01 22:59:21 +00:00
|
|
|
}
|
2013-07-20 20:27:47 +00:00
|
|
|
else if (inputData.contains("leather"))
|
2013-06-01 22:59:21 +00:00
|
|
|
{
|
|
|
|
invent.setBoots(new ItemStack(Material.LEATHER_BOOTS, 1));
|
2013-07-20 20:27:47 +00:00
|
|
|
invent.setLeggings(new ItemStack(Material.LEATHER_LEGGINGS, 1));
|
|
|
|
invent.setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE, 1));
|
|
|
|
invent.setHelmet(new ItemStack(Material.LEATHER_HELMET, 1));
|
2013-06-01 22:59:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
invent.setBoots(new ItemStack(Material.IRON_BOOTS, 1));
|
2013-07-20 20:27:47 +00:00
|
|
|
invent.setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
|
|
|
|
invent.setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
|
|
|
|
invent.setHelmet(new ItemStack(Material.IRON_HELMET, 1));
|
2013-06-01 22:59:21 +00:00
|
|
|
}
|
|
|
|
invent.setBootsDropChance(0f);
|
|
|
|
invent.setLeggingsDropChance(0f);
|
|
|
|
invent.setChestplateDropChance(0f);
|
|
|
|
invent.setHelmetDropChance(0f);
|
|
|
|
}
|
|
|
|
|
2013-12-22 00:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-06-01 22:59:21 +00:00
|
|
|
}
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|
2013-06-01 22:33:31 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-07-06 17:25:00 +00:00
|
|
|
final PigZombie zombie = ((PigZombie)spawned);
|
|
|
|
zombie.setVillager(false);
|
|
|
|
|
|
|
|
final EntityEquipment invent = zombie.getEquipment();
|
2013-06-01 22:33:31 +00:00
|
|
|
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)
|
|
|
|
{
|
2014-07-06 17:25:00 +00:00
|
|
|
final Zombie zombie = ((Zombie)spawned);
|
|
|
|
zombie.setVillager(false);
|
|
|
|
|
|
|
|
final EntityEquipment invent = zombie.getEquipment();
|
2013-06-01 22:33:31 +00:00
|
|
|
invent.setBoots(new ItemStack(Material.GOLD_BOOTS, 1));
|
|
|
|
invent.setBootsDropChance(0.0f);
|
|
|
|
}
|
|
|
|
|
2013-07-20 20:27:47 +00:00
|
|
|
if (type == EntityType.HORSE)
|
|
|
|
{
|
|
|
|
((Horse)spawned).setJumpStrength(1.2);
|
|
|
|
}
|
2013-06-01 22:33:31 +00:00
|
|
|
}
|
2012-11-11 18:55:02 +00:00
|
|
|
}
|