mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2025-01-10 18:28:28 +01:00
feat: Extract generic handler for config with ConfigurationSection
This commit is contained in:
parent
314ce11630
commit
d1370bfb8c
@ -0,0 +1,36 @@
|
||||
package com.onarandombox.MultiverseCore.configuration.handle;
|
||||
|
||||
import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class ConfigurationSectionHandle extends GenericConfigHandle<ConfigurationSection> {
|
||||
public static Builder<? extends Builder> builder(@NotNull ConfigurationSection configurationSection) {
|
||||
return new Builder<>(configurationSection);
|
||||
}
|
||||
|
||||
public ConfigurationSectionHandle(@NotNull ConfigurationSection configurationSection,
|
||||
@Nullable Logger logger,
|
||||
@Nullable NodeGroup nodes,
|
||||
@Nullable ConfigMigrator migrator) {
|
||||
super(logger, nodes, migrator);
|
||||
this.config = configurationSection;
|
||||
}
|
||||
|
||||
public static class Builder<B extends Builder<B>> extends GenericConfigHandle.Builder<ConfigurationSection, B> {
|
||||
private final ConfigurationSection configurationSection;
|
||||
|
||||
protected Builder(@NotNull ConfigurationSection configurationSection) {
|
||||
this.configurationSection = configurationSection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigurationSectionHandle build() {
|
||||
return new ConfigurationSectionHandle(configurationSection, logger, nodes, migrator);
|
||||
}
|
||||
}
|
||||
}
|
@ -7,12 +7,9 @@ import java.util.logging.Logger;
|
||||
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.ConfigNodeNotFoundException;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.ValueNode;
|
||||
import com.onarandombox.MultiverseCore.exceptions.MultiverseException;
|
||||
import io.vavr.control.Try;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@ -21,29 +18,22 @@ import org.jetbrains.annotations.Nullable;
|
||||
* Generic configuration handle for file based configurations.
|
||||
* @param <C> The configuration type.
|
||||
*/
|
||||
abstract class FileConfigHandle<C extends FileConfiguration> {
|
||||
abstract class FileConfigHandle<C extends FileConfiguration> extends GenericConfigHandle<C> {
|
||||
|
||||
protected final @NotNull Path configPath;
|
||||
protected final @NotNull File configFile;
|
||||
protected final @Nullable Logger logger;
|
||||
protected final @Nullable NodeGroup nodes;
|
||||
protected final @Nullable ConfigMigrator migrator;
|
||||
|
||||
protected C config;
|
||||
|
||||
protected FileConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) {
|
||||
super(logger, nodes, migrator);
|
||||
this.configPath = configPath;
|
||||
this.configFile = configPath.toFile();
|
||||
this.logger = logger;
|
||||
this.nodes = nodes;
|
||||
this.migrator = migrator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the configuration.
|
||||
*
|
||||
* @return True if the configuration was loaded successfully, false otherwise.
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean load() {
|
||||
if (!createConfigFile()) {
|
||||
Logging.severe("Failed to create config file: %s", configFile.getName());
|
||||
@ -85,20 +75,6 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
|
||||
*/
|
||||
protected abstract boolean loadConfigObject();
|
||||
|
||||
/**
|
||||
* Migrates the configuration.
|
||||
*/
|
||||
protected void migrateConfig() {
|
||||
if (migrator != null) {
|
||||
migrator.migrate(config);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the nodes.
|
||||
*/
|
||||
protected abstract void setUpNodes();
|
||||
|
||||
/**
|
||||
* Saves the configuration.
|
||||
*/
|
||||
@ -122,73 +98,13 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public Try<Object> get(@Nullable String name) {
|
||||
return nodes.findNode(name, ValueNode.class)
|
||||
.toTry(() -> new ConfigNodeNotFoundException(name))
|
||||
.map(node -> get((ValueNode<Object>) node));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of a node, if the node has a default value, it will be returned if the node is not found.
|
||||
*
|
||||
* @param node The node to get the value of.
|
||||
* @return The value of the node.
|
||||
*/
|
||||
public <T> T get(@NotNull ValueNode<T> node) {
|
||||
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 name The name of the node.
|
||||
* @param value The value to set.
|
||||
* @return True if the value was set, false otherwise.
|
||||
*/
|
||||
public Try<Void> set(@Nullable String name, Object value) {
|
||||
return nodes.findNode(name, ValueNode.class)
|
||||
.toTry(() -> new ConfigNodeNotFoundException(name))
|
||||
.flatMap(node -> set(node, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> Try<Void> set(@NotNull ValueNode<T> node, T value) {
|
||||
return node.validate(value).map(ignore -> {
|
||||
T oldValue = get(node);
|
||||
config.set(node.getPath(), value);
|
||||
node.onSetValue(oldValue, get(node));
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default value of a node.
|
||||
*
|
||||
* @param node The node to set the default value of.
|
||||
*/
|
||||
public void setDefault(@NotNull ValueNode node) {
|
||||
config.set(node.getPath(), node.getDefaultValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for {@link FileConfigHandle}.
|
||||
*
|
||||
* @param <C> The configuration type.
|
||||
* @param <B> The builder type.
|
||||
*/
|
||||
public static abstract class Builder<C extends FileConfiguration, B extends Builder<C, B>> {
|
||||
public static abstract class Builder<C extends FileConfiguration, B extends Builder<C, B>> extends GenericConfigHandle.Builder<C, B> {
|
||||
|
||||
protected @NotNull Path configPath;
|
||||
protected @Nullable Logger logger;
|
||||
@ -199,50 +115,6 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
|
||||
this.configPath = configPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the logger.
|
||||
*
|
||||
* @param logger The logger.
|
||||
* @return The builder.
|
||||
*/
|
||||
public B logger(@Nullable Logger logger) {
|
||||
this.logger = logger;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the logger.
|
||||
*
|
||||
* @param plugin The plugin to get the logger from.
|
||||
* @return The builder.
|
||||
*/
|
||||
public B logger(Plugin plugin) {
|
||||
this.logger = plugin.getLogger();
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the nodes.
|
||||
*
|
||||
* @param nodes The nodes.
|
||||
* @return The builder.
|
||||
*/
|
||||
public B nodes(@Nullable NodeGroup nodes) {
|
||||
this.nodes = nodes;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the migrator.
|
||||
*
|
||||
* @param migrator The migrator.
|
||||
* @return The builder.
|
||||
*/
|
||||
public B migrator(@Nullable ConfigMigrator migrator) {
|
||||
this.migrator = migrator;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the configuration handle.
|
||||
*
|
||||
|
@ -0,0 +1,193 @@
|
||||
package com.onarandombox.MultiverseCore.configuration.handle;
|
||||
|
||||
import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.ConfigNodeNotFoundException;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.ValueNode;
|
||||
import io.vavr.control.Try;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public abstract class GenericConfigHandle<C extends ConfigurationSection> {
|
||||
protected final @Nullable Logger logger;
|
||||
protected final @Nullable NodeGroup nodes;
|
||||
protected final @Nullable ConfigMigrator migrator;
|
||||
|
||||
protected C config;
|
||||
|
||||
public GenericConfigHandle(@Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) {
|
||||
this.logger = logger;
|
||||
this.nodes = nodes;
|
||||
this.migrator = migrator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the configuration.
|
||||
*
|
||||
* @return True if the configuration was loaded successfully, false otherwise.
|
||||
*/
|
||||
public boolean load() {
|
||||
migrateConfig();
|
||||
setUpNodes();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrates the configuration.
|
||||
*/
|
||||
protected void migrateConfig() {
|
||||
if (migrator != null) {
|
||||
migrator.migrate(config);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the nodes.
|
||||
*/
|
||||
protected void setUpNodes() {
|
||||
if (nodes == null || nodes.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
nodes.forEach(node -> {
|
||||
if (node instanceof ValueNode valueNode) {
|
||||
set(valueNode, config.getObject(valueNode.getPath(), valueNode.getType(), valueNode.getDefaultValue()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public Try<Object> get(@Nullable String name) {
|
||||
return nodes.findNode(name, ValueNode.class)
|
||||
.toTry(() -> new ConfigNodeNotFoundException(name))
|
||||
.map(node -> get((ValueNode<Object>) node));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of a node, if the node has a default value, it will be returned if the node is not found.
|
||||
*
|
||||
* @param node The node to get the value of.
|
||||
* @return The value of the node.
|
||||
*/
|
||||
public <T> T get(@NotNull ValueNode<T> node) {
|
||||
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 name The name of the node.
|
||||
* @param value The value to set.
|
||||
* @return True if the value was set, false otherwise.
|
||||
*/
|
||||
public Try<Void> set(@Nullable String name, Object value) {
|
||||
return nodes.findNode(name, ValueNode.class)
|
||||
.toTry(() -> new ConfigNodeNotFoundException(name))
|
||||
.flatMap(node -> set(node, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> Try<Void> set(@NotNull ValueNode<T> node, T value) {
|
||||
return node.validate(value).map(ignore -> {
|
||||
T oldValue = get(node);
|
||||
config.set(node.getPath(), value);
|
||||
node.onSetValue(oldValue, get(node));
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default value of a node.
|
||||
*
|
||||
* @param node The node to set the default value of.
|
||||
*/
|
||||
public void setDefault(@NotNull ValueNode node) {
|
||||
config.set(node.getPath(), node.getDefaultValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for {@link FileConfigHandle}.
|
||||
*
|
||||
* @param <C> The configuration type.
|
||||
* @param <B> The builder type.
|
||||
*/
|
||||
public static abstract class Builder<C extends ConfigurationSection, B extends GenericConfigHandle.Builder<C, B>> {
|
||||
|
||||
protected @Nullable Logger logger;
|
||||
protected @Nullable NodeGroup nodes;
|
||||
protected @Nullable ConfigMigrator migrator;
|
||||
|
||||
protected Builder() {}
|
||||
|
||||
/**
|
||||
* Sets the logger.
|
||||
*
|
||||
* @param logger The logger.
|
||||
* @return The builder.
|
||||
*/
|
||||
public B logger(@Nullable Logger logger) {
|
||||
this.logger = logger;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the logger.
|
||||
*
|
||||
* @param plugin The plugin to get the logger from.
|
||||
* @return The builder.
|
||||
*/
|
||||
public B logger(Plugin plugin) {
|
||||
this.logger = plugin.getLogger();
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the nodes.
|
||||
*
|
||||
* @param nodes The nodes.
|
||||
* @return The builder.
|
||||
*/
|
||||
public B nodes(@Nullable NodeGroup nodes) {
|
||||
this.nodes = nodes;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the migrator.
|
||||
*
|
||||
* @param migrator The migrator.
|
||||
* @return The builder.
|
||||
*/
|
||||
public B migrator(@Nullable ConfigMigrator migrator) {
|
||||
this.migrator = migrator;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the configuration handle.
|
||||
*
|
||||
* @return The configuration handle.
|
||||
*/
|
||||
public abstract @NotNull GenericConfigHandle<C> build();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected B self() {
|
||||
return (B) this;
|
||||
}
|
||||
}
|
||||
}
|
@ -45,24 +45,6 @@ public class YamlConfigHandle extends FileConfigHandle<YamlConfiguration> {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void setUpNodes() {
|
||||
if (nodes == null || nodes.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
YamlConfiguration oldConfig = config;
|
||||
config = new YamlConfiguration();
|
||||
nodes.forEach(node -> {
|
||||
if (node instanceof ValueNode valueNode) {
|
||||
set(valueNode, oldConfig.getObject(valueNode.getPath(), valueNode.getType(), valueNode.getDefaultValue()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -2,6 +2,7 @@ package com.onarandombox.MultiverseCore.configuration.migration;
|
||||
|
||||
import co.aikar.commands.ACFUtil;
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
/**
|
||||
@ -20,7 +21,7 @@ public class BooleanMigratorAction implements MigratorAction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void migrate(FileConfiguration config) {
|
||||
public void migrate(ConfigurationSection config) {
|
||||
config.set(path, ACFUtil.isTruthy(config.getString(path, "")));
|
||||
Logging.info("Converted %s to boolean %s", path, config.getBoolean(path));
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import java.util.List;
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.ValueNode;
|
||||
import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
/**
|
||||
@ -37,7 +38,7 @@ public class ConfigMigrator {
|
||||
*
|
||||
* @param config The target settings instance to migrate.
|
||||
*/
|
||||
public void migrate(FileConfiguration config) {
|
||||
public void migrate(ConfigurationSection config) {
|
||||
double versionNumber = config.getDouble(versionNode.getPath());
|
||||
for (VersionMigrator versionMigrator : versionMigrators) {
|
||||
if (versionNumber < versionMigrator.getVersion()) {
|
||||
|
@ -2,6 +2,7 @@ package com.onarandombox.MultiverseCore.configuration.migration;
|
||||
|
||||
import co.aikar.commands.ACFUtil;
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
/**
|
||||
@ -20,7 +21,7 @@ public class IntegerMigratorAction implements MigratorAction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void migrate(FileConfiguration config) {
|
||||
public void migrate(ConfigurationSection config) {
|
||||
config.set(path, ACFUtil.parseInt(config.getString(path)));
|
||||
Logging.info("Converted %s to integer %s", path, config.getInt(path));
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.onarandombox.MultiverseCore.configuration.migration;
|
||||
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
/**
|
||||
@ -28,7 +29,7 @@ public class InvertBoolMigratorAction implements MigratorAction {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void migrate(FileConfiguration config) {
|
||||
public void migrate(ConfigurationSection config) {
|
||||
boolean boolValue = !config.getBoolean(path);
|
||||
config.set(path, boolValue);
|
||||
Logging.info("Inverted %s to boolean %s", path, boolValue);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.onarandombox.MultiverseCore.configuration.migration;
|
||||
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
/**
|
||||
@ -12,5 +13,5 @@ public interface MigratorAction {
|
||||
*
|
||||
* @param config The target settings instance to migrate.
|
||||
*/
|
||||
void migrate(FileConfiguration config);
|
||||
void migrate(ConfigurationSection config);
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.onarandombox.MultiverseCore.configuration.migration;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
/**
|
||||
@ -33,7 +34,7 @@ public class MoveMigratorAction implements MigratorAction {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void migrate(FileConfiguration config) {
|
||||
public void migrate(ConfigurationSection config) {
|
||||
Optional.ofNullable(config.get(fromPath))
|
||||
.ifPresent(value -> {
|
||||
config.set(toPath, value);
|
||||
|
@ -3,6 +3,7 @@ package com.onarandombox.MultiverseCore.configuration.migration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
/**
|
||||
@ -33,7 +34,7 @@ public class VersionMigrator {
|
||||
*
|
||||
* @param config The target settings instance to migrate.
|
||||
*/
|
||||
public void migrate(FileConfiguration config) {
|
||||
public void migrate(ConfigurationSection config) {
|
||||
actions.forEach(action -> action.migrate(config));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user