Skip to content

Commit

Permalink
Merge pull request #3169 from Multiverse/zax71/MV5/confirm-command-cu…
Browse files Browse the repository at this point in the history
…stom-timeout

Add config option for `/mv confirm` timeout
  • Loading branch information
benwoo1110 authored Feb 13, 2025
2 parents eb20386 + 188a60a commit 9956142
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public void addToQueue(CommandQueuePayload payload) {

Logging.finer("Add new command to queue for sender %s.", senderName);
this.queuedCommandMap.put(senderName, payload);
payload.expireTask(runExpireLater(senderName, payload.validDuration()));
payload.expireTask(runExpireLater(senderName, config.getConfirmTimeout()));

payload.issuer().sendInfo(payload.prompt());
var confirmCommand = "/mv confirm";
if (config.getUseConfirmOtp()) {
confirmCommand += " " + payload.otp();
}
payload.issuer().sendMessage(String.format("Run %s%s %sto continue. This will expire in %s seconds.",
ChatColor.GREEN, confirmCommand, ChatColor.WHITE, payload.validDuration()));
ChatColor.GREEN, confirmCommand, ChatColor.WHITE, config.getConfirmTimeout()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ public static CommandQueuePayload issuer(@NotNull MVCommandIssuer issuer) {
}

private static final String DEFAULT_PROMPT_MESSAGE = "The command you are trying to run is deemed dangerous."; // todo: localize
private static final int DEFAULT_VALID_TIME = 10;

private final int otp;
private final MVCommandIssuer issuer;
private Runnable action = () -> {};
private int validDuration = DEFAULT_VALID_TIME;
private Message prompt = Message.of(DEFAULT_PROMPT_MESSAGE);
private BukkitTask expireTask;

Expand Down Expand Up @@ -76,26 +74,6 @@ public int otp() {
return otp;
}

/**
* Sets the duration in which the command is valid for confirm in seconds.
*
* @param validDuration The target duration in seconds.
* @return The same {@link CommandQueuePayload} for method chaining.
*/
public CommandQueuePayload validDuration(int validDuration) {
this.validDuration = validDuration;
return this;
}

/**
* Gets the duration in which the command is valid for confirm in seconds.
*
* @return The duration in seconds.
*/
public int validDuration() {
return validDuration;
}

/**
* Sets the question to ask sender to confirm.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,14 @@ public boolean getUseConfirmOtp() {
return configHandle.get(configNodes.useConfirmOtp);
}

public Integer getConfirmTimeout() {
return configHandle.get(configNodes.confirmTimeout);
}

public Try<Void> setConfirmTimeout(int confirmTimeout) {
return configHandle.set(configNodes.confirmTimeout, confirmTimeout);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ private <N extends Node> N node(N node) {
.name("use-confirm-otp")
.build());

final ConfigNode<Integer> confirmTimeout = node(ConfigNode.builder("command.confirm-timeout", Integer.class)
.comment("")
.comment("The amount of time in seconds before `/mv confirm` times out")
.defaultValue(30)
.name("confirm-timeout")
.validator(value -> (value <= 0)
? Try.failure(new MultiverseException("Confirm timeout must be a positive number!"))
: Try.success(null))
.build());

private final ConfigHeaderNode miscHeader = node(ConfigHeaderNode.builder("misc")
.comment("")
.comment("")
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/fresh_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ command:
resolve-alias-name: true
confirm-mode: enable
use-confirm-otp: true
confirm-timeout: 30

misc:
global-debug: 0
Expand Down

0 comments on commit 9956142

Please sign in to comment.