diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java index 6d19ca24..4fdef2c1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java @@ -54,7 +54,7 @@ import static com.onarandombox.MultiverseCore.worldnew.helpers.DataStore.WorldCo /** * This manager contains all the world managing functions that your heart desires. */ -@Service +@Service // SUPPRESS CHECKSTYLE: ClassFanOutComplexity This is the world manager, it's going to be complex. public class WorldManager { private static final List CLONE_IGNORE_FILES = Arrays.asList("uid.dat", "session.lock"); @@ -100,7 +100,7 @@ public class WorldManager { * @return The result of the load. */ public Try initAllWorlds() { - return populateWorldFromConfig().andThenTry(() -> { + return updateWorldsFromConfig().andThenTry(() -> { loadDefaultWorlds(); autoLoadWorlds(); saveWorldsConfig(); @@ -108,30 +108,33 @@ public class WorldManager { } /** - * Populate world map from the worlds.yml config. + * Updates the current set of worlds to match the worlds config. * - * @return The result of the world map population. + * @return A successful Try if the worlds.yml config was loaded successfully. */ - private Try populateWorldFromConfig() { + private Try updateWorldsFromConfig() { return worldsConfigManager.load().mapTry(result -> { - var newWorldConfigs = result._1(); - var removedWorlds = result._2(); - - newWorldConfigs.forEach(worldConfig -> getWorld(worldConfig.getWorldName()) - .peek(unloadedWorld -> unloadedWorld.setWorldConfig(worldConfig)) - .onEmpty(() -> { - MultiverseWorld mvWorld = new MultiverseWorld(worldConfig.getWorldName(), worldConfig); - worldsMap.put(mvWorld.getName(), mvWorld); - })); - - removedWorlds.forEach(worldName -> removeWorld(worldName) - .onFailure(failure -> Logging.severe("Failed to unload world %s: %s", worldName, failure)) - .onSuccess(success -> Logging.fine("Unloaded world %s as it was removed from config", worldName))); - + loadNewWorldConfigs(result._1()); + removeWorldsNotInConfigs(result._2()); return null; }); } + private void loadNewWorldConfigs(Collection newWorldConfigs) { + newWorldConfigs.forEach(worldConfig -> getWorld(worldConfig.getWorldName()) + .peek(unloadedWorld -> unloadedWorld.setWorldConfig(worldConfig)) + .onEmpty(() -> { + MultiverseWorld mvWorld = new MultiverseWorld(worldConfig.getWorldName(), worldConfig); + worldsMap.put(mvWorld.getName(), mvWorld); + })); + } + + private void removeWorldsNotInConfigs(Collection removedWorlds) { + removedWorlds.forEach(worldName -> removeWorld(worldName) + .onFailure(failure -> Logging.severe("Failed to unload world %s: %s", worldName, failure)) + .onSuccess(success -> Logging.fine("Unloaded world %s as it was removed from config", worldName))); + } + /** * Load worlds that are already loaded by bukkit before Multiverse-Core is loaded. */