/
PauseCommand.java
168 lines (154 loc) · 6.52 KB
/
PauseCommand.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
package com.denizenscript.denizen.scripts.commands.npc;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.trait.waypoint.Waypoints;
import java.util.HashMap;
import java.util.Map;
public class PauseCommand extends AbstractCommand {
public static class ResumeCommand extends PauseCommand {
public ResumeCommand() {
setName("resume");
setSyntax("resume [waypoints/activity] (<duration>)");
}
}
public PauseCommand() {
setName("pause");
setSyntax("pause [waypoints/activity] (<duration>)");
setRequiredArguments(1, 2);
isProcedural = false;
}
// <--[command]
// @Name Pause
// @Syntax pause [waypoints/activity] (<duration>)
// @Required 1
// @Maximum 2
// @Plugin Citizens
// @Short Pauses an NPC's waypoint navigation or goal activity temporarily or indefinitely.
// @Group npc
//
// @Description
// The pause command pauses an NPC's waypoint navigation or goal activity temporarily or indefinitely.
// This works along side <@link command resume>.
//
// "Waypoints" refers to the NPC's path navigation, usually set via "/npc path".
//
// "Activity" refers to the Citizens AI Goal system, which may be used by some plugins but usually is not.
//
// If no duration is specified, the resume command must be used to unpause it.
//
// @Tags
// <NPCTag.is_navigating>
//
// @Usage
// Use to pause an NPC's waypoint navigation indefinitely.
// - pause waypoints
//
// @Usage
// Use to pause an NPC's goal activity temporarily.
// - pause activity 1m
//
// @Usage
// Use to pause an NPC's waypoint navigation and then resume it.
// - pause waypoints
// - resume waypoints
// -->
// <--[command]
// @Name Resume
// @Syntax resume [waypoints/activity] (<duration>)
// @Required 1
// @Plugin Citizens
// @Short Resumes an NPC's waypoint navigation or goal activity temporarily or indefinitely.
// @Group npc
//
// @Description
// The resume command resumes an NPC's waypoint navigation or goal activity temporarily or indefinitely.
// This works along side <@link command pause>.
// See the documentation of the pause command for more details.
//
// @Tags
// <NPCTag.is_navigating>
//
// @Usage
// Use to pause an NPC's waypoint navigation and then resume it.
// - pause waypoints
// - resume waypoints
// -->
private Map<String, Integer> durations = new HashMap<>();
enum PauseType {ACTIVITY, WAYPOINTS, NAVIGATION}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matchesArgumentType(DurationTag.class)
&& !scriptEntry.hasObject("duration")) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("pause_type")
&& arg.matchesEnum(PauseType.values())) {
scriptEntry.addObject("pause_type", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("pause_type")) {
throw new InvalidArgumentsException("Must specify a pause type!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
DurationTag duration = scriptEntry.getObjectTag("duration");
ElementTag pauseTypeElement = scriptEntry.getElement("pause_type");
PauseType pauseType = PauseType.valueOf(pauseTypeElement.asString().toUpperCase());
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (duration == null ? "" : duration.debug()) + pauseTypeElement.debug());
}
NPCTag npc = null;
if (Utilities.getEntryNPC(scriptEntry) != null) {
npc = Utilities.getEntryNPC(scriptEntry);
}
pause(npc, pauseType, !scriptEntry.getCommandName().equalsIgnoreCase("RESUME"));
if (duration != null) {
if (durations.containsKey(npc.getCitizen().getId() + pauseType.name())) {
try {
Denizen.getInstance().getServer().getScheduler().cancelTask(durations.get(npc.getCitizen().getId() + pauseType.name()));
}
catch (Exception e) {
Debug.echoError(scriptEntry.getResidingQueue(), "There was an error pausing that!");
Debug.echoError(scriptEntry.getResidingQueue(), e);
}
}
Debug.echoDebug(scriptEntry, "Running delayed task: Unpause " + pauseType.toString());
final NPCTag theNpc = npc;
final ScriptEntry se = scriptEntry;
durations.put(npc.getId() + pauseType.name(), Denizen.getInstance()
.getServer().getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(),
() -> {
Debug.echoDebug(se, "Running delayed task: Pausing " + pauseType.toString());
pause(theNpc, pauseType, false);
}, duration.getTicks()));
}
}
public void pause(NPCTag denizen, PauseType pauseType, boolean pause) {
switch (pauseType) {
case WAYPOINTS:
denizen.getCitizen().getOrAddTrait(Waypoints.class).getCurrentProvider().setPaused(pause);
if (pause) {
denizen.getNavigator().cancelNavigation();
}
return;
case ACTIVITY:
denizen.getCitizen().getDefaultGoalController().setPaused(pause);
return;
case NAVIGATION:
// TODO: Finish this
}
}
}