mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2025-01-10 18:28:28 +01:00
Use specific exception for not found node.
This commit is contained in:
parent
2a5b670ce6
commit
259189f23e
@ -1,6 +1,5 @@
|
|||||||
package com.onarandombox.MultiverseCore.commands;
|
package com.onarandombox.MultiverseCore.commands;
|
||||||
|
|
||||||
import co.aikar.commands.BukkitCommandIssuer;
|
|
||||||
import co.aikar.commands.annotation.CommandAlias;
|
import co.aikar.commands.annotation.CommandAlias;
|
||||||
import co.aikar.commands.annotation.CommandCompletion;
|
import co.aikar.commands.annotation.CommandCompletion;
|
||||||
import co.aikar.commands.annotation.CommandPermission;
|
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.Single;
|
||||||
import co.aikar.commands.annotation.Subcommand;
|
import co.aikar.commands.annotation.Subcommand;
|
||||||
import co.aikar.commands.annotation.Syntax;
|
import co.aikar.commands.annotation.Syntax;
|
||||||
|
import com.onarandombox.MultiverseCore.commandtools.MVCommandIssuer;
|
||||||
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
|
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
|
||||||
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
|
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
|
||||||
import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue;
|
import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue;
|
||||||
import com.onarandombox.MultiverseCore.config.MVCoreConfig;
|
import com.onarandombox.MultiverseCore.config.MVCoreConfig;
|
||||||
|
import com.onarandombox.MultiverseCore.exceptions.MultiverseException;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jvnet.hk2.annotations.Service;
|
import org.jvnet.hk2.annotations.Service;
|
||||||
@ -34,7 +35,7 @@ public class ConfigCommand extends MultiverseCommand {
|
|||||||
@CommandCompletion("@mvconfigs")
|
@CommandCompletion("@mvconfigs")
|
||||||
@Syntax("<name> [new-value]")
|
@Syntax("<name> [new-value]")
|
||||||
@Description("") //TODO
|
@Description("") //TODO
|
||||||
public void onConfigCommand(BukkitCommandIssuer issuer,
|
public void onConfigCommand(MVCommandIssuer issuer,
|
||||||
|
|
||||||
@Syntax("<name>")
|
@Syntax("<name>")
|
||||||
@Description("") //TODO
|
@Description("") //TODO
|
||||||
@ -53,18 +54,26 @@ public class ConfigCommand extends MultiverseCommand {
|
|||||||
updateConfigValue(issuer, name, value.getValue());
|
updateConfigValue(issuer, name, value.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showConfigValue(BukkitCommandIssuer issuer, String name) {
|
private void showConfigValue(MVCommandIssuer issuer, String name) {
|
||||||
config.getProperty(name)
|
config.getProperty(name)
|
||||||
.onSuccess(value -> issuer.sendMessage(name + "is currently set to " + value))
|
.onSuccess(value -> issuer.sendMessage(name + "is currently set to " + value))
|
||||||
.onFailure(e -> issuer.sendMessage("Unable to get " + name + ": " + e.getMessage()));
|
.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)
|
config.setProperty(name, value)
|
||||||
.onSuccess(ignore -> {
|
.onSuccess(ignore -> {
|
||||||
config.save();
|
config.save();
|
||||||
issuer.sendMessage("Successfully set " + name + " to " + value);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,10 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import com.dumptruckman.minecraft.util.Logging;
|
import com.dumptruckman.minecraft.util.Logging;
|
||||||
import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator;
|
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.NodeGroup;
|
||||||
import com.onarandombox.MultiverseCore.configuration.node.ValueNode;
|
import com.onarandombox.MultiverseCore.configuration.node.ValueNode;
|
||||||
|
import com.onarandombox.MultiverseCore.exceptions.MultiverseException;
|
||||||
import io.vavr.control.Try;
|
import io.vavr.control.Try;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
@ -127,7 +129,7 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
|
|||||||
*/
|
*/
|
||||||
public Try<Object> get(@Nullable String name) {
|
public Try<Object> get(@Nullable String name) {
|
||||||
return nodes.findNode(name, ValueNode.class)
|
return nodes.findNode(name, ValueNode.class)
|
||||||
.toTry(() -> new Exception("Node not found"))
|
.toTry(() -> new ConfigNodeNotFoundException(name))
|
||||||
.map(node -> get((ValueNode<Object>) node));
|
.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) {
|
public Try<Void> set(@Nullable String name, Object value) {
|
||||||
return nodes.findNode(name, ValueNode.class)
|
return nodes.findNode(name, ValueNode.class)
|
||||||
.toTry(() -> new Exception("Node not found"))
|
.toTry(() -> new ConfigNodeNotFoundException(name))
|
||||||
.flatMap(node -> set(node, value));
|
.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) {
|
public <T> Try<Void> set(@NotNull ValueNode<T> node, T value) {
|
||||||
if (!node.validate(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);
|
T oldValue = get(node);
|
||||||
config.set(node.getPath(), value);
|
config.set(node.getPath(), value);
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
public enum MVCorei18n implements MessageKeyProvider {
|
public enum MVCorei18n implements MessageKeyProvider {
|
||||||
// config status
|
// config status
|
||||||
CONFIG_SAVE_FAILED,
|
CONFIG_SAVE_FAILED,
|
||||||
|
CONFIG_NODE_NOTFOUND,
|
||||||
|
|
||||||
// check command
|
// check command
|
||||||
CHECK_CHECKING,
|
CHECK_CHECKING,
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
# configuration
|
# configuration
|
||||||
mv-core.config.save.failed=Unable to save Multiverse-Core config.yml. Your changes will be temporary!
|
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 check
|
||||||
mv-core.check.description=Checks if a player can teleport to a destination.
|
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 usage
|
||||||
mv-core.usage.description=Show Multiverse-Core command usage.
|
mv-core.usage.description=Show Multiverse-Core command usage.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user