/
AnchorCommand.java
121 lines (90 loc) · 4.39 KB
/
AnchorCommand.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package net.aufdemrand.denizen.scripts.commands.npc;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.trait.Anchors;
import static net.aufdemrand.denizen.objects.aH.Argument;
/**
* Controls a NPC's 'Anchors' trait.
*
* @author aufdemrand
*
*/
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 {
// Parse Arguments
for (Argument arg : aH.interpret(scriptEntry.getArguments())) {
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)
scriptEntry.addObject("range", arg.asElement());
else if (!scriptEntry.hasObject("id")
&& arg.matchesPrefix(ID_ARG))
// add anchor ID
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();
}
if (!scriptEntry.hasNPC())
throw new InvalidArgumentsException("This command requires a linked NPC!");
if (!scriptEntry.hasObject("action"))
throw new InvalidArgumentsException("Must specify an 'Anchor Action'. Valid: " + Action.values());
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
Action action = (Action) scriptEntry.getObject("action");
dLocation location = (dLocation) scriptEntry.getObject("location");
Element range = (Element) scriptEntry.getObject("range");
Element id = (Element) scriptEntry.getObject("id");
// Report to dB
dB.report(scriptEntry, getName(),
aH.debugObj("NPC", scriptEntry.getNPC().toString())
+ action.name() + id.debug()
+ (location != null ? location.debug() : "")
+ (range != null ? range.debug() : "" ));
dNPC npc = scriptEntry.getNPC();
switch (action) {
case ADD:
npc.getCitizen().getTrait(Anchors.class).addAnchor(id.asString(), location);
return;
case ASSUME:
npc.getEntity().teleport(npc.getCitizen().getTrait(Anchors.class)
.getAnchor(id.asString()).getLocation());
return;
case WALKNEAR:
npc.getNavigator().setTarget(
Utilities.getWalkableLocationNear(npc.getCitizen().getTrait(Anchors.class)
.getAnchor(id.asString()).getLocation(), range.asInt()));
return;
case WALKTO:
npc.getNavigator().setTarget(npc.getCitizen().getTrait(Anchors.class)
.getAnchor(id.asString()).getLocation());
return;
case REMOVE:
npc.getCitizen().getTrait(Anchors.class)
.removeAnchor(npc.getCitizen().getTrait(Anchors.class)
.getAnchor(id.asString()));
}
}
}