diff --git a/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java b/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java index 90d0a1846..70bfb706e 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java @@ -633,6 +633,16 @@ public String getServerPropertiesPath() { return configHandle.get(configNodes.serverPropertiesPath); } + @ApiStatus.AvailableSince("5.3") + public Try setAutoDetectGeneratorPlugins(boolean autoDetectGeneratorPlugins) { + return configHandle.set(configNodes.autoDetectGeneratorPlugins, autoDetectGeneratorPlugins); + } + + @ApiStatus.AvailableSince("5.3") + public boolean getAutoDetectGeneratorPlugins() { + return configHandle.get(configNodes.autoDetectGeneratorPlugins); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java b/src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java index e07d6e979..fb768a219 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java @@ -520,6 +520,15 @@ private N node(N node) { .name("server-properties-path") .build()); + final ConfigNode autoDetectGeneratorPlugins = node(ConfigNode.builder("misc.auto-detect-generator-plugins", Boolean.class) + .comment("") + .comment("When enabled, Multiverse will attempt to automatically detect world generator plugins installed on your server.") + .comment("This option only affects tab-completion within `/mv create` command and output of `/mv generators` command.") + .comment("Disabling this will not have any affect on the usages of world generator plugins itself.") + .defaultValue(true) + .name("auto-detect-generator-plugins") + .build()); + final ConfigNode globalDebug = node(ConfigNode.builder("misc.global-debug", Integer.class) .comment("") .comment("This is our debug flag to help identify issues with Multiverse.") diff --git a/src/main/java/org/mvplugins/multiverse/core/world/generators/GeneratorProvider.java b/src/main/java/org/mvplugins/multiverse/core/world/generators/GeneratorProvider.java index e9ba7c1ee..5de3b146e 100644 --- a/src/main/java/org/mvplugins/multiverse/core/world/generators/GeneratorProvider.java +++ b/src/main/java/org/mvplugins/multiverse/core/world/generators/GeneratorProvider.java @@ -27,6 +27,7 @@ import org.jvnet.hk2.annotations.Service; import org.mvplugins.multiverse.core.MultiverseCore; +import org.mvplugins.multiverse.core.config.CoreConfig; import org.mvplugins.multiverse.core.utils.FileUtils; import org.mvplugins.multiverse.core.utils.REPatterns; @@ -38,15 +39,17 @@ public final class GeneratorProvider implements Listener { private final Map defaultGenerators; private final Map generatorPlugins; + private final CoreConfig coreConfig; private final FileUtils fileUtils; @Inject - GeneratorProvider(@NotNull MultiverseCore multiverseCore, @NotNull FileUtils fileUtils) { + GeneratorProvider(@NotNull MultiverseCore plugin, @NotNull CoreConfig coreConfig, @NotNull FileUtils fileUtils) { + this.coreConfig = coreConfig; this.fileUtils = fileUtils; defaultGenerators = new HashMap<>(); generatorPlugins = new HashMap<>(); - Bukkit.getPluginManager().registerEvents(this, multiverseCore); + Bukkit.getPluginManager().registerEvents(this, plugin); loadDefaultWorldGenerators(); loadPluginGenerators(); } @@ -73,11 +76,14 @@ private void loadDefaultWorldGenerators() { * Find generator plugins from plugins loaded and register them. */ private void loadPluginGenerators() { - Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(plugin -> { + if (!coreConfig.getAutoDetectGeneratorPlugins()) { + return; + } + for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { if (testIsGeneratorPlugin(plugin)) { registerGeneratorPlugin(new SimpleGeneratorPlugin(plugin.getName())); } - }); + } } /** @@ -88,14 +94,19 @@ private void loadPluginGenerators() { */ private boolean testIsGeneratorPlugin(Plugin plugin) { String worldName = Bukkit.getWorlds().stream().findFirst().map(World::getName).orElse("world"); - return Try.of(() -> plugin.getDefaultWorldGenerator(worldName, "") != null) - .recover(IllegalArgumentException.class, true) - .recover(throwable -> { - Logging.warning("Plugin %s threw an exception when testing if it is a generator plugin!", - plugin.getName()); - throwable.printStackTrace(); - return false; - }).getOrElse(false); + try { + return plugin.getDefaultWorldGenerator(worldName, null) != null; + } catch (UnsupportedOperationException e) { + // Some plugins throw this if they don't support world generation + return false; + } catch (Throwable t) { + Logging.warning("Plugin %s threw an exception when testing if it is a generator plugin! ", + plugin.getName()); + Logging.warning("This is NOT a bug in Multiverse. Do NOT report this to Multiverse support."); + t.printStackTrace(); + // Assume it's a generator plugin since it tried to do something, most likely the id is wrong. + return true; + } } /** @@ -206,6 +217,9 @@ public Collection getRegisteredGeneratorPlugins() { */ @EventHandler private void onPluginEnable(PluginEnableEvent event) { + if (!coreConfig.getAutoDetectGeneratorPlugins()) { + return; + } if (!testIsGeneratorPlugin(event.getPlugin())) { Logging.finest("Plugin %s is not a generator plugin.", event.getPlugin().getName()); return; diff --git a/src/test/resources/configs/fresh_config.yml b/src/test/resources/configs/fresh_config.yml index 8e2f56b00..0df83a617 100644 --- a/src/test/resources/configs/fresh_config.yml +++ b/src/test/resources/configs/fresh_config.yml @@ -55,6 +55,7 @@ event-priority: misc: bukkit-yml-path: bukkit.yml server-properties-path: server.properties + auto-detect-generator-plugins: true global-debug: 0 debug-permissions: false silent-start: false