From 1eb33c69723407a415fab6b03527088d235ff9ab Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Sun, 10 Sep 2023 01:35:53 -0400 Subject: [PATCH] Clean up some return counts in WorldManager. --- .../MultiverseCore/worldnew/WorldManager.java | 140 ++++++++++-------- 1 file changed, 81 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java index 4fdef2c1..1ee61c48 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java @@ -15,10 +15,7 @@ import com.onarandombox.MultiverseCore.worldnew.generators.GeneratorProvider; import com.onarandombox.MultiverseCore.worldnew.helpers.DataStore.GameRulesStore; import com.onarandombox.MultiverseCore.worldnew.helpers.DataTransfer; import com.onarandombox.MultiverseCore.worldnew.helpers.FilesManipulator; -import com.onarandombox.MultiverseCore.worldnew.options.CloneWorldOptions; -import com.onarandombox.MultiverseCore.worldnew.options.CreateWorldOptions; -import com.onarandombox.MultiverseCore.worldnew.options.ImportWorldOptions; -import com.onarandombox.MultiverseCore.worldnew.options.RegenWorldOptions; +import com.onarandombox.MultiverseCore.worldnew.options.*; import com.onarandombox.MultiverseCore.worldnew.results.CloneWorldResult; import com.onarandombox.MultiverseCore.worldnew.results.CreateWorldResult; import com.onarandombox.MultiverseCore.worldnew.results.DeleteWorldResult; @@ -169,29 +166,40 @@ public class WorldManager { * @return The result of the creation. */ public Result createWorld(CreateWorldOptions options) { - // Params validations + return invalidateCreateWorldOptions(options).getOrElse(() -> createValidatedWorld(options)); + } + + private Option> invalidateCreateWorldOptions( + CreateWorldOptions options) { + Result result = null; + if (!worldNameChecker.isValidWorldName(options.worldName())) { - return worldActionResult(CreateWorldResult.Failure.INVALID_WORLDNAME, options.worldName()); - } - if (getLoadedWorld(options.worldName()).isDefined()) { - return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName()); - } - if (getWorld(options.worldName()).isDefined()) { - return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName()); - } - File worldFolder = new File(Bukkit.getWorldContainer(), options.worldName()); - if (worldFolder.exists()) { - return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_FOLDER, options.worldName()); + result = worldActionResult(CreateWorldResult.Failure.INVALID_WORLDNAME, options.worldName()); + } else if (getLoadedWorld(options.worldName()).isDefined()) { + result = worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName()); + } else if (getWorld(options.worldName()).isDefined()) { + result = worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName()); + } else if (hasWorldFolder(options.worldName())) { + result = worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_FOLDER, options.worldName()); } + return Option.of(result); + } + + private boolean hasWorldFolder(String worldName) { + File worldFolder = new File(Bukkit.getWorldContainer(), worldName); + return worldFolder.exists(); + } + + private Result createValidatedWorld( + CreateWorldOptions options) { String parsedGenerator = parseGenerator(options.worldName(), options.generator()); return createBukkitWorld(WorldCreator.name(options.worldName()) .environment(options.environment()) .generateStructures(options.generateStructures()) .generator(parsedGenerator) .seed(options.seed()) - .type(options.worldType())) - .fold( + .type(options.worldType())).fold( exception -> worldActionResult(CreateWorldResult.Failure.BUKKIT_CREATION_FAILED, options.worldName(), exception.getMessage()), world -> { @@ -200,7 +208,6 @@ public class WorldManager { }); } - /** * Imports an existing world folder. * @@ -208,25 +215,32 @@ public class WorldManager { * @return The result of the import. */ public Result importWorld(ImportWorldOptions options) { - // Params validations + return invalidateImportWorldOptions(options).getOrElse(() -> importValidatedWorld(options)); + } + + private Option> invalidateImportWorldOptions( + ImportWorldOptions options) { + Result result = null; + if (!worldNameChecker.isValidWorldName(options.worldName())) { - return worldActionResult(ImportWorldResult.Failure.INVALID_WORLDNAME, options.worldName()); - } - if (!worldNameChecker.isValidWorldFolder(options.worldName())) { - return worldActionResult(ImportWorldResult.Failure.WORLD_FOLDER_INVALID, options.worldName()); - } - if (isLoadedWorld(options.worldName())) { - return worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName()); - } - if (isWorld(options.worldName())) { - return worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName()); + result = worldActionResult(ImportWorldResult.Failure.INVALID_WORLDNAME, options.worldName()); + } else if (!worldNameChecker.isValidWorldFolder(options.worldName())) { + result = worldActionResult(ImportWorldResult.Failure.WORLD_FOLDER_INVALID, options.worldName()); + } else if (isLoadedWorld(options.worldName())) { + result = worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName()); + } else if (isWorld(options.worldName())) { + result = worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName()); } + return Option.of(result); + } + + private Result importValidatedWorld( + ImportWorldOptions options) { String parsedGenerator = parseGenerator(options.worldName(), options.generator()); return createBukkitWorld(WorldCreator.name(options.worldName()) .environment(options.environment()) - .generator(parsedGenerator)) - .fold( + .generator(parsedGenerator)).fold( exception -> worldActionResult(ImportWorldResult.Failure.BUKKIT_CREATION_FAILED, options.worldName(), exception.getMessage()), world -> { @@ -294,17 +308,27 @@ public class WorldManager { * @return The result of the load. */ public Result loadWorld(@NotNull MultiverseWorld mvWorld) { - // Params validations + return invalidateWorldToLoad(mvWorld).getOrElse(() -> loadValidatedWorld(mvWorld)); + } + + private Option> invalidateWorldToLoad( + @NotNull MultiverseWorld mvWorld) { + Result result = null; + if (loadTracker.contains(mvWorld.getName())) { // This is to prevent recursive calls by WorldLoadEvent Logging.fine("World already loading: " + mvWorld.getName()); - return worldActionResult(LoadWorldResult.Failure.WORLD_ALREADY_LOADING, mvWorld.getName()); - } - if (isLoadedWorld(mvWorld)) { + result = worldActionResult(LoadWorldResult.Failure.WORLD_ALREADY_LOADING, mvWorld.getName()); + } else if (isLoadedWorld(mvWorld)) { Logging.severe("World already loaded: " + mvWorld.getName()); - return worldActionResult(LoadWorldResult.Failure.WORLD_EXIST_LOADED, mvWorld.getName()); + result = worldActionResult(LoadWorldResult.Failure.WORLD_EXIST_LOADED, mvWorld.getName()); } + return Option.of(result); + } + + private Result loadValidatedWorld( + @NotNull MultiverseWorld mvWorld) { return createBukkitWorld(WorldCreator.name(mvWorld.getName()) .environment(mvWorld.getEnvironment()) .generator(Strings.isNullOrEmpty(mvWorld.getGenerator()) ? null : mvWorld.getGenerator()) @@ -356,8 +380,8 @@ public class WorldManager { * @param world The multiverse world to unload. * @return The result of the unload action. */ - public Result - unloadWorld(@NotNull LoadedMultiverseWorld world) { + public Result unloadWorld( + @NotNull LoadedMultiverseWorld world) { if (unloadTracker.contains(world.getName())) { // This is to prevent recursive calls by WorldUnloadEvent Logging.fine("World already unloading: " + world.getName()); @@ -388,8 +412,8 @@ public class WorldManager { * @param worldName The name of the world to remove. * @return The result of the remove. */ - public Result - removeWorld(@NotNull String worldName) { + public Result removeWorld( + @NotNull String worldName) { return getWorld(worldName) .map(this::removeWorld) .getOrElse(() -> worldActionResult(RemoveWorldResult.Failure.WORLD_NON_EXISTENT, worldName)); @@ -415,8 +439,8 @@ public class WorldManager { * @param loadedWorld The multiverse world to remove. * @return The result of the remove. */ - public Result - removeWorld(@NotNull LoadedMultiverseWorld loadedWorld) { + public Result removeWorld( + @NotNull LoadedMultiverseWorld loadedWorld) { var result = unloadWorld(loadedWorld); if (result.isFailure()) { return Result.failure(RemoveWorldResult.Failure.UNLOAD_FAILED, result.getReasonMessage()); @@ -527,8 +551,8 @@ public class WorldManager { })); } - private Result - cloneWorldValidateWorld(@NotNull CloneWorldOptions options) { + private Result cloneWorldValidateWorld( + @NotNull CloneWorldOptions options) { String newWorldName = options.newWorldName(); if (!worldNameChecker.isValidWorldName(newWorldName)) { Logging.severe("Invalid world name: " + newWorldName); @@ -548,8 +572,8 @@ public class WorldManager { return Result.success(); } - private Result - cloneWorldCopyFolder(@NotNull CloneWorldOptions options) { + private Result cloneWorldCopyFolder( + @NotNull CloneWorldOptions options) { // TODO: Check null? File worldFolder = options.world().getBukkitWorld().map(World::getWorldFolder).getOrNull(); File newWorldFolder = new File(Bukkit.getWorldContainer(), options.newWorldName()); @@ -560,8 +584,14 @@ public class WorldManager { } private void cloneWorldTransferData(@NotNull CloneWorldOptions options, @NotNull LoadedMultiverseWorld newWorld) { - LoadedMultiverseWorld world = options.world(); + DataTransfer dataTransfer = transferData(options, options.world()); + dataTransfer.pasteAllTo(newWorld); + } + + private DataTransfer transferData( + @NotNull KeepWorldSettingsOptions options, @NotNull LoadedMultiverseWorld world) { DataTransfer dataTransfer = new DataTransfer<>(); + if (options.keepWorldConfig()) { dataTransfer.addDataStore(new WorldConfigStore(), world); } @@ -571,7 +601,8 @@ public class WorldManager { if (options.keepWorldBorder()) { dataTransfer.addDataStore(new WorldBorderStore(), world); } - dataTransfer.pasteAllTo(newWorld); + + return dataTransfer; } /** @@ -584,16 +615,7 @@ public class WorldManager { // TODO: Teleport players out of world, and back in after regen LoadedMultiverseWorld world = options.world(); - DataTransfer dataTransfer = new DataTransfer<>(); - if (options.keepWorldConfig()) { - dataTransfer.addDataStore(new WorldConfigStore(), world); - } - if (options.keepGameRule()) { - dataTransfer.addDataStore(new GameRulesStore(), world); - } - if (options.keepWorldBorder()) { - dataTransfer.addDataStore(new WorldBorderStore(), world); - } + DataTransfer dataTransfer = transferData(options, world); CreateWorldOptions createWorldOptions = CreateWorldOptions.worldName(world.getName()) .environment(world.getEnvironment())