-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-393 Add implementation for command cooldowns; Fix typo in requirem…
…ent package name (#393) * Add implementation for command cooldowns; Fix typo in requirement package name * Fix registration of global scoped validators. * Fix overwriting of validator, in case of same scopes --------- Co-authored-by: Rollczi <[email protected]>
- Loading branch information
Showing
20 changed files
with
315 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
litecommands-annotations/src/dev/rollczi/litecommands/annotations/cooldown/Cooldown.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package dev.rollczi.litecommands.annotations.cooldown; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Cooldown { | ||
|
||
String key(); | ||
|
||
long count(); | ||
|
||
ChronoUnit unit(); | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...tations/src/dev/rollczi/litecommands/annotations/cooldown/CooldownAnnotationResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dev.rollczi.litecommands.annotations.cooldown; | ||
|
||
import dev.rollczi.litecommands.annotations.AnnotationInvoker; | ||
import dev.rollczi.litecommands.annotations.AnnotationProcessor; | ||
import dev.rollczi.litecommands.cooldown.CooldownContext; | ||
import dev.rollczi.litecommands.meta.Meta; | ||
import java.time.Duration; | ||
|
||
public class CooldownAnnotationResolver<SENDER> implements AnnotationProcessor<SENDER> { | ||
|
||
@Override | ||
public AnnotationInvoker<SENDER> process(AnnotationInvoker<SENDER> invoker) { | ||
return invoker | ||
.on(Cooldown.class, ((annotation, metaHolder) -> metaHolder.meta() | ||
.put(Meta.COOLDOWN, getCooldownContext(annotation)))); | ||
} | ||
|
||
private CooldownContext getCooldownContext(Cooldown cooldown) { | ||
return new CooldownContext(cooldown.key(), Duration.of(cooldown.count(), cooldown.unit())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...nnotations/test/dev/rollczi/litecommands/annotations/cooldown/CooldownAnnotationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package dev.rollczi.litecommands.annotations.cooldown; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
import dev.rollczi.litecommands.annotations.LiteTestSpec; | ||
import dev.rollczi.litecommands.annotations.command.Command; | ||
import dev.rollczi.litecommands.annotations.execute.Execute; | ||
import dev.rollczi.litecommands.cooldown.CooldownState; | ||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import org.awaitility.Awaitility; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CooldownAnnotationTest extends LiteTestSpec { | ||
|
||
@Command(name = "test") | ||
@Cooldown(key = "test-cooldown", count = 1, unit = ChronoUnit.SECONDS) | ||
static class TestCommand { | ||
|
||
@Execute | ||
void execute() {} | ||
|
||
} | ||
|
||
@Test | ||
void test() { | ||
platform.execute("test"); | ||
|
||
CooldownState cooldownState = platform.execute("test") | ||
.assertFailedAs(CooldownState.class); | ||
|
||
assertEquals("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("test").isSuccessful()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
litecommands-core/src/dev/rollczi/litecommands/cooldown/CooldownCompositeKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dev.rollczi.litecommands.cooldown; | ||
|
||
import dev.rollczi.litecommands.identifier.Identifier; | ||
import java.util.Objects; | ||
|
||
class CooldownCompositeKey { | ||
|
||
private final Identifier identifier; | ||
private final String cooldownKey; | ||
|
||
public CooldownCompositeKey(Identifier identifier, String cooldownKey) { | ||
this.identifier = identifier; | ||
this.cooldownKey = cooldownKey; | ||
} | ||
|
||
public Identifier getIdentifier() { | ||
return identifier; | ||
} | ||
|
||
public String getCooldownKey() { | ||
return cooldownKey; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) { | ||
return true; | ||
} | ||
|
||
if (object == null || getClass() != object.getClass()) { | ||
return false; | ||
} | ||
|
||
CooldownCompositeKey that = (CooldownCompositeKey) object; | ||
return Objects.equals(identifier, that.identifier) && Objects.equals(cooldownKey, that.cooldownKey); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(identifier, cooldownKey); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
litecommands-core/src/dev/rollczi/litecommands/cooldown/CooldownContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dev.rollczi.litecommands.cooldown; | ||
|
||
import java.time.Duration; | ||
|
||
public class CooldownContext { | ||
|
||
private final String key; | ||
private final Duration duration; | ||
|
||
public CooldownContext(String key, Duration duration) { | ||
this.key = key; | ||
this.duration = duration; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public Duration getDuration() { | ||
return duration; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
litecommands-core/src/dev/rollczi/litecommands/cooldown/CooldownState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package dev.rollczi.litecommands.cooldown; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
public class CooldownState { | ||
|
||
private final CooldownContext cooldownContext; | ||
private final Duration remainingDuration; | ||
private final Instant expirationTime; | ||
|
||
public CooldownState(CooldownContext cooldownContext, Duration remainingDuration, Instant expirationTime) { | ||
this.cooldownContext = cooldownContext; | ||
this.remainingDuration = remainingDuration; | ||
this.expirationTime = expirationTime; | ||
} | ||
|
||
public CooldownContext getCooldownContext() { | ||
return cooldownContext; | ||
} | ||
|
||
public Duration getRemainingDuration() { | ||
return remainingDuration; | ||
} | ||
|
||
public Instant getExpirationTime() { | ||
return expirationTime; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
litecommands-core/src/dev/rollczi/litecommands/cooldown/CooldownStateResultHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dev.rollczi.litecommands.cooldown; | ||
|
||
import static dev.rollczi.litecommands.message.LiteMessages.COMMAND_COOLDOWN; | ||
|
||
import dev.rollczi.litecommands.handler.result.ResultHandler; | ||
import dev.rollczi.litecommands.handler.result.ResultHandlerChain; | ||
import dev.rollczi.litecommands.invocation.Invocation; | ||
import dev.rollczi.litecommands.message.MessageRegistry; | ||
|
||
public class CooldownStateResultHandler<SENDER> implements ResultHandler<SENDER, CooldownState> { | ||
|
||
private final MessageRegistry<SENDER> messageRegistry; | ||
|
||
public CooldownStateResultHandler(MessageRegistry<SENDER> messageRegistry) { | ||
this.messageRegistry = messageRegistry; | ||
} | ||
|
||
@Override | ||
public void handle(Invocation<SENDER> invocation, CooldownState cooldownState, ResultHandlerChain<SENDER> chain) { | ||
this.messageRegistry.getInvoked(COMMAND_COOLDOWN, invocation, cooldownState) | ||
.ifPresent(object -> chain.resolve(invocation, object)); | ||
} | ||
|
||
} | ||
|
52 changes: 52 additions & 0 deletions
52
litecommands-core/src/dev/rollczi/litecommands/cooldown/CooldownStateValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package dev.rollczi.litecommands.cooldown; | ||
|
||
import dev.rollczi.litecommands.argument.suggester.input.SuggestionInput; | ||
import dev.rollczi.litecommands.flow.Flow; | ||
import dev.rollczi.litecommands.invocation.Invocation; | ||
import dev.rollczi.litecommands.meta.Meta; | ||
import dev.rollczi.litecommands.meta.MetaHolder; | ||
import dev.rollczi.litecommands.validator.Validator; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import net.jodah.expiringmap.ExpiringMap; | ||
|
||
public class CooldownStateValidator<SENDER> implements Validator<SENDER> { | ||
|
||
private final ExpiringMap<CooldownCompositeKey, Instant> cooldowns; | ||
|
||
public CooldownStateValidator() { | ||
this.cooldowns = ExpiringMap.builder() | ||
.variableExpiration() | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Flow validate(Invocation<SENDER> invocation, MetaHolder metaHolder) { | ||
if (invocation.arguments() instanceof SuggestionInput<?>) { | ||
return Flow.continueFlow(); | ||
} | ||
|
||
List<CooldownContext> cooldownContexts = metaHolder.metaCollector().collect(Meta.COOLDOWN); | ||
if (cooldownContexts.isEmpty()) { | ||
return Flow.continueFlow(); | ||
} | ||
|
||
return validateCooldown(invocation, cooldownContexts.get(0)); | ||
} | ||
|
||
private Flow validateCooldown(Invocation<SENDER> invocation, CooldownContext cooldownContext) { | ||
CooldownCompositeKey compositeKey = new CooldownCompositeKey(invocation.platformSender().getIdentifier(), cooldownContext.getKey()); | ||
|
||
Instant now = Instant.now(); | ||
Instant expirationTime = cooldowns.get(compositeKey); | ||
if (expirationTime != null && expirationTime.isAfter(now)) { | ||
return Flow.terminateFlow(new CooldownState(cooldownContext, Duration.between(now, expirationTime), expirationTime)); | ||
} | ||
|
||
cooldowns.put(compositeKey, now.plus(cooldownContext.getDuration()), cooldownContext.getDuration().toNanos(), TimeUnit.NANOSECONDS); | ||
return Flow.continueFlow(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.