/
RateLimitCommand.java
95 lines (88 loc) · 3.93 KB
/
RateLimitCommand.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
package com.denizenscript.denizencore.scripts.commands.queue;
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.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import java.util.HashMap;
public class RateLimitCommand extends AbstractCommand {
public RateLimitCommand() {
setName("ratelimit");
setSyntax("ratelimit [<object>] [<duration>]");
setRequiredArguments(2, 2);
isProcedural = true;
}
// <--[command]
// @Name RateLimit
// @Syntax ratelimit [<object>] [<duration>]
// @Required 2
// @Maximum 2
// @Short Limits the rate that queues may process a script at.
// @Group queue
//
// @Description
// Limits the rate that queues may process a script at.
// If another queue tries to run the same script faster than the duration, that second queue will be stopped.
//
// Note that the rate limiting is tracked based on two unique factors: the object input, and the specific script line.
// That is to say: if you have a 'ratelimit <player> 10s', and then a few lines down a 'ratelimit <player> 10s',
// those are two separate rate limiters.
// Additionally, if you have a 'ratelimit <player> 10s' and two different players run it, they each have a separate rate limit applied.
//
// Note that this uses game delta tick time, not system realtime.
//
// @Tags
// None
//
// @Usage
// Use to show a message to a player no faster than once every ten seconds.
// - ratelimit <player> 10s
// - narrate "Wow!"
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (arg.matchesArgumentType(DurationTag.class)
&& !scriptEntry.hasObject("duration")
&& arg.limitToOnlyPrefix("duration")) {
if (!scriptEntry.hasObject("object")) {
Deprecations.outOfOrderArgs.warn(scriptEntry);
}
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("object")
&& arg.limitToOnlyPrefix("object")) {
scriptEntry.addObject("object", arg.getRawElement());
}
else {
arg.reportUnhandled();
}
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
DurationTag duration = scriptEntry.getObjectTag("duration");
ElementTag object = scriptEntry.getElement("object");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), duration, object);
}
if (scriptEntry.internal.specialProcessedData == null) {
scriptEntry.internal.specialProcessedData = new HashMap<>(2);
}
HashMap<String, Long> map = (HashMap<String, Long>) scriptEntry.internal.specialProcessedData;
String key = object.asLowerString();
Long endTime = map.get(key);
long curTime = DenizenCore.serverTimeMillis;
if (endTime != null && curTime < endTime) {
Debug.echoDebug(scriptEntry, "Rate limit applied with " + (endTime - curTime) + "ms left.");
scriptEntry.getResidingQueue().clear();
scriptEntry.getResidingQueue().stop();
return;
}
map.put(key, curTime + duration.getMillis());
}
}