TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commandspawnmob.java

257 lines
6.1 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Mob;
import com.earth2me.essentials.Mob.MobException;
2011-11-18 17:42:26 +00:00
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import java.util.Locale;
2011-07-28 00:15:47 +00:00
import java.util.Random;
import org.bukkit.DyeColor;
2011-11-18 17:42:26 +00:00
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.block.Block;
2011-11-18 17:42:26 +00:00
import org.bukkit.entity.*;
public class Commandspawnmob extends EssentialsCommand
{
public Commandspawnmob()
{
super("spawnmob");
}
2011-11-25 04:54:32 +00:00
@Override
2011-12-01 13:42:39 +00:00
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
if (args.length < 1)
{
throw new NotEnoughArgumentsException(_("mobsAvailable", Util.joinList(Mob.getMobList())));
}
2011-11-25 04:54:32 +00:00
2011-12-01 13:42:39 +00:00
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];
}
}
2011-11-25 04:54:32 +00:00
Entity spawnedMob = null;
Mob mob = null;
Entity spawnedMount = null;
Mob mobMount = null;
2011-11-25 04:54:32 +00:00
mob = Mob.fromName(mobType);
if (mob == null)
{
throw new Exception(_("invalidMob"));
}
2011-11-25 04:54:32 +00:00
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"));
}
2011-11-25 04:54:32 +00:00
2011-11-18 18:33:22 +00:00
final Block block = Util.getTarget(user).getBlock();
2011-11-20 13:33:17 +00:00
if (block == null)
2011-09-17 17:50:57 +00:00
{
throw new Exception(_("unableToSpawnMob"));
}
2011-12-01 13:42:39 +00:00
final Location loc = block.getLocation();
final Location sloc = Util.getSafeDestination(loc);
2011-06-27 09:46:57 +00:00
try
{
spawnedMob = mob.spawn(user, server, sloc);
2011-06-27 09:46:57 +00:00
}
catch (MobException e)
{
throw new Exception(_("unableToSpawnMob"));
2011-06-27 09:46:57 +00:00
}
2011-11-25 04:54:32 +00:00
if (mountType != null)
{
mobMount = Mob.fromName(mountType);
if (mobMount == null)
{
user.sendMessage(_("invalidMob"));
return;
}
2011-11-25 04:54:32 +00:00
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
{
2011-06-27 09:46:57 +00:00
spawnedMount = mobMount.spawn(user, server, loc);
}
catch (MobException e)
{
throw new Exception(_("unableToSpawnMob"));
}
spawnedMob.setPassenger(spawnedMount);
}
if (mobData != null)
{
changeMobData(mob.getType(), spawnedMob, mobData, user);
}
if (spawnedMount != null && mountData != null)
{
changeMobData(mobMount.getType(), spawnedMount, mountData, user);
}
if (args.length == 2)
{
int mobCount = Integer.parseInt(args[1]);
int serverLimit = ess.getSettings().getSpawnMobLimit();
if (mobCount > serverLimit)
{
mobCount = serverLimit;
user.sendMessage(_("mobSpawnLimit"));
}
2011-11-25 04:54:32 +00:00
try
{
for (int i = 1; i < mobCount; i++)
{
2011-06-27 09:46:57 +00:00
spawnedMob = mob.spawn(user, server, loc);
if (mobMount != null)
{
try
{
2011-06-27 09:46:57 +00:00
spawnedMount = mobMount.spawn(user, server, loc);
}
catch (MobException e)
{
throw new Exception(_("unableToSpawnMob"));
}
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(args[1] + " " + 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);
}
}
else
{
user.sendMessage(mob.name + " " + _("spawned"));
}
}
2011-11-25 04:54:32 +00:00
2011-12-01 13:42:39 +00:00
private void changeMobData(final CreatureType type, final Entity spawned, final String data, final User user) throws Exception
{
if (type == CreatureType.SLIME || type == CreatureType.MAGMA_CUBE)
{
try
{
((Slime)spawned).setSize(Integer.parseInt(data));
}
catch (Exception e)
{
throw new Exception(_("slimeMalformedSize"), e);
}
}
if ((type == CreatureType.SHEEP
|| type == CreatureType.COW
2011-11-30 03:44:33 +00:00
|| type == CreatureType.MUSHROOM_COW
|| type == CreatureType.CHICKEN
|| type == CreatureType.PIG
|| type == CreatureType.WOLF)
&& data.equalsIgnoreCase("baby"))
{
((Animals)spawned).setAge(-24000);
return;
}
if (type == CreatureType.SHEEP)
{
if (data.toLowerCase(Locale.ENGLISH).contains("baby")) {
((Sheep)spawned).setAge(-24000);
}
final String color = data.toUpperCase(Locale.ENGLISH).replace("BABY", "");
try
{
if (color.equalsIgnoreCase("random"))
2011-07-28 00:15:47 +00:00
{
Random rand = new Random();
((Sheep)spawned).setColor(DyeColor.values()[rand.nextInt(DyeColor.values().length)]);
2011-07-28 00:15:47 +00:00
}
else
{
((Sheep)spawned).setColor(DyeColor.valueOf(color));
2011-07-28 00:15:47 +00:00
}
}
catch (Exception e)
{
throw new Exception(_("sheepMalformedColor"), e);
}
}
if (type == CreatureType.WOLF
2011-11-25 05:04:19 +00:00
&& data.toLowerCase(Locale.ENGLISH).startsWith("tamed"))
{
2011-11-18 19:30:05 +00:00
final Wolf wolf = ((Wolf)spawned);
2011-06-28 22:32:30 +00:00
wolf.setTamed(true);
wolf.setOwner(user);
wolf.setSitting(true);
2011-11-25 05:04:19 +00:00
if (data.equalsIgnoreCase("tamedbaby"))
{
((Animals)spawned).setAge(-24000);
}
}
if (type == CreatureType.WOLF
2011-11-25 05:04:19 +00:00
&& data.toLowerCase(Locale.ENGLISH).startsWith("angry"))
{
((Wolf)spawned).setAngry(true);
2011-11-25 05:04:19 +00:00
if (data.equalsIgnoreCase("angrybaby"))
{
((Animals)spawned).setAge(-24000);
}
}
if (type == CreatureType.CREEPER && data.equalsIgnoreCase("powered"))
{
((Creeper)spawned).setPowered(true);
}
}
}