Solved How to make a plugin able to access another plugin's methods?

Discussion in 'Plugin Development' started by NepsName, Feb 14, 2015.

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

    NepsName

    Hi everyone!

    My question is simple, and I have tried this in so many ways that I just gave up on trying to do it myself and now I resort to the forums.

    I have a main plugin, that has a lot of classes and methods that are the structure of the server, but now I want to make more plugins, that will need to access these methods and classes, how can I do that?
     
  2. Offline

    mrCookieSlime Retired Staff

    Moved to Plugin Developement.
     
  3. Offline

    aaomidi

    You add it as a dependency and basically just call the classes/methods written in that dependency.

    However if you're using private fields with no getters, you're not going to be able to access them.
     
    Darkpicasa likes this.
  4. Offline

    Darkpicasa

    For example:
    in your main plugin:
    public static MainClassName getInstance() {
    return this.plugin;
    }
    public static MainClassName plugin;
    public void onEnable() {
    plugin = this;
    }


    Other plugin:
    *Add main plugin to build path*
    import bla.bla.bla.MainClassName;
    MainClassName instance = MainClassName.getInstance();
     
  5. Offline

    NepsName

    Really glad someone has been able to answer me since I have really been struggling with this, but it didn't work.

    First, doing:

    Code:
    // The main class name is: Test
    
    public static Test getInstance() {
         return this.plugin;
    }
    gives "Cannot use this in a static context"

    Then I tried switching "this" with "Test", and the error went away.

    But when I went to the other plugin and did:

    Code:
    Test instance = Test.getInstance(); 
    it gave "The method getInstance() is undefined for the type Test"


    What now? I did the importing and basically everything you said...
     
  6. Offline

    teej107

    @NepsName
    Code:
    Bukkit#getPluginManager()#getPlugin("somePlugin");
    And then cast the Plugin to your Plugin class.

    EDIT: I would post a link to the JavaDocs but they seem to be down.
     
  7. Offline

    mythbusterma

    @NepsName

    Do what Teej said instead, dynamic resolution allows for smoother error handling than making your main class a singleton (as this will just return null if the plugin is not found).

    Although, that really shouldn't be an issue, as the server won't load your plugin if dependencies aren't satisfied.
     
  8. Offline

    NepsName

    @teej107

    Just this by itself should work?

    And can you give me a code example of how I am supposed to do that casting? Not sure how to do that.
     
    Last edited: Feb 14, 2015
  9. Offline

    Ambamore2000

    PluginClass pluginApi = (PluginClass) Bukkit.getPluginManager().getPlugin("Plugin");

    This is how it'd look for WorldGuard:

    WorldGuardPlugin worldGuard = (WorldGuardPlugin) Bukkit.getPluginManager().getPlugin("WorldGuard");
     
  10. Offline

    NepsName

    Couldn't have gone better, thanks to everyone for helping, the problem has been fixed! :D
     
Thread Status:
Not open for further replies.

Share This Page