From b701da15ffc8bed466458b60919bf86d4e995056 Mon Sep 17 00:00:00 2001 From: Zax71 <67716263+zax71@users.noreply.github.com> Date: Wed, 12 Feb 2025 19:19:34 +0000 Subject: [PATCH 1/2] Got the config done but DI is hard --- .../commandtools/queue/CommandQueuePayload.java | 13 +++++++++++-- .../multiverse/core/config/MVCoreConfig.java | 8 ++++++++ .../multiverse/core/config/MVCoreConfigNodes.java | 7 +++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueuePayload.java b/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueuePayload.java index cdd463c4d..eda22a41f 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueuePayload.java +++ b/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueuePayload.java @@ -1,16 +1,20 @@ package org.mvplugins.multiverse.core.commandtools.queue; import co.aikar.commands.ACFUtil; +import jakarta.inject.Inject; import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jvnet.hk2.annotations.Service; import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer; +import org.mvplugins.multiverse.core.config.MVCoreConfig; import org.mvplugins.multiverse.core.locale.message.Message; /** * Represents a single command used in {@link CommandQueueManager} for confirming before running potentially * dangerous action. */ +@Service public class CommandQueuePayload { /** @@ -19,20 +23,25 @@ public class CommandQueuePayload { * @param issuer The issuer of the command * @return The new {@link CommandQueuePayload} */ + public static CommandQueuePayload issuer(@NotNull MVCommandIssuer issuer) { return new CommandQueuePayload(issuer); } - + @Inject + private MVCoreConfig config; // TODO: Why on earth is this null? 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 int validDuration = config.getConfirmTimeout(); private Message prompt = Message.of(DEFAULT_PROMPT_MESSAGE); private BukkitTask expireTask; + + + protected CommandQueuePayload(@NotNull MVCommandIssuer issuer) { this.otp = ACFUtil.rand(100, 999); this.issuer = issuer; diff --git a/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java b/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java index 9f4a44403..23e3156a1 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java @@ -467,6 +467,14 @@ public boolean getUseConfirmOtp() { return configHandle.get(configNodes.useConfirmOtp); } + public Integer getConfirmTimeout() { + return configHandle.get(configNodes.confirmTimeout); + } + + public Try setConfirmTimeout(int confirmTimeout) { + return configHandle.set(configNodes.confirmTimeout, confirmTimeout); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java b/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java index cfdc63145..7e4c707e9 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java @@ -320,6 +320,13 @@ private N node(N node) { .name("use-confirm-otp") .build()); + final ConfigNode confirmTimeout = node(ConfigNode.builder("command.confirm-timeout", Integer.class) + .comment("") + .comment("The amount of time in seconds before `/mv confirm` times out") + .defaultValue(10) + .name("confirm-timeout") + .build()); + private final ConfigHeaderNode miscHeader = node(ConfigHeaderNode.builder("misc") .comment("") .comment("") From 188a60ade7d085082ddd23b244a30d8e6a81da6b Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 13 Feb 2025 13:27:49 +0800 Subject: [PATCH 2/2] Remove validDuration from ConfirmQueuePayload and directly reference config value --- .../queue/CommandQueueManager.java | 4 +-- .../queue/CommandQueuePayload.java | 33 +------------------ .../core/config/MVCoreConfigNodes.java | 5 ++- src/test/resources/fresh_config.yml | 1 + 4 files changed, 8 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueueManager.java b/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueueManager.java index 0d4a5ff7d..333b40289 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueueManager.java +++ b/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueueManager.java @@ -68,7 +68,7 @@ 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"; @@ -76,7 +76,7 @@ public void addToQueue(CommandQueuePayload payload) { 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())); } /** diff --git a/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueuePayload.java b/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueuePayload.java index eda22a41f..be4936b9f 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueuePayload.java +++ b/src/main/java/org/mvplugins/multiverse/core/commandtools/queue/CommandQueuePayload.java @@ -1,20 +1,16 @@ package org.mvplugins.multiverse.core.commandtools.queue; import co.aikar.commands.ACFUtil; -import jakarta.inject.Inject; import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jvnet.hk2.annotations.Service; import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer; -import org.mvplugins.multiverse.core.config.MVCoreConfig; import org.mvplugins.multiverse.core.locale.message.Message; /** * Represents a single command used in {@link CommandQueueManager} for confirming before running potentially * dangerous action. */ -@Service public class CommandQueuePayload { /** @@ -23,25 +19,18 @@ public class CommandQueuePayload { * @param issuer The issuer of the command * @return The new {@link CommandQueuePayload} */ - public static CommandQueuePayload issuer(@NotNull MVCommandIssuer issuer) { return new CommandQueuePayload(issuer); } - @Inject - private MVCoreConfig config; // TODO: Why on earth is this null? + 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 = config.getConfirmTimeout(); private Message prompt = Message.of(DEFAULT_PROMPT_MESSAGE); private BukkitTask expireTask; - - - protected CommandQueuePayload(@NotNull MVCommandIssuer issuer) { this.otp = ACFUtil.rand(100, 999); this.issuer = issuer; @@ -85,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. * diff --git a/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java b/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java index 7e4c707e9..0c34dd8fb 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java @@ -323,8 +323,11 @@ private N node(N node) { final ConfigNode confirmTimeout = node(ConfigNode.builder("command.confirm-timeout", Integer.class) .comment("") .comment("The amount of time in seconds before `/mv confirm` times out") - .defaultValue(10) + .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") diff --git a/src/test/resources/fresh_config.yml b/src/test/resources/fresh_config.yml index c4be66260..b548615c3 100644 --- a/src/test/resources/fresh_config.yml +++ b/src/test/resources/fresh_config.yml @@ -33,6 +33,7 @@ command: resolve-alias-name: true confirm-mode: enable use-confirm-otp: true + confirm-timeout: 30 misc: global-debug: 0