Skip to content

Commit

Permalink
Working on NPC Command Update
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Dec 15, 2013
1 parent a81fd40 commit 32f6dff
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 60 deletions.
Expand Up @@ -24,9 +24,6 @@ public class AnchorCommand extends AbstractCommand {

private enum Action { ADD, REMOVE, ASSUME, WALKTO, WALKNEAR }

public static final String RANGE_ARG = "range, r";
public static final String ID_ARG = "id, i";

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

Expand All @@ -35,34 +32,30 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException

if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values()))
// add Action
scriptEntry.addObject("action", Action.valueOf(arg.getValue().toUpperCase()));


else if (!scriptEntry.hasObject("range")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)
&& arg.matchesPrefix(RANGE_ARG))
// add range (for WALKNEAR)
&& arg.matchesPrefix("range, r"))
scriptEntry.addObject("range", arg.asElement());


else if (!scriptEntry.hasObject("id")
&& arg.matchesPrefix(ID_ARG))
// add anchor ID
&& arg.matchesPrefix("id, i"))
scriptEntry.addObject("id", arg.asElement());


else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class))
// add location (for ADD)
scriptEntry.addObject("location", arg.asType(dLocation.class));

else
arg.reportUnhandled();
else arg.reportUnhandled();
}

// Check required arguments
if (!scriptEntry.hasNPC())
throw new InvalidArgumentsException("This command requires a linked NPC!");
throw new InvalidArgumentsException("NPC linked was missing or invalid.");

if (!scriptEntry.hasObject("action"))
throw new InvalidArgumentsException("Must specify an 'Anchor Action'. Valid: " + Action.values());
Expand Down
Expand Up @@ -10,72 +10,64 @@
import net.aufdemrand.denizen.utilities.debugging.dB;

/**
* TODO: Document usage
* Controls a NPC's 'Assignment' trait.
*
* @author Jeremy Schroeder
*
*/
public class AssignmentCommand extends AbstractCommand {

private enum Action {SET, REMOVE}
private enum Action { SET, REMOVE }

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Initialize fields
dScript script = null;
Action action = Action.SET;

// Parse arguments
for (String arg : scriptEntry.getArguments()) {

// If script argument, check the type -- must be 'assignment'
if (aH.matchesScript(arg)) {
script = aH.getScriptFrom(arg);
if (script != null && !script.getType().equalsIgnoreCase("assignment")) {
dB.echoError("Script type must be 'ASSIGNMENT'. Script specified is '" + script.getType() + "'.");
script = null;
}

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

if (arg.matchesEnum(Action.values())
&& !scriptEntry.hasObject("action"))
scriptEntry.addObject("action", Action.valueOf(arg.getValue().toUpperCase()));


else if (arg.matchesArgumentType(dScript.class)
&& !scriptEntry.hasObject("script")) {
// Check the type of script.. it must be an assignment-type container
if (arg.asType(dScript.class) != null
&& arg.asType(dScript.class).getType().equalsIgnoreCase("assignment"))
scriptEntry.addObject("script", arg.asType(dScript.class));
else
throw new InvalidArgumentsException("Script specified is not an 'assignment-type' container.");
}

// Get desired action
else if (aH.matchesArg("SET, REMOVE", arg))
action = Action.valueOf(arg.toUpperCase());

else
dB.echoError("Unnknown argument '" + arg + "'");
else arg.reportUnhandled();
}

// If 'SET'ting with no 'script' throws an error.
if (action == Action.SET && script == null)
throw new InvalidArgumentsException("Missing 'script' argument!");
// If no NPC attached, throw an error
// Check required arguments
if (scriptEntry.getNPC() == null)
throw new InvalidArgumentsException("This command requires a linked npc!");
throw new InvalidArgumentsException("NPC linked was missing or invalid.");

if (scriptEntry.getObject("action").equals(Action.SET) && !scriptEntry.hasObject("script"))
throw new InvalidArgumentsException("Script specified was missing or invalid.");

// Add objects that need to be passed to execute() to the scriptEntry
scriptEntry.addObject("script", script)
.addObject("action", action);
}

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Fetch objects
Action action = (Action) scriptEntry.getObject("action");
dScript script = (dScript) scriptEntry.getObject("script");

// Report to dB
dB.report(scriptEntry, getName(),
aH.debugObj("Action", action.toString())
+ (script != null ? script.debug() : "")
+ aH.debugObj("NPC", scriptEntry.getNPC().toString()));
dB.report(scriptEntry, getName(), scriptEntry.getNPC().debug()
+ scriptEntry.reportObject("action")
+ scriptEntry.reportObject("script"));

// Perform desired action
if (action == Action.SET)
if (scriptEntry.getObject("action").equals(Action.SET))
scriptEntry.getNPC().getCitizen().getTrait(AssignmentTrait.class)
.setAssignment(script.getName(), scriptEntry.getPlayer());
.setAssignment(scriptEntry.getdObjectAs("script", dScript.class).getName(),
scriptEntry.getPlayer());

else
if (action == Action.REMOVE)
else if (scriptEntry.getObject("action").equals(Action.REMOVE))
scriptEntry.getNPC().getCitizen().getTrait(AssignmentTrait.class)
.removeAssignment(scriptEntry.getPlayer());
}
Expand Down
Expand Up @@ -7,12 +7,8 @@
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;

import org.bukkit.Location;

/**
* Creates a NPC.
*
Expand All @@ -28,7 +24,8 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException

if (!scriptEntry.hasObject("entity_type")
&& arg.matchesArgumentType(dEntity.class)) {
dEntity ent = dEntity.valueOf(arg.getValue());
// Avoid duplication of objects
dEntity ent = arg.asType(dEntity.class);
if (!ent.isGeneric())
throw new InvalidArgumentsException("Entity supplied must be generic!");
scriptEntry.addObject("entity_type", ent);
Expand Down
Expand Up @@ -7,9 +7,7 @@
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.trait.LookClose;

/**
Expand Down Expand Up @@ -46,7 +44,7 @@ else if (arg.matchesArgumentType(dNPC.class))
// Only required thing is a valid NPC. This may be an already linked
// NPC, or one specified by arguments
if (scriptEntry.getNPC() == null)
throw new InvalidArgumentsException("This command requires an NPC!");
throw new InvalidArgumentsException("NPC linked was missing or invalid.");

}

Expand Down

0 comments on commit 32f6dff

Please sign in to comment.