Making an API then referencing it in another plugin?

Discussion in 'Plugin Development' started by AstramG, Mar 30, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    AstramG

    I am currently developing for a server and I need to make my plugins in the form of an API so the other plugins can hook into my plugin. I learned that to make it have API functions, just make the functions public. However, once I create another plugin, how do I reference the functions from the first plugin (The API). I've tried googling it, but couldn't find a good information that I understood, so I came here to ask. Thank you!
     
  2. Offline

    stirante

    Just add to library your api. After that you can get from Bukkit instance of that plugin (Bukkit.getPluginManager().getPlugin("NameOfPluginInPlugin.ymlFile")). After that check for null if plugin isn't installed on server and cast it to your main plugin class.
     
  3. Offline

    AstramG

    stirante How would I cast it, do you have an example?
     
  4. Offline

    Technius

    In the plugins that are hooking into your API, make sure to add a "depend" field. For example,
    Code:
    depend: [YourPlugin]
    
    Now, you can access your API like this.

    Code:
    YourPlugin api = (YourPlugin) Bukkit.getServer().getPluginManager().getPlugin("YourPlugin");
    api.yourMethod();
    Object variable = api.yourvariable;
    
     
  5. Offline

    stirante

    MainClass object = (MainClass) objectReturnedFromMethodIPostedAbove;

    Post above described it better.
     
  6. Offline

    dillyg10

    If you need the plugin at the very beginning, you can do that...
    Easy way
    On one plugin, have a static instance of that plugin,
    then when you ref it from another plugin you call that reffed instance
    FOR EXAMPLE:
    Code:
    public class PluginA{
     PluginA pluginAInstace;
     public void onEnable(){
       pluginAInstance = this;
     } 
    }
     
    //The plugin your referencing it from
    public class PluginB{
     PluginA pluginAInstace;
     public void someMethod(){
       PluginA.pluginAInstance.whatever();
     }
    }
     
  7. Offline

    stirante

    But if api isn't on server you will get ClassNotFoundException. That's why better idea would be getting from bukkit loaded plugin with api name. If this plugin is null api isn't installed and you can disable plugin and inform server admin about it. If it's not null cast to main class of api. It's much safer than your idea.
     
Thread Status:
Not open for further replies.

Share This Page