From 12cb2f2a82d77eb4394014597b9c30bc0372d3c4 Mon Sep 17 00:00:00 2001 From: KHobbits Date: Sun, 11 Nov 2012 18:55:02 +0000 Subject: [PATCH] Cleanup spawnmob classes Spawnmob sign nolonger calls Command Spawnmob other, now spawns at targets feet, rather than where the target is looking Console can now spawnmob other --- .../src/com/earth2me/essentials/Mob.java | 8 +- .../src/com/earth2me/essentials/SpawnMob.java | 255 ++++++++++++++++ .../essentials/commands/Commandspawnmob.java | 276 ++---------------- .../essentials/signs/SignSpawnmob.java | 12 +- 4 files changed, 293 insertions(+), 258 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/SpawnMob.java diff --git a/Essentials/src/com/earth2me/essentials/Mob.java b/Essentials/src/com/earth2me/essentials/Mob.java index 79ac63d0d..fccef1b24 100644 --- a/Essentials/src/com/earth2me/essentials/Mob.java +++ b/Essentials/src/com/earth2me/essentials/Mob.java @@ -6,6 +6,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.Location; import org.bukkit.Server; +import org.bukkit.World; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -80,7 +81,12 @@ public enum Mob public LivingEntity spawn(final Player player, final Server server, final Location loc) throws MobException { - final LivingEntity entity = player.getWorld().spawn(loc, (Class)this.bukkitType.getEntityClass()); + return spawn(player.getWorld(), server, loc); + } + + public LivingEntity spawn(final World world, final Server server, final Location loc) throws MobException + { + final LivingEntity entity = world.spawn(loc, (Class)this.bukkitType.getEntityClass()); if (entity == null) { logger.log(Level.WARNING, _("unableToSpawnMob")); diff --git a/Essentials/src/com/earth2me/essentials/SpawnMob.java b/Essentials/src/com/earth2me/essentials/SpawnMob.java new file mode 100644 index 000000000..8ea5c9492 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/SpawnMob.java @@ -0,0 +1,255 @@ +package com.earth2me.essentials; + +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.Mob.MobException; +import java.util.HashSet; +import java.util.Locale; +import java.util.Random; +import java.util.Set; +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 +{ + public static String mobList(final User user) + { + final Set mobList = Mob.getMobList(); + final Set availableList = new HashSet(); + for (String mob : mobList) + { + if (user.isAuthorized("essentials.spawnmob." + mob.toLowerCase())) + { + 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 = mobString.split(","); + String[] mobParts = parts[0].split(":"); + + returnString[0] = mobParts[0]; + if (mobParts.length == 2) + { + returnString[1] = mobParts[1]; + } + + if (parts.length > 1) + { + String[] mountParts = parts[1].split(":"); + 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 User user, final String[] Data, int mobCount) throws Exception + { + final Block block = Util.getTarget(user).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 User target, final String[] Data, int mobCount) throws Exception + { + spawnmob(ess, server, sender, target, target.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 User target, final Location loc, final String[] Data, int mobCount) throws Exception + { + final Location sloc = Util.getSafeDestination(loc); + final String mobType = Data[0]; + final String mobData = Data[1]; + final String mountType = Data[2]; + final String mountData = Data[3]; + + Mob mob = Mob.fromName(mobType); + Mob mobMount = null; + + checkSpawnable(ess, sender, mob); + + if (mountType != null) + { + mobMount = Mob.fromName(mountType); + checkSpawnable(ess, sender, mobMount); + } + + int serverLimit = ess.getSettings().getSpawnMobLimit(); + 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.name.toLowerCase(Locale.ENGLISH) + mob.suffix + " " + _("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 User target, final Location sloc, Mob mob, String mobData, Mob mobMount, String mountData) throws Exception + { + Entity spawnedMob = mob.spawn(sloc.getWorld(), server, sloc); + Entity spawnedMount = null; + + if (mobMount != null) + { + spawnedMount = mobMount.spawn(sloc.getWorld(), server, sloc); + spawnedMob.setPassenger(spawnedMount); + } + if (mobData != null) + { + changeMobData(mob.getType(), spawnedMob, mobData, target); + } + if (spawnedMount != null && mountData != null) + { + changeMobData(mobMount.getType(), spawnedMount, mountData, target); + } + } + + private static void checkSpawnable(IEssentials ess, CommandSender sender, Mob mob) throws Exception + { + if (mob == null) + { + throw new Exception(_("invalidMob")); + } + + if (ess.getSettings().getProtectPreventSpawn(mob.getType().toString().toLowerCase(Locale.ENGLISH))) + { + throw new Exception(_("disabledToSpawnMob")); + } + + if (sender instanceof User && !((User)sender).isAuthorized("essentials.spawnmob." + mob.name.toLowerCase())) + { + throw new Exception(_("noPermToSpawnMob")); + } + } + + private static void changeMobData(final EntityType type, final Entity spawned, String data, final User 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.getBase()); + } + 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); + } + } + } + } +} diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandspawnmob.java b/Essentials/src/com/earth2me/essentials/commands/Commandspawnmob.java index 489a4ba91..5c43898a8 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandspawnmob.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandspawnmob.java @@ -2,20 +2,11 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.Mob; -import com.earth2me.essentials.Mob.MobException; +import com.earth2me.essentials.SpawnMob; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; -import java.util.HashSet; -import java.util.Locale; -import java.util.Random; -import java.util.Set; -import org.bukkit.DyeColor; -import org.bukkit.Location; import org.bukkit.Server; -import org.bukkit.block.Block; -import org.bukkit.entity.Villager.Profession; -import org.bukkit.entity.*; -import org.bukkit.material.Colorable; +import org.bukkit.command.CommandSender; public class Commandspawnmob extends EssentialsCommand @@ -30,256 +21,41 @@ public class Commandspawnmob extends EssentialsCommand { if (args.length < 1) { - final Set mobList = Mob.getMobList(); - final Set availableList = new HashSet(); - for (String mob : mobList) - { - if (user.isAuthorized("essentials.spawnmob." + mob.toLowerCase())) - { - 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 = mountparts[0].split(":"); - 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 = mountparts[1].split(":"); - mountType = parts[0]; - 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(_("invalidMob")); - } - - if (ess.getSettings().getProtectPreventSpawn(mob.getType().toString().toLowerCase(Locale.ENGLISH))) - { - throw new Exception(_("disabledToSpawnMob")); - } - if (!user.isAuthorized("essentials.spawnmob." + mob.name.toLowerCase())) - { - throw new Exception(_("noPermToSpawnMob")); - } - - final Block block = Util.getTarget(user).getBlock(); - if (block == null) - { - throw new Exception(_("unableToSpawnMob")); - } - User otherUser = null; - if (args.length >= 3) - { - otherUser = getPlayer(ess.getServer(), args, 2); - } - final Location loc = (otherUser == null) ? block.getLocation() : otherUser.getLocation(); - final Location sloc = Util.getSafeDestination(loc); - try - { - spawnedMob = mob.spawn(user, server, sloc); - } - catch (MobException e) - { - throw new Exception(_("unableToSpawnMob"), e); - } - - if (mountType != null) - { - mobMount = Mob.fromName(mountType); - if (mobMount == null) - { - user.sendMessage(_("invalidMob")); - return; - } - - if (ess.getSettings().getProtectPreventSpawn(mobMount.getType().toString().toLowerCase(Locale.ENGLISH))) - { - throw new Exception(_("disabledToSpawnMob")); - } - if (!user.isAuthorized("essentials.spawnmob." + mobMount.name.toLowerCase())) - { - throw new Exception(_("noPermToSpawnMob")); - } - try - { - spawnedMount = mobMount.spawn(user, server, loc); - } - catch (MobException e) - { - throw new Exception(_("unableToSpawnMob"), e); - } - spawnedMob.setPassenger(spawnedMount); - } - if (mobData != null) - { - changeMobData(mob.getType(), spawnedMob, mobData, user); - } - if (spawnedMount != null && mountData != null) - { - changeMobData(mobMount.getType(), spawnedMount, mountData, user); - } + int mobCount = 1; if (args.length >= 2) { - int mobCount = Integer.parseInt(args[1]); - int serverLimit = ess.getSettings().getSpawnMobLimit(); - if (mobCount > serverLimit) - { - mobCount = serverLimit; - user.sendMessage(_("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(_("unableToSpawnMob"), e); - } - spawnedMob.setPassenger(spawnedMount); - } - if (mobData != null) - { - changeMobData(mob.getType(), spawnedMob, mobData, user); - } - if (spawnedMount != null && mountData != null) - { - changeMobData(mobMount.getType(), spawnedMount, mountData, user); - } - } - user.sendMessage(mobCount + " " + mob.name.toLowerCase(Locale.ENGLISH) + mob.suffix + " " + _("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); - } + mobCount = Integer.parseInt(args[1]); } - else - { - user.sendMessage(mob.name + " " + _("spawned")); - } - } - private void changeMobData(final EntityType type, final Entity spawned, String data, final User user) throws Exception - { - data = data.toLowerCase(Locale.ENGLISH); - - if (spawned instanceof Slime) + if (args.length >= 3) { - 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(); + final User target = getPlayer(ess.getServer(), args, 2); + SpawnMob.spawnmob(ess, server, user, target, mobData, mobCount); return; } - if (spawned instanceof Colorable) + + SpawnMob.spawnmob(ess, server, user, mobData, mobCount); + } + + @Override + public void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception + { + if (args.length < 3) { - 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.getBase()); - } - 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); - } - } + final String mobList = Util.joinList(Mob.getMobList()); + throw new NotEnoughArgumentsException(_("mobsAvailable", mobList)); } + + String[] mobData = SpawnMob.mobData(args[0]); + int mobCount = Integer.parseInt(args[1]); + + final User target = getPlayer(ess.getServer(), args, 2); + SpawnMob.spawnmob(ess, server, sender, target, mobData, mobCount); } } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignSpawnmob.java b/Essentials/src/com/earth2me/essentials/signs/SignSpawnmob.java index 7c4cfcd54..23f08a414 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignSpawnmob.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignSpawnmob.java @@ -2,6 +2,7 @@ package com.earth2me.essentials.signs; import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.SpawnMob; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.commands.Commandspawnmob; @@ -27,20 +28,17 @@ public class SignSpawnmob extends EssentialsSign { final Trade charge = getTrade(sign, 3, ess); charge.isAffordableFor(player); - Commandspawnmob command = new Commandspawnmob(); - command.setEssentials(ess); - String[] args = new String[] - { - sign.getLine(2), sign.getLine(1) - }; + try { - command.run(ess.getServer(), player, "spawnmob", args); + String[] mobData = SpawnMob.mobData(sign.getLine(2)); + SpawnMob.spawnmob(ess, ess.getServer(), player, player, mobData, Integer.parseInt(sign.getLine(1))); } catch (Exception ex) { throw new SignException(ex.getMessage(), ex); } + charge.charge(player); return true; }