-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
FollowCommand.java
179 lines (166 loc) · 7.81 KB
/
FollowCommand.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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
public class FollowCommand extends AbstractCommand {
public FollowCommand() {
setName("follow");
setSyntax("follow (followers:<entity>|...) (stop/target:<entity>) (lead:<#.#>) (max:<#.#>) (speed:<#.#>) (allow_wander)");
setRequiredArguments(0, 6);
}
// <--[command]
// @Name Follow
// @Syntax follow (followers:<entity>|...) (stop/target:<entity>) (lead:<#.#>) (max:<#.#>) (speed:<#.#>) (allow_wander)
// @Required 0
// @Maximum 6
// @Short Causes a list of entities to follow a target.
// @Group entity
//
// @Description
// Causes a list of entities to follow a target.
//
// Specify the list of followers or just one. If no follower is specified, will use the linked NPC.
//
// Specify either the target to follow, or 'stop'. If no target is specified, will use the linked player.
//
// Use 'speed' to set the movement speed multiplier.
// Use 'lead' to set how far away the follower will remain from the target (ie, it won't try to get closer than the 'lead' distance).
// Use 'max' to set the maximum distance between the follower and the target before the follower will automatically start teleporting to keep up.
// Use 'allow_wander' to allow the entity to wander randomly.
//
// The 'max' and 'allow_wander' arguments can only be used on non-NPC entities.
//
// @Tags
// <NPCTag.navigator.target_entity> returns the entity the npc is following.
//
// @Usage
// To make an NPC follow the player in an interact script.
// - follow
//
// @Usage
// To make an NPC stop following.
// - follow stop
//
// @Usage
// To explicitly make an NPC follow the player.
// - follow followers:<npc> target:<player>
//
// @Usage
// To make an NPC follow the player, slowly and at distance.
// - follow speed:0.7 lead:10
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("stop") &&
arg.matches("stop")) {
scriptEntry.addObject("stop", new ElementTag(true));
}
else if (!scriptEntry.hasObject("lead") &&
arg.matchesFloat() &&
arg.matchesPrefix("l", "lead")) {
scriptEntry.addObject("lead", arg.asElement());
}
else if (!scriptEntry.hasObject("max") &&
arg.matchesFloat() &&
arg.matchesPrefix("max")) {
scriptEntry.addObject("max", arg.asElement());
}
else if (!scriptEntry.hasObject("allow_wander") &&
arg.matches("allow_wander")) {
scriptEntry.addObject("allow_wander", new ElementTag(true));
}
else if (!scriptEntry.hasObject("speed") &&
arg.matchesFloat() &&
arg.matchesPrefix("s", "speed")) {
scriptEntry.addObject("speed", arg.asElement());
}
else if (!scriptEntry.hasObject("entities") &&
arg.matchesPrefix("followers", "follower") &&
arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class));
}
else if (!scriptEntry.hasObject("target") &&
arg.matchesArgumentType(EntityTag.class)) {
scriptEntry.addObject("target", arg.asType(EntityTag.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("target")) {
if (Utilities.entryHasPlayer(scriptEntry)) {
scriptEntry.addObject("target", Utilities.getEntryPlayer(scriptEntry).getDenizenEntity());
}
else if (!scriptEntry.hasObject("stop")) {
throw new InvalidArgumentsException("This command requires a linked player!");
}
}
if (!scriptEntry.hasObject("entities")) {
if (!Utilities.entryHasNPC(scriptEntry)) {
throw new InvalidArgumentsException("This command requires a linked NPC!");
}
else {
scriptEntry.addObject("entities",
new ListTag(Utilities.getEntryNPC(scriptEntry)));
}
}
scriptEntry.defaultObject("stop", new ElementTag(false)).defaultObject("allow_wander", new ElementTag(false));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag stop = scriptEntry.getElement("stop");
ElementTag lead = scriptEntry.getElement("lead");
ElementTag maxRange = scriptEntry.getElement("max");
ElementTag allowWander = scriptEntry.getElement("allow_wander");
ElementTag speed = scriptEntry.getElement("speed");
ListTag entities = scriptEntry.getObjectTag("entities");
EntityTag target = scriptEntry.getObjectTag("target");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
(Utilities.getEntryPlayer(scriptEntry) != null ? Utilities.getEntryPlayer(scriptEntry).debug() : "")
+ (!stop.asBoolean() ? ArgumentHelper.debugObj("Action", "FOLLOW") : ArgumentHelper.debugObj("Action", "STOP"))
+ (lead != null ? lead.debug() : "")
+ (maxRange != null ? maxRange.debug() : "")
+ allowWander.debug()
+ entities.debug()
+ target.debug());
}
for (EntityTag entity : entities.filter(EntityTag.class, scriptEntry)) {
if (entity.isCitizensNPC()) {
NPCTag npc = entity.getDenizenNPC();
if (lead != null) {
npc.getNavigator().getLocalParameters().distanceMargin(lead.asDouble());
}
if (speed != null) {
npc.getNavigator().getLocalParameters().speedModifier(speed.asFloat());
}
if (stop.asBoolean()) {
npc.getNavigator().cancelNavigation();
}
else {
npc.getNavigator().setTarget(target.getBukkitEntity(), false);
}
}
else {
if (stop.asBoolean()) {
NMSHandler.getEntityHelper().stopFollowing(entity.getBukkitEntity());
}
else {
NMSHandler.getEntityHelper().follow(target.getBukkitEntity(), entity.getBukkitEntity(),
speed != null ? speed.asDouble() : 0.3, lead != null ? lead.asDouble() : 5,
maxRange != null ? maxRange.asDouble() : 8, allowWander.asBoolean());
}
}
}
}
}