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, exception -> worldActionResult(LoadWorldResult.BUKKIT_CREATION_FAILED,
mvWorld.getName(), exception), mvWorld.getName(), exception),
world -> { world -> {
// TODO: Check worldConfig null WorldConfig worldConfig = worldsConfigManager.getWorldConfig(mvWorld.getName()).get();
WorldConfig worldConfig = worldsConfigManager.getWorldConfig(mvWorld.getName());
LoadedMultiverseWorld loadedWorld = new LoadedMultiverseWorld( LoadedMultiverseWorld loadedWorld = new LoadedMultiverseWorld(
world, world,
worldConfig, worldConfig,

View File

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

View File

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

View File

@ -29,7 +29,7 @@ class WorldConfigTest : TestWithMockBukkit() {
throw IllegalStateException("WorldsConfigManager is not available as a service") } throw IllegalStateException("WorldsConfigManager is not available as a service") }
assertTrue(worldConfigManager.load().isSuccess) 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") } throw IllegalStateException("WorldConfig for world is not available") }
assertNotNull(worldConfig); assertNotNull(worldConfig);
} }