Skip to content

Commit

Permalink
feat: convert cooldown examples to snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Jun 1, 2024
1 parent 02e90af commit 7476b35
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 45 deletions.
9 changes: 7 additions & 2 deletions code/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 2 additions & 0 deletions code/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ 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" }

[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" }
Original file line number Diff line number Diff line change
@@ -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<YourSenderType> repository = CooldownRepository.forMap(new HashMap<>());
CooldownConfiguration<YourSenderType> configuration = CooldownConfiguration.<YourSenderType>builder()
// ...
.repository(repository)
.addActiveCooldownListener(((sender, command, cooldown, remainingTime) -> { /* ... */}))
.build();
// --8<-- [end:configuration]
}

public void cleanupExample() {
// --8<-- [start:cleanup]
CooldownRepository<YourSenderType> repository = CooldownRepository.forMap(new HashMap<>());
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
CooldownConfiguration<YourSenderType> configuration = CooldownConfiguration.<YourSenderType>builder()
// ...
.repository(repository)
.addCreationListener(new ScheduledCleanupCreationListener<>(executorService, repository))
.build();
// --8<-- [end:cleanup]
}

public void registrationExample(
final @NonNull CommandManager<YourSenderType> commandManager,
final @NonNull CooldownManager<YourSenderType> cooldownManager
) {
// --8<-- [start:registration]
commandManager.registerCommandPostProcessor(
cooldownManager.createPostprocessor()
);
// --8<-- [end:registration]
}

public void creationExample(final @NonNull CooldownConfiguration<YourSenderType> configuration) {
// --8<-- [start:creation]
CooldownManager<YourSenderType> cooldownManager = CooldownManager.cooldownManager(
configuration
);
// --8<-- [end:creation]
}

public void mappingExample() {
// --8<-- [start:mapping]
CooldownRepository<YourSenderType> repository = CooldownRepository.mapping(
YourSenderType::uuid,
CooldownRepository.forMap(new HashMap<>())
);
// --8<-- [end:mapping]
}

public void cooldownExample() {
// --8<-- [start:cooldown]
Cooldown<YourSenderType> cooldown = Cooldown.of(
DurationFunction.constant(Duration.ofMinutes(5L))
);
Cooldown<YourSenderType> grouped = Cooldown.of(
DurationFunction.constant(Duration.ofMinutes(5L)),
CooldownGroup.named("group-name")
);
// --8<-- [end:cooldown]
}

private record YourSenderType(@NonNull UUID uuid) {

}
}
50 changes: 7 additions & 43 deletions docs/processors/cooldown.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<YourSenderType> 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.<YourSenderType>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.
Expand All @@ -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<UUID, CooldownProfile>())
);
```
{{ 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.<YourSenderType>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:

Expand Down

0 comments on commit 7476b35

Please sign in to comment.