Skip to content

Commit

Permalink
Update Spawn command to new argument system.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Jul 10, 2013
1 parent 01bae16 commit 91e8cd4
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 74 deletions.
37 changes: 34 additions & 3 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -15,6 +15,8 @@
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_6_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftAnimals;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftCreature;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;

Expand Down Expand Up @@ -329,9 +331,9 @@ public void spawnAt(Location location) {
// air or portals, keep trying
while (data1.equals("RANDOM") &&
(material.isBlock() == false ||
material == Material.AIR ||
material == Material.PORTAL ||
material == Material.ENDER_PORTAL)) {
material == Material.AIR ||
material == Material.PORTAL ||
material == Material.ENDER_PORTAL)) {

material = dMaterial.valueOf(data1).getMaterial();
}
Expand Down Expand Up @@ -374,18 +376,32 @@ public void spawnAt(Location location) {
if (ent instanceof Creeper && data1.equalsIgnoreCase("POWERED")) {
((Creeper) entity).setPowered(true);
}
// Allow setting of blocks held by endermen
else if (ent instanceof Enderman && dMaterial.matches(data1)) {
((Enderman) entity).setCarriedMaterial(dMaterial.valueOf(data1).getMaterialData());
}
// Allow setting of ocelot types
else if (ent instanceof Ocelot) {
setSubtype(Ocelot.class, "Type", "setCatType", data1);
}
// Allow setting of skeleton types and their weapons
else if (ent instanceof Skeleton) {
setSubtype(Skeleton.class, "SkeletonType", "setSkeletonType", data1);

// Give skeletons bows by default, unless data2 specifies
// a different weapon
if (dItem.matches(data2) == false) {
data2 = "bow";
}

((Skeleton) entity).getEquipment()
.setItemInHand(dItem.valueOf(data2).getItemStack());
}
// Allow setting of slime sizes
else if (ent instanceof Slime && aH.matchesInteger(data1)) {
((Slime) entity).setSize(aH.getIntegerFrom(data1));
}
// Allow setting of villager professions
else if (ent instanceof Villager) {
setSubtype(Villager.class, "Profession", "setProfession", data1);
}
Expand Down Expand Up @@ -429,6 +445,21 @@ public dEntity rememberAs(String id) {
public void teleport(Location location) {
this.getBukkitEntity().teleport(location);
}

/**
* Make this entity target another living entity, attempting both
* old entity AI and new entity AI targeting methods
*
* @param target The LivingEntity target
*/

public void target(LivingEntity target) {

((CraftCreature) entity).getHandle().
setGoalTarget(((CraftLivingEntity) target).getHandle());

((CraftCreature) entity).setTarget(target);
}

/**
* Set the subtype of this entity by using the chosen method and Enum from
Expand Down
Expand Up @@ -266,7 +266,7 @@ public void registerCoreMembers() {
"SIT", "sit (location:x,y,z,world)", 0);

registerCoreMember(SpawnCommand.class,
"SPAWN", "spawn [entity:name] (location:<x,y,z,world>) (target:[n@#]|[p@name])", 1);
"SPAWN", "spawn [entities:<entity>|...] (location:<x,y,z,world>) (target:<entity>)", 1);

registerCoreMember(StandCommand.class,
"STAND", "stand", 0);
Expand Down
Expand Up @@ -50,7 +50,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
// Check to make sure required arguments have been filled

if ((!scriptEntry.hasObject("entities")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITY");
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES");

if (!scriptEntry.hasObject("effect") && !scriptEntry.hasObject("animation"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ANIMATION");
Expand All @@ -68,8 +68,8 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
(EntityEffect) scriptEntry.getObject("effect") : null;

// Report to dB
dB.report(getName(), animation != null ? animation.name() : effect.name()
+ aH.debugObj("entities", entities.toString()));
dB.report(getName(), (animation != null ? animation.name() : effect.name()) +
aH.debugObj("entities", entities.toString()));

// Go through all the entities and animate them
for (dEntity entity : entities) {
Expand Down
Expand Up @@ -42,7 +42,7 @@ else if (!scriptEntry.hasObject("duration")
// Check to make sure required arguments have been filled

if ((!scriptEntry.hasObject("entities")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITY");
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES");
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -55,7 +55,7 @@ else if (!scriptEntry.hasObject("entities")
if ((!scriptEntry.hasObject("entities")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES");

// Use the NPC or player's locations as the origin if one is not specified
// Use the NPC or player's locations as the location if one is not specified

if ((!scriptEntry.hasObject("location"))) {

Expand Down
Expand Up @@ -44,7 +44,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException

if (!scriptEntry.hasObject("origin")
&& arg.matchesArgumentType(dEntity.class)
&& arg.matchesPrefix("origin, o, source, s")) {
&& arg.matchesPrefix("origin, o, source, shooter, s")) {
// Entity arg
scriptEntry.addObject("origin", arg.asType(dEntity.class).setPrefix("entity"));
}
Expand Down
@@ -1,26 +1,21 @@
package net.aufdemrand.denizen.scripts.commands.entity;

import java.util.List;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;

import org.bukkit.craftbukkit.v1_6_R2.entity.CraftCreature;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Skeleton;

/**
* Spawn entities at a location.
* If no location is chosen, the entities are spawned at the NPC's location.
* Spawn entities at a location. If no location is chosen,
* the entities are spawned at the NPC or player's location.
*
* @author David Cernat
*/
Expand All @@ -29,71 +24,75 @@ public class SpawnCommand extends AbstractCommand {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

// Initialize necessary fields
EntityType entityType = null;
//Integer qty = null;
dLocation location = null;
LivingEntity target = null;

for (String arg : scriptEntry.getArguments()) {
if (aH.matchesEntityType(arg)) {
entityType = aH.getEntityTypeFrom(arg);
dB.echoDebug("...entity set to '%s'.", arg);

if (!scriptEntry.hasObject("entities")
&& arg.matchesPrefix("entity, entities, e")) {
// Entity arg
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}
else if (aH.matchesLocation(arg)) {
location = aH.getLocationFrom(arg);
dB.echoDebug("...location set to '%s'.", arg);


else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class)) {
// Location arg
scriptEntry.addObject("location", arg.asType(dLocation.class));
}
else if (aH.matchesValueArg("TARGET", arg, ArgumentType.Custom)) {
target = dEntity.valueOf(aH.getStringFrom(arg)).getLivingEntity();
dB.echoDebug("...target set to '%s' at " +
target.getLocation().getBlockX() + "," +
target.getLocation().getBlockY() + "," +
target.getLocation().getBlockZ() + "," +
target.getLocation().getWorld().getName(), arg);

else if (!scriptEntry.hasObject("target")
&& arg.matchesArgumentType(dEntity.class)
&& arg.matchesPrefix("target")) {
// Entity arg
scriptEntry.addObject("target", arg.asType(dEntity.class));
}
else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}

if (entityType == null) throw new InvalidArgumentsException(Messages.ERROR_INVALID_ENTITY);

// Stash objects
scriptEntry.addObject("entityType", entityType);
scriptEntry.addObject("location", location);
scriptEntry.addObject("target", target);
// Check to make sure required arguments have been filled

if ((!scriptEntry.hasObject("entities")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES");

// Use the NPC or player's locations as the location if one is not specified

if ((!scriptEntry.hasObject("location"))) {

if (scriptEntry.hasNPC())
scriptEntry.addObject("location", scriptEntry.getNPC().getLocation());
else if (scriptEntry.hasPlayer())
scriptEntry.addObject("location", scriptEntry.getPlayer().getLocation());
else
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "LOCATION");
}
}

@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects

final dLocation location = scriptEntry.hasObject("location") ?
(dLocation) scriptEntry.getObject("location") :
(dLocation) scriptEntry.getNPC().getLocation();

EntityType entityType = (EntityType) scriptEntry.getObject("entityType");
LivingEntity target = (LivingEntity) scriptEntry.getObject("target");

final Entity entity = location.getWorld().spawnEntity(
location, entityType);

// If target is not null and entity is a Creature, make entity go after the target
if (target != null && entity instanceof CraftCreature) {

((CraftCreature) entity).getHandle().
setGoalTarget(((CraftLivingEntity) target).getHandle());

((CraftCreature) entity).setTarget(target);
}
// Get objects
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
dLocation location = (dLocation) scriptEntry.getObject("location");
dEntity target = (dEntity) scriptEntry.getObject("target");

// Report to dB
dB.report(getName(), aH.debugObj("entities", entities.toString()) +
aH.debugObj("location", location) +
(target != null ? aH.debugObj("target", target) : ""));

// Go through all the entities and spawn them or teleport them,
// then set their targets if applicable

// If entity is a Skeleton, give it a bow
if (entity instanceof Skeleton) {
for (dEntity entity : entities) {
if (entity.isSpawned() == false) {
entity.spawnAt(location);
}
else {
entity.teleport(location);
}

((Skeleton) entity).getEquipment().setItemInHand(aH.getItemFrom("BOW").getItemStack());
}
}

if (target != null && target.isLivingEntity()) {
entity.target(target.getLivingEntity());
}
}
}
}

0 comments on commit 91e8cd4

Please sign in to comment.