diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/ConfigurationSectionHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/ConfigurationSectionHandle.java new file mode 100644 index 00000000..08885bd5 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/ConfigurationSectionHandle.java @@ -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 { + public static 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> extends GenericConfigHandle.Builder { + private final ConfigurationSection configurationSection; + + protected Builder(@NotNull ConfigurationSection configurationSection) { + this.configurationSection = configurationSection; + } + + @Override + public @NotNull ConfigurationSectionHandle build() { + return new ConfigurationSectionHandle(configurationSection, logger, nodes, migrator); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java index aa74e57c..e7d9a7e8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java @@ -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 The configuration type. */ -abstract class FileConfigHandle { +abstract class FileConfigHandle extends GenericConfigHandle { 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 { */ 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 { 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 get(@Nullable String name) { - return nodes.findNode(name, ValueNode.class) - .toTry(() -> new ConfigNodeNotFoundException(name)) - .map(node -> get((ValueNode) 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 get(@NotNull ValueNode 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 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 The type of the node value. - */ - public Try set(@NotNull ValueNode 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 The configuration type. * @param The builder type. */ - public static abstract class Builder> { + public static abstract class Builder> extends GenericConfigHandle.Builder { protected @NotNull Path configPath; protected @Nullable Logger logger; @@ -199,50 +115,6 @@ abstract class FileConfigHandle { 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. * diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/GenericConfigHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/GenericConfigHandle.java new file mode 100644 index 00000000..f833c68f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/GenericConfigHandle.java @@ -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 { + 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 get(@Nullable String name) { + return nodes.findNode(name, ValueNode.class) + .toTry(() -> new ConfigNodeNotFoundException(name)) + .map(node -> get((ValueNode) 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 get(@NotNull ValueNode 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 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 The type of the node value. + */ + public Try set(@NotNull ValueNode 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 The configuration type. + * @param The builder type. + */ + public static abstract class Builder> { + + 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 build(); + + @SuppressWarnings("unchecked") + protected B self() { + return (B) this; + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/YamlConfigHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/YamlConfigHandle.java index cfc385b8..0a6e2cbe 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/YamlConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/YamlConfigHandle.java @@ -45,24 +45,6 @@ public class YamlConfigHandle extends FileConfigHandle { 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} */ diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/BooleanMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/BooleanMigratorAction.java index f89eeb47..37632d46 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/BooleanMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/BooleanMigratorAction.java @@ -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)); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java index 09381979..526b8871 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java @@ -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()) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/IntegerMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/IntegerMigratorAction.java index 2c2eef07..bbb7d7a3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/IntegerMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/IntegerMigratorAction.java @@ -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)); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java index 72366d7d..177d60d6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java @@ -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); diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MigratorAction.java index 3aaed4e4..fe45a0a3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MigratorAction.java @@ -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); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MoveMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MoveMigratorAction.java index 498c440b..af546643 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MoveMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MoveMigratorAction.java @@ -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); diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/VersionMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/VersionMigrator.java index 0bb072a0..064086a1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/VersionMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/VersionMigrator.java @@ -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)); }