-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
CooldownCommand.java
252 lines (218 loc) · 10.2 KB
/
CooldownCommand.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.denizenscript.denizen.scripts.commands.core;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
public class CooldownCommand extends AbstractCommand {
public CooldownCommand() {
setName("cooldown");
setSyntax("cooldown [<duration>] (global) (script:<script>)");
setRequiredArguments(1, 3);
isProcedural = false;
}
// <--[command]
// @Name Cooldown
// @Syntax cooldown [<duration>] (global) (script:<script>)
// @Required 1
// @Maximum 3
// @Short Temporarily disables an interact script for the linked player.
// @Group core
//
// @Description
// Temporarily disables an interact script for the linked player.
//
// Cooldown requires a type (player or global), a script, and a duration.
// It also requires a valid link to a PlayerTag if using a non-global cooldown.
//
// To cooldown non-interact scripts automatically, consider <@link command ratelimit>.
//
// Cooldown periods are persistent through a server restart as they are saved in the 'saves.yml'.
//
// @Tags
// <ScriptTag.cooled_down[player]>
// <ScriptTag.cooldown>
//
// @Usage
// Use to keep the current interact script from meeting requirements.
// - cooldown 20m
//
// @Usage
// Use to keep a player from activating a script for a specified duration.
// - cooldown 11h script:bonus_script
// - cooldown 5s script:hit_indicator
//
// @Usage
// Use the 'global' argument to indicate the script to be on cooldown for all players.
// - cooldown global 24h script:daily_treasure_offering
// -->
private enum Type {GLOBAL, PLAYER}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Defaults are Type.PLAYER and the attached Script
scriptEntry.addObject("type", Type.PLAYER);
scriptEntry.addObject("script", scriptEntry.getScript());
// Parse arguments.. we need a type, duration, and script.
for (Argument arg : scriptEntry.getProcessedArgs()) {
// Type may be PLAYER or GLOBAL.. must not have a prefix.
if (!arg.hasPrefix() && arg.matchesEnum(Type.values())) {
scriptEntry.addObject("type", Type.valueOf(arg.getValue().toUpperCase()));
}
// DurationTag does not need a prefix, but is required.
else if (!scriptEntry.hasObject("duration")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
// Require a prefix on the script, since it's optional.
else if (arg.matchesPrefix("script", "s")) {
// Check matchesArgumentType afterwards so we don't default
// to the attached script unintentionally.
if (arg.matchesArgumentType(ScriptTag.class)) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
else {
throw new InvalidArgumentsException("Specified an invalid script!");
}
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("duration")) {
throw new InvalidArgumentsException("Requires a valid duration!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ScriptTag script = scriptEntry.getObjectTag("script");
DurationTag duration = scriptEntry.getObjectTag("duration");
Type type = (scriptEntry.hasObject("type") ?
(Type) scriptEntry.getObject("type") : Type.PLAYER);
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("Type", type.name())
+ script.debug()
+ (type.name().equalsIgnoreCase("player") ? Utilities.getEntryPlayer(scriptEntry).debug() : "")
+ duration.debug());
}
// Perform cooldown
switch (type) {
case PLAYER:
setCooldown(Utilities.getEntryPlayer(scriptEntry),
duration,
script.getName(),
false);
break;
case GLOBAL:
setCooldown(null,
duration,
script.getName(),
true);
break;
}
}
/**
* Gets the duration of a script cool-down.
*
* @param player the Player to check, null if only checking Global.
* @param scriptName the name of the script to check
* @return a DurationTag of the time remaining
*/
public static DurationTag getCooldownDuration(PlayerTag player, String scriptName) {
// Change to UPPERCASE so there's no case-sensitivity.
scriptName = scriptName.toUpperCase();
DurationTag duration = new DurationTag(0);
// Check current entry GLOBALLY, reset it if necessary
if (DenizenAPI.getSaves().contains("Global.Scripts." + scriptName + ".Cooldown Time")) {
if (System.currentTimeMillis()
< DenizenAPI.getSaves().getLong("Global.Scripts." + scriptName + ".Cooldown Time")) {
duration = new DurationTag((double) (DenizenAPI.getSaves().getLong("Global.Scripts." + scriptName
+ ".Cooldown Time") - System.currentTimeMillis()) / 1000);
}
}
// No player specified? No need to check any further...
if (player == null) {
return duration;
}
// If no entry for the script, return true
if (!DenizenAPI.getSaves().contains("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time")) {
return duration;
}
// If there is an entry, check against the time
if (System.currentTimeMillis()
<= DenizenAPI.getSaves().getLong("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time")) {
DurationTag player_dur = new DurationTag((double) (DenizenAPI.getSaves().getLong("Players." + player.getSaveName() + ".Scripts."
+ scriptName + ".Cooldown Time") - System.currentTimeMillis()) / 1000);
if (player_dur.getSeconds() > duration.getSeconds()) {
return player_dur;
}
}
return duration;
}
/**
* Checks if a script is cooled-down. If a cool-down is currently in progress,
* its requirements will fail and it will not trigger. If the script is being cooled down
* globally, this will also return false.
*
* @param player the Player to check, null if only checking Global.
* @param scriptName the name of the script to check
* @return true if the script is cool
*/
public static boolean checkCooldown(PlayerTag player, String scriptName) {
// Change to UPPERCASE so there's no case-sensitivity.
scriptName = scriptName.toUpperCase();
// Check current entry GLOBALLY, reset it if necessary
if (DenizenAPI.getSaves().contains("Global.Scripts." + scriptName + ".Cooldown Time")) {
if (System.currentTimeMillis()
< DenizenAPI.getSaves().getLong("Global.Scripts." + scriptName + ".Cooldown Time")) {
return false;
}
else {
DenizenAPI.getSaves().set("Global.Scripts." + scriptName + ".Cooldown Time", null);
}
}
// No player specified? No need to check any further...
if (player == null) {
return true;
}
// If no entry for the script, return true
if (!DenizenAPI.getSaves().contains("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time")) {
return true;
}
// If there is an entry, check against the time
if (System.currentTimeMillis()
>= DenizenAPI.getSaves().getLong("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time")) {
DenizenAPI.getSaves().set("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time", null);
return true;
}
return false;
}
/**
* Sets a cooldown for a Denizen Script. Can be for a specific Player, or GLOBAL.
*
* @param player if not a global cooldown, the Player to set the cooldown for
* @param duration the duration of the cooldown period, in seconds
* @param scriptName the name of the script to cooldown
* @param global whether the script should be cooled down globally
*/
public static void setCooldown(PlayerTag player, DurationTag duration, String scriptName, boolean global) {
scriptName = scriptName.toUpperCase();
// Set global cooldown
if (global) {
DenizenAPI.getSaves().set("Global.Scripts." + scriptName + ".Cooldown Time",
System.currentTimeMillis()
+ (duration.getSecondsAsInt() * 1000));
// or set Player cooldown
}
else {
DenizenAPI.getSaves().set("Players." + player.getSaveName() + ".Scripts." + scriptName + ".Cooldown Time",
System.currentTimeMillis()
+ (duration.getSecondsAsInt() * 1000));
}
}
}