-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
CooldownCommand.java
152 lines (140 loc) · 6.34 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
package com.denizenscript.denizen.scripts.commands.core;
import com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.DenizenCore;
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.objects.core.TimeTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import java.util.function.Consumer;
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 addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (ScriptContainer script : ScriptRegistry.scriptContainers.values()) {
if (script instanceof InteractScriptContainer) {
addOne.accept(script.getName());
}
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matchesPrefix("script", "s")
&& arg.matchesArgumentType(ScriptTag.class)) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
else if (arg.matchesEnum(Type.values())) {
scriptEntry.addObject("type", Type.valueOf(arg.getValue().toUpperCase()));
}
else if (!scriptEntry.hasObject("duration")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("type", Type.PLAYER);
scriptEntry.defaultObject("script", scriptEntry.getScript());
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, (type.name().equalsIgnoreCase("player") ? Utilities.getEntryPlayer(scriptEntry) : null), duration);
}
switch (type) {
case PLAYER:
setCooldown(Utilities.getEntryPlayer(scriptEntry), duration, script.getName(), false);
break;
case GLOBAL:
setCooldown(null, duration, script.getName(), true);
break;
}
}
public static DurationTag getCooldownDuration(PlayerTag player, String scriptName) {
TimeTag expires = DenizenCore.serverFlagMap.getFlagExpirationTime("__interact_cooldown." + scriptName);
if (expires != null) {
return new DurationTag((expires.millis() - TimeTag.now().millis()) / 1000.0);
}
if (player == null) {
return new DurationTag(0);
}
expires = player.getFlagTracker().getFlagExpirationTime("__interact_cooldown." + scriptName);
if (expires != null) {
return new DurationTag((expires.millis() - TimeTag.now().millis()) / 1000.0);
}
return new DurationTag(0);
}
public static boolean checkCooldown(PlayerTag player, String scriptName) {
DurationTag cooldown = getCooldownDuration(player, scriptName);
if (cooldown.getSeconds() > 0) {
return false;
}
return true;
}
public static void setCooldown(PlayerTag player, DurationTag duration, String scriptName, boolean global) {
TimeTag cooldownTime = new TimeTag(TimeTag.now().millis() + duration.getMillis());
if (global) {
DenizenCore.serverFlagMap.setFlag("__interact_cooldown." + scriptName, cooldownTime, cooldownTime);
}
else {
player.getFlagTracker().setFlag("__interact_cooldown." + scriptName, cooldownTime, cooldownTime);
}
}
}