Clean up some return counts in WorldManager.

This commit is contained in:
Jeremy Wood 2023-09-10 01:35:53 -04:00
parent e81296015e
commit 1eb33c6972
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B

View File

@ -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.DataStore.GameRulesStore;
import com.onarandombox.MultiverseCore.worldnew.helpers.DataTransfer; import com.onarandombox.MultiverseCore.worldnew.helpers.DataTransfer;
import com.onarandombox.MultiverseCore.worldnew.helpers.FilesManipulator; import com.onarandombox.MultiverseCore.worldnew.helpers.FilesManipulator;
import com.onarandombox.MultiverseCore.worldnew.options.CloneWorldOptions; import com.onarandombox.MultiverseCore.worldnew.options.*;
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.results.CloneWorldResult; import com.onarandombox.MultiverseCore.worldnew.results.CloneWorldResult;
import com.onarandombox.MultiverseCore.worldnew.results.CreateWorldResult; import com.onarandombox.MultiverseCore.worldnew.results.CreateWorldResult;
import com.onarandombox.MultiverseCore.worldnew.results.DeleteWorldResult; import com.onarandombox.MultiverseCore.worldnew.results.DeleteWorldResult;
@ -169,29 +166,40 @@ public class WorldManager {
* @return The result of the creation. * @return The result of the creation.
*/ */
public Result<CreateWorldResult.Success, CreateWorldResult.Failure> createWorld(CreateWorldOptions options) { public Result<CreateWorldResult.Success, CreateWorldResult.Failure> createWorld(CreateWorldOptions options) {
// Params validations return invalidateCreateWorldOptions(options).getOrElse(() -> createValidatedWorld(options));
}
private Option<Result<CreateWorldResult.Success, CreateWorldResult.Failure>> invalidateCreateWorldOptions(
CreateWorldOptions options) {
Result<CreateWorldResult.Success, CreateWorldResult.Failure> result = null;
if (!worldNameChecker.isValidWorldName(options.worldName())) { if (!worldNameChecker.isValidWorldName(options.worldName())) {
return worldActionResult(CreateWorldResult.Failure.INVALID_WORLDNAME, options.worldName()); result = worldActionResult(CreateWorldResult.Failure.INVALID_WORLDNAME, options.worldName());
} } else if (getLoadedWorld(options.worldName()).isDefined()) {
if (getLoadedWorld(options.worldName()).isDefined()) { result = worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName());
return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName()); } else if (getWorld(options.worldName()).isDefined()) {
} result = worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName());
if (getWorld(options.worldName()).isDefined()) { } else if (hasWorldFolder(options.worldName())) {
return worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName()); result = worldActionResult(CreateWorldResult.Failure.WORLD_EXIST_FOLDER, options.worldName());
}
File worldFolder = new File(Bukkit.getWorldContainer(), options.worldName());
if (worldFolder.exists()) {
return 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<CreateWorldResult.Success, CreateWorldResult.Failure> createValidatedWorld(
CreateWorldOptions options) {
String parsedGenerator = parseGenerator(options.worldName(), options.generator()); String parsedGenerator = parseGenerator(options.worldName(), options.generator());
return createBukkitWorld(WorldCreator.name(options.worldName()) return createBukkitWorld(WorldCreator.name(options.worldName())
.environment(options.environment()) .environment(options.environment())
.generateStructures(options.generateStructures()) .generateStructures(options.generateStructures())
.generator(parsedGenerator) .generator(parsedGenerator)
.seed(options.seed()) .seed(options.seed())
.type(options.worldType())) .type(options.worldType())).fold(
.fold(
exception -> worldActionResult(CreateWorldResult.Failure.BUKKIT_CREATION_FAILED, exception -> worldActionResult(CreateWorldResult.Failure.BUKKIT_CREATION_FAILED,
options.worldName(), exception.getMessage()), options.worldName(), exception.getMessage()),
world -> { world -> {
@ -200,7 +208,6 @@ public class WorldManager {
}); });
} }
/** /**
* Imports an existing world folder. * Imports an existing world folder.
* *
@ -208,25 +215,32 @@ public class WorldManager {
* @return The result of the import. * @return The result of the import.
*/ */
public Result<ImportWorldResult.Success, ImportWorldResult.Failure> importWorld(ImportWorldOptions options) { public Result<ImportWorldResult.Success, ImportWorldResult.Failure> importWorld(ImportWorldOptions options) {
// Params validations return invalidateImportWorldOptions(options).getOrElse(() -> importValidatedWorld(options));
}
private Option<Result<ImportWorldResult.Success, ImportWorldResult.Failure>> invalidateImportWorldOptions(
ImportWorldOptions options) {
Result<ImportWorldResult.Success, ImportWorldResult.Failure> result = null;
if (!worldNameChecker.isValidWorldName(options.worldName())) { if (!worldNameChecker.isValidWorldName(options.worldName())) {
return worldActionResult(ImportWorldResult.Failure.INVALID_WORLDNAME, options.worldName()); result = worldActionResult(ImportWorldResult.Failure.INVALID_WORLDNAME, options.worldName());
} } else if (!worldNameChecker.isValidWorldFolder(options.worldName())) {
if (!worldNameChecker.isValidWorldFolder(options.worldName())) { result = worldActionResult(ImportWorldResult.Failure.WORLD_FOLDER_INVALID, options.worldName());
return worldActionResult(ImportWorldResult.Failure.WORLD_FOLDER_INVALID, options.worldName()); } else if (isLoadedWorld(options.worldName())) {
} result = worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName());
if (isLoadedWorld(options.worldName())) { } else if (isWorld(options.worldName())) {
return worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_LOADED, options.worldName()); result = worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName());
}
if (isWorld(options.worldName())) {
return worldActionResult(ImportWorldResult.Failure.WORLD_EXIST_UNLOADED, options.worldName());
} }
return Option.of(result);
}
private Result<ImportWorldResult.Success, ImportWorldResult.Failure> importValidatedWorld(
ImportWorldOptions options) {
String parsedGenerator = parseGenerator(options.worldName(), options.generator()); String parsedGenerator = parseGenerator(options.worldName(), options.generator());
return createBukkitWorld(WorldCreator.name(options.worldName()) return createBukkitWorld(WorldCreator.name(options.worldName())
.environment(options.environment()) .environment(options.environment())
.generator(parsedGenerator)) .generator(parsedGenerator)).fold(
.fold(
exception -> worldActionResult(ImportWorldResult.Failure.BUKKIT_CREATION_FAILED, exception -> worldActionResult(ImportWorldResult.Failure.BUKKIT_CREATION_FAILED,
options.worldName(), exception.getMessage()), options.worldName(), exception.getMessage()),
world -> { world -> {
@ -294,17 +308,27 @@ public class WorldManager {
* @return The result of the load. * @return The result of the load.
*/ */
public Result<LoadWorldResult.Success, LoadWorldResult.Failure> loadWorld(@NotNull MultiverseWorld mvWorld) { public Result<LoadWorldResult.Success, LoadWorldResult.Failure> loadWorld(@NotNull MultiverseWorld mvWorld) {
// Params validations return invalidateWorldToLoad(mvWorld).getOrElse(() -> loadValidatedWorld(mvWorld));
}
private Option<Result<LoadWorldResult.Success, LoadWorldResult.Failure>> invalidateWorldToLoad(
@NotNull MultiverseWorld mvWorld) {
Result<LoadWorldResult.Success, LoadWorldResult.Failure> result = null;
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 worldActionResult(LoadWorldResult.Failure.WORLD_ALREADY_LOADING, mvWorld.getName()); result = worldActionResult(LoadWorldResult.Failure.WORLD_ALREADY_LOADING, mvWorld.getName());
} } else if (isLoadedWorld(mvWorld)) {
if (isLoadedWorld(mvWorld)) {
Logging.severe("World already loaded: " + mvWorld.getName()); 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<LoadWorldResult.Success, LoadWorldResult.Failure> loadValidatedWorld(
@NotNull MultiverseWorld mvWorld) {
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())
@ -356,8 +380,8 @@ public class WorldManager {
* @param world The multiverse world to unload. * @param world The multiverse world to unload.
* @return The result of the unload action. * @return The result of the unload action.
*/ */
public Result<UnloadWorldResult.Success, UnloadWorldResult.Failure> public Result<UnloadWorldResult.Success, UnloadWorldResult.Failure> unloadWorld(
unloadWorld(@NotNull LoadedMultiverseWorld world) { @NotNull LoadedMultiverseWorld world) {
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());
@ -388,8 +412,8 @@ public class WorldManager {
* @param worldName The name of the world to remove. * @param worldName The name of the world to remove.
* @return The result of the remove. * @return The result of the remove.
*/ */
public Result<RemoveWorldResult.Success, RemoveWorldResult.Failure> public Result<RemoveWorldResult.Success, RemoveWorldResult.Failure> removeWorld(
removeWorld(@NotNull String worldName) { @NotNull String worldName) {
return getWorld(worldName) return getWorld(worldName)
.map(this::removeWorld) .map(this::removeWorld)
.getOrElse(() -> worldActionResult(RemoveWorldResult.Failure.WORLD_NON_EXISTENT, worldName)); .getOrElse(() -> worldActionResult(RemoveWorldResult.Failure.WORLD_NON_EXISTENT, worldName));
@ -415,8 +439,8 @@ public class WorldManager {
* @param loadedWorld The multiverse world to remove. * @param loadedWorld The multiverse world to remove.
* @return The result of the remove. * @return The result of the remove.
*/ */
public Result<RemoveWorldResult.Success, RemoveWorldResult.Failure> public Result<RemoveWorldResult.Success, RemoveWorldResult.Failure> removeWorld(
removeWorld(@NotNull LoadedMultiverseWorld loadedWorld) { @NotNull LoadedMultiverseWorld loadedWorld) {
var result = unloadWorld(loadedWorld); var result = unloadWorld(loadedWorld);
if (result.isFailure()) { if (result.isFailure()) {
return Result.failure(RemoveWorldResult.Failure.UNLOAD_FAILED, result.getReasonMessage()); return Result.failure(RemoveWorldResult.Failure.UNLOAD_FAILED, result.getReasonMessage());
@ -527,8 +551,8 @@ public class WorldManager {
})); }));
} }
private Result<CloneWorldResult.Success, CloneWorldResult.Failure> private Result<CloneWorldResult.Success, CloneWorldResult.Failure> cloneWorldValidateWorld(
cloneWorldValidateWorld(@NotNull CloneWorldOptions options) { @NotNull CloneWorldOptions options) {
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);
@ -548,8 +572,8 @@ public class WorldManager {
return Result.success(); return Result.success();
} }
private Result<CloneWorldResult.Success, CloneWorldResult.Failure> private Result<CloneWorldResult.Success, CloneWorldResult.Failure> cloneWorldCopyFolder(
cloneWorldCopyFolder(@NotNull CloneWorldOptions options) { @NotNull CloneWorldOptions options) {
// TODO: Check null? // TODO: Check null?
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());
@ -560,8 +584,14 @@ public class WorldManager {
} }
private void cloneWorldTransferData(@NotNull CloneWorldOptions options, @NotNull LoadedMultiverseWorld newWorld) { private void cloneWorldTransferData(@NotNull CloneWorldOptions options, @NotNull LoadedMultiverseWorld newWorld) {
LoadedMultiverseWorld world = options.world(); DataTransfer<LoadedMultiverseWorld> dataTransfer = transferData(options, options.world());
dataTransfer.pasteAllTo(newWorld);
}
private DataTransfer<LoadedMultiverseWorld> transferData(
@NotNull KeepWorldSettingsOptions options, @NotNull LoadedMultiverseWorld world) {
DataTransfer<LoadedMultiverseWorld> dataTransfer = new DataTransfer<>(); DataTransfer<LoadedMultiverseWorld> dataTransfer = new DataTransfer<>();
if (options.keepWorldConfig()) { if (options.keepWorldConfig()) {
dataTransfer.addDataStore(new WorldConfigStore(), world); dataTransfer.addDataStore(new WorldConfigStore(), world);
} }
@ -571,7 +601,8 @@ public class WorldManager {
if (options.keepWorldBorder()) { if (options.keepWorldBorder()) {
dataTransfer.addDataStore(new WorldBorderStore(), world); 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 // TODO: Teleport players out of world, and back in after regen
LoadedMultiverseWorld world = options.world(); LoadedMultiverseWorld world = options.world();
DataTransfer<LoadedMultiverseWorld> dataTransfer = new DataTransfer<>(); DataTransfer<LoadedMultiverseWorld> dataTransfer = transferData(options, world);
if (options.keepWorldConfig()) {
dataTransfer.addDataStore(new WorldConfigStore(), world);
}
if (options.keepGameRule()) {
dataTransfer.addDataStore(new GameRulesStore(), world);
}
if (options.keepWorldBorder()) {
dataTransfer.addDataStore(new WorldBorderStore(), world);
}
CreateWorldOptions createWorldOptions = CreateWorldOptions.worldName(world.getName()) CreateWorldOptions createWorldOptions = CreateWorldOptions.worldName(world.getName())
.environment(world.getEnvironment()) .environment(world.getEnvironment())