/
WalkCommand.java
91 lines (63 loc) · 3.06 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
package net.aufdemrand.denizen.scripts.commands.npc;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
/**
* Handles NPC walking with the Citizens API.
*
* @author Jeremy Schroeder
*/
public class WalkCommand extends AbstractCommand {
// 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);
}
// Check for required information
if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException("Must specify a location!");
if (scriptEntry.getNPC() == null
|| !scriptEntry.getNPC().isValid()
|| !scriptEntry.getNPC().isSpawned())
throw new InvalidArgumentsException("Must have a valid spawned NPC attached.");
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Fetch required objects
dLocation loc = (dLocation) scriptEntry.getObject("location");
Element speed = (Element) scriptEntry.getObject("speed");
Element auto_range = (Element) scriptEntry.getObject("auto_range");
// Debug the execution
dB.report(getName(), loc.debug()
+ (speed != null ? speed.debug() : "")
+ (auto_range != null ? auto_range.debug() : ""));
// Do the execution
if (auto_range != null
&& auto_range == Element.TRUE) {
double distance = scriptEntry.getNPC().getLocation().distance(loc);
if (scriptEntry.getNPC().getNavigator().getLocalParameters().range() < distance)
scriptEntry.getNPC().getNavigator().getDefaultParameters().range((float) distance + 10);
}
scriptEntry.getNPC().getNavigator().setTarget(loc);
if (speed != null)
scriptEntry.getNPC().getNavigator().getLocalParameters().speedModifier(speed.asFloat());
}
}