Replace properties method in MVConfig to StringPropertyHandle

This commit is contained in:
Ben Woo 2023-09-22 23:09:31 +08:00
parent f2e193eda3
commit 4095829eca
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
6 changed files with 30 additions and 91 deletions

View File

@ -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<Void> 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<String> 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<Object> 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<Void> 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<Void> setPropertyString(String name, String value);
StringPropertyHandle getStringPropertyHandle();
/**
* Sets world access permissions should be enforced.

View File

@ -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);

View File

@ -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<String> 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());
}

View File

@ -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<String> suggestPropertyValues(String name, String input) {
return stringPropertyHandle.getPropertySuggestedValues(name, input);
}
@Override
public Try<Object> getProperty(String name) {
return stringPropertyHandle.getProperty(name);
}
@Override
public Try<Void> setProperty(String name, Object value) {
return stringPropertyHandle.setProperty(name, value);
}
@Override
public Try<Void> setPropertyString(String name, String value) {
return stringPropertyHandle.setPropertyString(name, value);
public StringPropertyHandle getStringPropertyHandle() {
return stringPropertyHandle;
}
@Override

View File

@ -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.

View File

@ -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)
}
}