Skip to content

Commit

Permalink
turn the notifier into a listener collection instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Jan 6, 2024
1 parent f155f42 commit 03f67f6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
6 changes: 4 additions & 2 deletions cloud-processors-cooldown/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ but an example would be:
```java
CooldownConfiguration configuration = CooldownConfiguration.<YourSenderType>builder()
.repository(CooldownRepository.forMap(new HashMap<>()))
.addActiveCooldownListener(...)
.addCooldownCreationListener(...)
.cooldownNotifier(notifier)
.build();
```
The notifier is an interface that is invoked when a sender is prevented from using a command due to an
active cooldown.
The listeners are invoked when different events take place. The active cooldown listener in particular may be used to
inform the command sender that their command execution got blocked due to an active cooldown.

The repository stores active cooldowns for a command sender in the form of cooldown profiles.
The cooldowns are grouped by their `CooldownGroup`, by default a unique group will be created per command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.immutables.value.Value;
import org.incendo.cloud.processors.cooldown.listener.CooldownActiveListener;
import org.incendo.cloud.processors.cooldown.listener.CooldownCreationListener;
import org.incendo.cloud.processors.cooldown.profile.CooldownProfileFactory;
import org.incendo.cloud.processors.cooldown.profile.StandardCooldownProfileFactory;
Expand Down Expand Up @@ -66,16 +67,19 @@ public interface CooldownConfiguration<C> {
@NonNull CooldownRepository<C> repository();

/**
* Returns a consumer that gets invoked when a cooldown is active and is preventing the command
* from executing.
* Returns the active cooldown listeners.
*
* @return notifier for active cooldowns
* <p>The active cooldown listeners are invoked when a cooldown is preventing a sender from executing a command.</p>
*
* @return active cooldown listeners
*/
@NonNull CooldownNotifier<C> cooldownNotifier();
@NonNull List<CooldownActiveListener<C>> activeCooldownListeners();

/**
* Returns the creation listeners.
*
* <p>The creation listeners are invoked when a new cooldown is created.</p>
*
* @return creation listeners
*/
@NonNull List<CooldownCreationListener<C>> creationListeners();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ public void accept(final @NonNull CommandPostprocessingContext<C> context) {
final CooldownInstance cooldownInstance = profile.getCooldown(group);
if (cooldownInstance != null) {
final Instant endTime = cooldownInstance.creationTime().plus(cooldownInstance.duration());
this.cooldownManager.configuration().cooldownNotifier().notify(
final Duration remainingTime = Duration.between(Instant.now(this.cooldownManager.configuration().clock()), endTime);
this.cooldownManager.configuration().activeCooldownListeners().forEach(listener -> listener.cooldownActive(
context.commandContext().sender(),
context.command(),
cooldownInstance,
Duration.between(Instant.now(this.cooldownManager.configuration().clock()), endTime)
);
remainingTime
));
ConsumerService.interrupt();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.processors.cooldown;
package org.incendo.cloud.processors.cooldown.listener;

import cloud.commandframework.Command;
import java.time.Duration;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.processors.cooldown.CooldownInstance;

/**
* Notifies command senders that they have an active cooldown.
Expand All @@ -34,24 +36,20 @@
* @since 1.0.0
*/
@API(status = API.Status.STABLE, since = "1.0.0")
public interface CooldownNotifier<C> {
public interface CooldownActiveListener<C> {

/**
* Returns a notifier that does nothing.
*
* @param <C> command sender type
* @return the notifier
*/
static <C> @NonNull CooldownNotifier<C> noOp() {
return (sender, cooldown, remainingTime) -> {};
}

/**
* Notifies the given {@code sender} that they have an active cooldown preventing them from using the command.
* Invoked when a cooldown is preventing the {@code sender} from executing a command.
*
* @param sender command sender to notify
* @param command command that could not be executed
* @param cooldown cooldown instance
* @param remainingTime remaining time
*/
void notify(@NonNull C sender, @NonNull CooldownInstance cooldown, @NonNull Duration remainingTime);
void cooldownActive(
@NonNull C sender,
@NonNull Command<C> command,
@NonNull CooldownInstance cooldown,
@NonNull Duration remainingTime
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import org.incendo.cloud.processors.cooldown.CooldownGroup;
import org.incendo.cloud.processors.cooldown.CooldownInstance;
import org.incendo.cloud.processors.cooldown.CooldownManager;
import org.incendo.cloud.processors.cooldown.CooldownNotifier;
import org.incendo.cloud.processors.cooldown.CooldownRepository;
import org.incendo.cloud.processors.cooldown.DurationFunction;
import org.incendo.cloud.processors.cooldown.listener.CooldownActiveListener;
import org.incendo.cloud.processors.cooldown.listener.CooldownCreationListener;
import org.incendo.cloud.processors.cooldown.profile.CooldownProfile;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -62,7 +62,7 @@ class CooldownManagerTest {
@Mock
private TestCommandSender commandSender;
@Mock
private CooldownNotifier<TestCommandSender> notifier;
private CooldownActiveListener<TestCommandSender> notifier;
@Mock(strictness = Mock.Strictness.LENIENT)
private CommandExecutionHandler<TestCommandSender> commandExecutionHandler;
@Mock
Expand All @@ -79,9 +79,9 @@ void setup() {
this.cooldownManager = CooldownManager.of(
CooldownConfiguration.<TestCommandSender>builder()
.repository(CooldownRepository.forMap(new HashMap<>()))
.cooldownNotifier(this.notifier)
.addActiveCooldownListener(this.notifier)
.clock(this.clock)
.addCreationListeners(this.listener)
.addCreationListener(this.listener)
.build()
);
this.commandManager.registerCommandPostProcessor(this.cooldownManager.createPostprocessor());
Expand All @@ -106,7 +106,7 @@ void testAppliesCooldown() {

// Assert
verify(this.commandExecutionHandler).executeFuture(any());
verify(this.notifier).notify(eq(this.commandSender), any(), any());
verify(this.notifier).cooldownActive(eq(this.commandSender), any(), any(), any());
verify(this.listener).cooldownCreated(eq(this.commandSender), any(), any());
}

Expand All @@ -129,7 +129,7 @@ void testCooldownExpired() {

// Assert
verify(this.commandExecutionHandler, times(2)).executeFuture(any());
verify(this.notifier, never()).notify(eq(this.commandSender), any(), any());
verify(this.notifier, never()).cooldownActive(eq(this.commandSender), any(), any(), any());
verify(this.listener, times(2)).cooldownCreated(eq(this.commandSender), any(), any());
}

Expand Down Expand Up @@ -164,7 +164,7 @@ void testCooldownGroupsDifferentGroups() {

// Assert
verify(this.commandExecutionHandler, times(2)).executeFuture(any());
verify(this.notifier, never()).notify(eq(this.commandSender), any(), any());
verify(this.notifier, never()).cooldownActive(eq(this.commandSender), any(), any(), any());
verify(this.listener, times(2)).cooldownCreated(eq(this.commandSender), any(), any());
}

Expand Down Expand Up @@ -195,7 +195,7 @@ void testCooldownGroupsSameGroup() {

// Assert
verify(this.commandExecutionHandler, times(1)).executeFuture(any());
verify(this.notifier).notify(eq(this.commandSender), any(), any());
verify(this.notifier).cooldownActive(eq(this.commandSender), any(), any(), any());
verify(this.listener).cooldownCreated(eq(this.commandSender), any(), any());
}

Expand Down

0 comments on commit 03f67f6

Please sign in to comment.