mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2025-01-10 10:17:41 +01:00
Use Try<Void> for config loading and saving
This commit is contained in:
parent
f1ee2ee227
commit
0513865f43
@ -358,7 +358,8 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
|
||||
*/
|
||||
@Override
|
||||
public boolean saveAllConfigs() {
|
||||
return configProvider.get().save()
|
||||
// TODO: Make this all Try<Void>
|
||||
return configProvider.get().save().isSuccess()
|
||||
&& worldManagerProvider.get().saveWorldsConfig()
|
||||
&& anchorManagerProvider.get().saveAnchors();
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public interface MVConfig {
|
||||
* Loads the config from disk.
|
||||
* @return True if the config was loaded successfully.
|
||||
*/
|
||||
boolean load();
|
||||
Try<Void> load();
|
||||
|
||||
/**
|
||||
* Whether the config has been loaded.
|
||||
@ -23,7 +23,7 @@ public interface MVConfig {
|
||||
/**
|
||||
* Saves the config to disk.
|
||||
*/
|
||||
boolean save();
|
||||
Try<Void> save();
|
||||
|
||||
/**
|
||||
* Gets the nodes for the config.
|
||||
|
@ -50,7 +50,8 @@ public class ReloadCommand extends MultiverseCommand {
|
||||
public void onReloadCommand(@NotNull BukkitCommandIssuer issuer) {
|
||||
issuer.sendInfo(MVCorei18n.RELOAD_RELOADING);
|
||||
try {
|
||||
this.config.load();
|
||||
// TODO: Make this all Try<Void>
|
||||
this.config.load().getOrElseThrow(e -> new RuntimeException("Failed to load config", e));
|
||||
this.worldManager.initAllWorlds();
|
||||
this.anchorManager.loadAnchors();
|
||||
} catch (Exception e) {
|
||||
|
@ -89,7 +89,7 @@ public class MVCoreConfig implements MVConfig {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
public Try<Void> load() {
|
||||
migrateFromOldConfigFile();
|
||||
return configHandle.load();
|
||||
}
|
||||
@ -100,9 +100,8 @@ public class MVCoreConfig implements MVConfig {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean save() {
|
||||
configHandle.save();
|
||||
return true;
|
||||
public Try<Void> save() {
|
||||
return configHandle.save();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.onarandombox.MultiverseCore.configuration.handle;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -9,6 +10,7 @@ import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.CommentedNode;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.ValueNode;
|
||||
import io.github.townyadvanced.commentedconfiguration.CommentedConfiguration;
|
||||
import io.vavr.control.Try;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@ -35,9 +37,12 @@ public class CommentedYamlConfigHandle extends FileConfigHandle<CommentedConfigu
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected boolean loadConfigObject() {
|
||||
protected void loadConfigObject() throws IOException {
|
||||
config = new CommentedConfiguration(configPath, logger);
|
||||
return config.load();
|
||||
if (!config.load()) {
|
||||
throw new IOException("Failed to load commented config file " + configPath
|
||||
+ ". See console for details.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,9 +76,9 @@ public class CommentedYamlConfigHandle extends FileConfigHandle<CommentedConfigu
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean save() {
|
||||
config.save();
|
||||
return true;
|
||||
public Try<Void> save() {
|
||||
// TODO: There is no way to check if the save was successful.
|
||||
return Try.run(() -> config.save());
|
||||
}
|
||||
|
||||
public static class Builder extends FileConfigHandle.Builder<CommentedConfiguration, Builder> {
|
||||
|
@ -2,6 +2,7 @@ package com.onarandombox.MultiverseCore.configuration.handle;
|
||||
|
||||
import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
|
||||
import io.vavr.control.Try;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@ -16,7 +17,7 @@ public class ConfigurationSectionHandle extends GenericConfigHandle<Configuratio
|
||||
return new Builder<>(configurationSection);
|
||||
}
|
||||
|
||||
public ConfigurationSectionHandle(@NotNull ConfigurationSection configurationSection,
|
||||
protected ConfigurationSectionHandle(@NotNull ConfigurationSection configurationSection,
|
||||
@Nullable Logger logger,
|
||||
@Nullable NodeGroup nodes,
|
||||
@Nullable ConfigMigrator migrator) {
|
||||
@ -24,6 +25,17 @@ public class ConfigurationSectionHandle extends GenericConfigHandle<Configuratio
|
||||
this.config = configurationSection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the configuration with a new configuration section.
|
||||
*
|
||||
* @param section The configuration section.
|
||||
* @return Whether the configuration was loaded or its given error.
|
||||
*/
|
||||
public Try<Void> load(@NotNull ConfigurationSection section) {
|
||||
this.config = section;
|
||||
return load();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for {@link ConfigurationSectionHandle}.
|
||||
*
|
||||
|
@ -5,12 +5,11 @@ import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
|
||||
import io.vavr.control.Try;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@ -33,49 +32,43 @@ abstract class FileConfigHandle<C extends FileConfiguration> extends GenericConf
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean load() {
|
||||
boolean newFileCreated;
|
||||
try {
|
||||
newFileCreated = createConfigFile();
|
||||
} catch (IOException e) {
|
||||
Logging.severe("Failed to create config file: %s", configFile.getName());
|
||||
Logging.severe(e.getMessage());
|
||||
return false;
|
||||
}
|
||||
if (!loadConfigObject()) {
|
||||
Logging.severe("Failed to load config file: %s", configFile.getName());
|
||||
return false;
|
||||
}
|
||||
if (!newFileCreated) {
|
||||
migrateConfig();
|
||||
}
|
||||
setUpNodes();
|
||||
return true;
|
||||
public Try<Void> load() {
|
||||
boolean isNewFile = !configFile.exists();
|
||||
return createConfigFile()
|
||||
.andThenTry(this::loadConfigObject)
|
||||
.andThenTry(() -> {
|
||||
if (!isNewFile) {
|
||||
migrateConfig();
|
||||
}
|
||||
setUpNodes();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new config file if file does not exist
|
||||
* Create a new config file if file does not exist.
|
||||
*
|
||||
* @return True if file exist or created successfully, otherwise false.
|
||||
* @return Whether the file was created or its given error.
|
||||
*/
|
||||
protected boolean createConfigFile() throws IOException {
|
||||
if (configFile.exists()) {
|
||||
return false;
|
||||
}
|
||||
return configFile.createNewFile();
|
||||
protected Try<Void> createConfigFile() {
|
||||
return Try.run(() -> {
|
||||
if (configFile.exists()) {
|
||||
return;
|
||||
}
|
||||
if (!configFile.createNewFile()) {
|
||||
throw new IOException("Failed to create config file: " + configFile.getName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the configuration object.
|
||||
*
|
||||
* @return True if the configuration was loaded successfully, false otherwise.
|
||||
*/
|
||||
protected abstract boolean loadConfigObject();
|
||||
protected abstract void loadConfigObject() throws IOException, InvalidConfigurationException;
|
||||
|
||||
/**
|
||||
* Saves the configuration.
|
||||
*/
|
||||
public abstract boolean save();
|
||||
public abstract Try<Void> save();
|
||||
|
||||
/**
|
||||
* Checks if the configuration is loaded.
|
||||
|
@ -3,9 +3,7 @@ package com.onarandombox.MultiverseCore.configuration.handle;
|
||||
import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.ConfigNodeNotFoundException;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.NodeSerializer;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.ValueNode;
|
||||
import io.vavr.control.Option;
|
||||
import io.vavr.control.Try;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
@ -24,7 +22,7 @@ public abstract class GenericConfigHandle<C extends ConfigurationSection> {
|
||||
|
||||
protected C config;
|
||||
|
||||
public GenericConfigHandle(@Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) {
|
||||
protected GenericConfigHandle(@Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) {
|
||||
this.logger = logger;
|
||||
this.nodes = nodes;
|
||||
this.migrator = migrator;
|
||||
@ -33,12 +31,13 @@ public abstract class GenericConfigHandle<C extends ConfigurationSection> {
|
||||
/**
|
||||
* Loads the configuration.
|
||||
*
|
||||
* @return True if the configuration was loaded successfully, false otherwise.
|
||||
* @return Whether the configuration was loaded or its given error.
|
||||
*/
|
||||
public boolean load() {
|
||||
migrateConfig();
|
||||
setUpNodes();
|
||||
return true;
|
||||
public Try<Void> load() {
|
||||
return Try.run(() -> {
|
||||
migrateConfig();
|
||||
setUpNodes();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6,7 +6,7 @@ import java.util.logging.Logger;
|
||||
|
||||
import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
|
||||
import com.onarandombox.MultiverseCore.configuration.node.ValueNode;
|
||||
import io.vavr.control.Try;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -35,27 +35,17 @@ public class YamlConfigHandle extends FileConfigHandle<YamlConfiguration> {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected boolean loadConfigObject() {
|
||||
protected void loadConfigObject() throws IOException, InvalidConfigurationException {
|
||||
config = new YamlConfiguration();
|
||||
try {
|
||||
config.load(configFile);
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
config.load(configFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean save() {
|
||||
try {
|
||||
config.save(configFile);
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
public Try<Void> save() {
|
||||
return Try.run(() -> config.save(configFile));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -95,7 +95,9 @@ public class WorldManager {
|
||||
* Loads all worlds from the worlds config.
|
||||
*/
|
||||
public void initAllWorlds() {
|
||||
populateWorldFromConfig();
|
||||
if (!populateWorldFromConfig()) {
|
||||
return;
|
||||
}
|
||||
loadDefaultWorlds();
|
||||
autoLoadOfflineWorlds();
|
||||
saveWorldsConfig();
|
||||
@ -104,8 +106,13 @@ public class WorldManager {
|
||||
/**
|
||||
* Generate offline worlds from the worlds config.
|
||||
*/
|
||||
private void populateWorldFromConfig() {
|
||||
worldsConfigManager.load();
|
||||
private boolean populateWorldFromConfig() {
|
||||
Try<Void> load = worldsConfigManager.load();
|
||||
if (load.isFailure()) {
|
||||
Logging.severe("Failed to load worlds config: " + load.getCause().getMessage());
|
||||
load.getCause().printStackTrace();
|
||||
return false;
|
||||
}
|
||||
worldsConfigManager.getAllWorldConfigs().forEach(worldConfig -> {
|
||||
getMVWorld(worldConfig.getWorldName())
|
||||
.peek(mvWorld -> mvWorld.setWorldConfig(worldConfig));
|
||||
@ -116,6 +123,7 @@ public class WorldManager {
|
||||
offlineWorldsMap.put(offlineWorld.getName(), offlineWorld);
|
||||
});
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -785,6 +793,11 @@ public class WorldManager {
|
||||
* @return true if it had successfully saved the file.
|
||||
*/
|
||||
public boolean saveWorldsConfig() {
|
||||
return worldsConfigManager.save();
|
||||
return worldsConfigManager.save()
|
||||
.onFailure(failure -> {
|
||||
Logging.severe("Failed to save worlds config: %s", failure);
|
||||
failure.printStackTrace();
|
||||
})
|
||||
.isSuccess();
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ public class WorldConfig {
|
||||
load();
|
||||
}
|
||||
|
||||
public boolean load() {
|
||||
public Try<Void> load() {
|
||||
return configHandle.load();
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.onarandombox.MultiverseCore.worldnew.config;
|
||||
|
||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||
import io.vavr.control.Try;
|
||||
import jakarta.inject.Inject;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@ -8,7 +9,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jvnet.hk2.annotations.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -23,32 +23,36 @@ public class WorldsConfigManager {
|
||||
private YamlConfiguration worldsConfig;
|
||||
|
||||
@Inject
|
||||
public WorldsConfigManager(@NotNull MultiverseCore core) {
|
||||
WorldsConfigManager(@NotNull MultiverseCore core) {
|
||||
worldConfigMap = new HashMap<>();
|
||||
worldConfigFile = core.getDataFolder().toPath().resolve(CONFIG_FILENAME).toFile();
|
||||
load();
|
||||
}
|
||||
|
||||
public void load() {
|
||||
worldsConfig = YamlConfiguration.loadConfiguration(worldConfigFile);
|
||||
public Try<Void> load() {
|
||||
worldConfigMap.clear();
|
||||
for (String worldName : getAllWorldsInConfig()) {
|
||||
worldConfigMap.put(worldName, new WorldConfig(worldName, getWorldConfigSection(worldName)));
|
||||
}
|
||||
|
||||
return Try.run(() -> {
|
||||
if (!worldConfigFile.exists()) {
|
||||
worldConfigFile.createNewFile();
|
||||
}
|
||||
worldsConfig = new YamlConfiguration();
|
||||
worldsConfig.load(worldConfigFile);
|
||||
}).andThenTry(() -> {
|
||||
for (String worldName : getAllWorldsInConfig()) {
|
||||
worldConfigMap.put(worldName, new WorldConfig(worldName, getWorldConfigSection(worldName)));
|
||||
}
|
||||
}).onFailure(e -> {
|
||||
worldsConfig = null;
|
||||
worldConfigMap.clear();
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isLoaded() {
|
||||
return worldsConfig != null;
|
||||
}
|
||||
|
||||
public boolean save() {
|
||||
try {
|
||||
worldsConfig.save(worldConfigFile);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
public Try<Void> save() {
|
||||
return Try.run(() -> worldsConfig.save(worldConfigFile));
|
||||
}
|
||||
|
||||
public Set<String> getAllWorldsInConfig() {
|
||||
|
@ -26,8 +26,8 @@ class ConfigTest : TestWithMockBukkit() {
|
||||
assertNotNull(defaultConfig)
|
||||
File(Path.of(multiverseCore.dataFolder.absolutePath, "config.yml").absolutePathString()).writeText(defaultConfig)
|
||||
|
||||
assertTrue(config.load())
|
||||
assertTrue(config.save())
|
||||
assertTrue(config.load().isSuccess)
|
||||
assertTrue(config.save().isSuccess)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -40,8 +40,8 @@ class ConfigTest : TestWithMockBukkit() {
|
||||
val oldConfig = getResourceAsText("/old_config.yml")
|
||||
assertNotNull(oldConfig)
|
||||
multiverseCore.dataFolder.toPath().resolve("config.yml").toFile().writeText(oldConfig)
|
||||
assertTrue(config.load())
|
||||
assertTrue(config.save())
|
||||
assertTrue(config.load().isSuccess)
|
||||
assertTrue(config.save().isSuccess)
|
||||
|
||||
assertEquals(true, config.enforceAccess)
|
||||
assertEquals(false, config.isEnablePrefixChat)
|
||||
|
@ -22,8 +22,10 @@ class WorldConfigMangerTest : TestWithMockBukkit() {
|
||||
assertNotNull(defaultConfig)
|
||||
File(Path.of(multiverseCore.dataFolder.absolutePath, "worlds2.yml").absolutePathString()).writeText(defaultConfig)
|
||||
|
||||
worldConfigManager =
|
||||
WorldsConfigManager(multiverseCore)
|
||||
worldConfigManager = multiverseCore.getService(WorldsConfigManager::class.java).takeIf { it != null } ?: run {
|
||||
throw IllegalStateException("WorldsConfigManager is not available as a service") }
|
||||
|
||||
assertTrue(worldConfigManager.load().isSuccess)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -3,6 +3,7 @@ package org.mvplugins.multiverse.core.world
|
||||
import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig
|
||||
import com.onarandombox.MultiverseCore.worldnew.config.WorldsConfigManager
|
||||
import org.bukkit.Location
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.mvplugins.multiverse.core.TestWithMockBukkit
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
@ -15,7 +16,7 @@ import kotlin.test.assertTrue
|
||||
|
||||
class WorldConfigTest : TestWithMockBukkit() {
|
||||
|
||||
private lateinit var worldConfigFile : WorldsConfigManager
|
||||
private lateinit var worldConfigManager : WorldsConfigManager
|
||||
private lateinit var worldConfig : WorldConfig
|
||||
|
||||
@BeforeTest
|
||||
@ -24,9 +25,12 @@ class WorldConfigTest : TestWithMockBukkit() {
|
||||
assertNotNull(defaultConfig)
|
||||
File(Path.of(multiverseCore.dataFolder.absolutePath, "worlds2.yml").absolutePathString()).writeText(defaultConfig)
|
||||
|
||||
worldConfigFile =
|
||||
WorldsConfigManager(multiverseCore)
|
||||
worldConfig = worldConfigFile.getWorldConfig("world")
|
||||
worldConfigManager = multiverseCore.getService(WorldsConfigManager::class.java).takeIf { it != null } ?: run {
|
||||
throw IllegalStateException("WorldsConfigManager is not available as a service") }
|
||||
|
||||
assertTrue(worldConfigManager.load().isSuccess)
|
||||
worldConfig = worldConfigManager.getWorldConfig("world")
|
||||
assertNotNull(worldConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user