diff --git a/config/mv_checks.xml b/config/mv_checks.xml index 129bdce0..78a095c1 100644 --- a/config/mv_checks.xml +++ b/config/mv_checks.xml @@ -451,6 +451,7 @@ value="Method type name ''{0}'' must match pattern ''{1}''."/> + diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java index e81af40e..6d19ca24 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java @@ -5,7 +5,10 @@ import com.google.common.base.Strings; import com.onarandombox.MultiverseCore.api.BlockSafety; import com.onarandombox.MultiverseCore.api.LocationManipulation; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; +import com.onarandombox.MultiverseCore.utils.message.MessageReplacement; +import com.onarandombox.MultiverseCore.utils.result.FailureReason; import com.onarandombox.MultiverseCore.utils.result.Result; +import com.onarandombox.MultiverseCore.utils.result.SuccessReason; import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig; import com.onarandombox.MultiverseCore.worldnew.config.WorldsConfigManager; import com.onarandombox.MultiverseCore.worldnew.generators.GeneratorProvider; @@ -54,8 +57,6 @@ import static com.onarandombox.MultiverseCore.worldnew.helpers.DataStore.WorldCo @Service public class WorldManager { - private static final String WORLD_PLACEHOLDER = "{world}"; - private static final String ERROR_PLACEHOLDER = "{error}"; private static final List CLONE_IGNORE_FILES = Arrays.asList("uid.dat", "session.lock"); private final Map worldsMap; @@ -167,21 +168,17 @@ public class WorldManager { public Result createWorld(CreateWorldOptions options) { // Params validations if (!worldNameChecker.isValidWorldName(options.worldName())) { - return Result.failure(CreateWorldResult.Failure.INVALID_WORLDNAME, - replace(WORLD_PLACEHOLDER).with(options.worldName())); + return worldActionResult(CreateWorldResult.Failure.INVALID_WORLDNAME, options.worldName()); } if (getLoadedWorld(options.worldName()).isDefined()) { - return Result.failure(CreateWorldResult.Failure.WORLD_EXIST_LOADED, - replace(WORLD_PLACEHOLDER).with(options.worldName())); + return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName()); } if (getWorld(options.worldName()).isDefined()) { - return Result.failure(CreateWorldResult.Failure.WORLD_EXIST_UNLOADED, - replace(WORLD_PLACEHOLDER).with(options.worldName())); + return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName()); } File worldFolder = new File(Bukkit.getWorldContainer(), options.worldName()); if (worldFolder.exists()) { - return Result.failure(CreateWorldResult.Failure.WORLD_EXIST_FOLDER, - replace(WORLD_PLACEHOLDER).with(options.worldName())); + return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_FOLDER, options.worldName()); } String parsedGenerator = parseGenerator(options.worldName(), options.generator()); @@ -192,16 +189,15 @@ public class WorldManager { .seed(options.seed()) .type(options.worldType())) .fold( - exception -> Result.failure(CreateWorldResult.Failure.BUKKIT_CREATION_FAILED, - replace(WORLD_PLACEHOLDER).with(options.worldName()), - replace(ERROR_PLACEHOLDER).with(exception.getMessage())), + exception -> worldActionResult(CreateWorldResult.Failure.BUKKIT_CREATION_FAILED, + options.worldName(), exception.getMessage()), world -> { newLoadedMultiverseWorld(world, parsedGenerator, options.useSpawnAdjust()); - return Result.success(CreateWorldResult.Success.CREATED, - replace(WORLD_PLACEHOLDER).with(world.getName())); + return worldActionResult(CreateWorldResult.Success.CREATED, world.getName()); }); } + /** * Imports an existing world folder. * @@ -211,20 +207,16 @@ public class WorldManager { public Result importWorld(ImportWorldOptions options) { // Params validations if (!worldNameChecker.isValidWorldName(options.worldName())) { - return Result.failure(ImportWorldResult.Failure.INVALID_WORLDNAME, - replace(WORLD_PLACEHOLDER).with(options.worldName())); + return worldActionResult(ImportWorldResult.Failure.INVALID_WORLDNAME, options.worldName()); } if (!worldNameChecker.isValidWorldFolder(options.worldName())) { - return Result.failure(ImportWorldResult.Failure.WORLD_FOLDER_INVALID, - replace(WORLD_PLACEHOLDER).with(options.worldName())); + return worldActionResult(ImportWorldResult.Failure.WORLD_FOLDER_INVALID, options.worldName()); } if (isLoadedWorld(options.worldName())) { - return Result.failure(ImportWorldResult.Failure.WORLD_EXIST_LOADED, - replace(WORLD_PLACEHOLDER).with(options.worldName())); + return worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName()); } if (isWorld(options.worldName())) { - return Result.failure(ImportWorldResult.Failure.WORLD_EXIST_UNLOADED, - replace(WORLD_PLACEHOLDER).with(options.worldName())); + return worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName()); } String parsedGenerator = parseGenerator(options.worldName(), options.generator()); @@ -232,13 +224,11 @@ public class WorldManager { .environment(options.environment()) .generator(parsedGenerator)) .fold( - exception -> Result.failure(ImportWorldResult.Failure.BUKKIT_CREATION_FAILED, - replace(WORLD_PLACEHOLDER).with(options.worldName()), - replace(ERROR_PLACEHOLDER).with(exception.getMessage())), + exception -> worldActionResult(ImportWorldResult.Failure.BUKKIT_CREATION_FAILED, + options.worldName(), exception.getMessage()), world -> { newLoadedMultiverseWorld(world, parsedGenerator, options.useSpawnAdjust()); - return Result.success(ImportWorldResult.Success.IMPORTED, - replace(WORLD_PLACEHOLDER).with(options.worldName())); + return worldActionResult(ImportWorldResult.Success.IMPORTED, options.worldName()); }); } @@ -290,10 +280,8 @@ public class WorldManager { return getWorld(worldName) .map(this::loadWorld) .getOrElse(() -> worldNameChecker.isValidWorldFolder(worldName) - ? Result.failure(LoadWorldResult.Failure.WORLD_EXIST_FOLDER, - replace(WORLD_PLACEHOLDER).with(worldName)) - : Result.failure(LoadWorldResult.Failure.WORLD_NON_EXISTENT, - replace(WORLD_PLACEHOLDER).with(worldName))); + ? worldActionResult(LoadWorldResult.Failure.WORLD_EXIST_FOLDER, worldName) + : worldActionResult(LoadWorldResult.Failure.WORLD_NON_EXISTENT, worldName)); } /** @@ -307,22 +295,19 @@ public class WorldManager { if (loadTracker.contains(mvWorld.getName())) { // This is to prevent recursive calls by WorldLoadEvent Logging.fine("World already loading: " + mvWorld.getName()); - return Result.failure(LoadWorldResult.Failure.WORLD_ALREADY_LOADING, - replace(WORLD_PLACEHOLDER).with(mvWorld.getName())); + return worldActionResult(LoadWorldResult.Failure.WORLD_ALREADY_LOADING, mvWorld.getName()); } if (isLoadedWorld(mvWorld)) { Logging.severe("World already loaded: " + mvWorld.getName()); - return Result.failure(LoadWorldResult.Failure.WORLD_EXIST_LOADED, - replace(WORLD_PLACEHOLDER).with(mvWorld.getName())); + return worldActionResult(LoadWorldResult.Failure.WORLD_EXIST_LOADED, mvWorld.getName()); } return createBukkitWorld(WorldCreator.name(mvWorld.getName()) .environment(mvWorld.getEnvironment()) .generator(Strings.isNullOrEmpty(mvWorld.getGenerator()) ? null : mvWorld.getGenerator()) .seed(mvWorld.getSeed())).fold( - exception -> Result.failure(LoadWorldResult.Failure.BUKKIT_CREATION_FAILED, - replace(WORLD_PLACEHOLDER).with(mvWorld.getName()), - replace(ERROR_PLACEHOLDER).with(exception.getMessage())), + exception -> worldActionResult(LoadWorldResult.Failure.BUKKIT_CREATION_FAILED, + mvWorld.getName(), exception.getMessage()), world -> { // TODO: Check worldConfig null WorldConfig worldConfig = worldsConfigManager.getWorldConfig(mvWorld.getName()); @@ -334,8 +319,7 @@ public class WorldManager { locationManipulation); loadedWorldsMap.put(loadedWorld.getName(), loadedWorld); saveWorldsConfig(); - return Result.success(LoadWorldResult.Success.LOADED, - replace(WORLD_PLACEHOLDER).with(loadedWorld.getName())); + return worldActionResult(LoadWorldResult.Success.LOADED, loadedWorld.getName()); }); } @@ -359,10 +343,8 @@ public class WorldManager { return getLoadedWorld(worldName) .map(this::unloadWorld) .getOrElse(() -> isUnloadedWorld(worldName) - ? Result.failure(UnloadWorldResult.Failure.WORLD_UNLOADED, - replace(WORLD_PLACEHOLDER).with(worldName)) - : Result.failure(UnloadWorldResult.Failure.WORLD_NON_EXISTENT, - replace(WORLD_PLACEHOLDER).with(worldName))); + ? worldActionResult(UnloadWorldResult.Failure.WORLD_UNLOADED, worldName) + : worldActionResult(UnloadWorldResult.Failure.WORLD_NON_EXISTENT, worldName)); } /** @@ -376,27 +358,23 @@ public class WorldManager { if (unloadTracker.contains(world.getName())) { // This is to prevent recursive calls by WorldUnloadEvent Logging.fine("World already unloading: " + world.getName()); - return Result.failure(UnloadWorldResult.Failure.WORLD_ALREADY_UNLOADING, - replace(WORLD_PLACEHOLDER).with(world.getName())); + return worldActionResult(UnloadWorldResult.Failure.WORLD_ALREADY_UNLOADING, world.getName()); } // TODO: removePlayersFromWorld? return unloadBukkitWorld(world.getBukkitWorld().getOrNull()).fold( - exception -> Result.failure(UnloadWorldResult.Failure.BUKKIT_UNLOAD_FAILED, - replace(WORLD_PLACEHOLDER).with(world.getName()), - replace(ERROR_PLACEHOLDER).with(exception.getMessage())), + exception -> worldActionResult(UnloadWorldResult.Failure.BUKKIT_UNLOAD_FAILED, + world.getName(), exception.getMessage()), success -> Option.of(loadedWorldsMap.remove(world.getName())).fold( () -> { Logging.severe("Failed to remove world from map: " + world.getName()); - return Result.failure(UnloadWorldResult.Failure.WORLD_NON_EXISTENT, - replace(WORLD_PLACEHOLDER).with(world.getName())); + return worldActionResult(UnloadWorldResult.Failure.WORLD_NON_EXISTENT, world.getName()); }, mvWorld -> { Logging.fine("Removed MultiverseWorld from map: " + world.getName()); mvWorld.getWorldConfig().deferenceMVWorld(); - return Result.success(UnloadWorldResult.Success.UNLOADED, - replace(WORLD_PLACEHOLDER).with(world.getName())); + return worldActionResult(UnloadWorldResult.Success.UNLOADED, world.getName()); })); } @@ -411,8 +389,7 @@ public class WorldManager { removeWorld(@NotNull String worldName) { return getWorld(worldName) .map(this::removeWorld) - .getOrElse(() -> Result.failure(RemoveWorldResult.Failure.WORLD_NON_EXISTENT, - replace(WORLD_PLACEHOLDER).with(worldName))); + .getOrElse(() -> worldActionResult(RemoveWorldResult.Failure.WORLD_NON_EXISTENT, worldName)); } /** @@ -470,8 +447,7 @@ public class WorldManager { public Result deleteWorld(@NotNull String worldName) { return getWorld(worldName) .map(this::deleteWorld) - .getOrElse(() -> Result.failure(DeleteWorldResult.Failure.WORLD_NON_EXISTENT, - replace(WORLD_PLACEHOLDER).with(worldName))); + .getOrElse(() -> worldActionResult(DeleteWorldResult.Failure.WORLD_NON_EXISTENT, worldName)); } /** @@ -486,8 +462,7 @@ public class WorldManager { () -> { var result = loadWorld(world); if (result.isFailure()) { - return Result.failure(DeleteWorldResult.Failure.LOAD_FAILED, - replace(WORLD_PLACEHOLDER).with(world.getName())); + return worldActionResult(DeleteWorldResult.Failure.LOAD_FAILED, world.getName()); } return deleteWorld(world); }, @@ -505,8 +480,7 @@ public class WorldManager { File worldFolder = world.getBukkitWorld().map(World::getWorldFolder).getOrNull(); if (worldFolder == null || !worldNameChecker.isValidWorldFolder(worldFolder)) { Logging.severe("Failed to get world folder for world: " + world.getName()); - return Result.failure(DeleteWorldResult.Failure.WORLD_FOLDER_NOT_FOUND, - replace(WORLD_PLACEHOLDER).with(world.getName())); + return worldActionResult(DeleteWorldResult.Failure.WORLD_FOLDER_NOT_FOUND, world.getName()); } var result = removeWorld(world); @@ -517,11 +491,9 @@ public class WorldManager { // Erase world files from disk // TODO: Possible config options to keep certain files return filesManipulator.deleteFolder(worldFolder).fold( - exception -> Result.failure(DeleteWorldResult.Failure.FAILED_TO_DELETE_FOLDER, - replace(WORLD_PLACEHOLDER).with(world.getName()), - replace(ERROR_PLACEHOLDER).with(exception.getMessage())), - success -> Result.success(DeleteWorldResult.Success.DELETED, - replace(WORLD_PLACEHOLDER).with(world.getName()))); + exception -> worldActionResult(DeleteWorldResult.Failure.FAILED_TO_DELETE_FOLDER, + world.getName(), exception.getMessage()), + success -> worldActionResult(DeleteWorldResult.Success.DELETED, world.getName())); } /** @@ -542,13 +514,12 @@ public class WorldManager { failure.getReasonMessage()), success -> Result.success())) .onSuccessThen(s -> getLoadedWorld(options.newWorldName()).fold( - () -> Result.failure(CloneWorldResult.Failure.MV_WORLD_FAILED, - replace(WORLD_PLACEHOLDER).with(options.newWorldName())), + () -> worldActionResult(CloneWorldResult.Failure.MV_WORLD_FAILED, options.newWorldName()), mvWorld -> { cloneWorldTransferData(options, mvWorld); saveWorldsConfig(); return Result.success(CloneWorldResult.Success.CLONED, - replace(WORLD_PLACEHOLDER).with(options.world().getName()), + replaceWorldName(options.world().getName()), replace("{newworld}").with(mvWorld.getName())); })); } @@ -558,22 +529,18 @@ public class WorldManager { String newWorldName = options.newWorldName(); if (!worldNameChecker.isValidWorldName(newWorldName)) { Logging.severe("Invalid world name: " + newWorldName); - return Result.failure(CloneWorldResult.Failure.INVALID_WORLDNAME, - replace(WORLD_PLACEHOLDER).with(newWorldName)); + return worldActionResult(CloneWorldResult.Failure.INVALID_WORLDNAME, newWorldName); } if (worldNameChecker.isValidWorldFolder(newWorldName)) { - return Result.failure(CloneWorldResult.Failure.WORLD_EXIST_FOLDER, - replace(WORLD_PLACEHOLDER).with(newWorldName)); + return worldActionResult(CloneWorldResult.Failure.WORLD_EXIST_FOLDER, newWorldName); } if (isLoadedWorld(newWorldName)) { Logging.severe("World already loaded when attempting to clone: " + newWorldName); - return Result.failure(CloneWorldResult.Failure.WORLD_EXIST_LOADED, - replace(WORLD_PLACEHOLDER).with(newWorldName)); + return worldActionResult(CloneWorldResult.Failure.WORLD_EXIST_LOADED, newWorldName); } if (isWorld(newWorldName)) { Logging.severe("World already exist unloaded: " + newWorldName); - return Result.failure(CloneWorldResult.Failure.WORLD_EXIST_UNLOADED, - replace(WORLD_PLACEHOLDER).with(newWorldName)); + return worldActionResult(CloneWorldResult.Failure.WORLD_EXIST_UNLOADED, newWorldName); } return Result.success(); } @@ -584,9 +551,8 @@ public class WorldManager { File worldFolder = options.world().getBukkitWorld().map(World::getWorldFolder).getOrNull(); File newWorldFolder = new File(Bukkit.getWorldContainer(), options.newWorldName()); return filesManipulator.copyFolder(worldFolder, newWorldFolder, CLONE_IGNORE_FILES).fold( - exception -> Result.failure(CloneWorldResult.Failure.COPY_FAILED, - replace(WORLD_PLACEHOLDER).with(options.world().getName()), - replace(ERROR_PLACEHOLDER).with(exception.getMessage())), + exception -> worldActionResult(CloneWorldResult.Failure.COPY_FAILED, options.world().getName(), + exception.getMessage()), success -> Result.success()); } @@ -647,7 +613,30 @@ public class WorldManager { dataTransfer.pasteAllTo(newWorld); saveWorldsConfig(); }); - return Result.success(RegenWorldResult.Success.REGENERATED, replace(WORLD_PLACEHOLDER).with(world.getName())); + return worldActionResult(RegenWorldResult.Success.REGENERATED, world.getName()); + } + + private Result worldActionResult( + @NotNull S messageKeyProvider, @NotNull String worldName) { + return Result.success(messageKeyProvider, replaceWorldName(worldName)); + } + + private Result worldActionResult( + @NotNull F messageKeyProvider, @NotNull String worldName) { + return Result.failure(messageKeyProvider, replaceWorldName(worldName)); + } + + private Result worldActionResult( + @NotNull F messageKeyProvider, @NotNull String worldName, @NotNull String errorMessage) { + return Result.failure(messageKeyProvider, replaceWorldName(worldName), replaceError(errorMessage)); + } + + private MessageReplacement replaceWorldName(@NotNull String worldName) { + return replace("{world}").with(worldName); + } + + private MessageReplacement replaceError(@NotNull String errorMessage) { + return replace("{error}").with(errorMessage); } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/package-info.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/package-info.java new file mode 100644 index 00000000..4e49a86f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/package-info.java @@ -0,0 +1 @@ +package com.onarandombox.MultiverseCore.worldnew.config; diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/entrycheck/package-info.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/entrycheck/package-info.java new file mode 100644 index 00000000..c52c7e52 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/entrycheck/package-info.java @@ -0,0 +1 @@ +package com.onarandombox.MultiverseCore.worldnew.entrycheck; diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/generators/package-info.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/generators/package-info.java new file mode 100644 index 00000000..7c4566ac --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/generators/package-info.java @@ -0,0 +1 @@ +package com.onarandombox.MultiverseCore.worldnew.generators; diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/helpers/package-info.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/helpers/package-info.java new file mode 100644 index 00000000..0fdb7ec8 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/helpers/package-info.java @@ -0,0 +1 @@ +package com.onarandombox.MultiverseCore.worldnew.helpers; diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/options/package-info.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/options/package-info.java new file mode 100644 index 00000000..779a61a6 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/options/package-info.java @@ -0,0 +1 @@ +package com.onarandombox.MultiverseCore.worldnew.options; diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/package-info.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/package-info.java new file mode 100644 index 00000000..6410fbf9 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/package-info.java @@ -0,0 +1 @@ +package com.onarandombox.MultiverseCore.worldnew; diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/results/package-info.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/results/package-info.java new file mode 100644 index 00000000..b7723fd5 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/results/package-info.java @@ -0,0 +1 @@ +package com.onarandombox.MultiverseCore.worldnew.results;