diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java index 4aa05878..36632653 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java @@ -6,7 +6,9 @@ import com.onarandombox.MultiverseCore.configuration.node.ConfigNode; import com.onarandombox.MultiverseCore.configuration.node.Node; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import com.onarandombox.MultiverseCore.event.MVDebugModeEvent; +import com.onarandombox.MultiverseCore.exceptions.MultiverseException; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; +import io.vavr.control.Try; import org.bukkit.plugin.PluginManager; class MVCoreConfigNodes { @@ -128,7 +130,9 @@ class MVCoreConfigNodes { .comment("This only applies if use-custom-portal-search is set to true.") .defaultValue(128) .name("custom-portal-search-radius") - .validator(value -> value >= 0) + .validator(value -> value < 0 + ? Try.failure(new MultiverseException("The value must be greater than or equal to 0.", null)) + : Try.success(null)) .build()); private final ConfigHeaderNode MESSAGING_HEADER = node(ConfigHeaderNode.builder("messaging") @@ -174,7 +178,9 @@ class MVCoreConfigNodes { .comment(" 3 = finest") .defaultValue(0) .name("global-debug") - .validator(value -> value >= 0 && value <= 3) + .validator(value -> (value < 0 || value > 3) + ? Try.failure(new MultiverseException("Debug level must be between 0 and 3.", null)) + : Try.success(null)) .onSetValue((oldValue, newValue) -> { int level = Logging.getDebugLevel(); Logging.setDebugLevel(newValue); 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 f5742fb5..aa74e57c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java @@ -165,13 +165,12 @@ abstract class FileConfigHandle { * @param The type of the node value. */ public Try set(@NotNull ValueNode node, T value) { - if (!node.validate(value)) { - return Try.failure(new MultiverseException("Validation failed", null)); // TODO replace validation - } - T oldValue = get(node); - config.set(node.getPath(), value); - node.onSetValue(oldValue, get(node)); - return Try.success(null); + return node.validate(value).map(ignore -> { + T oldValue = get(node); + config.set(node.getPath(), value); + node.onSetValue(oldValue, get(node)); + return null; + }); } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ConfigNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ConfigNode.java index 42d3f12f..d575b7e5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ConfigNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ConfigNode.java @@ -4,6 +4,7 @@ import java.util.function.BiConsumer; import java.util.function.Function; import io.vavr.control.Option; +import io.vavr.control.Try; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -31,7 +32,7 @@ public class ConfigNode extends ConfigHeaderNode implements ValueNode { protected final @Nullable String name; protected final @NotNull Class type; protected final @Nullable T defaultValue; - protected final @Nullable Function validator; + protected final @Nullable Function> validator; protected final @Nullable BiConsumer onSetValue; protected ConfigNode( @@ -40,7 +41,7 @@ public class ConfigNode extends ConfigHeaderNode implements ValueNode { @Nullable String name, @NotNull Class type, @Nullable T defaultValue, - @Nullable Function validator, + @Nullable Function> validator, @Nullable BiConsumer onSetValue ) { super(path, comments); @@ -79,11 +80,11 @@ public class ConfigNode extends ConfigHeaderNode implements ValueNode { * {@inheritDoc} */ @Override - public boolean validate(@Nullable T value) { + public Try validate(@Nullable T value) { if (validator != null) { return validator.apply(value); } - return true; + return Try.success(null); } /** @@ -107,7 +108,7 @@ public class ConfigNode extends ConfigHeaderNode implements ValueNode { protected @Nullable String name; protected @NotNull final Class type; protected @Nullable T defaultValue; - protected @Nullable Function validator; + protected @Nullable Function> validator; protected @Nullable BiConsumer onSetValue; /** @@ -144,7 +145,7 @@ public class ConfigNode extends ConfigHeaderNode implements ValueNode { return (B) this; } - public @NotNull B validator(@NotNull Function validator) { + public @NotNull B validator(@NotNull Function> validator) { this.validator = validator; return (B) this; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ValueNode.java index 6ef0cf81..9aebfae5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/ValueNode.java @@ -1,6 +1,7 @@ package com.onarandombox.MultiverseCore.configuration.node; import io.vavr.control.Option; +import io.vavr.control.Try; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -33,7 +34,7 @@ public interface ValueNode extends Node { * @param value The value to validate. * @return True if the value is valid, false otherwise. */ - boolean validate(@Nullable T value); + Try validate(@Nullable T value); /** * Called when the value of this node is set.