Make getWorldConfig return an Option

This commit is contained in:
Ben Woo 2023-09-11 11:49:52 +08:00
parent bf7a708fb3
commit 7490996ecb
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
4 changed files with 16 additions and 18 deletions

View File

@ -341,8 +341,7 @@ public class WorldManager {
exception -> worldActionResult(LoadWorldResult.BUKKIT_CREATION_FAILED,
mvWorld.getName(), exception),
world -> {
// TODO: Check worldConfig null
WorldConfig worldConfig = worldsConfigManager.getWorldConfig(mvWorld.getName());
WorldConfig worldConfig = worldsConfigManager.getWorldConfig(mvWorld.getName()).get();
LoadedMultiverseWorld loadedWorld = new LoadedMultiverseWorld(
world,
worldConfig,

View File

@ -3,13 +3,13 @@ package com.onarandombox.MultiverseCore.worldnew.config;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.MultiverseCore;
import io.vavr.Tuple2;
import io.vavr.control.Option;
import io.vavr.control.Try;
import jakarta.inject.Inject;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jvnet.hk2.annotations.Service;
import java.io.File;
@ -115,14 +115,13 @@ public final class WorldsConfigManager {
List<WorldConfig> newWorldsAdded = new ArrayList<>();
for (String worldName : allWorldsInConfig) {
WorldConfig worldConfig = getWorldConfig(worldName);
if (worldConfig == null) {
WorldConfig newWorldConfig = new WorldConfig(worldName, getWorldConfigSection(worldName));
worldConfigMap.put(worldName, newWorldConfig);
newWorldsAdded.add(newWorldConfig);
} else {
worldConfig.load(getWorldConfigSection(worldName));
}
getWorldConfig(worldName)
.peek(config -> config.load(getWorldConfigSection(worldName)))
.onEmpty(() -> {
WorldConfig newWorldConfig = new WorldConfig(worldName, getWorldConfigSection(worldName));
worldConfigMap.put(worldName, newWorldConfig);
newWorldsAdded.add(newWorldConfig);
});
}
List<String> worldsRemoved = worldConfigMap.keySet().stream()
@ -158,10 +157,10 @@ public final class WorldsConfigManager {
* Gets the {@link WorldConfig} instance of all worlds in the worlds.yml file.
*
* @param worldName The name of the world to check.
* @return Whether the worlds.yml file contains the given world.
* @return The {@link WorldConfig} instance of the world, or empty option if it doesn't exist.
*/
public @Nullable WorldConfig getWorldConfig(@NotNull String worldName) {
return worldConfigMap.get(worldName);
public @NotNull Option<WorldConfig> getWorldConfig(@NotNull String worldName) {
return Option.of(worldConfigMap.get(worldName));
}
/**

View File

@ -41,13 +41,13 @@ class WorldConfigMangerTest : TestWithMockBukkit() {
assertTrue(worldConfigManager.load().isSuccess)
assertTrue(worldConfigManager.save().isSuccess)
val endWorldConfig = worldConfigManager.getWorldConfig("world_the_end")
val endWorldConfig = worldConfigManager.getWorldConfig("world_the_end").orNull
assertNotNull(endWorldConfig)
assertEquals("&aworld the end", endWorldConfig.alias)
assertEquals(Environment.THE_END, endWorldConfig.environment)
val worldConfig = worldConfigManager.getWorldConfig("world")
val worldConfig = worldConfigManager.getWorldConfig("world").orNull
assertNotNull(worldConfig)
assertEquals(-5176596003035866649, worldConfig.seed)
@ -65,7 +65,7 @@ class WorldConfigMangerTest : TestWithMockBukkit() {
@Test
fun `Updating existing world properties`() {
assertTrue(worldConfigManager.load().isSuccess)
val worldConfig = worldConfigManager.getWorldConfig("world")
val worldConfig = worldConfigManager.getWorldConfig("world").orNull
assertNotNull(worldConfig)
worldConfig.setProperty("adjust-spawn", true)

View File

@ -29,7 +29,7 @@ class WorldConfigTest : TestWithMockBukkit() {
throw IllegalStateException("WorldsConfigManager is not available as a service") }
assertTrue(worldConfigManager.load().isSuccess)
worldConfig = worldConfigManager.getWorldConfig("world").takeIf { it != null } ?: run {
worldConfig = worldConfigManager.getWorldConfig("world").orNull.takeIf { it != null } ?: run {
throw IllegalStateException("WorldConfig for world is not available") }
assertNotNull(worldConfig);
}