mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-25 12:05:14 +01:00
feat: Add a runnable when value is set.
This commit is contained in:
parent
3ccdfd7cf5
commit
358404b407
@ -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 <T> The type of the node value.
|
||||
*/
|
||||
public <T> void set(@NotNull TypedValueNode<T> node, T value) {
|
||||
public <T> boolean set(@NotNull TypedValueNode<T> node, T value) {
|
||||
if (node instanceof EnchancedValueNode) {
|
||||
return set((EnchancedValueNode<T>) 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 <T> The type of the node value.
|
||||
*/
|
||||
public <T> boolean set(@NotNull EnchancedValueNode<T> node, T value) {
|
||||
T oldValue = get(node);
|
||||
config.set(node.getPath(), value);
|
||||
node.onSetValue(oldValue, get(node));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<Boolean> 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<Boolean> SHOW_DONATION_MESSAGE = node(MVValueNode.builder("misc.show-donation-message", Boolean.class)
|
||||
|
@ -9,11 +9,13 @@ import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode;
|
||||
*
|
||||
* @param <T> The type of the node's value.
|
||||
*/
|
||||
public interface NamedValueNode<T> extends TypedValueNode<T> {
|
||||
public interface EnchancedValueNode<T> extends TypedValueNode<T> {
|
||||
/**
|
||||
* Gets the name of this node. Used for identifying the node from user input.
|
||||
*
|
||||
* @return The name of this node.
|
||||
*/
|
||||
Optional<String> getName();
|
||||
|
||||
void onSetValue(T oldValue, T newValue);
|
||||
}
|
@ -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 <T> The type of the value.
|
||||
*/
|
||||
public class MVValueNode<T> extends MVCommentedNode implements NamedValueNode<T> {
|
||||
public class MVValueNode<T> extends MVCommentedNode implements EnchancedValueNode<T> {
|
||||
|
||||
/**
|
||||
* Creates a new builder for a {@link MVValueNode}.
|
||||
@ -26,12 +27,14 @@ public class MVValueNode<T> extends MVCommentedNode implements NamedValueNode<T>
|
||||
protected final Class<T> type;
|
||||
protected final T defaultValue;
|
||||
protected final String name;
|
||||
protected final BiConsumer<T, T> onSetValue;
|
||||
|
||||
protected MVValueNode(String path, String[] comments, Class<T> type, T defaultValue, String name) {
|
||||
protected MVValueNode(String path, String[] comments, Class<T> type, T defaultValue, String name, BiConsumer<T, T> onSetValue) {
|
||||
super(path, comments);
|
||||
this.type = type;
|
||||
this.defaultValue = defaultValue;
|
||||
this.name = name;
|
||||
this.onSetValue = onSetValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,6 +61,11 @@ public class MVValueNode<T> extends MVCommentedNode implements NamedValueNode<T>
|
||||
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<T> extends MVCommentedNode implements NamedValueNode<T>
|
||||
|
||||
protected final Class<T> type;
|
||||
protected T defaultValue;
|
||||
private String name;
|
||||
protected String name;
|
||||
protected BiConsumer<T, T> onSetValue;
|
||||
|
||||
/**
|
||||
* Creates a new builder.
|
||||
@ -104,12 +113,17 @@ public class MVValueNode<T> extends MVCommentedNode implements NamedValueNode<T>
|
||||
return (B) this;
|
||||
}
|
||||
|
||||
public B onSetValue(@Nullable BiConsumer<T, T> onSetValue) {
|
||||
this.onSetValue = onSetValue;
|
||||
return (B) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public MVValueNode<T> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,14 +29,14 @@ public class NodeGroup implements Collection<CommentedNode> {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user