Skip to content

Commit

Permalink
Update Pose command to 0.9, add some new options
Browse files Browse the repository at this point in the history
You can now add and remove NPC poses.
This also stopped a few NPEs, such as the player being the target and it
never checking if an NPC exists... When an NPC HAS to exist.
  • Loading branch information
Morphan1 committed Dec 11, 2013
1 parent a90ff79 commit 9bd312a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 37 deletions.
Expand Up @@ -1770,20 +1770,36 @@ public void registerCoreMembers() {

// <--[command]
// @Name Pose
// @Syntax pose (player/npc) [id:<name>]
// @Syntax pose (add/remove/{assume}) [id:<name>] (player/{npc}) (<location>)
// @Required 1
// @Stable unstable
// @Short Rotates the player or NPC to match a pose.
// @Stable stable
// @Short Rotates the player or NPC to match a pose, or adds/removes an NPC's poses.
// @Author aufdemrand
//
// @Description
// Todo
// Makes a player or NPC assume the position of a pose saved on an NPC, removes a
// pose with a specified ID from the current linked NPC, or adds a pose to the NPC
// with an ID and a location, although the only thing that matters in the location
// is the pitch and yaw.
//
// @Tags
// Todo
// <n@npc.has_pose[<name>]>
// <n@npc.get_pose[<name>]>
//
// @Usage
// Todo
// Make an NPC assume a pose.
// - pose id:MyPose1
//
// @Usage
// Add a pose to an NPC. (Note that only the last 2 numbers matter)
// - pose add id:MyPose2 l@0,0,0,-2.3,5.4,world
//
// @Usage
// Remove a pose from an NPC.
// - pose remove id:MyPose1
// -->
registerCoreMember(PoseCommand.class,
"POSE", "pose (player/npc) [id:<name>]", 1);
"POSE", "pose (add/remove/{assume}) [id:<name>] (player/{npc}) (<location>)", 1);


// <--[command]
Expand Down
Expand Up @@ -5,6 +5,7 @@

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
Expand All @@ -29,71 +30,105 @@ private enum Action { ADD, REMOVE, ASSUME}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

Action action = Action.ASSUME;
TargetType targetType = TargetType.NPC;
String id = null;

// Parse Arguments
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesArg("ADD, ASSUME, REMOVE", arg)) {
action = Action.valueOf(aH.getStringFrom(arg).toUpperCase());
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (arg.matches("add, assume, remove")) {
scriptEntry.addObject("action", Action.valueOf(arg.getValue().toUpperCase()));
}

} else if (aH.matchesValueArg("ID", arg, aH.ArgumentType.String)) {
id = aH.getStringFrom(arg);
else if (arg.matchesPrefix("id")) {
scriptEntry.addObject("pose_id", arg.getValue());
}

} else if (aH.matchesArg("PLAYER", arg)) {
targetType = TargetType.PLAYER;
dB.echoDebug(scriptEntry, "Setting pose on PLAYER!");
else if (arg.matches("player")) {
scriptEntry.addObject("target", TargetType.PLAYER);
}

} else
dB.echoError("Unknown argument '" + arg + "'");
else if (arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("pose_loc", arg.asType(dLocation.class));
}

}

// If TARGET is NPC/PLAYER and no NPC/PLAYER available, throw exception.
if (targetType == TargetType.PLAYER && scriptEntry.getPlayer() == null)
throw new InvalidArgumentsException("This command requires a linked player!");
else if (targetType == TargetType.NPC && scriptEntry.getNPC() == null)
// Even if the target is a player, this command requires an NPC to get the pose from.
if (!scriptEntry.hasNPC())
throw new InvalidArgumentsException("This command requires a linked NPC!");
scriptEntry.addObject("target", targetType)
.addObject("action", action).addObject("id", id);

// It also requires a pose ID
if (!scriptEntry.hasObject("id"))
throw new InvalidArgumentsException("No ID specified!");

// Set default objects
scriptEntry.defaultObject("target", TargetType.NPC);
scriptEntry.defaultObject("action", Action.ASSUME);

// If the target is a player, it needs a player! However, you can't ADD/REMOVE poses
// from players, so only allow ASSUME.
if (scriptEntry.getObject("target") == TargetType.PLAYER) {
if (scriptEntry.getObject("action") != Action.ASSUME)
throw new InvalidArgumentsException("You cannot add or remove poses from a player.");
else if (!scriptEntry.hasPlayer())
throw new InvalidArgumentsException("This command requires a linked player!");
}

}

@SuppressWarnings("incomplete-switch")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects

// Get objects
TargetType target = (TargetType) scriptEntry.getObject("target");
dNPC npc = scriptEntry.getNPC();
Action action = (Action) scriptEntry.getObject("action");
String id = (String) scriptEntry.getObject("id");
String id = (String) scriptEntry.getObject("pose_id");
dLocation pose_loc = (dLocation) scriptEntry.getObject("pose_loc");

// Report to dB
dB.report(scriptEntry, getName(),
aH.debugObj(target.toString(), npc.toString())
aH.debugObj("Target", target.toString())
+ (target == TargetType.PLAYER ? scriptEntry.getPlayer().debug() : "")
+ npc.debug()
+ aH.debugObj("Action", action.toString())
+ aH.debugObj("Id", id));
+ aH.debugObj("Id", id)
+ (pose_loc != null ? pose_loc.debug() : ""));

if (!npc.getCitizen().hasTrait(Poses.class))
npc.getCitizen().addTrait(Poses.class);

Poses poses = npc.getCitizen().getTrait(Poses.class);

switch (action) {

case ASSUME:
if (!poses.hasPose(id))
throw new CommandExecutionException("Pose \"" + id + "\" doesn't exist for " + npc.toString());

if (target.name().equals("NPC"))
npc.getCitizen().getTrait(Poses.class).assumePose(id);
poses.assumePose(id);
else {
Player player = scriptEntry.getPlayer().getPlayerEntity();
Location location = player.getLocation();
location.setYaw(npc.getCitizen().getTrait(Poses.class).getPose(id).getYaw());
location.setPitch(npc.getCitizen().getTrait(Poses.class).getPose(id).getPitch());
location.setYaw(poses.getPose(id).getYaw());
location.setPitch(poses.getPose(id).getPitch());

// The only way to change a player's yaw and pitch in Bukkit
// is to use teleport on him/her
player.teleport(location);
}
}
break;

// TODO: ADD ADD/REMOVE
case ADD:
if (!poses.addPose(id, pose_loc))
throw new CommandExecutionException(npc.toString() + " already has that pose!");
break;

case REMOVE:
if (!poses.removePose(id))
throw new CommandExecutionException(npc.toString() + " does not have that pose!");
break;

}

}
}

0 comments on commit 9bd312a

Please sign in to comment.