Skip to content

Commit

Permalink
Added permission for @cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Nov 2, 2024
1 parent b348bb9 commit 2c81d35
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@

TimeUnit unit();

String permission() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,10 @@ private CommandUsage<S> loadUsage(
imperat.replacePlaceholders(permission.value())
);

if (cooldown != null)
builder.cooldown(cooldown.value(), cooldown.unit());
if (cooldown != null) {
String cooldownPerm = cooldown.permission();
builder.cooldown(cooldown.value(), cooldown.unit(), cooldownPerm.isEmpty() ? null : cooldownPerm);
}

if (async != null)
builder.coordinator(CommandCoordinator.async());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ public Builder<S> permission(String permission) {
}

public Builder<S> cooldown(long value, TimeUnit unit) {
this.cooldown = new UsageCooldown(value, unit);
return cooldown(value, unit, null);
}

public Builder<S> cooldown(long value, TimeUnit unit, @Nullable String permission) {
this.cooldown = new UsageCooldown(value, unit, permission);
return this;
}

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

import dev.velix.imperat.Imperat;
import dev.velix.imperat.command.cooldown.CooldownHandler;
import dev.velix.imperat.command.cooldown.DefaultCooldownHandler;
import dev.velix.imperat.command.cooldown.UsageCooldown;
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.context.ExecutionContext;
Expand Down Expand Up @@ -40,7 +39,7 @@ final class CommandUsageImpl<S extends Source> implements CommandUsage<S> {

CommandUsageImpl(@NotNull CommandExecution<S> execution, boolean help) {
this.execution = execution;
this.cooldownHandler = new DefaultCooldownHandler<>(this);
this.cooldownHandler = CooldownHandler.createDefault(this);
this.commandCoordinator = CommandCoordinator.sync();
this.help = help;
}
Expand Down Expand Up @@ -96,7 +95,7 @@ public boolean hasFlag(String input) {
* @return the flag from the raw input, null if it cannot be a flag
*/
@Override
public @Nullable FlagData getFlagFromRaw(String rawInput) {
public @Nullable FlagData<S> getFlagFromRaw(String rawInput) {
boolean isSingle = SINGLE_FLAG.matcher(rawInput).matches();
boolean isDouble = DOUBLE_FLAG.matcher(rawInput).matches();

Expand All @@ -108,7 +107,7 @@ public boolean hasFlag(String input) {

for (var param : parameters) {
if (!param.isFlag()) continue;
FlagData flag = param.asFlagParameter().flagData();
FlagData<S> flag = param.asFlagParameter().flagData();
if (flag.acceptsInput(inputFlagAlias)) {
return flag;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ public interface CooldownHolder {
void setCooldown(UsageCooldown cooldown);

default void setCooldown(long value, TimeUnit unit) {
setCooldown(new UsageCooldown(value, unit));
setCooldown(value, unit, null);
}

default void setCooldown(long value, TimeUnit unit, @Nullable String permission) {
setCooldown(new UsageCooldown(value, unit, permission));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ default boolean hasCooldown(S source) {
* @return the last time the sender executed {@link CommandUsage}
*/
Optional<Long> getLastTimeExecuted(S source);

static <S extends Source> CooldownHandler<S> createDefault(CommandUsage<S> usage) {
return new DefaultCooldownHandler<>(usage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import java.util.Map;
import java.util.Optional;

public final class DefaultCooldownHandler<S extends Source> implements CooldownHandler<S> {
final class DefaultCooldownHandler<S extends Source> implements CooldownHandler<S> {

private final Map<S, Long> lastTimeExecuted = new HashMap<>();
private final CommandUsage<S> usage;

public DefaultCooldownHandler(CommandUsage<S> usage) {
DefaultCooldownHandler(CommandUsage<S> usage) {
this.usage = usage;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.velix.imperat.command.cooldown;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
Expand All @@ -14,7 +15,7 @@
* @see Duration
*/
@ApiStatus.AvailableSince("1.0.0")
public record UsageCooldown(long value, TimeUnit unit) {
public record UsageCooldown(long value, TimeUnit unit, @Nullable String permission) {

public long toMillis() {
return unit.toMillis(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ public void process(
CommandUsage<S> usage
) throws ImperatException {
var source = context.source();
if (usage.getCooldownHandler().hasCooldown(source)) {
throw new CooldownException(
usage.getCooldownHandler().getUsageCooldown().orElseThrow().toMillis(),
usage.getCooldownHandler().getLastTimeExecuted(source).orElse(0L)
);
var handler = usage.getCooldownHandler();
var cooldown = usage.getCooldown();

if (handler.hasCooldown(source)) {
assert cooldown != null;
if (!imperat.getPermissionResolver().hasPermission(source, cooldown.permission())) {
throw new CooldownException(
cooldown.toMillis(),
handler.getLastTimeExecuted(source).orElse(0L)
);
}
}
usage.getCooldownHandler().registerExecutionMoment(source);
handler.registerExecutionMoment(source);
}

}
34 changes: 34 additions & 0 deletions core/src/test/java/dev/velix/imperat/CustomCooldownProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.velix.imperat;

import dev.velix.imperat.command.CommandUsage;
import dev.velix.imperat.command.cooldown.CooldownHandler;
import dev.velix.imperat.command.processors.CommandPreProcessor;
import dev.velix.imperat.context.Context;
import dev.velix.imperat.context.Source;
import dev.velix.imperat.exception.CooldownException;
import dev.velix.imperat.exception.ImperatException;

public final class CustomCooldownProcessor<S extends Source> implements CommandPreProcessor<S> {
/**
* Processes context BEFORE the resolving operation.
*
* @param imperat the api
* @param context the context
* @param usage The usage detected
* @throws ImperatException the exception to throw if something happens
*/
@Override
public void process(Imperat<S> imperat, Context<S> context, CommandUsage<S> usage) throws ImperatException {
CooldownHandler<S> cooldownHandler = usage.getCooldownHandler();
S source = context.source();

if (cooldownHandler.hasCooldown(source) && !imperat.getPermissionResolver().hasPermission(source, "yourpermission")) {
//if there's a cooldown and the source doesn't have a specific permission, let's send him the cooldown message through the exception below
throw new CooldownException(
cooldownHandler.getUsageCooldown().orElseThrow().toMillis(),
cooldownHandler.getLastTimeExecuted(source).orElse(0L)
);
}

}
}

0 comments on commit 2c81d35

Please sign in to comment.