Reduce Result boilerplate in WorldManager.

This commit is contained in:
Jeremy Wood 2023-09-09 11:14:01 -04:00
parent ba5c054298
commit c1d2fe48f0
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B

View File

@ -6,7 +6,9 @@ import com.onarandombox.MultiverseCore.api.BlockSafety;
import com.onarandombox.MultiverseCore.api.LocationManipulation; import com.onarandombox.MultiverseCore.api.LocationManipulation;
import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
import com.onarandombox.MultiverseCore.utils.message.MessageReplacement; 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.Result;
import com.onarandombox.MultiverseCore.utils.result.SuccessReason;
import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig; import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig;
import com.onarandombox.MultiverseCore.worldnew.config.WorldsConfigManager; import com.onarandombox.MultiverseCore.worldnew.config.WorldsConfigManager;
import com.onarandombox.MultiverseCore.worldnew.generators.GeneratorProvider; import com.onarandombox.MultiverseCore.worldnew.generators.GeneratorProvider;
@ -166,18 +168,17 @@ public class WorldManager {
public Result<CreateWorldResult.Success, CreateWorldResult.Failure> createWorld(CreateWorldOptions options) { public Result<CreateWorldResult.Success, CreateWorldResult.Failure> createWorld(CreateWorldOptions options) {
// Params validations // Params validations
if (!worldNameChecker.isValidWorldName(options.worldName())) { if (!worldNameChecker.isValidWorldName(options.worldName())) {
return Result.failure(CreateWorldResult.Failure.INVALID_WORLDNAME, replaceWorldName(options.worldName())); return worldActionResult(CreateWorldResult.Failure.INVALID_WORLDNAME, options.worldName());
} }
if (getLoadedWorld(options.worldName()).isDefined()) { if (getLoadedWorld(options.worldName()).isDefined()) {
return Result.failure(CreateWorldResult.Failure.WORLD_EXIST_LOADED, replaceWorldName(options.worldName())); return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName());
} }
if (getWorld(options.worldName()).isDefined()) { if (getWorld(options.worldName()).isDefined()) {
return Result.failure(CreateWorldResult.Failure.WORLD_EXIST_UNLOADED, return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName());
replaceWorldName(options.worldName()));
} }
File worldFolder = new File(Bukkit.getWorldContainer(), options.worldName()); File worldFolder = new File(Bukkit.getWorldContainer(), options.worldName());
if (worldFolder.exists()) { if (worldFolder.exists()) {
return Result.failure(CreateWorldResult.Failure.WORLD_EXIST_FOLDER, replaceWorldName(options.worldName())); return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_FOLDER, options.worldName());
} }
String parsedGenerator = parseGenerator(options.worldName(), options.generator()); String parsedGenerator = parseGenerator(options.worldName(), options.generator());
@ -188,13 +189,11 @@ public class WorldManager {
.seed(options.seed()) .seed(options.seed())
.type(options.worldType())) .type(options.worldType()))
.fold( .fold(
exception -> Result.failure(CreateWorldResult.Failure.BUKKIT_CREATION_FAILED, exception -> worldActionResult(CreateWorldResult.Failure.BUKKIT_CREATION_FAILED,
replaceWorldName(options.worldName()), options.worldName(), exception.getMessage()),
replaceError(exception.getMessage())),
world -> { world -> {
newLoadedMultiverseWorld(world, parsedGenerator, options.useSpawnAdjust()); newLoadedMultiverseWorld(world, parsedGenerator, options.useSpawnAdjust());
return Result.success(CreateWorldResult.Success.CREATED, return worldActionResult(CreateWorldResult.Success.CREATED, options.worldName());
replaceWorldName(world.getName()));
}); });
} }
@ -208,18 +207,16 @@ public class WorldManager {
public Result<ImportWorldResult.Success, ImportWorldResult.Failure> importWorld(ImportWorldOptions options) { public Result<ImportWorldResult.Success, ImportWorldResult.Failure> importWorld(ImportWorldOptions options) {
// Params validations // Params validations
if (!worldNameChecker.isValidWorldName(options.worldName())) { if (!worldNameChecker.isValidWorldName(options.worldName())) {
return Result.failure(ImportWorldResult.Failure.INVALID_WORLDNAME, replaceWorldName(options.worldName())); return worldActionResult(ImportWorldResult.Failure.INVALID_WORLDNAME, options.worldName());
} }
if (!worldNameChecker.isValidWorldFolder(options.worldName())) { if (!worldNameChecker.isValidWorldFolder(options.worldName())) {
return Result.failure(ImportWorldResult.Failure.WORLD_FOLDER_INVALID, return worldActionResult(ImportWorldResult.Failure.WORLD_FOLDER_INVALID, options.worldName());
replaceWorldName(options.worldName()));
} }
if (isLoadedWorld(options.worldName())) { if (isLoadedWorld(options.worldName())) {
return Result.failure(ImportWorldResult.Failure.WORLD_EXIST_LOADED, replaceWorldName(options.worldName())); return worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName());
} }
if (isWorld(options.worldName())) { if (isWorld(options.worldName())) {
return Result.failure(ImportWorldResult.Failure.WORLD_EXIST_UNLOADED, return worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName());
replaceWorldName(options.worldName()));
} }
String parsedGenerator = parseGenerator(options.worldName(), options.generator()); String parsedGenerator = parseGenerator(options.worldName(), options.generator());
@ -227,13 +224,11 @@ public class WorldManager {
.environment(options.environment()) .environment(options.environment())
.generator(parsedGenerator)) .generator(parsedGenerator))
.fold( .fold(
exception -> Result.failure(ImportWorldResult.Failure.BUKKIT_CREATION_FAILED, exception -> worldActionResult(ImportWorldResult.Failure.BUKKIT_CREATION_FAILED,
replaceWorldName(options.worldName()), options.worldName(), exception.getMessage()),
replaceError(exception.getMessage())),
world -> { world -> {
newLoadedMultiverseWorld(world, parsedGenerator, options.useSpawnAdjust()); newLoadedMultiverseWorld(world, parsedGenerator, options.useSpawnAdjust());
return Result.success(ImportWorldResult.Success.IMPORTED, return worldActionResult(ImportWorldResult.Success.IMPORTED, options.worldName());
replaceWorldName(options.worldName()));
}); });
} }
@ -285,8 +280,8 @@ public class WorldManager {
return getWorld(worldName) return getWorld(worldName)
.map(this::loadWorld) .map(this::loadWorld)
.getOrElse(() -> worldNameChecker.isValidWorldFolder(worldName) .getOrElse(() -> worldNameChecker.isValidWorldFolder(worldName)
? Result.failure(LoadWorldResult.Failure.WORLD_EXIST_FOLDER, replaceWorldName(worldName)) ? worldActionResult(LoadWorldResult.Failure.WORLD_EXIST_FOLDER, worldName)
: Result.failure(LoadWorldResult.Failure.WORLD_NON_EXISTENT, replaceWorldName(worldName))); : worldActionResult(LoadWorldResult.Failure.WORLD_NON_EXISTENT, worldName));
} }
/** /**
@ -300,20 +295,19 @@ public class WorldManager {
if (loadTracker.contains(mvWorld.getName())) { if (loadTracker.contains(mvWorld.getName())) {
// This is to prevent recursive calls by WorldLoadEvent // This is to prevent recursive calls by WorldLoadEvent
Logging.fine("World already loading: " + mvWorld.getName()); Logging.fine("World already loading: " + mvWorld.getName());
return Result.failure(LoadWorldResult.Failure.WORLD_ALREADY_LOADING, replaceWorldName(mvWorld.getName())); return worldActionResult(LoadWorldResult.Failure.WORLD_ALREADY_LOADING, mvWorld.getName());
} }
if (isLoadedWorld(mvWorld)) { if (isLoadedWorld(mvWorld)) {
Logging.severe("World already loaded: " + mvWorld.getName()); Logging.severe("World already loaded: " + mvWorld.getName());
return Result.failure(LoadWorldResult.Failure.WORLD_EXIST_LOADED, replaceWorldName(mvWorld.getName())); return worldActionResult(LoadWorldResult.Failure.WORLD_EXIST_LOADED, mvWorld.getName());
} }
return createBukkitWorld(WorldCreator.name(mvWorld.getName()) return createBukkitWorld(WorldCreator.name(mvWorld.getName())
.environment(mvWorld.getEnvironment()) .environment(mvWorld.getEnvironment())
.generator(Strings.isNullOrEmpty(mvWorld.getGenerator()) ? null : mvWorld.getGenerator()) .generator(Strings.isNullOrEmpty(mvWorld.getGenerator()) ? null : mvWorld.getGenerator())
.seed(mvWorld.getSeed())).fold( .seed(mvWorld.getSeed())).fold(
exception -> Result.failure(LoadWorldResult.Failure.BUKKIT_CREATION_FAILED, exception -> worldActionResult(LoadWorldResult.Failure.BUKKIT_CREATION_FAILED,
replaceWorldName(mvWorld.getName()), mvWorld.getName(), exception.getMessage()),
replaceError(exception.getMessage())),
world -> { world -> {
// TODO: Check worldConfig null // TODO: Check worldConfig null
WorldConfig worldConfig = worldsConfigManager.getWorldConfig(mvWorld.getName()); WorldConfig worldConfig = worldsConfigManager.getWorldConfig(mvWorld.getName());
@ -325,8 +319,7 @@ public class WorldManager {
locationManipulation); locationManipulation);
loadedWorldsMap.put(loadedWorld.getName(), loadedWorld); loadedWorldsMap.put(loadedWorld.getName(), loadedWorld);
saveWorldsConfig(); saveWorldsConfig();
return Result.success(LoadWorldResult.Success.LOADED, return worldActionResult(LoadWorldResult.Success.LOADED, loadedWorld.getName());
replaceWorldName(loadedWorld.getName()));
}); });
} }
@ -350,8 +343,8 @@ public class WorldManager {
return getLoadedWorld(worldName) return getLoadedWorld(worldName)
.map(this::unloadWorld) .map(this::unloadWorld)
.getOrElse(() -> isUnloadedWorld(worldName) .getOrElse(() -> isUnloadedWorld(worldName)
? Result.failure(UnloadWorldResult.Failure.WORLD_UNLOADED, replaceWorldName(worldName)) ? worldActionResult(UnloadWorldResult.Failure.WORLD_UNLOADED, worldName)
: Result.failure(UnloadWorldResult.Failure.WORLD_NON_EXISTENT, replaceWorldName(worldName))); : worldActionResult(UnloadWorldResult.Failure.WORLD_NON_EXISTENT, worldName));
} }
/** /**
@ -365,26 +358,23 @@ public class WorldManager {
if (unloadTracker.contains(world.getName())) { if (unloadTracker.contains(world.getName())) {
// This is to prevent recursive calls by WorldUnloadEvent // This is to prevent recursive calls by WorldUnloadEvent
Logging.fine("World already unloading: " + world.getName()); Logging.fine("World already unloading: " + world.getName());
return Result.failure(UnloadWorldResult.Failure.WORLD_ALREADY_UNLOADING, replaceWorldName(world.getName())); return worldActionResult(UnloadWorldResult.Failure.WORLD_ALREADY_UNLOADING, world.getName());
} }
// TODO: removePlayersFromWorld? // TODO: removePlayersFromWorld?
return unloadBukkitWorld(world.getBukkitWorld().getOrNull()).fold( return unloadBukkitWorld(world.getBukkitWorld().getOrNull()).fold(
exception -> Result.failure(UnloadWorldResult.Failure.BUKKIT_UNLOAD_FAILED, exception -> worldActionResult(UnloadWorldResult.Failure.BUKKIT_UNLOAD_FAILED,
replaceWorldName(world.getName()), world.getName(), exception.getMessage()),
replaceError(exception.getMessage())),
success -> Option.of(loadedWorldsMap.remove(world.getName())).fold( success -> Option.of(loadedWorldsMap.remove(world.getName())).fold(
() -> { () -> {
Logging.severe("Failed to remove world from map: " + world.getName()); Logging.severe("Failed to remove world from map: " + world.getName());
return Result.failure(UnloadWorldResult.Failure.WORLD_NON_EXISTENT, return worldActionResult(UnloadWorldResult.Failure.WORLD_NON_EXISTENT, world.getName());
replaceWorldName(world.getName()));
}, },
mvWorld -> { mvWorld -> {
Logging.fine("Removed MultiverseWorld from map: " + world.getName()); Logging.fine("Removed MultiverseWorld from map: " + world.getName());
mvWorld.getWorldConfig().deferenceMVWorld(); mvWorld.getWorldConfig().deferenceMVWorld();
return Result.success(UnloadWorldResult.Success.UNLOADED, return worldActionResult(UnloadWorldResult.Success.UNLOADED, world.getName());
replaceWorldName(world.getName()));
})); }));
} }
@ -399,8 +389,7 @@ public class WorldManager {
removeWorld(@NotNull String worldName) { removeWorld(@NotNull String worldName) {
return getWorld(worldName) return getWorld(worldName)
.map(this::removeWorld) .map(this::removeWorld)
.getOrElse(() -> Result.failure(RemoveWorldResult.Failure.WORLD_NON_EXISTENT, .getOrElse(() -> worldActionResult(RemoveWorldResult.Failure.WORLD_NON_EXISTENT, worldName));
replaceWorldName(worldName)));
} }
/** /**
@ -458,8 +447,7 @@ public class WorldManager {
public Result<DeleteWorldResult.Success, DeleteWorldResult.Failure> deleteWorld(@NotNull String worldName) { public Result<DeleteWorldResult.Success, DeleteWorldResult.Failure> deleteWorld(@NotNull String worldName) {
return getWorld(worldName) return getWorld(worldName)
.map(this::deleteWorld) .map(this::deleteWorld)
.getOrElse(() -> Result.failure(DeleteWorldResult.Failure.WORLD_NON_EXISTENT, .getOrElse(() -> worldActionResult(DeleteWorldResult.Failure.WORLD_NON_EXISTENT, worldName));
replaceWorldName(worldName)));
} }
/** /**
@ -474,7 +462,7 @@ public class WorldManager {
() -> { () -> {
var result = loadWorld(world); var result = loadWorld(world);
if (result.isFailure()) { if (result.isFailure()) {
return Result.failure(DeleteWorldResult.Failure.LOAD_FAILED, replaceWorldName(world.getName())); return worldActionResult(DeleteWorldResult.Failure.LOAD_FAILED, world.getName());
} }
return deleteWorld(world); return deleteWorld(world);
}, },
@ -492,7 +480,7 @@ public class WorldManager {
File worldFolder = world.getBukkitWorld().map(World::getWorldFolder).getOrNull(); File worldFolder = world.getBukkitWorld().map(World::getWorldFolder).getOrNull();
if (worldFolder == null || !worldNameChecker.isValidWorldFolder(worldFolder)) { if (worldFolder == null || !worldNameChecker.isValidWorldFolder(worldFolder)) {
Logging.severe("Failed to get world folder for world: " + world.getName()); Logging.severe("Failed to get world folder for world: " + world.getName());
return Result.failure(DeleteWorldResult.Failure.WORLD_FOLDER_NOT_FOUND, replaceWorldName(world.getName())); return worldActionResult(DeleteWorldResult.Failure.WORLD_FOLDER_NOT_FOUND, world.getName());
} }
var result = removeWorld(world); var result = removeWorld(world);
@ -503,10 +491,9 @@ public class WorldManager {
// Erase world files from disk // Erase world files from disk
// TODO: Possible config options to keep certain files // TODO: Possible config options to keep certain files
return filesManipulator.deleteFolder(worldFolder).fold( return filesManipulator.deleteFolder(worldFolder).fold(
exception -> Result.failure(DeleteWorldResult.Failure.FAILED_TO_DELETE_FOLDER, exception -> worldActionResult(DeleteWorldResult.Failure.FAILED_TO_DELETE_FOLDER,
replaceWorldName(world.getName()), world.getName(), exception.getMessage()),
replaceError(exception.getMessage())), success -> worldActionResult(DeleteWorldResult.Success.DELETED, world.getName()));
success -> Result.success(DeleteWorldResult.Success.DELETED, replaceWorldName(world.getName())));
} }
/** /**
@ -527,8 +514,7 @@ public class WorldManager {
failure.getReasonMessage()), failure.getReasonMessage()),
success -> Result.success())) success -> Result.success()))
.onSuccessThen(s -> getLoadedWorld(options.newWorldName()).fold( .onSuccessThen(s -> getLoadedWorld(options.newWorldName()).fold(
() -> Result.failure(CloneWorldResult.Failure.MV_WORLD_FAILED, () -> worldActionResult(CloneWorldResult.Failure.MV_WORLD_FAILED, options.newWorldName()),
replaceWorldName(options.newWorldName())),
mvWorld -> { mvWorld -> {
cloneWorldTransferData(options, mvWorld); cloneWorldTransferData(options, mvWorld);
saveWorldsConfig(); saveWorldsConfig();
@ -543,18 +529,18 @@ public class WorldManager {
String newWorldName = options.newWorldName(); String newWorldName = options.newWorldName();
if (!worldNameChecker.isValidWorldName(newWorldName)) { if (!worldNameChecker.isValidWorldName(newWorldName)) {
Logging.severe("Invalid world name: " + newWorldName); Logging.severe("Invalid world name: " + newWorldName);
return Result.failure(CloneWorldResult.Failure.INVALID_WORLDNAME, replaceWorldName(newWorldName)); return worldActionResult(CloneWorldResult.Failure.INVALID_WORLDNAME, newWorldName);
} }
if (worldNameChecker.isValidWorldFolder(newWorldName)) { if (worldNameChecker.isValidWorldFolder(newWorldName)) {
return Result.failure(CloneWorldResult.Failure.WORLD_EXIST_FOLDER, replaceWorldName(newWorldName)); return worldActionResult(CloneWorldResult.Failure.WORLD_EXIST_FOLDER, newWorldName);
} }
if (isLoadedWorld(newWorldName)) { if (isLoadedWorld(newWorldName)) {
Logging.severe("World already loaded when attempting to clone: " + newWorldName); Logging.severe("World already loaded when attempting to clone: " + newWorldName);
return Result.failure(CloneWorldResult.Failure.WORLD_EXIST_LOADED, replaceWorldName(newWorldName)); return worldActionResult(CloneWorldResult.Failure.WORLD_EXIST_LOADED, newWorldName);
} }
if (isWorld(newWorldName)) { if (isWorld(newWorldName)) {
Logging.severe("World already exist unloaded: " + newWorldName); Logging.severe("World already exist unloaded: " + newWorldName);
return Result.failure(CloneWorldResult.Failure.WORLD_EXIST_UNLOADED, replaceWorldName(newWorldName)); return worldActionResult(CloneWorldResult.Failure.WORLD_EXIST_UNLOADED, newWorldName);
} }
return Result.success(); return Result.success();
} }
@ -565,9 +551,8 @@ public class WorldManager {
File worldFolder = options.world().getBukkitWorld().map(World::getWorldFolder).getOrNull(); File worldFolder = options.world().getBukkitWorld().map(World::getWorldFolder).getOrNull();
File newWorldFolder = new File(Bukkit.getWorldContainer(), options.newWorldName()); File newWorldFolder = new File(Bukkit.getWorldContainer(), options.newWorldName());
return filesManipulator.copyFolder(worldFolder, newWorldFolder, CLONE_IGNORE_FILES).fold( return filesManipulator.copyFolder(worldFolder, newWorldFolder, CLONE_IGNORE_FILES).fold(
exception -> Result.failure(CloneWorldResult.Failure.COPY_FAILED, exception -> worldActionResult(CloneWorldResult.Failure.COPY_FAILED, options.world().getName(),
replaceWorldName(options.world().getName()), exception.getMessage()),
replaceError(exception.getMessage())),
success -> Result.success()); success -> Result.success());
} }
@ -628,7 +613,22 @@ public class WorldManager {
dataTransfer.pasteAllTo(newWorld); dataTransfer.pasteAllTo(newWorld);
saveWorldsConfig(); saveWorldsConfig();
}); });
return Result.success(RegenWorldResult.Success.REGENERATED, replaceWorldName(world.getName())); return worldActionResult(RegenWorldResult.Success.REGENERATED, world.getName());
}
private <F extends FailureReason, S extends SuccessReason> Result<S, F> worldActionResult(
@NotNull S messageKeyProvider, @NotNull String worldName) {
return Result.success(messageKeyProvider, replaceWorldName(worldName));
}
private <F extends FailureReason, S extends SuccessReason> Result<S, F> worldActionResult(
@NotNull F messageKeyProvider, @NotNull String worldName) {
return Result.failure(messageKeyProvider, replaceWorldName(worldName));
}
private <F extends FailureReason, S extends SuccessReason> Result<S, F> worldActionResult(
@NotNull F messageKeyProvider, @NotNull String worldName, @NotNull String errorMessage) {
return Result.failure(messageKeyProvider, replaceWorldName(worldName), replaceError(errorMessage));
} }
private MessageReplacement replaceWorldName(@NotNull String worldName) { private MessageReplacement replaceWorldName(@NotNull String worldName) {