-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
WalkCommand.java
192 lines (143 loc) · 6.57 KB
/
WalkCommand.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package net.aufdemrand.denizen.scripts.commands.npc;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.commands.Holdable;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.ai.event.NavigationCancelEvent;
import net.citizensnpcs.api.ai.event.NavigationCompleteEvent;
import net.citizensnpcs.api.ai.event.NavigationEvent;
import net.citizensnpcs.api.ai.flocking.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Handles NPC walking with the Citizens API.
*
* @author Jeremy Schroeder
*/
public class WalkCommand extends AbstractCommand implements Listener, Holdable {
// percentage
// walk [location] (speed:#.#) (auto_range)
//
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Interpret arguments
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class))
scriptEntry.addObject("location", arg.asType(dLocation.class));
else if (!scriptEntry.hasObject("speed")
&& arg.matchesPrimitive(aH.PrimitiveType.Percentage)
&& arg.matchesPrefix("s, speed"))
scriptEntry.addObject("speed", arg.asElement());
else if (!scriptEntry.hasObject("auto_range")
&& arg.matches("auto_range"))
scriptEntry.addObject("auto_range", Element.TRUE);
else if (!scriptEntry.hasObject("radius")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)
&& arg.matchesPrefix("radius"))
scriptEntry.addObject("radius", arg.asElement());
else if (!scriptEntry.hasObject("npcs")
&& arg.matchesArgumentList(dNPC.class))
scriptEntry.addObject("npcs", arg.asType(dList.class).filter(dNPC.class));
else
arg.reportUnhandled();
}
// Check for required information
if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException("Must specify a location!");
if (!scriptEntry.hasObject("npcs")) {
if (scriptEntry.getNPC() == null
|| !scriptEntry.getNPC().isValid()
|| !scriptEntry.getNPC().isSpawned())
throw new InvalidArgumentsException("Must have a valid spawned NPC attached.");
else
scriptEntry.addObject("npcs", Arrays.asList(scriptEntry.getNPC()));
}
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Fetch required objects
dLocation loc = (dLocation) scriptEntry.getObject("location");
Element speed = scriptEntry.getElement("speed");
Element auto_range = scriptEntry.getElement("auto_range");
Element radius = scriptEntry.getElement("radius");
List<dNPC> npcs = (List<dNPC>) scriptEntry.getObject("npcs");
// Debug the execution
dB.report(scriptEntry, getName(), loc.debug()
+ (speed != null ? speed.debug() : "")
+ (auto_range != null ? auto_range.debug() : "")
+ (radius != null ? radius.debug(): "")
+ (aH.debugObj("npcs", npcs)));
// Do the execution
for (dNPC npc: npcs) {
if (!npc.isSpawned()) {
dB.echoError(scriptEntry.getResidingQueue(), "NPC " + npc.identify() + " is not spawned!");
continue;
}
if (auto_range != null
&& auto_range == Element.TRUE) {
double distance = npc.getLocation().distance(loc);
if (npc.getNavigator().getLocalParameters().range() < distance + 10)
npc.getNavigator().getDefaultParameters().range((float) distance + 10);
// TODO: Should be using local params rather than default?
}
npc.getNavigator().setTarget(loc);
if (speed != null)
npc.getNavigator().getLocalParameters().speedModifier(speed.asFloat());
if (radius != null) {
NPCFlock flock = new RadiusNPCFlock(radius.asDouble());
Flocker flocker = new Flocker(npc.getCitizen(), flock, new SeparationBehavior(Flocker.LOW_INFLUENCE),
new CohesionBehavior(Flocker.LOW_INFLUENCE), new AlignmentBehavior(Flocker.HIGH_INFLUENCE));
npc.getNavigator().getLocalParameters().addRunCallback(flocker);
}
}
if (scriptEntry.shouldWaitFor()) {
held.add(scriptEntry);
scriptEntry.addObject("tally", new ArrayList<dNPC>(npcs));
}
}
// Held script entries
public static List<ScriptEntry> held = new ArrayList<ScriptEntry>();
@EventHandler
public void finish(NavigationCompleteEvent e) {
if (held.isEmpty()) return;
checkHeld(e);
}
@EventHandler
public void cancel(NavigationCancelEvent e) {
if (held.isEmpty()) return;
checkHeld(e);
}
public void checkHeld(NavigationEvent e) {
if (e.getNPC() == null)
return;
// Check each held entry -- the scriptExecuter is waiting on
// the entry to be marked 'waited for'.
for (int i = 0; i < held.size(); i++) {
ScriptEntry entry = held.get(i);
// Get all NPCs associated with the entry. They must all
// finish navigation before the entry can be let go
List<dNPC> tally = (List<dNPC>) entry.getObject("tally");
// If the NPC is the NPC from the event, take it from the list.
tally.remove(dNPC.mirrorCitizensNPC(e.getNPC()));
// Check if tally is empty.
if (tally.isEmpty()) {
entry.setFinished(true);
held.remove(i);
i--;
}
}
}
@Override
public void onEnable() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
.registerEvents(this, DenizenAPI.getCurrentInstance());
}
}