From 46dc68961bf5f6e300bfc9d3a0486f3af10df2c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Fri, 5 Jan 2024 21:14:38 +0100 Subject: [PATCH] make it possible to override the fallback group --- .../incendo/cloud/processors/cooldown/Cooldown.java | 5 +++-- .../processors/cooldown/CooldownConfiguration.java | 12 ++++++++++++ .../processors/cooldown/CooldownPostprocessor.java | 2 +- .../processors/cooldown/annotations/Cooldown.java | 4 ++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/Cooldown.java b/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/Cooldown.java index 6cbd447..6f38385 100644 --- a/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/Cooldown.java +++ b/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/Cooldown.java @@ -42,7 +42,8 @@ public interface Cooldown extends Command.Builder.Applicable { /** - * Returns a new cooldown with the given {@code duration} using {@link CooldownGroup#command(Command)} as the group. + * Returns a new cooldown with the given {@code duration} + * using {@link CooldownConfiguration#fallbackGroup()} as the group. * * @param command sender type * @param duration the duration @@ -86,7 +87,7 @@ public interface Cooldown extends Command.Builder.Applicable { /** * Returns the group that this instance belongs to. - * If set to {@code null} then {@link CooldownGroup#command(Command)} will be used. + * If set to {@code null} then {@link CooldownConfiguration#fallbackGroup()} will be used. * * @return the cooldown group */ diff --git a/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/CooldownConfiguration.java b/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/CooldownConfiguration.java index f3bf1e0..5cff402 100644 --- a/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/CooldownConfiguration.java +++ b/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/CooldownConfiguration.java @@ -23,9 +23,11 @@ // package org.incendo.cloud.processors.cooldown; +import cloud.commandframework.Command; import cloud.commandframework.context.CommandContext; import java.time.Clock; import java.util.List; +import java.util.function.Function; import java.util.function.Predicate; import org.apiguardian.api.API; import org.checkerframework.checker.nullness.qual.NonNull; @@ -106,4 +108,14 @@ public interface CooldownConfiguration { default @NonNull CooldownProfileFactory profileFactory() { return new StandardCooldownProfileFactory(this); } + + /** + * Returns the function that determines the fallback {@link CooldownGroup} in case no group has been provided + * to the {@link Cooldown}. + * + * @return the fallback group function + */ + default @NonNull Function<@NonNull Command, @NonNull CooldownGroup> fallbackGroup() { + return CooldownGroup::command; + } } diff --git a/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/CooldownPostprocessor.java b/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/CooldownPostprocessor.java index 3cd297e..a978e83 100644 --- a/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/CooldownPostprocessor.java +++ b/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/CooldownPostprocessor.java @@ -68,7 +68,7 @@ public void accept(final @NonNull CommandPostprocessingContext context) { if (cooldown.group() != null) { group = Objects.requireNonNull(cooldown.group(), "group"); } else { - group = CooldownGroup.command(context.command()); + group = this.cooldownManager.configuration().fallbackGroup().apply(context.command()); } final CooldownInstance cooldownInstance = profile.getCooldown(group); diff --git a/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/annotations/Cooldown.java b/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/annotations/Cooldown.java index 093c6fc..2b33b92 100644 --- a/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/annotations/Cooldown.java +++ b/cloud-processors-cooldown/src/main/java/org/incendo/cloud/processors/cooldown/annotations/Cooldown.java @@ -23,7 +23,6 @@ // package org.incendo.cloud.processors.cooldown.annotations; -import cloud.commandframework.Command; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -31,6 +30,7 @@ import java.time.temporal.ChronoUnit; import org.apiguardian.api.API; import org.checkerframework.checker.nullness.qual.NonNull; +import org.incendo.cloud.processors.cooldown.CooldownConfiguration; /** * Adds a cooldown to the command. @@ -59,7 +59,7 @@ /** * Returns the cooldown group. * - *

If this is empty then {@link org.incendo.cloud.processors.cooldown.CooldownGroup#command(Command)} will be used.

+ *

If this is empty then {@link CooldownConfiguration#fallbackGroup()} will be used.

* * @return the cooldown group */