From 5cc5e6828f02331ffe7718f0a3316148da501ed3 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Tue, 28 Mar 2023 17:15:15 +0800 Subject: [PATCH] refactor: Allow for nullable NodeGroup --- .../MultiverseCore/config/MVCoreConfig.java | 3 ++- .../handle/CommentedYamlConfigHandle.java | 14 ++++++---- .../handle/FileConfigHandle.java | 20 ++++++++++---- .../handle/YamlConfigHandle.java | 26 +++++++++++++++---- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java index 446ea970..725e54e1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java @@ -34,8 +34,9 @@ public class MVCoreConfig implements MVConfig { MVCoreConfig(@NotNull MultiverseCore core, @NotNull PluginManager pluginManager) { this.configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME); this.configNodes = new MVCoreConfigNodes(pluginManager); - this.configHandle = CommentedYamlConfigHandle.builder(configPath, configNodes.getNodes()) + this.configHandle = CommentedYamlConfigHandle.builder(configPath) .logger(Logging.getLogger()) + .nodes(configNodes.getNodes()) .migrator(ConfigMigrator.builder(configNodes.VERSION) .addVersionMigrator(VersionMigrator.builder(5.0) .addAction(MoveMigratorAction.of("multiverse-configuration.enforceaccess", "world.enforce-access")) diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/CommentedYamlConfigHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/CommentedYamlConfigHandle.java index d2f49f09..47adcbbc 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/CommentedYamlConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/CommentedYamlConfigHandle.java @@ -14,11 +14,11 @@ import org.jetbrains.annotations.Nullable; public class CommentedYamlConfigHandle extends FileConfigHandle { - public static @NotNull Builder builder(@NotNull Path configPath, @NotNull NodeGroup nodes) { - return new Builder(configPath, nodes); + public static @NotNull Builder builder(@NotNull Path configPath) { + return new Builder(configPath); } - protected CommentedYamlConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @NotNull NodeGroup nodes, @Nullable ConfigMigrator migrator) { + protected CommentedYamlConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) { super(configPath, logger, nodes, migrator); } @@ -30,6 +30,10 @@ public class CommentedYamlConfigHandle extends FileConfigHandle { - protected Builder(@NotNull Path configPath, @NotNull NodeGroup nodes) { - super(configPath, nodes); + protected Builder(@NotNull Path configPath) { + super(configPath); } @Override 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 058ff757..f6dc4342 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/FileConfigHandle.java @@ -20,12 +20,12 @@ abstract class FileConfigHandle { protected final @NotNull Path configPath; protected final @NotNull File configFile; protected final @Nullable Logger logger; - protected final @NotNull NodeGroup nodes; + protected final @Nullable NodeGroup nodes; protected final @Nullable ConfigMigrator migrator; protected C config; - protected FileConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @NotNull NodeGroup nodes, @Nullable ConfigMigrator migrator) { + protected FileConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) { this.configPath = configPath; this.configFile = configPath.toFile(); this.logger = logger; @@ -158,12 +158,11 @@ abstract class FileConfigHandle { protected @NotNull Path configPath; protected @Nullable Logger logger; - protected @NotNull NodeGroup nodes; + protected @Nullable NodeGroup nodes; protected @Nullable ConfigMigrator migrator; - protected Builder(@NotNull Path configPath, @NotNull NodeGroup nodes) { + protected Builder(@NotNull Path configPath) { this.configPath = configPath; - this.nodes = nodes; } /** @@ -177,11 +176,22 @@ abstract class FileConfigHandle { 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(); } + public B nodes(@Nullable NodeGroup nodes) { + this.nodes = nodes; + return self(); + } + /** * Sets the migrator. * 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 167c33cb..5cd24015 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/YamlConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/handle/YamlConfigHandle.java @@ -14,14 +14,17 @@ import org.jetbrains.annotations.Nullable; public class YamlConfigHandle extends FileConfigHandle { - public static @NotNull Builder builder(@NotNull Path configPath, @NotNull NodeGroup nodes) { - return new Builder<>(configPath, nodes); + public static @NotNull Builder builder(@NotNull Path configPath) { + return new Builder<>(configPath); } - protected YamlConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @NotNull NodeGroup nodes, @Nullable ConfigMigrator migrator) { + protected YamlConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) { super(configPath, logger, nodes, migrator); } + /** + * {@inheritDoc} + */ @Override protected boolean loadConfigObject() { config = new YamlConfiguration(); @@ -33,8 +36,15 @@ 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 -> { @@ -44,6 +54,9 @@ public class YamlConfigHandle extends FileConfigHandle { }); } + /** + * {@inheritDoc} + */ @Override public boolean save() { try { @@ -56,10 +69,13 @@ public class YamlConfigHandle extends FileConfigHandle { public static class Builder> extends FileConfigHandle.Builder { - protected Builder(@NotNull Path configPath, @NotNull NodeGroup nodes) { - super(configPath, nodes); + protected Builder(@NotNull Path configPath) { + super(configPath); } + /** + * {@inheritDoc} + */ @Override public @NotNull YamlConfigHandle build() { return new YamlConfigHandle(configPath, logger, nodes, migrator);