Use Try<Void> rather than Try<Boolean> for ConfigHandle#set.

This commit is contained in:
Jeremy Wood 2023-03-28 17:25:48 -04:00
parent 94251be048
commit e5bb6bc23b
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B
5 changed files with 13 additions and 19 deletions

View File

@ -47,7 +47,7 @@ public interface MVConfig {
* @param value The value of the property.
* @return True if the property was set successfully.
*/
Try<Boolean> setProperty(String name, Object value);
Try<Void> setProperty(String name, Object value);
/**
* Sets world access permissions should be enforced.

View File

@ -9,12 +9,10 @@ import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.Single;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import com.onarandombox.MultiverseCore.api.MVConfig;
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue;
import com.onarandombox.MultiverseCore.config.MVCoreConfig;
import io.vavr.control.Try;
import jakarta.inject.Inject;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
@ -58,19 +56,15 @@ public class ConfigCommand extends MultiverseCommand {
private void showConfigValue(BukkitCommandIssuer issuer, String name) {
config.getProperty(name)
.onSuccess(value -> issuer.sendMessage(name + "is currently set to " + value))
.onFailure(throwable -> issuer.sendMessage("Unable to get " + name + ": " + throwable.getMessage()));
.onFailure(e -> issuer.sendMessage("Unable to get " + name + ": " + e.getMessage()));
}
private void updateConfigValue(BukkitCommandIssuer issuer, String name, Object value) {
config.setProperty(name, value)
.onSuccess(success -> {
if (success) {
config.save();
issuer.sendMessage("Successfully set " + name + " to " + value);
} else {
issuer.sendMessage("Unable to set " + name + " to " + value);
}
.onSuccess(ignore -> {
config.save();
issuer.sendMessage("Successfully set " + name + " to " + value);
})
.onFailure(throwable -> issuer.sendMessage("Unable to set " + name + " to " + value + ": " + throwable.getMessage()));
.onFailure(e -> issuer.sendMessage("Unable to set " + name + " to " + value + ": " + e.getMessage()));
}
}

View File

@ -116,7 +116,7 @@ public class MVCoreConfig implements MVConfig {
}
@Override
public Try<Boolean> setProperty(String name, Object value) {
public Try<Void> setProperty(String name, Object value) {
return configHandle.set(name, value);
}

View File

@ -148,7 +148,7 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
* @param value The value to set.
* @return True if the value was set, false otherwise.
*/
public Try<Boolean> set(@Nullable String name, Object value) {
public Try<Void> set(@Nullable String name, Object value) {
return nodes.findNode(name, ValueNode.class)
.toTry(() -> new Exception("Node not found"))
.flatMap(node -> set(node, value));
@ -162,14 +162,14 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
* @return True if the value was set, false otherwise.
* @param <T> The type of the node value.
*/
public <T> Try<Boolean> set(@NotNull ValueNode<T> node, T value) {
public <T> Try<Void> set(@NotNull ValueNode<T> node, T value) {
if (!node.validate(value)) {
return Try.failure(new Exception("Validation failed"));
}
T oldValue = get(node);
config.set(node.getPath(), value);
node.onSetValue(oldValue, get(node));
return Try.success(true);
return Try.success(null);
}
/**

View File

@ -77,13 +77,13 @@ class ConfigTest : TestWithMockBukkit() {
@Test
fun `Updating an existing config property with setProperty reflects the changes in getProperty`() {
assertTrue(config.setProperty("enforce-access", true).get())
assertTrue(config.setProperty("enforce-access", true).isSuccess)
assertEquals(true, config.getProperty("enforce-access").get())
assertTrue(config.setProperty("first-spawn-location", "world2").get())
assertTrue(config.setProperty("first-spawn-location", "world2").isSuccess)
assertEquals("world2", config.getProperty("first-spawn-location").get())
assertTrue(config.setProperty("global-debug", 1).get())
assertTrue(config.setProperty("global-debug", 1).isSuccess)
assertEquals(1, config.getProperty("global-debug").get())
}