Using structures from another plugin

Discussion in 'Plugin Development' started by Ribesg, Nov 10, 2011.

Thread Status:
Not open for further replies.
  1. Hi.
    I've 2 plugins. One for bans, one general.
    In the bans one, I have a classe who contains the list of banned people/ip etc.
    I want to get this object in the general one.
    How could I do ?

    I already add dependencies, but I don't know how to do in the code.
    Code:java
    1. Plugin bans = getServer().getPluginManager().getPlugin("Itsn3wBan");
    2. Itsn3wBanList banList = bans.banList;


    I would like to do something like this...

    Found it.
    Code:java
    1. public Itsn3wPlayers getKnownPlayers() {
    2. if (getServer().getPluginManager().isPluginEnabled("Itsn3wBan")) {
    3. Itsn3wBan banPlugin = (Itsn3wBan) getServer().getPluginManager().getPlugin("Itsn3wBan");
    4. return banPlugin.players;
    5. } else {
    6. return null;
    7. }
    8. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  2. If you have a hard dependency (which you apparently have in that case), you can also do something easier than that by using static stuff.
    For example, use an extra class as your API and make access to its methods static. When your main plugin is loaded, you set a static field containing the instance of the plugin, which the static methods can work with.

    example snippet:
    Code:java
    1. private static MyPluginClass instance;
    2.  
    3. static init(MyPluginClass instance){
    4. this.instance = instance;
    5. }
    6.  
    7. public static ReturnStuffClassThingy getFoobar(){
    8. return instance.myAwesomeProperty;
    9. }

    In your onLoad/onEnable method of the main plugin, you'd write something like
    Code:
    TheAPIClassname.init(this);
    And at any point of the other plugin, import the API class from the first plugin, and simply write
    Code:
    TheApiClassname.getFoobar()
    Without ever having to mess with loaded plugins and stuff. Keep your dependency declaration in plugin.yml though.
     
Thread Status:
Not open for further replies.

Share This Page