Skip to content

Commit

Permalink
rename CooldownCondition to ThreadExecutorCooldownCondition and fix n…
Browse files Browse the repository at this point in the history
…ullability problem
  • Loading branch information
Revxrsal committed Dec 29, 2024
1 parent 960b8a3 commit 24f02b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
5 changes: 3 additions & 2 deletions common/src/main/java/revxrsal/commands/Lamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,9 @@ public Builder() {
responseHandler(CompletionStageResponseHandler.INSTANCE);
responseHandler(OptionalResponseHandler.INSTANCE);
commandCondition(PermissionConditionChecker.INSTANCE);
commandCondition(CooldownCondition.INSTANCE);
hooks().onPostCommandExecuted(CooldownCondition.INSTANCE);
ThreadExecutorCooldownCondition cooldownCondition = new ThreadExecutorCooldownCondition();
commandCondition(cooldownCondition);
hooks().onPostCommandExecuted(cooldownCondition);
accept(KotlinFeatureRegistry.INSTANCE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import revxrsal.commands.annotation.Cooldown;
import revxrsal.commands.exception.CooldownException;
import revxrsal.commands.hook.PostCommandExecutedHook;
Expand All @@ -38,11 +39,10 @@
import java.util.concurrent.ScheduledExecutorService;

@ApiStatus.Internal
public enum CooldownCondition implements CommandCondition<CommandActor>, PostCommandExecutedHook<CommandActor> {

INSTANCE;
public final class ThreadExecutorCooldownCondition implements CommandCondition<CommandActor>, PostCommandExecutedHook<CommandActor> {

private static final ScheduledExecutorService COOLDOWN_POOL = Executors.newSingleThreadScheduledExecutor();

private final Map<UUID, Map<Integer, Long>> cooldowns = new ConcurrentHashMap<>();

@Override
Expand All @@ -55,12 +55,16 @@ public void onPostExecuted(@NotNull ExecutableCommand<CommandActor> command, @No
}

@Override public void test(@NotNull ExecutionContext<CommandActor> context) {
Cooldown cooldown = context.command().annotations().get(Cooldown.class);
if (cooldown == null || cooldown.value() == 0) return;
@Nullable Cooldown cooldown = context.command().annotations().get(Cooldown.class);
if (cooldown == null || cooldown.value() == 0)
return;
UUID uuid = context.actor().uniqueId();
Map<Integer, Long> spans = cooldowns.get(uuid);
if (spans == null) return;
long created = spans.get(context.command().hashCode());
@Nullable Map<Integer, Long> spans = cooldowns.get(uuid);
if (spans == null)
return;
@Nullable Long created = spans.get(context.command().hashCode());
if (created == null)
return;
long passed = System.currentTimeMillis() - created;
long left = cooldown.unit().toMillis(cooldown.value()) - passed;
if (left > 0 && left < 1000)
Expand Down

0 comments on commit 24f02b8

Please sign in to comment.