mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2024-10-05 16:24:47 +00:00
9c96ad9065
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1114 e251c2fe-e539-e718-e476-b85c1f46cddb
231 lines
5.9 KiB
Java
231 lines
5.9 KiB
Java
package com.earth2me.essentials.commands;
|
|
|
|
import net.minecraft.server.WorldServer;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.Server;
|
|
import org.bukkit.craftbukkit.entity.CraftEntity;
|
|
import com.earth2me.essentials.Essentials;
|
|
import com.earth2me.essentials.User;
|
|
import com.earth2me.essentials.Mob;
|
|
import com.earth2me.essentials.Mob.MobException;
|
|
import com.earth2me.essentials.TargetBlock;
|
|
import net.minecraft.server.EntityWolf;
|
|
import net.minecraft.server.PathEntity;
|
|
import org.bukkit.DyeColor;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.block.Block;
|
|
import org.bukkit.craftbukkit.CraftWorld;
|
|
import org.bukkit.craftbukkit.entity.CraftSheep;
|
|
import org.bukkit.craftbukkit.entity.CraftSlime;
|
|
import org.bukkit.craftbukkit.entity.CraftWolf;
|
|
|
|
|
|
public class Commandspawnmob extends EssentialsCommand
|
|
{
|
|
public Commandspawnmob()
|
|
{
|
|
super("spawnmob");
|
|
}
|
|
|
|
@Override
|
|
public String[] getTriggers()
|
|
{
|
|
return new String[]
|
|
{
|
|
getName(), "mob"
|
|
};
|
|
}
|
|
|
|
@Override
|
|
public void run(Server server, Essentials parent, User user, String commandLabel, String[] args) throws Exception
|
|
{
|
|
if (args.length < 1)
|
|
{
|
|
user.sendMessage("§cUsage: /spawnmob [mob]<:data><,mount<:data>> <quantity>");
|
|
user.sendMessage("§7Mobs: Zombie PigZombie Skeleton Slime Chicken Pig Monster Spider Creeper Ghast Squid Giant Cow Sheep Wolf");
|
|
return;
|
|
}
|
|
|
|
|
|
String[] mountparts = args[0].split(",");
|
|
String[] parts = mountparts[0].split(":");
|
|
String mobType = parts[0];
|
|
mobType = mobType.equalsIgnoreCase("PigZombie") ? "PigZombie" : capitalCase(mobType);
|
|
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];
|
|
mountType = mountType.equalsIgnoreCase("PigZombie") ? "PigZombie" : capitalCase(mountType);
|
|
if (parts.length == 2) {
|
|
mountData = parts[1];
|
|
}
|
|
}
|
|
|
|
|
|
CraftEntity spawnedMob = null;
|
|
Mob mob = null;
|
|
CraftEntity spawnedMount = null;
|
|
Mob mobMount = null;
|
|
|
|
mob = Mob.fromName(mobType);
|
|
if (mob == null)
|
|
{
|
|
user.sendMessage("Invalid mob type.");
|
|
return;
|
|
}
|
|
user.charge(this);
|
|
WorldServer world = ((CraftWorld)user.getWorld()).getHandle();
|
|
try
|
|
{
|
|
spawnedMob = mob.spawn(user, server);
|
|
}
|
|
catch (MobException e)
|
|
{
|
|
user.sendMessage("Unable to spawn mob.");
|
|
return;
|
|
}
|
|
int[] ignore = {8, 9};
|
|
Location loc = (new TargetBlock(user, 300, 0.2, ignore)).getTargetBlock().getLocation();
|
|
|
|
Block block = user.getWorld().getBlockAt(loc);
|
|
while (!(block.getType() == Material.AIR || block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER ))
|
|
{
|
|
loc.setY(loc.getY() + 1);
|
|
block = user.getWorld().getBlockAt(loc);
|
|
}
|
|
spawnedMob.teleportTo(loc);
|
|
world.a(spawnedMob.getHandle());
|
|
|
|
if (mountType != null)
|
|
{
|
|
mobMount = Mob.fromName(mountType);
|
|
if (mobMount == null)
|
|
{
|
|
user.sendMessage("Invalid mob type.");
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
spawnedMount = mobMount.spawn(user, server);
|
|
}
|
|
catch (MobException e)
|
|
{
|
|
user.sendMessage("Unable to spawn mob.");
|
|
return;
|
|
}
|
|
spawnedMount.teleportTo(spawnedMob);
|
|
spawnedMount.getHandle().setPassengerOf(spawnedMob.getHandle());
|
|
world.a(spawnedMount.getHandle());
|
|
}
|
|
if (mobData != null) {
|
|
changeMobData(mob.name, spawnedMob, mobData, user);
|
|
}
|
|
if (spawnedMount != null && mountData != null) {
|
|
changeMobData(mobMount.name, spawnedMount, mountData, user);
|
|
}
|
|
if (args.length == 2)
|
|
{
|
|
int mobCount = Integer.parseInt(args[1]);
|
|
int serverLimit = Essentials.getSettings().getSpawnMobLimit();
|
|
if(mobCount > serverLimit)
|
|
{
|
|
mobCount = serverLimit;
|
|
user.sendMessage("Mob quantity limited to server limit");
|
|
}
|
|
|
|
try
|
|
{
|
|
for (int i = 1; i < mobCount; i++)
|
|
{
|
|
spawnedMob = mob.spawn(user, server);
|
|
spawnedMob.teleportTo(loc);
|
|
world.a(spawnedMob.getHandle());
|
|
if (mobMount != null)
|
|
{
|
|
try
|
|
{
|
|
spawnedMount = mobMount.spawn(user, server);
|
|
}
|
|
catch (MobException e)
|
|
{
|
|
user.sendMessage("Unable to spawn mob.");
|
|
return;
|
|
}
|
|
spawnedMount.teleportTo(spawnedMob);
|
|
spawnedMount.getHandle().setPassengerOf(spawnedMob.getHandle());
|
|
world.a(spawnedMount.getHandle());
|
|
}
|
|
if (mobData != null) {
|
|
changeMobData(mob.name, spawnedMob, mobData, user);
|
|
}
|
|
if (spawnedMount != null && mountData != null) {
|
|
changeMobData(mobMount.name, spawnedMount, mountData, user);
|
|
}
|
|
}
|
|
user.sendMessage(args[1] + " " + mob.name.toLowerCase() + mob.s + " spawned.");
|
|
}
|
|
catch (MobException e1)
|
|
{
|
|
throw new Exception("Unable to spawn mobs. Insert bad excuse here.");
|
|
}
|
|
catch (NumberFormatException e2)
|
|
{
|
|
throw new Exception("A number goes there, silly.");
|
|
}
|
|
catch (NullPointerException np)
|
|
{
|
|
throw new Exception("That mob likes to be alone");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
user.sendMessage(mob.name + " spawned.");
|
|
}
|
|
}
|
|
|
|
private String capitalCase(String s)
|
|
{
|
|
return s.toUpperCase().charAt(0) + s.toLowerCase().substring(1);
|
|
}
|
|
|
|
private void changeMobData(String type, CraftEntity spawned, String data, User user) throws Exception {
|
|
if ("Slime".equalsIgnoreCase(type)) {
|
|
try
|
|
{
|
|
((CraftSlime)spawned).setSize(Integer.parseInt(data));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new Exception("Malformed size.");
|
|
}
|
|
}
|
|
if ("Sheep".equalsIgnoreCase(type)) {
|
|
try
|
|
{
|
|
((CraftSheep)spawned).setColor(DyeColor.valueOf(data.toUpperCase()));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new Exception("Malformed color.");
|
|
}
|
|
}
|
|
if ("Wolf".equalsIgnoreCase(type) && data.equalsIgnoreCase("tamed")) {
|
|
EntityWolf wolf = ((CraftWolf) spawned).getHandle();
|
|
wolf.d(true);
|
|
wolf.a((PathEntity) null);
|
|
wolf.b(true);
|
|
wolf.health = 20;
|
|
wolf.a(user.getName());
|
|
wolf.world.a(wolf, (byte) 7);
|
|
}
|
|
if ("Wolf".equalsIgnoreCase(type) && data.equalsIgnoreCase("angry")) {
|
|
((CraftWolf)spawned).setAngry(true);
|
|
}
|
|
}
|
|
}
|