diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 956ff030e..1caeea7fe 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -5,6 +5,9 @@ import java.util.Optional; import org.apache.commons.lang.exception.ExceptionUtils; import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.server.ServerCommandEvent; import org.bukkit.generator.ChunkGenerator; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -52,7 +55,7 @@ import world.bentobox.bentobox.versions.ServerCompatibility; * Main BentoBox class * @author tastybento, Poslovitch */ -public class BentoBox extends JavaPlugin { +public class BentoBox extends JavaPlugin implements Listener { private static BentoBox instance; @@ -227,7 +230,7 @@ public class BentoBox extends JavaPlugin { // Make sure all worlds are already registered to Multiverse. hooksManager.registerHook(new MultiverseCoreHook()); hooksManager.registerHook(new MyWorldsHook()); - islandWorldManager.registerWorldsToMultiverse(); + islandWorldManager.registerWorldsToMultiverse(true); // TODO: re-enable after implementation //hooksManager.registerHook(new DynmapHook()); @@ -300,6 +303,8 @@ public class BentoBox extends JavaPlugin { manager.registerEvents(new BannedCommands(this), this); // Death counter manager.registerEvents(new DeathListener(this), this); + // MV unregister + manager.registerEvents(this, this); // Island Delete Manager islandChunkDeletionManager = new IslandChunkDeletionManager(this); islandDeletionManager = new IslandDeletionManager(this); @@ -321,6 +326,15 @@ public class BentoBox extends JavaPlugin { if (islandsManager != null) { islandsManager.shutdown(); } + + } + + @EventHandler + public void onServerStop(ServerCommandEvent e) { + if (islandWorldManager != null && (e.getCommand().equalsIgnoreCase("stop") || e.getCommand().equalsIgnoreCase("restart"))) { + // Unregister any MV worlds if () { + islandWorldManager.registerWorldsToMultiverse(false); + } } /** diff --git a/src/main/java/world/bentobox/bentobox/hooks/MultiverseCoreHook.java b/src/main/java/world/bentobox/bentobox/hooks/MultiverseCoreHook.java index 62ba2abe9..f58bfd87f 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/MultiverseCoreHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/MultiverseCoreHook.java @@ -49,6 +49,12 @@ public class MultiverseCoreHook extends Hook implements WorldManagementHook { } } + @Override + public void unregisterWorld(World world) { + String cmd = "mv remove " + world.getName(); + Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmd); + } + @Override public boolean hook() { return true; // The hook process shouldn't fail diff --git a/src/main/java/world/bentobox/bentobox/hooks/WorldManagementHook.java b/src/main/java/world/bentobox/bentobox/hooks/WorldManagementHook.java index 4f79a96c3..daa9fcaa2 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/WorldManagementHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/WorldManagementHook.java @@ -13,8 +13,17 @@ public interface WorldManagementHook { /** * Register the world with the World Management hook * + * * @param world - world to register * @param islandWorld - if true, then this is an island world */ void registerWorld(World world, boolean islandWorld); + + /** + * Unregisters a world. + * @param world - world to unregister + */ + default void unregisterWorld(World world) { + // Do nothing + } } diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java index 3b2678e8b..cdc64a64a 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java @@ -50,36 +50,48 @@ public class IslandWorldManager { gameModes = new HashMap<>(); } - public void registerWorldsToMultiverse() { + /** + * Registers or unregisters worlds with world management plugins + * + * @param reg true to register, false to remove registration + * + * Updated 1.24.0 + */ + public void registerWorldsToMultiverse(boolean reg) { gameModes.values().stream().distinct().forEach(gm -> { - registerToWorldManagementPlugins(gm.getOverWorld(), true); + registerToWorldManagementPlugins(gm.getOverWorld(), true, reg); if (gm.getWorldSettings().isNetherGenerate()) { - registerToWorldManagementPlugins(gm.getNetherWorld(), gm.getWorldSettings().isNetherIslands()); + registerToWorldManagementPlugins(gm.getNetherWorld(), gm.getWorldSettings().isNetherIslands(), reg); } if (gm.getWorldSettings().isEndGenerate()) { - registerToWorldManagementPlugins(gm.getEndWorld(), gm.getWorldSettings().isEndIslands()); + registerToWorldManagementPlugins(gm.getEndWorld(), gm.getWorldSettings().isEndIslands(), reg); } }); } - /** - * Registers a world with world management plugins - * - * @param world the World to register - * @param islandWorld true if this is an island world - */ - private void registerToWorldManagementPlugins(@NonNull World world, boolean islandWorld) { - if (plugin.getHooks() != null) { - for (Hook hook : plugin.getHooks().getHooks()) { - if (hook instanceof final WorldManagementHook worldManagementHook) { - if (Bukkit.isPrimaryThread()) { - worldManagementHook.registerWorld(world, islandWorld); - } else { - Bukkit.getScheduler().runTask(plugin, () -> worldManagementHook.registerWorld(world, islandWorld)); - } + + private void registerToWorldManagementPlugins(@NonNull World world, boolean islandWorld, boolean reg) { + if (plugin.getHooks() == null) { + return; + } + for (Hook hook : plugin.getHooks().getHooks()) { + if (hook instanceof final WorldManagementHook worldManagementHook) { + if (Bukkit.isPrimaryThread()) { + runTask(worldManagementHook, world, islandWorld, reg); + } else { + Bukkit.getScheduler().runTask(plugin, () -> runTask(worldManagementHook, world, islandWorld, reg)); } } } + + } + + private void runTask(WorldManagementHook worldManagementHook, @NonNull World world, boolean islandWorld, boolean reg) { + if (reg) { + worldManagementHook.registerWorld(world, islandWorld); + } else { + worldManagementHook.unregisterWorld(world); + } } /** @@ -169,27 +181,27 @@ public class IslandWorldManager { // Add worlds to map gameModes.put(world, gameMode); // Call Multiverse - registerToWorldManagementPlugins(world, true); + registerToWorldManagementPlugins(world, true, true); if (settings.isNetherGenerate()) { gameModes.put(gameMode.getNetherWorld(), gameMode); if (settings.isNetherIslands()) { - registerToWorldManagementPlugins(gameMode.getNetherWorld(), true); + registerToWorldManagementPlugins(gameMode.getNetherWorld(), true, true); } } if (settings.isEndGenerate()) { gameModes.put(gameMode.getEndWorld(), gameMode); if (settings.isEndIslands()) { - registerToWorldManagementPlugins(gameMode.getEndWorld(), true); + registerToWorldManagementPlugins(gameMode.getEndWorld(), true, true); } } // Set default island settings plugin.getFlagsManager().getFlags().stream(). - filter(f -> f.getType().equals(Flag.Type.PROTECTION)). - forEach(f -> settings.getDefaultIslandFlagNames().putIfAbsent(f.getID(), f.getDefaultRank())); + filter(f -> f.getType().equals(Flag.Type.PROTECTION)). + forEach(f -> settings.getDefaultIslandFlagNames().putIfAbsent(f.getID(), f.getDefaultRank())); plugin.getFlagsManager().getFlags().stream(). - filter(f -> f.getType().equals(Flag.Type.SETTING)). - forEach(f -> settings.getDefaultIslandSettingNames().putIfAbsent(f.getID(), f.getDefaultRank())); + filter(f -> f.getType().equals(Flag.Type.SETTING)). + forEach(f -> settings.getDefaultIslandSettingNames().putIfAbsent(f.getID(), f.getDefaultRank())); Bukkit.getScheduler().runTask(plugin, () -> { // Set world difficulty @@ -494,8 +506,8 @@ public class IslandWorldManager { */ public String getFriendlyName(@NonNull World world) { return gameModes.containsKey(world) ? - gameModes.get(world).getWorldSettings().getFriendlyName() : - world.getName(); + gameModes.get(world).getWorldSettings().getFriendlyName() : + world.getName(); } /** @@ -720,8 +732,8 @@ public class IslandWorldManager { public Map getDefaultIslandFlags(@NonNull World world) { return this.gameModes.containsKey(world) ? - this.convertToFlags(this.gameModes.get(world).getWorldSettings().getDefaultIslandFlagNames()) : - Collections.emptyMap(); + this.convertToFlags(this.gameModes.get(world).getWorldSettings().getDefaultIslandFlagNames()) : + Collections.emptyMap(); } /** @@ -742,8 +754,8 @@ public class IslandWorldManager { public Map getDefaultIslandSettings(@NonNull World world) { return this.gameModes.containsKey(world) ? - this.convertToFlags(this.gameModes.get(world).getWorldSettings().getDefaultIslandSettingNames()) : - Collections.emptyMap(); + this.convertToFlags(this.gameModes.get(world).getWorldSettings().getDefaultIslandSettingNames()) : + Collections.emptyMap(); } public boolean isUseOwnGenerator(@NonNull World world) { @@ -955,7 +967,7 @@ public class IslandWorldManager { { Map flagMap = new HashMap<>(); flagNamesMap.forEach((key, value) -> - this.plugin.getFlagsManager().getFlag(key).ifPresent(flag -> flagMap.put(flag, value))); + this.plugin.getFlagsManager().getFlag(key).ifPresent(flag -> flagMap.put(flag, value))); return flagMap; } } diff --git a/src/test/java/world/bentobox/bentobox/managers/IslandWorldManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/IslandWorldManagerTest.java index c846a168d..00474ac58 100644 --- a/src/test/java/world/bentobox/bentobox/managers/IslandWorldManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/IslandWorldManagerTest.java @@ -114,7 +114,7 @@ public class IslandWorldManagerTest { */ @Test public void testRegisterWorldsToMultiverse() { - iwm.registerWorldsToMultiverse(); + iwm.registerWorldsToMultiverse(true); } /**