mirror of
https://github.com/essentials/Essentials.git
synced 2025-08-13 10:04:51 +02:00
Spawnmob cleanup
This commit is contained in:
265
Essentials/src/net/ess3/SpawnMob.java
Normal file
265
Essentials/src/net/ess3/SpawnMob.java
Normal file
@@ -0,0 +1,265 @@
|
||||
package net.ess3;
|
||||
|
||||
|
||||
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 java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.*;
|
||||
import org.bukkit.material.Colorable;
|
||||
|
||||
|
||||
public class SpawnMob
|
||||
{
|
||||
private static transient Pattern colon = Pattern.compile(":");
|
||||
private static transient Pattern comma = Pattern.compile(",");
|
||||
|
||||
public static String mobList(final IUser user) throws NotEnoughArgumentsException
|
||||
{
|
||||
final Set<String> mobList = LivingEntities.getLivingEntityList();
|
||||
final Set<String> availableList = new HashSet<String>();
|
||||
for (String mob : mobList)
|
||||
{
|
||||
if (Permissions.SPAWNMOB.isAuthorized(user, mob))
|
||||
{
|
||||
availableList.add(mob);
|
||||
}
|
||||
}
|
||||
if (availableList.isEmpty())
|
||||
{
|
||||
availableList.add(_("none"));
|
||||
}
|
||||
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
|
||||
{
|
||||
Entity spawnedMob = sloc.getWorld().spawn(sloc, (Class<? extends LivingEntity>)mob.getEntityClass());
|
||||
Entity spawnedMount = null;
|
||||
|
||||
if (mobMount != null)
|
||||
{
|
||||
spawnedMount = sloc.getWorld().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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,281 +1,36 @@
|
||||
package net.ess3.commands;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import static net.ess3.I18n._;
|
||||
import net.ess3.api.ISettings;
|
||||
import net.ess3.SpawnMob;
|
||||
import net.ess3.api.IUser;
|
||||
import net.ess3.bukkit.LivingEntities;
|
||||
import net.ess3.bukkit.LivingEntities.MobException;
|
||||
import net.ess3.permissions.Permissions;
|
||||
import net.ess3.utils.LocationUtil;
|
||||
import net.ess3.utils.Util;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.*;
|
||||
import org.bukkit.entity.Villager.Profession;
|
||||
import org.bukkit.material.Colorable;
|
||||
|
||||
|
||||
public class Commandspawnmob extends EssentialsCommand
|
||||
{
|
||||
private final transient Pattern colon = Pattern.compile(":");
|
||||
|
||||
@Override
|
||||
public void run(final IUser user, final String commandLabel, final String[] args) throws Exception
|
||||
{
|
||||
if (args.length < 1)
|
||||
{
|
||||
final Set<String> mobList = LivingEntities.getLivingEntityList();
|
||||
final Set<String> availableList = new HashSet<String>();
|
||||
for (String mob : mobList)
|
||||
{
|
||||
if (Permissions.SPAWNMOB.isAuthorized(user, mob))
|
||||
{
|
||||
availableList.add(mob);
|
||||
}
|
||||
}
|
||||
if (availableList.isEmpty())
|
||||
{
|
||||
availableList.add(_("none"));
|
||||
}
|
||||
throw new NotEnoughArgumentsException(_("mobsAvailable", Util.joinList(availableList)));
|
||||
final String mobList = SpawnMob.mobList(user);
|
||||
throw new NotEnoughArgumentsException(_("mobsAvailable", mobList));
|
||||
}
|
||||
|
||||
String[] mobData = SpawnMob.mobData(args[0]);
|
||||
|
||||
final String[] mountparts = args[0].split(",");
|
||||
String[] parts = colon.split(mountparts[0]);
|
||||
String mobType = parts[0];
|
||||
String mobData = null;
|
||||
if (parts.length == 2)
|
||||
{
|
||||
mobData = parts[1];
|
||||
}
|
||||
String mountType = null;
|
||||
String mountData = null;
|
||||
if (mountparts.length > 1)
|
||||
{
|
||||
parts = colon.split(mountparts[1]);
|
||||
mountType = parts[0];
|
||||
if (parts.length == 2)
|
||||
{
|
||||
mountData = parts[1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Entity spawnedMob = null;
|
||||
EntityType mob = null;
|
||||
Entity spawnedMount = null;
|
||||
EntityType mobMount = null;
|
||||
|
||||
mob = LivingEntities.fromName(mobType);
|
||||
if (mob == null)
|
||||
{
|
||||
throw new Exception(_("invalidMob"));
|
||||
}
|
||||
|
||||
if (!Permissions.SPAWNMOB.isAuthorized(user, mob.getName()))
|
||||
{
|
||||
throw new Exception(_("noPermToSpawnMob"));
|
||||
}
|
||||
|
||||
final Block block = LocationUtil.getTarget(user.getPlayer()).getBlock();
|
||||
if (block == null)
|
||||
{
|
||||
throw new Exception(_("unableToSpawnMob"));
|
||||
}
|
||||
IUser otherUser = null;
|
||||
if (args.length >= 3)
|
||||
{
|
||||
otherUser = ess.getUserMap().getUser(args[2]);
|
||||
}
|
||||
final Location loc = (otherUser == null) ? block.getLocation() : otherUser.getPlayer().getLocation();
|
||||
final Location sloc = LocationUtil.getSafeDestination(loc);
|
||||
try
|
||||
{
|
||||
spawnedMob = user.getPlayer().getWorld().spawn(sloc, (Class<? extends LivingEntity>)mob.getEntityClass());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception(_("unableToSpawnMob"), e);
|
||||
}
|
||||
|
||||
if (mountType != null)
|
||||
{
|
||||
mobMount = LivingEntities.fromName(mountType);
|
||||
if (mobMount == null)
|
||||
{
|
||||
user.sendMessage(_("invalidMob"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Permissions.SPAWNMOB.isAuthorized(user, mobMount.getName()))
|
||||
{
|
||||
throw new Exception(_("noPermToSpawnMob"));
|
||||
}
|
||||
try
|
||||
{
|
||||
spawnedMount = user.getPlayer().getWorld().spawn(loc, (Class<? extends LivingEntity>)mobMount.getEntityClass());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception(_("unableToSpawnMob"), e);
|
||||
}
|
||||
spawnedMob.setPassenger(spawnedMount);
|
||||
}
|
||||
if (mobData != null)
|
||||
{
|
||||
changeMobData(mob, spawnedMob, mobData, user);
|
||||
}
|
||||
if (spawnedMount != null && mountData != null)
|
||||
{
|
||||
changeMobData(mobMount, spawnedMount, mountData, user);
|
||||
}
|
||||
int mobCount = 1;
|
||||
if (args.length >= 2)
|
||||
{
|
||||
int mobCount = Integer.parseInt(args[1]);
|
||||
mobCount = Integer.parseInt(args[1]);
|
||||
}
|
||||
|
||||
ISettings settings = ess.getSettings();
|
||||
if (args.length >= 3)
|
||||
{
|
||||
IUser target = ess.getUserMap().getUser(args[2]);
|
||||
SpawnMob.spawnmob(ess, server, user, target, mobData, mobCount);
|
||||
}
|
||||
|
||||
int serverLimit = settings.getData().getCommands().getSpawnmob().getLimit();
|
||||
|
||||
if (mobCount > serverLimit)
|
||||
{
|
||||
mobCount = serverLimit;
|
||||
user.sendMessage(_("mobSpawnLimit"));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 1; i < mobCount; i++)
|
||||
{
|
||||
spawnedMob = user.getPlayer().getWorld().spawn(sloc, (Class<? extends LivingEntity>)mob.getEntityClass());
|
||||
if (mobMount != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
spawnedMount = user.getPlayer().getWorld().spawn(loc, (Class<? extends LivingEntity>)mobMount.getEntityClass());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception(_("unableToSpawnMob"), e);
|
||||
}
|
||||
spawnedMob.setPassenger(spawnedMount);
|
||||
}
|
||||
if (mobData != null)
|
||||
{
|
||||
changeMobData(mob, spawnedMob, mobData, user);
|
||||
}
|
||||
if (spawnedMount != null && mountData != null)
|
||||
{
|
||||
changeMobData(mobMount, spawnedMount, mountData, user);
|
||||
}
|
||||
}
|
||||
user.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);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
user.sendMessage(mob.getName() + " " + _("spawned"));
|
||||
}
|
||||
}
|
||||
|
||||
private void changeMobData(final EntityType type, final Entity spawned, String data, final IUser user) 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"))
|
||||
{
|
||||
final Tameable tameable = ((Tameable)spawned);
|
||||
tameable.setTamed(true);
|
||||
tameable.setOwner(user.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 (Profession prof : Villager.Profession.values())
|
||||
{
|
||||
if (data.contains(prof.toString().toLowerCase(Locale.ENGLISH)))
|
||||
{
|
||||
((Villager)spawned).setProfession(prof);
|
||||
}
|
||||
}
|
||||
}
|
||||
SpawnMob.spawnmob(ess, server, user, mobData, mobCount);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user