Skip to content

Commit

Permalink
Rewrite Teleport command for new argument system.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Jul 14, 2013
1 parent 672cb6e commit b6dd770
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 159 deletions.
9 changes: 9 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ public LivingEntity getEntity() {
return null;
}
}

public dEntity getDenizenEntity() {
try {
return new dEntity(getCitizen().getBukkitEntity());
} catch (NullPointerException e) {
dB.log("Uh oh! Denizen has encountered a NPE while trying to fetch a NPC entity. Has this NPC been removed?");
return null;
}
}

public EntityType getEntityType() {
return getCitizen().getBukkitEntity().getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void registerCoreMembers() {
"INVISIBLE", "invisible [player/npc] [state:true/false/toggle]", 2);

registerCoreMember(ListenCommand.class,
"LISTEN", "listen (new/cancel/finish) [kill/block/item/itemdrop/travel] [<requirements>] [script:<name>] (id:<name>)", 2);
"LISTEN", "listen ({new}/cancel/finish) [kill/block/item/itemdrop/travel] [<requirements>] [script:<name>] (id:<name>)", 2);

registerCoreMember(LogCommand.class,
"LOG", "log [<text>] (type:severe/info/warning/fine/finer/finest) [file:<name>]", 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
false;

// Report to dB
dB.report(getName(), (cancel == true ? "cancel, " : "") +
dB.report(getName(), (cancel == true ? aH.debugObj("cancel", cancel) : "") +
aH.debugObj("origin", origin) +
aH.debugObj("entities", entities.toString()) +
aH.debugObj("speed", speed) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
false;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,156 +1,78 @@
package net.aufdemrand.denizen.scripts.commands.entity;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;

import java.util.ArrayList;
import java.util.List;

/**
* This command will teleport an entity to a new location.
* <br/>
* <pre>Usage: TELEPORT (NPC) [LOCATION:x,y,z,world] (TARGETS:[[NPCID|PlayerName](,)+])</pre>
* <br/>
* Examples:
* <br/><br/>
* 1) NPC wants to teleport the player to 100,100,100 in world:<br/>
* <pre>- teleport location:100,100,100,world</pre>
* <br/>
* 2) NPC wants to teleport herself to 50,50,50 in world:<br/>
* <pre>- teleport npc location:50,50,50,world</pre>
* <br/>
* 3) NPC wants to teleport NPC #456 and the player named "Dave" to 25,25,25
* in world:<br/>
* <pre>- teleport location:25,25,25,world targets:456,Dave</pre>
* <br/>
*/
public class TeleportCommand extends AbstractCommand {

public static final String NPC_ARG = "NPC";

/**
* This method will parse the arguments needed to execute the Teleport
* command from the given script entry. It verifies that the format of the
* command is accurate. If not, it will throw an InvalidArgumentException
* with the error message.
*
* @param scriptEntry The script entry processing this command.
*
* @throws InvalidArgumentsException
*/
@Override
public void parseArgs(ScriptEntry scriptEntry)
throws InvalidArgumentsException {

//
// List of entities to be teleported.
//
List<LivingEntity> teleportEntities = new ArrayList<LivingEntity> ();

//
// List of NPCs to be teleported.
//
List<NPC> teleportNPCs = new ArrayList<NPC> ();

//
// This is the location that the entity/entities are being teleported to.
//
dLocation teleportLocation = null;

//
// Process the arguments.
//
Boolean teleportPlayer = true;
for (String arg : scriptEntry.getArguments()) {
//
// Is this script attempting to teleport the NPC?
//
if (arg.equalsIgnoreCase(TeleportCommand.NPC_ARG)) {
teleportNPCs.add (scriptEntry.getNPC().getCitizen());
teleportPlayer = false;
}

// If argument is a location?
else if (aH.matchesLocation(arg))
teleportLocation = aH.getLocationFrom(arg);

else if (aH.matchesValueArg("TARGETS, TARGET", arg, ArgumentType.Custom)) {
teleportPlayer = false;
for (String target : aH.getListFrom(arg)) {
// Get entity
LivingEntity entity = dEntity.valueOf(target).getLivingEntity();
if (entity != null) {
if (CitizensAPI.getNPCRegistry().getNPC(entity) != null) {
teleportNPCs.add(CitizensAPI.getNPCRegistry().getNPC(entity));
continue;
} else if (entity instanceof Player) {
teleportEntities.add(aH.getPlayerFrom(target).getPlayerEntity());
continue;
}
}
dB.echoError("Invalid TARGET '%s'!", target);
}
}

else throw new InvalidArgumentsException(dB.Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}

//
// If we're teleporting the player, add them as a recipient.
//
if (teleportPlayer == true) {
teleportEntities.add(scriptEntry.getPlayer().getPlayerEntity());
}

// Check some required arguments, make sure they are valid
if (teleportLocation == null)
throw new InvalidArgumentsException("Missing LOCATION argument. No teleport location.");
if (teleportEntities.isEmpty() && teleportNPCs.isEmpty())
throw new InvalidArgumentsException("Missing TARGETS argument. Nothing to teleport.");

// Store objects in ScriptEntry for use in execute()
scriptEntry.addObject("location", teleportLocation)
.addObject("entities", teleportEntities)
.addObject("npcs", teleportNPCs);
}

/**
* Executes the Teleport command.
*
* @param scriptEntry the ScriptEntry
*/
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

dLocation teleportLocation = (dLocation) scriptEntry.getObject("location");
List<LivingEntity> teleportEntities = (List<LivingEntity>) scriptEntry.getObject("entities");
List<NPC> teleportNPCs = (List<NPC>) scriptEntry.getObject("npcs");

// Debug output
dB.echoApproval("<G>Executing '<Y>" + getName() + "<G>': "
+ teleportLocation.debug() + ", "
+ "Targets=<Y>'" + teleportEntities.toString() + "/" + teleportNPCs.toString() + "<G>'");

for (LivingEntity entity : teleportEntities) {
entity.teleport(teleportLocation);
}

for (NPC npc : teleportNPCs) {
npc.spawn(teleportLocation);
npc.getBukkitEntity().teleport(teleportLocation, PlayerTeleportEvent.TeleportCause.COMMAND);
}
}
}
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.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;

/**
* Teleports a list of entities to a location.
*
* @author David Cernat
*/

public class TeleportCommand extends AbstractCommand {

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

// Initialize necessary fields

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

if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class)) {
// Location arg
scriptEntry.addObject("location", arg.asType(dLocation.class));
}

else if (!scriptEntry.hasObject("entities")
&& arg.matchesPrefix("entity, entities, e, target, targets, t")) {
// Entity arg
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}

else if (arg.matches("npc")) {
// Entity arg
scriptEntry.addObject("npc", true);
}
}

// Check to make sure required arguments have been filled

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

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

dLocation location = (dLocation) scriptEntry.getObject("location");
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");

// If the "npc" argument was used, add the NPC to the list of entities,
// for compatibility with 0.8 scripts
if ((Boolean) scriptEntry.getObject("npc") == true) {
entities.add(scriptEntry.getNPC().getDenizenEntity());
}

// Report to dB
dB.report(getName(), aH.debugObj("location", location) +
aH.debugObj("entities", entities.toString()));

for (dEntity entity : entities) {

entity.teleport(location);
}
}
}

0 comments on commit b6dd770

Please sign in to comment.