-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
PauseCommand.java
170 lines (145 loc) · 5.79 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
169
170
package com.denizenscript.denizen.scripts.commands.npc;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.dB;
import com.denizenscript.denizen.objects.dNPC;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.aH;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.trait.waypoint.Waypoints;
import org.bukkit.entity.Player;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class PauseCommand extends AbstractCommand {
// <--[command]
// @Name Pause
// @Syntax pause [waypoints/activity] (<duration>)
// @Required 1
// @Short Pauses an NPC's waypoint navigation or goal activity temporarily or indefinitely.
// @Group npc
//
// @Description
// TODO: Document Command Details
//
// @Tags
// <n@npc.navigator.is_navigating>
//
// @Usage
// Use to pause an NPC's waypoint navigation indefinitely.
//
// @Usage
// - pause waypoints
// 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
// TODO: Document Command Details
//
// @Tags
// <n@npc.navigator.is_navigating>
//
// @Usage
// Use to pause an NPC's waypoint navigation indefinitely.
//
// @Usage
// - pause waypoints
// 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
// -->
private Map<String, Integer> durations = new ConcurrentHashMap<>(8, 0.9f, 1);
enum PauseType {ACTIVITY, WAYPOINTS, NAVIGATION}
int duration;
PauseType pauseType;
dNPC dNPC;
Player player;
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Set defaults with information from the ScriptEntry
duration = -1;
pauseType = null;
dNPC = null;
player = null;
if (Utilities.getEntryNPC(scriptEntry) != null) {
dNPC = Utilities.getEntryNPC(scriptEntry);
}
if (Utilities.getEntryPlayer(scriptEntry) != null) {
player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
}
// Parse arguments
// TODO: UPDATE COMMAND PARSING
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesDuration(arg)) {
duration = aH.getIntegerFrom(arg);
}
else if (aH.matchesArg("WAYPOINTS", arg) || aH.matchesArg("NAVIGATION", arg)
|| aH.matchesArg("ACTIVITY", arg) || aH.matchesArg("WAYPOINTS", arg)) {
// Could also maybe do for( ... : PauseType.values()) ... not sure which is faster.
pauseType = PauseType.valueOf(arg.toUpperCase());
}
else {
dB.echoError(scriptEntry.getResidingQueue(), "Unknown argument '" + arg + "'");
}
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
pause(dNPC, pauseType, !scriptEntry.getCommandName().equalsIgnoreCase("RESUME"));
// If duration...
if (duration > 0) {
if (durations.containsKey(dNPC.getCitizen().getId() + pauseType.name())) {
try {
DenizenAPI.getCurrentInstance().getServer().getScheduler().cancelTask(durations.get(dNPC.getCitizen().getId() + pauseType.name()));
}
catch (Exception e) {
dB.echoError(scriptEntry.getResidingQueue(), "There was an error pausing that!");
dB.echoError(scriptEntry.getResidingQueue(), e);
}
}
dB.echoDebug(scriptEntry, "Running delayed task: Unpause " + pauseType.toString());
final ScriptEntry se = scriptEntry;
durations.put(dNPC.getId() + pauseType.name(), DenizenAPI.getCurrentInstance()
.getServer().getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
@Override
public void run() {
dB.echoDebug(se, "Running delayed task: Pausing " + pauseType.toString());
pause(dNPC, pauseType, false);
}
}, duration * 20));
}
}
public void pause(dNPC denizen, PauseType pauseType, boolean pause) {
switch (pauseType) {
case WAYPOINTS:
denizen.getCitizen().getTrait(Waypoints.class).getCurrentProvider().setPaused(pause);
if (pause) {
denizen.getNavigator().cancelNavigation();
}
return;
case ACTIVITY:
denizen.getCitizen().getDefaultGoalController().setPaused(pause);
return;
case NAVIGATION:
// TODO: Finish this
}
}
}