From ac4922534e9a5eeeab756aec070b0db40fee7110 Mon Sep 17 00:00:00 2001 From: Irmo van den Berge Date: Wed, 19 Oct 2022 10:27:12 +0200 Subject: [PATCH] Register BentoBox World Generator option with MyWorlds (#2039) Signed-off-by: Irmo van den Berge --- pom.xml | 12 +++ .../world/bentobox/bentobox/BentoBox.java | 2 + .../bentobox/hooks/MultiverseCoreHook.java | 3 +- .../bentobox/bentobox/hooks/MyWorldsHook.java | 76 +++++++++++++++++++ .../bentobox/hooks/WorldManagementHook.java | 20 +++++ .../bentobox/managers/IslandWorldManager.java | 34 +++++---- src/main/resources/plugin.yml | 2 +- 7 files changed, 132 insertions(+), 17 deletions(-) create mode 100644 src/main/java/world/bentobox/bentobox/hooks/MyWorldsHook.java create mode 100644 src/main/java/world/bentobox/bentobox/hooks/WorldManagementHook.java diff --git a/pom.xml b/pom.xml index 4d7984f9b..e3b302bbe 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,7 @@ 2.10.9 d5f5e0bbd8 3.0-SNAPSHOT + 1.19-v2 ${build.version}-SNAPSHOT @@ -181,6 +182,11 @@ nms-repo https://repo.codemc.io/repository/nms/ + + + MG-Dev Jenkins CI Maven Repository + https://ci.mg-dev.eu/plugin/repository/everything + @@ -266,6 +272,12 @@ ${dynmap.version} provided + + com.bergerkiller.bukkit + MyWorlds + ${myworlds.version} + provided + com.github.TheBusyBiscuit diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 04bfc548d..ba5a5a7ee 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -20,6 +20,7 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.commands.BentoBoxCommand; import world.bentobox.bentobox.database.DatabaseSetup; import world.bentobox.bentobox.hooks.MultiverseCoreHook; +import world.bentobox.bentobox.hooks.MyWorldsHook; import world.bentobox.bentobox.hooks.VaultHook; import world.bentobox.bentobox.hooks.placeholders.PlaceholderAPIHook; import world.bentobox.bentobox.listeners.BannedCommands; @@ -225,6 +226,7 @@ public class BentoBox extends JavaPlugin { // Register Multiverse hook - MV loads AFTER BentoBox // Make sure all worlds are already registered to Multiverse. hooksManager.registerHook(new MultiverseCoreHook()); + hooksManager.registerHook(new MyWorldsHook()); islandWorldManager.registerWorldsToMultiverse(); // TODO: re-enable after implementation diff --git a/src/main/java/world/bentobox/bentobox/hooks/MultiverseCoreHook.java b/src/main/java/world/bentobox/bentobox/hooks/MultiverseCoreHook.java index 91b71c83e..62ba2abe9 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/MultiverseCoreHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/MultiverseCoreHook.java @@ -14,7 +14,7 @@ import world.bentobox.bentobox.api.hooks.Hook; * * @author Poslovitch */ -public class MultiverseCoreHook extends Hook { +public class MultiverseCoreHook extends Hook implements WorldManagementHook { private static final String MULTIVERSE_SET_GENERATOR = "mv modify set generator "; private static final String MULTIVERSE_IMPORT = "mv import "; @@ -28,6 +28,7 @@ public class MultiverseCoreHook extends Hook { * @param world - world to register * @param islandWorld - if true, then this is an island world */ + @Override public void registerWorld(World world, boolean islandWorld) { if (islandWorld) { // Only register generator if one is defined in the addon (is not null) diff --git a/src/main/java/world/bentobox/bentobox/hooks/MyWorldsHook.java b/src/main/java/world/bentobox/bentobox/hooks/MyWorldsHook.java new file mode 100644 index 000000000..ea065947a --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/hooks/MyWorldsHook.java @@ -0,0 +1,76 @@ +package world.bentobox.bentobox.hooks; + +import java.util.logging.Level; + +import org.bukkit.Material; +import org.bukkit.World; + +import com.bergerkiller.bukkit.mw.WorldConfigStore; + +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.hooks.Hook; + +/** + * Provides implementation and interfacing to interact with MyWorlds. + * + * @author bergerkiller (Irmo van den Berge) + */ +public class MyWorldsHook extends Hook implements WorldManagementHook { + + public MyWorldsHook() { + super("My_Worlds", Material.FILLED_MAP); + } + + /** + * Register the world with MyWorlds + * + * @param world - world to register + * @param islandWorld - if true, then this is an island world + */ + @Override + public void registerWorld(World world, boolean islandWorld) { + if (islandWorld) { + // Only register generator if one is defined in the addon (is not null) + boolean hasGenerator = BentoBox.getInstance().getIWM().getAddon(world).map(gm -> gm.getDefaultWorldGenerator(world.getName(), "") != null).orElse(false); + setUseBentoboxGenerator(world, hasGenerator); + } else { + // Set the generator to null - this will remove any previous registration + setUseBentoboxGenerator(world, false); + } + } + + private void setUseBentoboxGenerator(World world, boolean hasGenerator) { + String name = hasGenerator ? BentoBox.getInstance().getName() : null; + + try { + WorldConfigStore.get(world).setChunkGeneratorName(name); + + // Alternative Reflection way to do it, if a MyWorlds dependency isn't available at + // compile time. + /* + // WorldConfigStore -> public static WorldConfig get(World world); + Object worldConfig = Class.forName("com.bergerkiller.bukkit.mw.WorldConfigStore") + .getMethod("get", World.class) + .invoke(null, world); + + // WorldConfig -> public void setChunkGeneratorName(String name); + Class.forName("com.bergerkiller.bukkit.mw.WorldConfig") + .getMethod("setChunkGeneratorName", String.class) + .invoke(worldConfig, name); + */ + } catch (Throwable t) { + BentoBox.getInstance().getLogger().log(Level.SEVERE, + "Failed to register world " + world.getName() + " with MyWorlds", t); + } + } + + @Override + public boolean hook() { + return true; // The hook process shouldn't fail + } + + @Override + public String getFailureCause() { + return null; // 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 new file mode 100644 index 000000000..4f79a96c3 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/hooks/WorldManagementHook.java @@ -0,0 +1,20 @@ +package world.bentobox.bentobox.hooks; + +import org.bukkit.World; + +/** + * Hook for a type of Multi-World management plugin that must be made + * aware of the correct configuration of a BentoBox World. + * + * @author bergerkiller (Irmo van den Berge) + */ +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); +} diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java index 542be4c51..7394805d8 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java @@ -25,7 +25,8 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.flags.Flag; -import world.bentobox.bentobox.hooks.MultiverseCoreHook; +import world.bentobox.bentobox.api.hooks.Hook; +import world.bentobox.bentobox.hooks.WorldManagementHook; import world.bentobox.bentobox.lists.Flags; /** @@ -51,31 +52,34 @@ public class IslandWorldManager { public void registerWorldsToMultiverse() { gameModes.values().stream().distinct().forEach(gm -> { - registerToMultiverse(gm.getOverWorld(), true); + registerToWorldManagementPlugins(gm.getOverWorld(), true); if (gm.getWorldSettings().isNetherGenerate()) { - registerToMultiverse(gm.getNetherWorld(), gm.getWorldSettings().isNetherIslands()); + registerToWorldManagementPlugins(gm.getNetherWorld(), gm.getWorldSettings().isNetherIslands()); } if (gm.getWorldSettings().isEndGenerate()) { - registerToMultiverse(gm.getEndWorld(), gm.getWorldSettings().isEndIslands()); + registerToWorldManagementPlugins(gm.getEndWorld(), gm.getWorldSettings().isEndIslands()); } }); } /** - * Registers a world with Multiverse if Multiverse is available. + * Registers a world with world management plugins * * @param world the World to register * @param islandWorld true if this is an island world */ - private void registerToMultiverse(@NonNull World world, boolean islandWorld) { + private void registerToWorldManagementPlugins(@NonNull World world, boolean islandWorld) { if (plugin.getHooks() != null) { - plugin.getHooks().getHook("Multiverse-Core").ifPresent(hook -> { - if (Bukkit.isPrimaryThread()) { - ((MultiverseCoreHook) hook).registerWorld(world, islandWorld); - } else { - Bukkit.getScheduler().runTask(plugin, () -> ((MultiverseCoreHook) hook).registerWorld(world, islandWorld)); + for (Hook hook : plugin.getHooks().getHooks()) { + if (hook instanceof WorldManagementHook) { + final WorldManagementHook worldManagementHook = (WorldManagementHook) hook; + if (Bukkit.isPrimaryThread()) { + worldManagementHook.registerWorld(world, islandWorld); + } else { + Bukkit.getScheduler().runTask(plugin, () -> worldManagementHook.registerWorld(world, islandWorld)); + } } - }); + } } } @@ -156,17 +160,17 @@ public class IslandWorldManager { // Add worlds to map gameModes.put(world, gameMode); // Call Multiverse - registerToMultiverse(world, true); + registerToWorldManagementPlugins(world, true); if (settings.isNetherGenerate()) { gameModes.put(gameMode.getNetherWorld(), gameMode); if (settings.isNetherIslands()) { - registerToMultiverse(gameMode.getNetherWorld(), true); + registerToWorldManagementPlugins(gameMode.getNetherWorld(), true); } } if (settings.isEndGenerate()) { gameModes.put(gameMode.getEndWorld(), gameMode); if (settings.isEndIslands()) { - registerToMultiverse(gameMode.getEndWorld(), true); + registerToWorldManagementPlugins(gameMode.getEndWorld(), true); } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0089b0313..bc5ef1ccb 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -10,7 +10,7 @@ description: ${project.description} load: STARTUP -loadbefore: [Pladdon, Multiverse-Core, Residence] +loadbefore: [Pladdon, Multiverse-Core, My_Worlds, Residence] softdepend: - Citizens