Skip to content

Commit 2a8dee0

Browse files
committed
Add player delay support to proxy operator
1 parent 954488f commit 2a8dee0

2 files changed

Lines changed: 58 additions & 5 deletions

File tree

chatcontrol-proxy-core/src/main/java/org/mineacademy/chatcontrol/proxy/operator/ProxyOperator.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public abstract class ProxyOperator implements Rule {
6161
*/
6262
private Tuple<SimpleTime, String> delay;
6363

64+
/**
65+
* The delay between the next time this rule can be fired up for the given sender, with optional warning message
66+
*/
67+
private Tuple<SimpleTime, String> playerDelay;
68+
6469
/**
6570
* List of commands to run as player when rule matches
6671
*/
@@ -145,6 +150,11 @@ public abstract class ProxyOperator implements Rule {
145150
@Getter
146151
private long lastExecuted = -1;
147152

153+
/**
154+
* The time the operator was last executed for the given player name(s)
155+
*/
156+
private final Map<String, Long> lastExecutedForPlayers = new HashMap<>();
157+
148158
/**
149159
* @see Rule#onOperatorParse(java.lang.String[])
150160
*/
@@ -175,14 +185,25 @@ public final boolean onOperatorParse(final String[] args) {
175185
}
176186
}
177187

178-
else if ("delay".equals(args[0])) {
179-
this.checkNotSet(this.delay, "delay");
188+
else if ("delay".equals(args[0]) || "player delay".equals(param)) {
189+
final int offset = "player delay".equals(param) ? 1 : 0;
180190

181191
try {
182-
final SimpleTime time = SimpleTime.fromString(CommonCore.joinRange(1, 3, args));
183-
final String message = args.length > 2 ? CommonCore.joinRange(3, args) : null;
192+
final SimpleTime time = SimpleTime.fromString(CommonCore.joinRange(1 + offset, 3 + offset, args));
193+
final String message = args.length > 2 ? CommonCore.joinRange(3 + offset, args) : null;
194+
195+
final Tuple<SimpleTime, String> tuple = new Tuple<>(time, message);
196+
197+
if ("delay".equals(args[0])) {
198+
this.checkNotSet(this.delay, args[0]);
199+
200+
this.delay = tuple;
201+
202+
} else {
203+
this.checkNotSet(this.playerDelay, param);
184204

185-
this.delay = new Tuple<>(time, message);
205+
this.playerDelay = tuple;
206+
}
186207

187208
} catch (final Throwable ex) {
188209
CommonCore.throwError(ex, "Syntax error in 'delay' operator. Valid: <amount> <unit> (1 second, 2 minutes). Got: " + String.join(" ", args));
@@ -336,6 +357,7 @@ protected SerializedMap collectOptions() {
336357
//"Save Keys", this.saveData,
337358
"Expires", this.expires != -1 ? this.expires : null,
338359
"Delay", this.delay,
360+
"Player Delay", this.playerDelay != null ? this.playerDelay.getKey() + (this.playerDelay.getValue() != null && !this.playerDelay.getValue().isEmpty() ? ", warnmessage=" + this.playerDelay.getValue() : "") : "",
339361
"Player Commands", this.playerCommands,
340362
//"Console Commands", this.consoleCommands,
341363
"Proxy Commands", this.proxyCommands,
@@ -376,6 +398,21 @@ public final String toDisplayableString() {
376398
return CompChatColor.stripColorCodes(this.toString().replace("\t", " "));
377399
}
378400

401+
/**
402+
* @param playerName
403+
* @return
404+
*/
405+
protected final long getLastExecutedForPlayer(final String playerName) {
406+
return this.lastExecutedForPlayers.getOrDefault(playerName, -1L);
407+
}
408+
409+
/**
410+
* @param playerName
411+
*/
412+
protected final void setLastExecutedForPlayer(final String playerName) {
413+
this.lastExecutedForPlayers.put(playerName, System.currentTimeMillis());
414+
}
415+
379416
/**
380417
* @see java.lang.Object#equals(java.lang.Object)
381418
*/

chatcontrol-proxy-core/src/main/java/org/mineacademy/chatcontrol/proxy/operator/ProxyPlayerMessage.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,22 @@ protected void filter(final T message) throws EventHandledException {
487487
message.setLastExecuted(now);
488488
}
489489

490+
// Player Delay
491+
if (message.getPlayerDelay() != null && this.audience != null) {
492+
final SimpleTime time = message.getPlayerDelay().getKey();
493+
final long now = System.currentTimeMillis();
494+
495+
final long delay = Math.round((now - message.getLastExecutedForPlayer(this.audience.getName())) / 1000D);
496+
497+
if (delay < time.getTimeSeconds()) {
498+
Debugger.debug("operator", "\tbefore player delay: " + delay + " threshold: " + time.getTimeSeconds());
499+
500+
return;
501+
}
502+
503+
message.setLastExecutedForPlayer(this.audience.getName());
504+
}
505+
490506
boolean pickedMessage = false;
491507

492508
for (final FoundationPlayer online : Platform.getOnlinePlayers()) {

0 commit comments

Comments
 (0)