/
AnchorCommand.java
176 lines (154 loc) · 7.05 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.denizenscript.denizen.scripts.commands.npc;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.util.Anchor;
import java.util.Arrays;
public class AnchorCommand extends AbstractCommand {
// <--[command]
// @Name Anchor
// @Syntax anchor [id:<name>] [assume/remove/add <location>/walkto/walknear (r:#)]
// @Required 2
// @Short Controls an NPC's Anchor Trait.
// @Group npc
//
// @Description
// The anchor system inside Citizens2 allows locations to be 'bound' to an NPC, saved by an 'id'. The anchor
// command can add and remove new anchors, as well as the ability to teleport NPCs to anchors with the 'assume'
// argument.
// The Anchors Trait can also be used as a sort of 'waypoints' system. For ease of use, the anchor command
// provides function for NPCs to walk to or walk near an anchor.
// As the Anchor command is an NPC specific command, a valid npc object must be referenced in the script entry.
// If none is provided by default, the use of the 'npc:n@id' argument, replacing the id with the npcid of the
// NPC desired, can create a link, or alternatively override the default linked npc.
//
// @Tags
// <NPCTag.anchor[anchor_name]>
// <NPCTag.list_anchors>
// <NPCTag.has_anchors>
//
// @Usage
// Use to add and remove anchors to an NPC.
// - define location_name <context.message>
// - chat "I have saved this location as <[location_name]>.'
// - anchor add <npc.location> id:<[location_name]>
//
// @Usage
// Use to make an NPC walk to or walk near a saved anchor.
// - anchor walkto i:waypoint_1
// - anchor walknear i:waypoint_2 r:5
// -->
private enum Action {ADD, REMOVE, ASSUME, WALKTO, WALKNEAR}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Parse Arguments
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values())) {
scriptEntry.addObject("action", Action.valueOf(arg.getValue().toUpperCase()));
}
else if (!scriptEntry.hasObject("range")
&& arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Double)
&& arg.matchesPrefix("range", "r")) {
scriptEntry.addObject("range", arg.asElement());
}
else if (!scriptEntry.hasObject("id")
&& arg.matchesPrefix("id", "i")) {
scriptEntry.addObject("id", arg.asElement());
}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else {
arg.reportUnhandled();
}
}
// Check required arguments
if (!Utilities.entryHasNPC(scriptEntry)) {
throw new InvalidArgumentsException("NPC linked was missing or invalid.");
}
if (!scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Must specify an 'Anchor Action'. Valid: " + Arrays.asList(Action.values()));
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
// Get objects
Action action = (Action) scriptEntry.getObject("action");
LocationTag location = (LocationTag) scriptEntry.getObject("location");
ElementTag range = (ElementTag) scriptEntry.getObject("range");
ElementTag id = (ElementTag) scriptEntry.getObject("id");
NPCTag npc = Utilities.getEntryNPC(scriptEntry);
// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
npc.debug() + action.name() + id.debug()
+ (location != null ? location.debug() : "")
+ (range != null ? range.debug() : ""));
}
if (!npc.getCitizen().hasTrait(Anchors.class)) {
npc.getCitizen().addTrait(Anchors.class);
}
switch (action) {
case ADD:
npc.getCitizen().getTrait(Anchors.class).addAnchor(id.asString(), location);
return;
case ASSUME: {
Anchor n = npc.getCitizen().getTrait(Anchors.class)
.getAnchor(id.asString());
if (n == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid anchor name '" + id.asString() + "'");
}
else {
npc.getEntity().teleport(n.getLocation());
}
}
return;
case WALKNEAR: {
Anchor n = npc.getCitizen().getTrait(Anchors.class)
.getAnchor(id.asString());
if (n == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid anchor name '" + id.asString() + "'");
}
else if (range == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Must specify a range!");
}
else {
npc.getNavigator().setTarget(
Utilities.getWalkableLocationNear(n.getLocation(), range.asInt()));
}
}
return;
case WALKTO: {
Anchor n = npc.getCitizen().getTrait(Anchors.class)
.getAnchor(id.asString());
if (n == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid anchor name '" + id.asString() + "'");
}
else {
npc.getNavigator().setTarget(n.getLocation());
}
}
return;
case REMOVE: {
Anchor n = npc.getCitizen().getTrait(Anchors.class)
.getAnchor(id.asString());
if (n == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid anchor name '" + id.asString() + "'");
}
else {
npc.getCitizen().getTrait(Anchors.class).removeAnchor(n);
}
}
}
}
}