diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java index 75151ac9..3293e4af 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java @@ -6,6 +6,7 @@ import java.nio.file.Path; import java.util.logging.Logger; import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator; +import com.onarandombox.MultiverseCore.configuration.node.EnchancedValueNode; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import io.github.townyadvanced.commentedconfiguration.CommentedConfiguration; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; @@ -20,10 +21,22 @@ import org.jetbrains.annotations.Nullable; */ public class ConfigHandle { + /** + * A builder class for creating a ConfigHandle. + * + * @param configPath The path to the configuration file in string. + * @return A new Builder instance. + */ public static Builder builder(String configPath) { return new Builder(configPath); } + /** + * A builder class for creating a ConfigHandle. + * + * @param configPath The path to the configuration file. + * @return A new Builder instance. + */ public static Builder builder(Path configPath) { return new Builder(configPath); } @@ -178,17 +191,6 @@ public class ConfigHandle { return config.getObject(node.getPath(), node.getType(), node.getDefaultValue()); } - /** - * Sets the value of a node, if the validator is not null, it will be tested first. - * - * @param node The node to set the value of. - * @param value The value to set. - */ - public boolean set(@NotNull ValueNode node, Object value) { - config.set(node.getPath(), value); - return true; - } - /** * Set the value of the node by name. * @@ -201,6 +203,20 @@ public class ConfigHandle { .orElse(false); } + /** + * Sets the value of a node, if the validator is not null, it will be tested first. + * + * @param node The node to set the value of. + * @param value The value to set. + */ + public boolean set(@NotNull ValueNode node, Object value) { + if (node instanceof TypedValueNode) { + return set(node, value); + } + config.set(node.getPath(), value); + return true; + } + /** * Sets the value of a node, if the validator is not null, it will be tested first. * @@ -208,8 +224,27 @@ public class ConfigHandle { * @param value The value to set. * @param The type of the node value. */ - public void set(@NotNull TypedValueNode node, T value) { + public boolean set(@NotNull TypedValueNode node, T value) { + if (node instanceof EnchancedValueNode) { + return set((EnchancedValueNode) node, value); + } config.set(node.getPath(), value); + return true; + } + + /** + * Sets the value of a node, if the validator is not null, it will be tested first. + * + * @param node The node to set the value of. + * @param value The value to set. + * @return True if the value was set, false otherwise. + * @param The type of the node value. + */ + public boolean set(@NotNull EnchancedValueNode node, T value) { + T oldValue = get(node); + config.set(node.getPath(), value); + node.onSetValue(oldValue, get(node)); + return true; } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java index b5149fdf..8b64f5e2 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -1,5 +1,6 @@ package com.onarandombox.MultiverseCore.configuration; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.configuration.node.MVCommentedNode; import com.onarandombox.MultiverseCore.configuration.node.MVValueNode; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; @@ -163,6 +164,7 @@ public class MVConfigNodes { .comment(" 3 = finest") .defaultValue(0) .name("global-debug") + .onSetValue((oldValue, newValue) -> Logging.setDebugLevel(newValue)) .build()); public static final MVValueNode SILENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class) @@ -170,6 +172,7 @@ public class MVConfigNodes { .comment("If true, the startup console messages will no longer show.") .defaultValue(false) .name("silent-start") + .onSetValue((oldValue, newValue) -> Logging.setShowingConfig(!newValue)) .build()); public static final MVValueNode SHOW_DONATION_MESSAGE = node(MVValueNode.builder("misc.show-donation-message", Boolean.class) diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NamedValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnchancedValueNode.java similarity index 79% rename from src/main/java/com/onarandombox/MultiverseCore/configuration/node/NamedValueNode.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnchancedValueNode.java index 69f80b1d..1f3f5420 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NamedValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnchancedValueNode.java @@ -9,11 +9,13 @@ import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; * * @param The type of the node's value. */ -public interface NamedValueNode extends TypedValueNode { +public interface EnchancedValueNode extends TypedValueNode { /** * Gets the name of this node. Used for identifying the node from user input. * * @return The name of this node. */ Optional getName(); + + void onSetValue(T oldValue, T newValue); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java index adf9e94d..d5aa1acd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java @@ -1,15 +1,16 @@ package com.onarandombox.MultiverseCore.configuration.node; import java.util.Optional; +import java.util.function.BiConsumer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** - * Implementation of {@link NamedValueNode}. + * Implementation of {@link EnchancedValueNode}. * @param The type of the value. */ -public class MVValueNode extends MVCommentedNode implements NamedValueNode { +public class MVValueNode extends MVCommentedNode implements EnchancedValueNode { /** * Creates a new builder for a {@link MVValueNode}. @@ -26,12 +27,14 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode protected final Class type; protected final T defaultValue; protected final String name; + protected final BiConsumer onSetValue; - protected MVValueNode(String path, String[] comments, Class type, T defaultValue, String name) { + protected MVValueNode(String path, String[] comments, Class type, T defaultValue, String name, BiConsumer onSetValue) { super(path, comments); this.type = type; this.defaultValue = defaultValue; this.name = name; + this.onSetValue = onSetValue; } /** @@ -58,6 +61,11 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode return Optional.ofNullable(name); } + @Override + public void onSetValue(T oldValue, T newValue) { + onSetValue.accept(oldValue, newValue); + } + /** * Builder for {@link MVValueNode}. * @@ -68,7 +76,8 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode protected final Class type; protected T defaultValue; - private String name; + protected String name; + protected BiConsumer onSetValue; /** * Creates a new builder. @@ -104,12 +113,17 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode return (B) this; } + public B onSetValue(@Nullable BiConsumer onSetValue) { + this.onSetValue = onSetValue; + return (B) this; + } + /** * {@inheritDoc} */ @Override public MVValueNode build() { - return new MVValueNode<>(path, comments.toArray(new String[0]), type, defaultValue, name); + return new MVValueNode<>(path, comments.toArray(new String[0]), type, defaultValue, name, onSetValue); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java index 919c4889..be7ead94 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java @@ -29,14 +29,14 @@ public class NodeGroup implements Collection { } private void addNodeIndex(CommentedNode node) { - if (node instanceof NamedValueNode) { - ((NamedValueNode) node).getName().ifPresent(name -> nodesMap.put(name, node)); + if (node instanceof EnchancedValueNode) { + ((EnchancedValueNode) node).getName().ifPresent(name -> nodesMap.put(name, node)); } } private void removeNodeIndex(CommentedNode node) { - if (node instanceof NamedValueNode) { - ((NamedValueNode) node).getName().ifPresent(nodesMap::remove); + if (node instanceof EnchancedValueNode) { + ((EnchancedValueNode) node).getName().ifPresent(nodesMap::remove); } }