Use specific exception for not found node.

This commit is contained in:
Jeremy Wood 2023-03-30 01:41:25 -04:00
parent 2a5b670ce6
commit 259189f23e
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B
5 changed files with 35 additions and 9 deletions

View File

@ -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("<name> [new-value]")
@Description("") //TODO
public void onConfigCommand(BukkitCommandIssuer issuer,
public void onConfigCommand(MVCommandIssuer issuer,
@Syntax("<name>")
@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);
}
}
});
}
}

View File

@ -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<C extends FileConfiguration> {
*/
public Try<Object> get(@Nullable String name) {
return nodes.findNode(name, ValueNode.class)
.toTry(() -> new Exception("Node not found"))
.toTry(() -> new ConfigNodeNotFoundException(name))
.map(node -> get((ValueNode<Object>) node));
}
@ -150,7 +152,7 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
*/
public Try<Void> 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<C extends FileConfiguration> {
*/
public <T> Try<Void> set(@NotNull ValueNode<T> 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);

View File

@ -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);
}
}

View File

@ -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,

View File

@ -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.