public LivingEntity spawn(Location loc, EntityType type, String specialTypes,
CommandContext args, CommandSender sender) throws CommandException {
Entity spawned = loc.getWorld().spawn(loc, type.getEntityClass());
if (!(spawned instanceof LivingEntity)) {
spawned.remove();
throw new CommandException("Not a creature!");
}
LivingEntity creature = (LivingEntity) spawned;
if (args.hasFlag('d')) {
creature.setHealth(1);
}
if (args.hasFlag('i')) {
creature.setFireTicks(20 * 25);
}
if (args.hasFlag('r')) {
creature.setVelocity(new Vector(0, 2, 0));
}
if (args.hasFlag('b')) {
if (creature instanceof Ageable) {
((Ageable) creature).setBaby();
} else if (creature instanceof Zombie) {
((Zombie) creature).setBaby(true);
}
}
if (args.hasFlag('p')) {
creature.setCanPickupItems(true);
}
/*
if (args.hasFlag('i')) {
EntityLiving.bJ() // @TODO see about having this exposed to api?
}
*/
if (args.hasFlag('t') && creature instanceof Tameable) {
if (sender instanceof AnimalTamer) {
((Tameable) creature).setOwner((AnimalTamer) sender);
} else {
((Tameable) creature).setTamed(true);
}
}
if (creature instanceof Skeleton) {
creature.getEquipment().setItemInHand(new ItemStack(Material.BOW));
} else if (creature instanceof PigZombie) {
creature.getEquipment().setItemInHand(new ItemStack(Material.GOLD_SWORD));
}
String[] types = specialTypes.split(",");
if (!specialTypes.isEmpty() && types.length > 0) {
outerloop:
for (String specialType : types) {
switch (creature.getType()) {
case WOLF:
if (specialType.matches("(?i)angry")) {
((Wolf) creature).setAngry(true);
} else if (specialType.matches("(?i)sit(ting)?")) {
((Wolf) creature).setSitting(true);
}
continue;
case OCELOT:
Ocelot.Type catType;
try {
catType = Ocelot.Type.valueOf(specialType.toUpperCase());
} catch (IllegalArgumentException e) {
throw new CommandException("Unknown cat type '" + specialType + "'. Allowed values are: "
+ valueList(Ocelot.Type.class));
}
if (catType != null) {
((Ocelot) creature).setCatType(catType);
}
break outerloop; // only one color
case CREEPER:
if (specialType.matches("(?i)(power(ed)?|electric|lightning|shock(ed)?)")) {
((Creeper) creature).setPowered(true);
}
break outerloop;
case SHEEP:
if (specialType.matches("(?i)shear(ed)?")) {
((Sheep) creature).setSheared(true);
} else {
((Sheep) creature).setColor(ItemUtil.matchDyeColor(specialType));
}
continue;
case PIG:
if (specialType.matches("(?i)saddle(d)?")) {
((Pig) creature).setSaddle(true);
}
break outerloop;
case SLIME:
((Slime) creature).setSize(Integer.parseInt(specialType));
break outerloop;
case SKELETON:
if (specialType.matches("(?i)wither")) {
((Skeleton) creature).setSkeletonType(Skeleton.SkeletonType.WITHER);
}
break outerloop;
case PIG_ZOMBIE:
if (specialType.matches("(?i)angry")) {
((PigZombie) creature).setAngry(true);
return creature;
} else {
((PigZombie) creature).setAnger(Integer.parseInt(specialType));
}
break outerloop; // having both would be redundant
case ZOMBIE:
if (specialType.matches("(?i)villager")) {
((Zombie) creature).setVillager(true);
}
break;
case ENDERMAN:
ItemStack item = ItemUtil.getItem(specialType);
if (item == null) return creature;
((Enderman) creature).setCarriedMaterial(item.getData());
break outerloop; // only one set of hands
case IRON_GOLEM:
if (specialType.matches("(?i)(friendly|player(-created)?)")) {
((IronGolem) creature).setPlayerCreated(true);
}
break outerloop;
case VILLAGER:
Villager.Profession profession;
try {
profession = Villager.Profession.valueOf(specialType.toUpperCase());
} catch (IllegalArgumentException e) {
throw new CommandException("Unknown profession '" + specialType + "'. Allowed values are: "
+ valueList(Villager.Profession.class));
}
if (profession != null) {
((Villager) creature).setProfession(profession);
}
break outerloop; // only one profession
case HORSE:
Horse.Color color = null;
Horse.Style style = null;
Horse.Variant variant = null;
try {
color = Horse.Color.valueOf(specialType.toUpperCase());
} catch (IllegalArgumentException e) {}
if (color != null) {
((Horse) creature).setColor(color);
continue;
}
try {
style = Horse.Style.valueOf(specialType.toUpperCase());
} catch (IllegalArgumentException e) {}
if (style != null) {
((Horse) creature).setStyle(style);
continue;
}
try {
variant = Horse.Variant.valueOf(specialType.toUpperCase());
} catch (IllegalArgumentException e) {}
if (variant != null) {
((Horse) creature).setVariant(variant);
continue;
}
throw new CommandException("Unknown color, style, or variant '" + specialType + "'.");
default:
break outerloop; // can't do anything with this animal, regardless of all the types given
}
}
}