mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2025-01-10 18:28:28 +01:00
Use Try<Void> rather than Try<Boolean> for ConfigHandle#set.
This commit is contained in:
parent
94251be048
commit
e5bb6bc23b
@ -47,7 +47,7 @@ public interface MVConfig {
|
|||||||
* @param value The value of the property.
|
* @param value The value of the property.
|
||||||
* @return True if the property was set successfully.
|
* @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.
|
* Sets world access permissions should be enforced.
|
||||||
|
@ -9,12 +9,10 @@ import co.aikar.commands.annotation.Optional;
|
|||||||
import co.aikar.commands.annotation.Single;
|
import co.aikar.commands.annotation.Single;
|
||||||
import co.aikar.commands.annotation.Subcommand;
|
import co.aikar.commands.annotation.Subcommand;
|
||||||
import co.aikar.commands.annotation.Syntax;
|
import co.aikar.commands.annotation.Syntax;
|
||||||
import com.onarandombox.MultiverseCore.api.MVConfig;
|
|
||||||
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
|
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
|
||||||
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
|
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
|
||||||
import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue;
|
import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue;
|
||||||
import com.onarandombox.MultiverseCore.config.MVCoreConfig;
|
import com.onarandombox.MultiverseCore.config.MVCoreConfig;
|
||||||
import io.vavr.control.Try;
|
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jvnet.hk2.annotations.Service;
|
import org.jvnet.hk2.annotations.Service;
|
||||||
@ -58,19 +56,15 @@ public class ConfigCommand extends MultiverseCommand {
|
|||||||
private void showConfigValue(BukkitCommandIssuer issuer, String name) {
|
private void showConfigValue(BukkitCommandIssuer issuer, String name) {
|
||||||
config.getProperty(name)
|
config.getProperty(name)
|
||||||
.onSuccess(value -> issuer.sendMessage(name + "is currently set to " + value))
|
.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) {
|
private void updateConfigValue(BukkitCommandIssuer issuer, String name, Object value) {
|
||||||
config.setProperty(name, value)
|
config.setProperty(name, value)
|
||||||
.onSuccess(success -> {
|
.onSuccess(ignore -> {
|
||||||
if (success) {
|
config.save();
|
||||||
config.save();
|
issuer.sendMessage("Successfully set " + name + " to " + value);
|
||||||
issuer.sendMessage("Successfully set " + name + " to " + value);
|
|
||||||
} else {
|
|
||||||
issuer.sendMessage("Unable to 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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,7 +116,7 @@ public class MVCoreConfig implements MVConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Try<Boolean> setProperty(String name, Object value) {
|
public Try<Void> setProperty(String name, Object value) {
|
||||||
return configHandle.set(name, value);
|
return configHandle.set(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
|
|||||||
* @param value The value to set.
|
* @param value The value to set.
|
||||||
* @return True if the value was set, false otherwise.
|
* @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)
|
return nodes.findNode(name, ValueNode.class)
|
||||||
.toTry(() -> new Exception("Node not found"))
|
.toTry(() -> new Exception("Node not found"))
|
||||||
.flatMap(node -> set(node, value));
|
.flatMap(node -> set(node, value));
|
||||||
@ -162,14 +162,14 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
|
|||||||
* @return True if the value was set, false otherwise.
|
* @return True if the value was set, false otherwise.
|
||||||
* @param <T> The type of the node value.
|
* @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)) {
|
if (!node.validate(value)) {
|
||||||
return Try.failure(new Exception("Validation failed"));
|
return Try.failure(new Exception("Validation failed"));
|
||||||
}
|
}
|
||||||
T oldValue = get(node);
|
T oldValue = get(node);
|
||||||
config.set(node.getPath(), value);
|
config.set(node.getPath(), value);
|
||||||
node.onSetValue(oldValue, get(node));
|
node.onSetValue(oldValue, get(node));
|
||||||
return Try.success(true);
|
return Try.success(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -77,13 +77,13 @@ class ConfigTest : TestWithMockBukkit() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Updating an existing config property with setProperty reflects the changes in getProperty`() {
|
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())
|
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())
|
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())
|
assertEquals(1, config.getProperty("global-debug").get())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user