Solved Dependencies

Discussion in 'Spigot Plugin Development' started by Spedwards, Jun 1, 2016.

  1. I'm making a series of plugins and one hooks into another, and will only work if that other plugin is enabled. What I want to do is make it so it will work regardless of whether the dependency is enabled or not.

    I know that in the onEnable method, I can check whether it's active or not, but what can I do about the imports? If the plugin isn't there, won't the imports throw errors and stop the plugin, or does it only do that if it runs into the imported methods?
     
  2. If the classes don't exist, it will error when you try to access them.

    If you load a class that uses the imports, I'm pretty sure it will throw a class not found exception.
     
  3. A good answer is that the programmer doesn't know when the JVM is going to try to load an imported class. It usually won't try to load a class until it's needed, but this is certainly not always the case. By defining an import, you are telling the JVM that it can load this class whenever it wants, and it will be there at any time (even during static initialization).

    The safe thing to do is the store all your code that has to access imports that may or may not be there in a separate class, then only create an instance of that class once you know the plugin is loaded.
     
  4. Can you be more specific as to what you are trying to accomplish? Maybe there is a better way to do what you are trying to do.
     
  5. Use
    Code (Text):
    Depend: [PLUGINNAME]
    in your plugin.yml
    Just like @BananaCNetwork already said. ;)
     
  6. You can use this

    Code (Text):
    Depend: [PLUGINNAME]
    Or, in onEnable

    Code (Text):
    try {
      Class.forName("some.class.Name");
    } catch(ClassNotFoundException e) {
      getLogger().log(Level.SEVERE, "Could not found X dependency. Disabling...");
      getServer().getPluginManager().disablePlugin(this);
    }

    //or

    try {
      AMethodThatCanNotExistAtRuntime();
    } catch(NoClassDefFoundError e) {
      getLogger().log(Level.SEVERE, "Could not found X dependency. ");
      // do something
    }

     
    But you plugin can enable before X dependency, so 'some.class.Name' will not exist.
    So, you will need to add softdepend: [X_dependency_name]
     
    #7 DevLeeo, Jun 1, 2016
    Last edited: Jun 1, 2016
  7. I don't recommend doing this, since it will not preload a different plugin only disable yours.
     
  8. What, why it need to preload a "different plugin" ?
     
  9. This sounds like a problem that inversion of control would solve...
     
  10. Dont use the "depend" option in the plugin.yml many people have suggested. Having a depend in their will force your plugin to throw a nasty stacktrace and then disable itself when the dependency fails.

    Instead, you need to use "softdepend" because your dependency is a soft dependency since you want your plugin to still run even without said dependency, but will still try to load the dependency first if it is installed. Just be sure you tuck away ALL the code you use from said dependency plugin into a class that NEVER gets loaded until after you check if the plugin is enabled already. I like to call these classes "Hooks" so I know not to use them unless their dependency is loaded. ;)