From 259189f23e007f6a2ee39dc5e503167729919d6a Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Thu, 30 Mar 2023 01:41:25 -0400 Subject: [PATCH] Use specific exception for not found node. --- .../commands/ConfigCommand.java | 19 ++++++++++++++----- .../handle/FileConfigHandle.java | 8 +++++--- .../node/ConfigNodeNotFoundException.java | 14 ++++++++++++++ .../MultiverseCore/utils/MVCorei18n.java | 1 + .../resources/multiverse-core_en.properties | 2 +- 5 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/configuration/node/ConfigNodeNotFoundException.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java index fc1e5cdf..60c14b5b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java @@ -1,6 +1,5 @@ package com.onarandombox.MultiverseCore.commands; -import co.aikar.commands.BukkitCommandIssuer; import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandPermission; @@ -9,10 +8,12 @@ import co.aikar.commands.annotation.Optional; import co.aikar.commands.annotation.Single; import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; +import com.onarandombox.MultiverseCore.commandtools.MVCommandIssuer; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue; import com.onarandombox.MultiverseCore.config.MVCoreConfig; +import com.onarandombox.MultiverseCore.exceptions.MultiverseException; import jakarta.inject.Inject; import org.jetbrains.annotations.NotNull; import org.jvnet.hk2.annotations.Service; @@ -34,7 +35,7 @@ public class ConfigCommand extends MultiverseCommand { @CommandCompletion("@mvconfigs") @Syntax(" [new-value]") @Description("") //TODO - public void onConfigCommand(BukkitCommandIssuer issuer, + public void onConfigCommand(MVCommandIssuer issuer, @Syntax("") @Description("") //TODO @@ -53,18 +54,26 @@ public class ConfigCommand extends MultiverseCommand { updateConfigValue(issuer, name, value.getValue()); } - private void showConfigValue(BukkitCommandIssuer issuer, String name) { + private void showConfigValue(MVCommandIssuer issuer, String name) { config.getProperty(name) .onSuccess(value -> issuer.sendMessage(name + "is currently set to " + value)) .onFailure(e -> issuer.sendMessage("Unable to get " + name + ": " + e.getMessage())); } - private void updateConfigValue(BukkitCommandIssuer issuer, String name, Object value) { + private void updateConfigValue(MVCommandIssuer issuer, String name, Object value) { config.setProperty(name, value) .onSuccess(ignore -> { config.save(); issuer.sendMessage("Successfully set " + name + " to " + value); }) - .onFailure(e -> issuer.sendMessage("Unable to set " + name + " to " + value + ": " + e.getMessage())); + .onFailure(e -> { + issuer.sendMessage("Unable to set " + name + " to " + value + "."); + if (e instanceof MultiverseException) { + var message = ((MultiverseException) e).getMVMessage(); + if (message != null) { + issuer.sendError(message); + } + } + }); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java index a3d60a55..f5742fb5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java @@ -7,8 +7,10 @@ import java.util.logging.Logger; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator; +import com.onarandombox.MultiverseCore.configuration.node.ConfigNodeNotFoundException; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import com.onarandombox.MultiverseCore.configuration.node.ValueNode; +import com.onarandombox.MultiverseCore.exceptions.MultiverseException; import io.vavr.control.Try; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.Plugin; @@ -127,7 +129,7 @@ abstract class FileConfigHandle { */ public Try get(@Nullable String name) { return nodes.findNode(name, ValueNode.class) - .toTry(() -> new Exception("Node not found")) + .toTry(() -> new ConfigNodeNotFoundException(name)) .map(node -> get((ValueNode) node)); } @@ -150,7 +152,7 @@ abstract class FileConfigHandle { */ public Try set(@Nullable String name, Object value) { return nodes.findNode(name, ValueNode.class) - .toTry(() -> new Exception("Node not found")) + .toTry(() -> new ConfigNodeNotFoundException(name)) .flatMap(node -> set(node, value)); } @@ -164,7 +166,7 @@ abstract class FileConfigHandle { */ public Try set(@NotNull ValueNode node, T value) { if (!node.validate(value)) { - return Try.failure(new Exception("Validation failed")); + return Try.failure(new MultiverseException("Validation failed", null)); // TODO replace validation } T oldValue = get(node); config.set(node.getPath(), value); diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ConfigNodeNotFoundException.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ConfigNodeNotFoundException.java new file mode 100644 index 00000000..3917a17e --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ConfigNodeNotFoundException.java @@ -0,0 +1,14 @@ +package com.onarandombox.MultiverseCore.configuration.node; + +import com.onarandombox.MultiverseCore.exceptions.MultiverseException; +import org.jetbrains.annotations.Nullable; + +import static com.onarandombox.MultiverseCore.utils.MVCorei18n.CONFIG_NODE_NOTFOUND; +import static com.onarandombox.MultiverseCore.utils.message.MessageReplacement.replace; + +public class ConfigNodeNotFoundException extends MultiverseException { + + public ConfigNodeNotFoundException(@Nullable String nodeName) { + super(CONFIG_NODE_NOTFOUND.bundle("Config node not found: {node}", replace("{node}").with(nodeName)), null); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/MVCorei18n.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MVCorei18n.java index 1e5cbe35..388b619d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/MVCorei18n.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MVCorei18n.java @@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull; public enum MVCorei18n implements MessageKeyProvider { // config status CONFIG_SAVE_FAILED, + CONFIG_NODE_NOTFOUND, // check command CHECK_CHECKING, diff --git a/src/main/resources/multiverse-core_en.properties b/src/main/resources/multiverse-core_en.properties index 0cd452ea..31ed2ea7 100644 --- a/src/main/resources/multiverse-core_en.properties +++ b/src/main/resources/multiverse-core_en.properties @@ -1,5 +1,6 @@ # configuration mv-core.config.save.failed=Unable to save Multiverse-Core config.yml. Your changes will be temporary! +mv-core.config.node.notfound=Node not found in config: {node} # /mv check mv-core.check.description=Checks if a player can teleport to a destination. @@ -113,4 +114,3 @@ mv-core.unload.success=&aUnloaded world '{world}'! # /mv usage mv-core.usage.description=Show Multiverse-Core command usage. -