Skip to content

Commit

Permalink
GH-420 Add an option to define permission, which allows bypassing coo…
Browse files Browse the repository at this point in the history
…ldown (#420)
  • Loading branch information
Rafał Chomczyk authored Aug 22, 2024
1 parent ffd2239 commit 084e080
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@

ChronoUnit unit();

/**
* Permission, which will be used to bypass that cooldown.
*/
String bypass() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public AnnotationInvoker<SENDER> process(AnnotationInvoker<SENDER> invoker) {
}

private CooldownContext getCooldownContext(Cooldown cooldown) {
return new CooldownContext(cooldown.key(), Duration.of(cooldown.count(), cooldown.unit()));
return new CooldownContext(cooldown.key(), Duration.of(cooldown.count(), cooldown.unit()), cooldown.bypass());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.cooldown.CooldownState;
import dev.rollczi.litecommands.platform.PlatformSender;
import dev.rollczi.litecommands.unit.TestPlatformSender;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.awaitility.Awaitility;
Expand All @@ -23,6 +25,15 @@ void execute() {}

}

@Command(name = "bypass-test")
@Cooldown(key = "bypass-test-cooldown", count = 1, unit = ChronoUnit.SECONDS, bypass = "test.bypass")
static class TestWithBypassCommand {

@Execute
void execute() {}

}

@Test
void test() {
platform.execute("test");
Expand All @@ -39,4 +50,24 @@ void test() {
.until(() -> platform.execute("test").isSuccessful());
}

@Test
void testCooldownBypass() {
PlatformSender permittedSender = TestPlatformSender.permitted("test.bypass");
platform.execute(permittedSender, "bypass-test").assertSuccess();
platform.execute(permittedSender, "bypass-test").assertSuccess();

platform.execute("bypass-test");

CooldownState cooldownState = platform.execute("bypass-test")
.assertFailedAs(CooldownState.class);

assertEquals("bypass-test-cooldown", cooldownState.getCooldownContext().getKey());
assertEquals(Duration.ofSeconds(1), cooldownState.getCooldownContext().getDuration());
assertFalse(cooldownState.getRemainingDuration().isZero());

Awaitility.await()
.atMost(Duration.ofSeconds(3))
.until(() -> platform.execute("bypass-test").isSuccessful());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ public class CooldownContext {

private final String key;
private final Duration duration;
private final String bypassPermission;

public CooldownContext(String key, Duration duration) {
public CooldownContext(String key, Duration duration, String bypassPermission) {
this.key = key;
this.duration = duration;
this.bypassPermission = bypassPermission;
}

public String getKey() {
Expand All @@ -20,4 +22,8 @@ public Duration getDuration() {
return duration;
}

public String getBypassPermission() {
return bypassPermission;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public Flow validate(Invocation<SENDER> invocation, MetaHolder metaHolder) {
}

private Flow validateCooldown(Invocation<SENDER> invocation, CooldownContext cooldownContext) {
String bypassPermission = cooldownContext.getBypassPermission();
if (!bypassPermission.isEmpty() && invocation.platformSender().hasPermission(bypassPermission)) {

This comment has been minimized.

Copy link
@huanmeng-qwq

huanmeng-qwq Sep 1, 2024

Contributor

So shouldn't dev.rollczi.litecommands.platform.PlatformSender#hasPermission be removed?

This comment has been minimized.

Copy link
@rchomczyk

rchomczyk Sep 1, 2024

Contributor

Currently, this is used widely in internals, and after discussion with Rollczi we have got to conclusion that it would be okay to use it also there.

return Flow.continueFlow();
}

CooldownCompositeKey compositeKey = new CooldownCompositeKey(invocation.platformSender().getIdentifier(), cooldownContext.getKey());

Instant now = Instant.now();
Expand Down

0 comments on commit 084e080

Please sign in to comment.