diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java index 8a16d24e..a9d55bc6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java @@ -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, diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/WorldsConfigManager.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/WorldsConfigManager.java index cead5216..521b3d2a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/WorldsConfigManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/WorldsConfigManager.java @@ -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 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 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 getWorldConfig(@NotNull String worldName) { + return Option.of(worldConfigMap.get(worldName)); } /** diff --git a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigMangerTest.kt b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigMangerTest.kt index a7939dcc..67421209 100644 --- a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigMangerTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigMangerTest.kt @@ -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) diff --git a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt index 774f54b5..e5a3cb0f 100644 --- a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt @@ -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); }