mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-15 07:05:28 +01:00
Merge pull request #2904 from Multiverse/config-validation
Use Try<Void> for config validation
This commit is contained in:
commit
33457ac5d5
@ -58,7 +58,7 @@ public class ConfigCommand extends MultiverseCommand {
|
||||
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()));
|
||||
.onFailure(e -> issuer.sendMessage(e.getMessage()));
|
||||
}
|
||||
|
||||
private void updateConfigValue(MVCommandIssuer issuer, String name, Object value) {
|
||||
|
@ -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);
|
||||
|
@ -165,13 +165,12 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
|
||||
* @param <T> The type of the node value.
|
||||
*/
|
||||
public <T> Try<Void> set(@NotNull ValueNode<T> 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;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
protected final @Nullable String name;
|
||||
protected final @NotNull Class<T> type;
|
||||
protected final @Nullable T defaultValue;
|
||||
protected final @Nullable Function<T, Boolean> validator;
|
||||
protected final @Nullable Function<T, Try<Void>> validator;
|
||||
protected final @Nullable BiConsumer<T, T> onSetValue;
|
||||
|
||||
protected ConfigNode(
|
||||
@ -40,7 +41,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
@Nullable String name,
|
||||
@NotNull Class<T> type,
|
||||
@Nullable T defaultValue,
|
||||
@Nullable Function<T, Boolean> validator,
|
||||
@Nullable Function<T, Try<Void>> validator,
|
||||
@Nullable BiConsumer<T, T> onSetValue
|
||||
) {
|
||||
super(path, comments);
|
||||
@ -79,11 +80,11 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean validate(@Nullable T value) {
|
||||
public Try<Void> validate(@Nullable T value) {
|
||||
if (validator != null) {
|
||||
return validator.apply(value);
|
||||
}
|
||||
return true;
|
||||
return Try.success(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -107,7 +108,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
protected @Nullable String name;
|
||||
protected @NotNull final Class<T> type;
|
||||
protected @Nullable T defaultValue;
|
||||
protected @Nullable Function<T, Boolean> validator;
|
||||
protected @Nullable Function<T, Try<Void>> validator;
|
||||
protected @Nullable BiConsumer<T, T> onSetValue;
|
||||
|
||||
/**
|
||||
@ -144,7 +145,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
return (B) this;
|
||||
}
|
||||
|
||||
public @NotNull B validator(@NotNull Function<T, Boolean> validator) {
|
||||
public @NotNull B validator(@NotNull Function<T, Try<Void>> validator) {
|
||||
this.validator = validator;
|
||||
return (B) this;
|
||||
}
|
||||
|
@ -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<T> extends Node {
|
||||
* @param value The value to validate.
|
||||
* @return True if the value is valid, false otherwise.
|
||||
*/
|
||||
boolean validate(@Nullable T value);
|
||||
Try<Void> validate(@Nullable T value);
|
||||
|
||||
/**
|
||||
* Called when the value of this node is set.
|
||||
|
Loading…
Reference in New Issue
Block a user