Skip to content

Commit 0082cc9

Browse files
committed
Improve generator plugin detection and add an option to disable it
1 parent fa3adbe commit 0082cc9

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,16 @@ public String getServerPropertiesPath() {
633633
return configHandle.get(configNodes.serverPropertiesPath);
634634
}
635635

636+
@ApiStatus.AvailableSince("5.3")
637+
public Try<Void> setAutoDetectGeneratorPlugins(boolean autoDetectGeneratorPlugins) {
638+
return configHandle.set(configNodes.autoDetectGeneratorPlugins, autoDetectGeneratorPlugins);
639+
}
640+
641+
@ApiStatus.AvailableSince("5.3")
642+
public boolean getAutoDetectGeneratorPlugins() {
643+
return configHandle.get(configNodes.autoDetectGeneratorPlugins);
644+
}
645+
636646
/**
637647
* {@inheritDoc}
638648
*/

src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,15 @@ private <N extends Node> N node(N node) {
520520
.name("server-properties-path")
521521
.build());
522522

523+
final ConfigNode<Boolean> autoDetectGeneratorPlugins = node(ConfigNode.builder("misc.auto-detect-generator-plugins", Boolean.class)
524+
.comment("")
525+
.comment("When enabled, Multiverse will attempt to automatically detect world generator plugins installed on your server.")
526+
.comment("This option only affects tab-completion within `/mv create` command and output of `/mv generators` command.")
527+
.comment("Disabling this will not have any affect on the usages of world generator plugins itself.")
528+
.defaultValue(true)
529+
.name("auto-detect-generator-plugins")
530+
.build());
531+
523532
final ConfigNode<Integer> globalDebug = node(ConfigNode.builder("misc.global-debug", Integer.class)
524533
.comment("")
525534
.comment("This is our debug flag to help identify issues with Multiverse.")

src/main/java/org/mvplugins/multiverse/core/world/generators/GeneratorProvider.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.jvnet.hk2.annotations.Service;
2828

2929
import org.mvplugins.multiverse.core.MultiverseCore;
30+
import org.mvplugins.multiverse.core.config.CoreConfig;
3031
import org.mvplugins.multiverse.core.utils.FileUtils;
3132
import org.mvplugins.multiverse.core.utils.REPatterns;
3233

@@ -38,15 +39,17 @@
3839
public final class GeneratorProvider implements Listener {
3940
private final Map<String, String> defaultGenerators;
4041
private final Map<String, GeneratorPlugin> generatorPlugins;
42+
private final CoreConfig coreConfig;
4143
private final FileUtils fileUtils;
4244

4345
@Inject
44-
GeneratorProvider(@NotNull MultiverseCore multiverseCore, @NotNull FileUtils fileUtils) {
46+
GeneratorProvider(@NotNull MultiverseCore plugin, @NotNull CoreConfig coreConfig, @NotNull FileUtils fileUtils) {
47+
this.coreConfig = coreConfig;
4548
this.fileUtils = fileUtils;
4649
defaultGenerators = new HashMap<>();
4750
generatorPlugins = new HashMap<>();
4851

49-
Bukkit.getPluginManager().registerEvents(this, multiverseCore);
52+
Bukkit.getPluginManager().registerEvents(this, plugin);
5053
loadDefaultWorldGenerators();
5154
loadPluginGenerators();
5255
}
@@ -73,11 +76,14 @@ private void loadDefaultWorldGenerators() {
7376
* Find generator plugins from plugins loaded and register them.
7477
*/
7578
private void loadPluginGenerators() {
76-
Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(plugin -> {
79+
if (!coreConfig.getAutoDetectGeneratorPlugins()) {
80+
return;
81+
}
82+
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
7783
if (testIsGeneratorPlugin(plugin)) {
7884
registerGeneratorPlugin(new SimpleGeneratorPlugin(plugin.getName()));
7985
}
80-
});
86+
}
8187
}
8288

8389
/**
@@ -88,14 +94,19 @@ private void loadPluginGenerators() {
8894
*/
8995
private boolean testIsGeneratorPlugin(Plugin plugin) {
9096
String worldName = Bukkit.getWorlds().stream().findFirst().map(World::getName).orElse("world");
91-
return Try.of(() -> plugin.getDefaultWorldGenerator(worldName, "") != null)
92-
.recover(IllegalArgumentException.class, true)
93-
.recover(throwable -> {
94-
Logging.warning("Plugin %s threw an exception when testing if it is a generator plugin!",
95-
plugin.getName());
96-
throwable.printStackTrace();
97-
return false;
98-
}).getOrElse(false);
97+
try {
98+
return plugin.getDefaultWorldGenerator(worldName, null) != null;
99+
} catch (UnsupportedOperationException e) {
100+
// Some plugins throw this if they don't support world generation
101+
return false;
102+
} catch (Throwable t) {
103+
Logging.warning("Plugin %s threw an exception when testing if it is a generator plugin! ",
104+
plugin.getName());
105+
Logging.warning("This is NOT a bug in Multiverse. Do NOT report this to Multiverse support.");
106+
t.printStackTrace();
107+
// Assume it's a generator plugin since it tried to do something, most likely the id is wrong.
108+
return true;
109+
}
99110
}
100111

101112
/**
@@ -206,6 +217,9 @@ public Collection<GeneratorPlugin> getRegisteredGeneratorPlugins() {
206217
*/
207218
@EventHandler
208219
private void onPluginEnable(PluginEnableEvent event) {
220+
if (!coreConfig.getAutoDetectGeneratorPlugins()) {
221+
return;
222+
}
209223
if (!testIsGeneratorPlugin(event.getPlugin())) {
210224
Logging.finest("Plugin %s is not a generator plugin.", event.getPlugin().getName());
211225
return;

src/test/resources/configs/fresh_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ event-priority:
5555
misc:
5656
bukkit-yml-path: bukkit.yml
5757
server-properties-path: server.properties
58+
auto-detect-generator-plugins: true
5859
global-debug: 0
5960
debug-permissions: false
6061
silent-start: false

0 commit comments

Comments
 (0)