Move property handling to a separate class

This commit is contained in:
Ben Woo 2023-09-20 23:35:10 +08:00
parent 6fe87fbc7b
commit 4084cd4af1
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
4 changed files with 122 additions and 65 deletions

View File

@ -17,6 +17,7 @@ import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.MultiverseCore;
import org.mvplugins.multiverse.core.api.MVConfig;
import org.mvplugins.multiverse.core.configuration.handle.CommentedYamlConfigHandle;
import org.mvplugins.multiverse.core.configuration.handle.StringPropertyHandle;
import org.mvplugins.multiverse.core.configuration.migration.BooleanMigratorAction;
import org.mvplugins.multiverse.core.configuration.migration.ConfigMigrator;
import org.mvplugins.multiverse.core.configuration.migration.IntegerMigratorAction;
@ -33,6 +34,7 @@ public class MVCoreConfig implements MVConfig {
private final Path configPath;
private final MVCoreConfigNodes configNodes;
private final CommentedYamlConfigHandle configHandle;
private final StringPropertyHandle stringPropertyHandle;
@Inject
MVCoreConfig(@NotNull MultiverseCore core, @NotNull PluginManager pluginManager) {
@ -71,7 +73,7 @@ public class MVCoreConfig implements MVConfig {
.build())
.build())
.build();
this.stringPropertyHandle = new StringPropertyHandle(configHandle);
load();
save();
}
@ -115,22 +117,22 @@ public class MVCoreConfig implements MVConfig {
@Override
public Collection<String> suggestPropertyValues(String name, String input) {
return configHandle.suggestPropertyValues(name, input);
return stringPropertyHandle.getPropertySuggestedValues(name, input);
}
@Override
public Try<Object> getProperty(String name) {
return configHandle.getProperty(name);
return stringPropertyHandle.getProperty(name);
}
@Override
public Try<Void> setProperty(String name, Object value) {
return configHandle.setProperty(name, value);
return stringPropertyHandle.setProperty(name, value);
}
@Override
public Try<Void> setPropertyString(String name, String value) {
return configHandle.setPropertyString(name, value);
return stringPropertyHandle.setPropertyString(name, value);
}
@Override

View File

@ -1,7 +1,5 @@
package org.mvplugins.multiverse.core.configuration.handle;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger;
import io.vavr.control.Try;
@ -11,8 +9,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mvplugins.multiverse.core.configuration.migration.ConfigMigrator;
import org.mvplugins.multiverse.core.configuration.node.ConfigNodeNotFoundException;
import org.mvplugins.multiverse.core.configuration.node.Node;
import org.mvplugins.multiverse.core.configuration.node.NodeGroup;
import org.mvplugins.multiverse.core.configuration.node.ValueNode;
@ -21,12 +17,12 @@ import org.mvplugins.multiverse.core.configuration.node.ValueNode;
*/
public abstract class GenericConfigHandle<C extends ConfigurationSection> {
protected final @Nullable Logger logger;
protected final @Nullable NodeGroup nodes;
protected final @NotNull NodeGroup nodes;
protected final @Nullable ConfigMigrator migrator;
protected C config;
protected GenericConfigHandle(@Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) {
protected GenericConfigHandle(@Nullable Logger logger, @NotNull NodeGroup nodes, @Nullable ConfigMigrator migrator) {
this.logger = logger;
this.nodes = nodes;
this.migrator = migrator;
@ -70,58 +66,6 @@ public abstract class GenericConfigHandle<C extends ConfigurationSection> {
});
}
/**
* Auto-complete suggestions for a property.
*
* @param name The name of the node.
* @param input The current user input.
* @return A collection of possible string values.
*/
public Collection<String> suggestPropertyValues(@Nullable String name, @Nullable String input) {
return findNode(name, ValueNode.class)
.map(node -> node.suggest(input))
.getOrElse(Collections.emptyList());
}
/**
* Gets the value of a node, if the node has a default value, it will be returned if the node is not found.
*
* @param name The name of the node.
* @return The value of the node, or an error if the node was not found.
*/
public Try<Object> getProperty(@Nullable String name) {
return findNode(name, ValueNode.class).map(this::get);
}
/**
* Sets the string value of a node, if the validator is not null, it will be tested first.
*
* @param name The name of the node.
* @param value The string value to set.
* @return Empty try if the value was set, try containing an error otherwise.
*/
public Try<Void> setPropertyString(@Nullable String name, @Nullable String value) {
return findNode(name, ValueNode.class)
.flatMap(node -> node.parseFromString(value)
.flatMap(parsedValue -> set(node, parsedValue)));
}
/**
* Sets the value of a node, if the validator is not null, it will be tested first.
*
* @param name The name of the node.
* @param value The value to set.
* @return Empty try if the value was set, try containing an error otherwise.
*/
public Try<Void> setProperty(@Nullable String name, @Nullable Object value) {
return findNode(name, ValueNode.class).flatMap(node -> set(node, value));
}
private <T extends Node> Try<T> findNode(@Nullable String name, @NotNull Class<T> type) {
return nodes.findNode(name, type)
.toTry(() -> new ConfigNodeNotFoundException(name));
}
/**
* Gets the value of a node, if the node has a default value, it will be returned if the node is not found.
*
@ -157,6 +101,15 @@ public abstract class GenericConfigHandle<C extends ConfigurationSection> {
});
}
/**
* Gets the configuration. Mainly used for {@link StringPropertyHandle}.
*
* @return The configuration.
*/
@NotNull NodeGroup getNodes() {
return nodes;
}
/**
* Sets the default value of a node.
*

View File

@ -0,0 +1,99 @@
package org.mvplugins.multiverse.core.configuration.handle;
import java.util.Collection;
import java.util.Collections;
import io.vavr.control.Try;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mvplugins.multiverse.core.configuration.node.ConfigNodeNotFoundException;
import org.mvplugins.multiverse.core.configuration.node.Node;
import org.mvplugins.multiverse.core.configuration.node.ValueNode;
/**
* Handles setting of config with string names and values.
*/
public class StringPropertyHandle {
private final @NotNull GenericConfigHandle<?> handle;
/**
* Creates a new string property handle.
*
* @param handle The handle to wrap.
*/
public StringPropertyHandle(@NotNull GenericConfigHandle<?> handle) {
this.handle = handle;
}
/**
* Gets the names of all properties in this handle.
*
* @return The names of all properties in this handle.
*/
public Collection<String> getPropertyNames() {
return handle.getNodes().getNames();
}
/**
* Gets the type of a property.
*
* @param name The name of the property.
* @return The type of the property, or an error if the property was not found.
*/
public Try<Class<?>> getPropertyType(@Nullable String name) {
return findNode(name, ValueNode.class).map(ValueNode::getType);
}
/**
* Auto-complete suggestions for a property.
*
* @param name The name of the node.
* @param input The current user input.
* @return A collection of possible string values.
*/
public Collection<String> getPropertySuggestedValues(@Nullable String name, @Nullable String input) {
return findNode(name, ValueNode.class)
.map(node -> node.suggest(input))
.getOrElse(Collections.emptyList());
}
/**
* Gets the value of a node, if the node has a default value, it will be returned if the node is not found.
*
* @param name The name of the node.
* @return The value of the node, or an error if the node was not found.
*/
public Try<Object> getProperty(@Nullable String name) {
return findNode(name, ValueNode.class).map(node -> handle.get(node));
}
/**
* Sets the value of a node, if the validator is not null, it will be tested first.
*
* @param name The name of the node.
* @param value The value to set.
* @return Empty try if the value was set, try containing an error otherwise.
*/
public Try<Void> setProperty(@Nullable String name, @Nullable Object value) {
return findNode(name, ValueNode.class).flatMap(node -> handle.set(node, value));
}
/**
* Sets the string value of a node, if the validator is not null, it will be tested first.
*
* @param name The name of the node.
* @param value The string value to set.
* @return Empty try if the value was set, try containing an error otherwise.
*/
public Try<Void> setPropertyString(@Nullable String name, @Nullable String value) {
return findNode(name, ValueNode.class)
.flatMap(node -> node.parseFromString(value)
.flatMap(parsedValue -> handle.set(node, parsedValue)));
}
private <T extends Node> Try<T> findNode(@Nullable String name, @NotNull Class<T> type) {
return handle.getNodes().findNode(name, type)
.toTry(() -> new ConfigNodeNotFoundException(name));
}
}

View File

@ -16,6 +16,7 @@ import org.jetbrains.annotations.Nullable;
import org.mvplugins.multiverse.core.MultiverseCore;
import org.mvplugins.multiverse.core.configuration.handle.ConfigurationSectionHandle;
import org.mvplugins.multiverse.core.configuration.handle.StringPropertyHandle;
import org.mvplugins.multiverse.core.configuration.migration.BooleanMigratorAction;
import org.mvplugins.multiverse.core.configuration.migration.ConfigMigrator;
import org.mvplugins.multiverse.core.configuration.migration.IntegerMigratorAction;
@ -33,6 +34,7 @@ public final class WorldConfig {
private final String worldName;
private final WorldConfigNodes configNodes;
private final ConfigurationSectionHandle configHandle;
private final StringPropertyHandle stringPropertyHandle;
WorldConfig(
@NotNull String worldName,
@ -47,6 +49,7 @@ public final class WorldConfig {
.addVersionMigrator(initialVersionMigrator())
.build())
.build();
this.stringPropertyHandle = new StringPropertyHandle(configHandle);
load();
}
@ -119,11 +122,11 @@ public final class WorldConfig {
}
public Try<Object> getProperty(String name) {
return configHandle.getProperty(name);
return stringPropertyHandle.getProperty(name);
}
public Try<Void> setProperty(String name, Object value) {
return configHandle.setProperty(name, value);
return stringPropertyHandle.setProperty(name, value);
}
public boolean getAdjustSpawn() {