From 4095829eca952e77011fbfccfdd2374f454fa7dd Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 22 Sep 2023 23:09:31 +0800 Subject: [PATCH] Replace properties method in MVConfig to StringPropertyHandle --- .../multiverse/core/api/MVConfig.java | 48 ++----------------- .../core/commands/ConfigCommand.java | 4 +- .../commandtools/MVCommandCompletions.java | 5 +- .../multiverse/core/config/MVCoreConfig.java | 26 +--------- .../handle/StringPropertyHandle.java | 2 +- .../multiverse/core/config/ConfigTest.kt | 36 +++++++------- 6 files changed, 30 insertions(+), 91 deletions(-) diff --git a/src/main/java/org/mvplugins/multiverse/core/api/MVConfig.java b/src/main/java/org/mvplugins/multiverse/core/api/MVConfig.java index 1b20807a..4ba47a79 100644 --- a/src/main/java/org/mvplugins/multiverse/core/api/MVConfig.java +++ b/src/main/java/org/mvplugins/multiverse/core/api/MVConfig.java @@ -1,11 +1,9 @@ package org.mvplugins.multiverse.core.api; -import java.util.Collection; - import io.vavr.control.Try; import org.jvnet.hk2.annotations.Contract; -import org.mvplugins.multiverse.core.configuration.node.NodeGroup; +import org.mvplugins.multiverse.core.configuration.handle.StringPropertyHandle; import org.mvplugins.multiverse.core.placeholders.MultiverseCorePlaceholders; @Contract @@ -29,49 +27,11 @@ public interface MVConfig { Try save(); /** - * Gets the nodes for the config. + * Gets the handler for managing config with string names and values. * - * @return The nodes for the config. + * @return The config handle for string properties. */ - NodeGroup getNodes(); - - /** - * 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. - */ - Collection suggestPropertyValues(String name, String input); - - /** - * Gets a property from the config. - * - * @param name The name of the property. - * @return A {@link Try} with the value of the property, otherwise a {@link Try.Failure} if there is no property by - * that name. - */ - Try getProperty(String name); - - /** - * Sets a property in the config. - * - * @param name The name of the property. - * @param value The value of the property. - * @return An empty {@link Try} if the property was set successfully, otherwise a {@link Try.Failure} with the - * exception explaining why the property could not be set. - */ - Try setProperty(String name, Object value); - - /** - * Sets a string property in the config. - * - * @param name The name of the property. - * @param value The string value of the property. - * @return An empty {@link Try} if the property was set successfully, otherwise a {@link Try.Failure} with the - * exception explaining why the property could not be set. - */ - Try setPropertyString(String name, String value); + StringPropertyHandle getStringPropertyHandle(); /** * Sets world access permissions should be enforced. diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/ConfigCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/ConfigCommand.java index 979e8225..18ac8b06 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commands/ConfigCommand.java +++ b/src/main/java/org/mvplugins/multiverse/core/commands/ConfigCommand.java @@ -54,13 +54,13 @@ class ConfigCommand extends MultiverseCommand { } private void showConfigValue(MVCommandIssuer issuer, String name) { - config.getProperty(name) + config.getStringPropertyHandle().getProperty(name) .onSuccess(value -> issuer.sendMessage(name + "is currently set to " + value)) .onFailure(e -> issuer.sendMessage(e.getMessage())); } private void updateConfigValue(MVCommandIssuer issuer, String name, String value) { - config.setPropertyString(name, value) + config.getStringPropertyHandle().setPropertyString(name, value) .onSuccess(ignore -> { config.save(); issuer.sendMessage("Successfully set " + name + " to " + value); diff --git a/src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandCompletions.java b/src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandCompletions.java index 0f577086..2a7eda66 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandCompletions.java +++ b/src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandCompletions.java @@ -60,7 +60,7 @@ class MVCommandCompletions extends PaperCommandCompletions { registerAsyncCompletion("flags", this::suggestFlags); registerStaticCompletion("gamemodes", suggestEnums(GameMode.class)); registerStaticCompletion("gamerules", this::suggestGamerules); - registerStaticCompletion("mvconfigs", config.getNodes().getNames()); + registerStaticCompletion("mvconfigs", config.getStringPropertyHandle().getPropertyNames()); registerAsyncCompletion("mvconfigvalues", this::suggestMVConfigValues); registerAsyncCompletion("mvworlds", this::suggestMVWorlds); @@ -123,7 +123,8 @@ class MVCommandCompletions extends PaperCommandCompletions { private Collection suggestMVConfigValues(BukkitCommandCompletionContext context) { return Try.of(() -> context.getContextValue(String.class)) - .map(propertyName -> config.suggestPropertyValues(propertyName, context.getInput())) + .map(propertyName -> config.getStringPropertyHandle() + .getPropertySuggestedValues(propertyName, context.getInput())) .getOrElse(Collections.emptyList()); } diff --git a/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java b/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java index 10325cc9..a82f83df 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java @@ -3,7 +3,6 @@ package org.mvplugins.multiverse.core.config; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Collection; import java.util.Objects; import com.dumptruckman.minecraft.util.Logging; @@ -24,7 +23,6 @@ import org.mvplugins.multiverse.core.configuration.migration.IntegerMigratorActi import org.mvplugins.multiverse.core.configuration.migration.InvertBoolMigratorAction; import org.mvplugins.multiverse.core.configuration.migration.MoveMigratorAction; import org.mvplugins.multiverse.core.configuration.migration.VersionMigrator; -import org.mvplugins.multiverse.core.configuration.node.NodeGroup; @Service public class MVCoreConfig implements MVConfig { @@ -111,28 +109,8 @@ public class MVCoreConfig implements MVConfig { } @Override - public NodeGroup getNodes() { - return configNodes.getNodes(); - } - - @Override - public Collection suggestPropertyValues(String name, String input) { - return stringPropertyHandle.getPropertySuggestedValues(name, input); - } - - @Override - public Try getProperty(String name) { - return stringPropertyHandle.getProperty(name); - } - - @Override - public Try setProperty(String name, Object value) { - return stringPropertyHandle.setProperty(name, value); - } - - @Override - public Try setPropertyString(String name, String value) { - return stringPropertyHandle.setPropertyString(name, value); + public StringPropertyHandle getStringPropertyHandle() { + return stringPropertyHandle; } @Override diff --git a/src/main/java/org/mvplugins/multiverse/core/configuration/handle/StringPropertyHandle.java b/src/main/java/org/mvplugins/multiverse/core/configuration/handle/StringPropertyHandle.java index 8f90e866..72955a76 100644 --- a/src/main/java/org/mvplugins/multiverse/core/configuration/handle/StringPropertyHandle.java +++ b/src/main/java/org/mvplugins/multiverse/core/configuration/handle/StringPropertyHandle.java @@ -36,7 +36,7 @@ public class StringPropertyHandle { } /** - * Gets the type of a property. + * Gets the type of property. * * @param name The name of the property. * @return The type of the property, or an error if the property was not found. diff --git a/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt b/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt index 5ddae89b..becb8953 100644 --- a/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt @@ -52,14 +52,14 @@ class ConfigTest : TestWithMockBukkit() { @Test fun `Getting existing config property with getProperty returns expected value`() { - assertEquals(false, config.getProperty("enforce-access").get()) - assertEquals("world", config.getProperty("first-spawn-location").get()) + assertEquals(false, config.stringPropertyHandle.getProperty("enforce-access").get()) + assertEquals("world", config.stringPropertyHandle.getProperty("first-spawn-location").get()) } @Test fun `Getting non-existing config property with getProperty returns null`() { - assertTrue(config.getProperty("invalid-property").isFailure) - assertTrue(config.getProperty("version").isFailure) + assertTrue(config.stringPropertyHandle.getProperty("invalid-property").isFailure) + assertTrue(config.stringPropertyHandle.getProperty("version").isFailure) } @Test @@ -70,31 +70,31 @@ class ConfigTest : TestWithMockBukkit() { @Test fun `Updating an existing config property with setProperty reflects the changes in getProperty`() { - assertTrue(config.setProperty("enforce-access", true).isSuccess) - assertEquals(true, config.getProperty("enforce-access").get()) + assertTrue(config.stringPropertyHandle.setProperty("enforce-access", true).isSuccess) + assertEquals(true, config.stringPropertyHandle.getProperty("enforce-access").get()) - assertTrue(config.setProperty("first-spawn-location", "world2").isSuccess) - assertEquals("world2", config.getProperty("first-spawn-location").get()) + assertTrue(config.stringPropertyHandle.setProperty("first-spawn-location", "world2").isSuccess) + assertEquals("world2", config.stringPropertyHandle.getProperty("first-spawn-location").get()) - assertTrue(config.setProperty("global-debug", 1).isSuccess) - assertEquals(1, config.getProperty("global-debug").get()) + assertTrue(config.stringPropertyHandle.setProperty("global-debug", 1).isSuccess) + assertEquals(1, config.stringPropertyHandle.getProperty("global-debug").get()) } @Test fun `Updating an existing config property with setPropertyString reflects the changes in getProperty`() { - assertTrue(config.setPropertyString("enforce-access", "true").isSuccess) - assertEquals(true, config.getProperty("enforce-access").get()) + assertTrue(config.stringPropertyHandle.setPropertyString("enforce-access", "true").isSuccess) + assertEquals(true, config.stringPropertyHandle.getProperty("enforce-access").get()) - assertTrue(config.setPropertyString("first-spawn-location", "world2").isSuccess) - assertEquals("world2", config.getProperty("first-spawn-location").get()) + assertTrue(config.stringPropertyHandle.setPropertyString("first-spawn-location", "world2").isSuccess) + assertEquals("world2", config.stringPropertyHandle.getProperty("first-spawn-location").get()) - assertTrue(config.setPropertyString("global-debug", "1").isSuccess) - assertEquals(1, config.getProperty("global-debug").get()) + assertTrue(config.stringPropertyHandle.setPropertyString("global-debug", "1").isSuccess) + assertEquals(1, config.stringPropertyHandle.getProperty("global-debug").get()) } @Test fun `Updating a non-existing property with setProperty returns false`() { - assertTrue(config.setProperty("invalid-property", false).isFailure) - assertTrue(config.setProperty("version", 1.1).isFailure) + assertTrue(config.stringPropertyHandle.setProperty("invalid-property", false).isFailure) + assertTrue(config.stringPropertyHandle.setProperty("version", 1.1).isFailure) } }