/
AssignmentCommand.java
75 lines (57 loc) · 2.96 KB
/
AssignmentCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package net.aufdemrand.denizen.scripts.commands.npc;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.npc.traits.AssignmentTrait;
import net.aufdemrand.denizen.objects.dScript;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
/**
* Controls a NPC's 'Assignment' trait.
*
* @author Jeremy Schroeder
*
*/
public class AssignmentCommand extends AbstractCommand {
private enum Action { SET, REMOVE }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// 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.");
}
else arg.reportUnhandled();
}
// Check required arguments
if (scriptEntry.getNPC() == null)
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.");
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Report to dB
dB.report(scriptEntry, getName(), scriptEntry.getNPC().debug()
+ scriptEntry.reportObject("action")
+ scriptEntry.reportObject("script"));
// Perform desired action
if (scriptEntry.getObject("action").equals(Action.SET))
scriptEntry.getNPC().getCitizen().getTrait(AssignmentTrait.class)
.setAssignment(scriptEntry.getdObjectAs("script", dScript.class).getName(),
scriptEntry.getPlayer());
else if (scriptEntry.getObject("action").equals(Action.REMOVE))
scriptEntry.getNPC().getCitizen().getTrait(AssignmentTrait.class)
.removeAssignment(scriptEntry.getPlayer());
}
}