From 7476b35b712812c9e00e0824ef1a6b92d02a9262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sat, 1 Jun 2024 11:38:00 +0200 Subject: [PATCH] feat: convert cooldown examples to snippets --- code/build.gradle.kts | 9 +- code/gradle/libs.versions.toml | 2 + .../snippet/processors/CooldownExample.java | 90 +++++++++++++++++++ docs/processors/cooldown.md | 50 ++--------- 4 files changed, 106 insertions(+), 45 deletions(-) create mode 100644 code/src/main/java/org/incendo/cloud/snippet/processors/CooldownExample.java diff --git a/code/build.gradle.kts b/code/build.gradle.kts index f883304..b70ffc4 100644 --- a/code/build.gradle.kts +++ b/code/build.gradle.kts @@ -4,13 +4,18 @@ plugins { dependencies { implementation(libs.cloud.core) + + // Minecraft implementation(libs.cloud.minecraft.extras) + + // Processors + implementation(libs.cloud.processors.cooldown) } indra { javaVersions { - minimumToolchain(8) - target(8) + minimumToolchain(17) + target(17) } } diff --git a/code/gradle/libs.versions.toml b/code/gradle/libs.versions.toml index e887fc0..e0900b3 100644 --- a/code/gradle/libs.versions.toml +++ b/code/gradle/libs.versions.toml @@ -3,6 +3,7 @@ indra = "3.1.3" cloud = "2.0.0-rc.2" cloudMinecraft = "2.0.0-beta.8" +cloudProcessors = "1.0.0-beta.3" [plugins] indra = { id = "net.kyori.indra", version.ref = "indra" } @@ -10,3 +11,4 @@ indra = { id = "net.kyori.indra", version.ref = "indra" } [libraries] cloud-core = { group = "org.incendo", name = "cloud-core", version.ref = "cloud" } cloud-minecraft-extras = { group = "org.incendo", name = "cloud-minecraft-extras", version.ref = "cloudMinecraft" } +cloud-processors-cooldown = { group = "org.incendo", name = "cloud-processors-cooldown", version.ref = "cloudProcessors" } diff --git a/code/src/main/java/org/incendo/cloud/snippet/processors/CooldownExample.java b/code/src/main/java/org/incendo/cloud/snippet/processors/CooldownExample.java new file mode 100644 index 0000000..5877883 --- /dev/null +++ b/code/src/main/java/org/incendo/cloud/snippet/processors/CooldownExample.java @@ -0,0 +1,90 @@ +package org.incendo.cloud.snippet.processors; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.incendo.cloud.CommandManager; +import org.incendo.cloud.processors.cooldown.Cooldown; +import org.incendo.cloud.processors.cooldown.CooldownConfiguration; +import org.incendo.cloud.processors.cooldown.CooldownGroup; +import org.incendo.cloud.processors.cooldown.CooldownManager; +import org.incendo.cloud.processors.cooldown.CooldownRepository; +import org.incendo.cloud.processors.cooldown.DurationFunction; +import org.incendo.cloud.processors.cooldown.listener.ScheduledCleanupCreationListener; +import org.incendo.cloud.processors.cooldown.profile.CooldownProfile; +import java.time.Duration; +import java.util.HashMap; +import java.util.UUID; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; + +/** + * Example of Cooldown usage. + */ +public class CooldownExample { + + public void configurationExample() { + // --8<-- [start:configuration] + CooldownRepository repository = CooldownRepository.forMap(new HashMap<>()); + CooldownConfiguration configuration = CooldownConfiguration.builder() + // ... + .repository(repository) + .addActiveCooldownListener(((sender, command, cooldown, remainingTime) -> { /* ... */})) + .build(); + // --8<-- [end:configuration] + } + + public void cleanupExample() { + // --8<-- [start:cleanup] + CooldownRepository repository = CooldownRepository.forMap(new HashMap<>()); + ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + CooldownConfiguration configuration = CooldownConfiguration.builder() + // ... + .repository(repository) + .addCreationListener(new ScheduledCleanupCreationListener<>(executorService, repository)) + .build(); + // --8<-- [end:cleanup] + } + + public void registrationExample( + final @NonNull CommandManager commandManager, + final @NonNull CooldownManager cooldownManager + ) { + // --8<-- [start:registration] + commandManager.registerCommandPostProcessor( + cooldownManager.createPostprocessor() + ); + // --8<-- [end:registration] + } + + public void creationExample(final @NonNull CooldownConfiguration configuration) { + // --8<-- [start:creation] + CooldownManager cooldownManager = CooldownManager.cooldownManager( + configuration + ); + // --8<-- [end:creation] + } + + public void mappingExample() { + // --8<-- [start:mapping] + CooldownRepository repository = CooldownRepository.mapping( + YourSenderType::uuid, + CooldownRepository.forMap(new HashMap<>()) + ); + // --8<-- [end:mapping] + } + + public void cooldownExample() { + // --8<-- [start:cooldown] + Cooldown cooldown = Cooldown.of( + DurationFunction.constant(Duration.ofMinutes(5L)) + ); + Cooldown grouped = Cooldown.of( + DurationFunction.constant(Duration.ofMinutes(5L)), + CooldownGroup.named("group-name") + ); + // --8<-- [end:cooldown] + } + + private record YourSenderType(@NonNull UUID uuid) { + + } +} diff --git a/docs/processors/cooldown.md b/docs/processors/cooldown.md index 2b077c5..8e29f8b 100644 --- a/docs/processors/cooldown.md +++ b/docs/processors/cooldown.md @@ -10,23 +10,12 @@ Postprocessor that adds command cooldowns. The cooldown system is managed by a `CooldownManager`, so the first step is to create an instance of that: -```java -CooldownManager cooldownManager = CooldownManager.of( - configuration -); -``` +{{ snippet("processors/CooldownExample.java", section = "creation", title = "") }} The configuration is an instance of `CooldownConfiguration`. Refer to the JavaDocs for information about specific options, but an example would be: -```java -CooldownConfiguration.builder() - .repository(CooldownRepository.forMap(new HashMap<>())) - .addActiveCooldownListener(...) - .addCooldownCreationListener(...) - .cooldownNotifier(notifier) - .build(); -``` +{{ snippet("processors/CooldownExample.java", section = "configuration", title = "") }} 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. @@ -40,49 +29,24 @@ You may create a repository from a map, `CloudCache` or even implement your own. across multiple temporary sessions then you may use a mapping repository to store the cooldown profiles for a persistent key, rather than the potentially temporary command sender objects: -```java -CooldownRepository repository = CooldownRepository.mapping( - sender -> sender.uuid(), - CooldownRepository.forMap(new HashMap()) -); -``` +{{ snippet("processors/CooldownExample.java", section = "mapping", title = "") }} You may also customize how the cooldown profiles are created by passing a `CooldownProfileFactory` to the `CooldownConfiguration`. If you want to have the cooldowns automatically removed from the repository to prevent unused profiles from taking up memory you -may register a `ScheduledCleanupCreationListener`: +may register a `ScheduledCleanupCreationListener` to the configuration, using -```java -CooldownRepository repository = CooldownRepository.forMap(new HashMap<>()); -ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); -CooldownConfiguration configuration = CooldownConfiguration.builder() - // ... - .repository(repository) - .addCreationListener(new ScheduledCleanupCreationListener(executorService, repository)) - .build(); -``` +{{ snippet("processors/CooldownExample.java", section = "cleanup", title = "") }} You then need to register the postprocessor: -```java -commandManager.registerCommandPostProcessor( - cooldownManager.createPostprocessor() -); -``` +{{ snippet("processors/CooldownExample.java", section = "registration", title = "") }} ### Builders The cooldowns are configured using a `Cooldown` instance: -```java -Cooldown cooldown = Cooldown.of( - DurationFunction.constant(Duration.ofMinutes(5L)) -); -Cooldown cooldown = Cooldown.of( - DurationFunction.constant(Duration.ofMinutes(5L)), - CooldownGroup.named("group-name") -); -``` +{{ snippet("processors/CooldownExample.java", section = "cooldown", title = "") }} which can then be applied to the command by either manually setting the meta value: