Reputation: 318
I have a softdepend in my plugin.yml:
softdepend: [Plugin]
I check if the plugin is not in the server using this:
if(getServer().getPluginManager().getPlugin("Plugin") != null) {
// do stuff
}
Even though I am checking if the plugin exists in the plugins folder before running my code, I have imports from this plugin that throw errors when the plugin is not on the server. I am getting a InvalidPluginException
because of the imports I have from the softdepended plugin. I need these imports to run the code I use if the plugin is available in the plugins folder. NOTE: I do not want to disable the plugin if the softdepend plugin is not found.
How can I keep the imports from the softdepended plugin without it throwing this error.
I have tried looking for a similar question like this on the Bukkit Forums and Stack Overflow, I have had no luck.
The error looks like this:
org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: be/maximvdw/featherboard/api/PlaceholderAPI$PlaceholderRequestEventHandler at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] at org.bukkit.craftbukkit.v1_8_R2.CraftServer.loadPlugins(CraftServer.java:291) [craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] at net.minecraft.server.v1_8_R2.DedicatedServer.init(DedicatedServer.java:199) [craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:522) [craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] at java.lang.Thread.run(Unknown Source) [?:1.8.0_25] Caused by: java.lang.NoClassDefFoundError: be/maximvdw/featherboard/api/PlaceholderAPI$PlaceholderRequestEventHandler at (java:21) ~[?:?] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_25] at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_25] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_25] at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_25] at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_25] at org.bukkit.plugin.java.PluginClassLoader.(PluginClassLoader.java:76) ~[craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] ... 6 more Caused by: java.lang.ClassNotFoundException: be.maximvdw.featherboard.api.PlaceholderAPI$PlaceholderRequestEventHandler at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_25] at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_25] at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_25] at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_25] at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_25] at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_25] at (java:21) ~[?:?] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_25] at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_25] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_25] at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_25] at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_25] at org.bukkit.plugin.java.PluginClassLoader.(PluginClassLoader.java:76) ~[craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[craftbukkit.jar:git-Spigot-26dfd01-5cb9b70] ... 6 more [15:44:32]
Upvotes: 0
Views: 2006
Reputation: 111
Instead of enabling the plugin when the dependency is there, disable the plugin if it isn't there.
if (getServer().getPluginManager().getPlugin("Plugin") != null) {
// error message, whatever else you need here
getServer().getPluginManager().disablePlugin(this);
}
EDIT: I just realized that this might not be what you're trying to do. If you're trying to use code from a certain plugin that might not be on the server, for example in a listener, make 2 listener classes: one containing code that uses that plugin, and another that contains no code from that plugin. Then when you register the listeners, do something like this:
if (getServer().getPluginManager().getPlugin("Plugin") != null) {
getServer().getPluginManager().registerEvents(new ListenerWithDependencyCode(), this);
} else {
getServer().getPluginManager().registerEvents(new ListenerWithoutDependencyCode(), this);
}
Upvotes: 1