From 4c0cb9f17cbd302df48ee2ca1300dc6d441cff00 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 26 Jun 2023 22:08:49 -0700 Subject: [PATCH 1/2] Unregisters worlds with MV on shutdown. Fixes #2149 --- .../world/bentobox/bentobox/BentoBox.java | 18 ++++- .../bentobox/hooks/MultiverseCoreHook.java | 6 ++ .../bentobox/hooks/WorldManagementHook.java | 9 +++ .../bentobox/managers/IslandWorldManager.java | 78 +++++++++++-------- 4 files changed, 76 insertions(+), 35 deletions(-) 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 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 @@ * 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 @@ private void completeSetup(long loadTime) { // 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 @@ private void registerListeners() { 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 void onDisable() { 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 void registerWorld(World world, boolean islandWorld) { } } + @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 IslandWorldManager(BentoBox plugin) { 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 void addGameMode(@NonNull GameModeAddon gameMode) { // 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 int getMaxHomes(@NonNull World world) { */ 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 Optional getAddon(@Nullable World world) { 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 List getHiddenFlags(@NonNull World world) { 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 @@ private Map convertToFlags(Map flagNamesMap) { 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; } } From e846d26fab227ce2582fd810eb6fcfa5de0deabb Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 28 Jun 2023 21:02:39 -0700 Subject: [PATCH 2/2] Fix test Test doesn't do anything though... --- .../bentobox/bentobox/managers/IslandWorldManagerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 void tearDown() { */ @Test public void testRegisterWorldsToMultiverse() { - iwm.registerWorldsToMultiverse(); + iwm.registerWorldsToMultiverse(true); } /**