2727import org .jvnet .hk2 .annotations .Service ;
2828
2929import org .mvplugins .multiverse .core .MultiverseCore ;
30+ import org .mvplugins .multiverse .core .config .CoreConfig ;
3031import org .mvplugins .multiverse .core .utils .FileUtils ;
3132import org .mvplugins .multiverse .core .utils .REPatterns ;
3233
3839public 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 ;
0 commit comments