Solved Call methods from another plugin llike Bukkit's onEnable() method.

Discussion in 'Plugin Development' started by electro4fun, Mar 12, 2014.

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

    electro4fun

    I am wondering how Bukkit calls plugin's onEnable() methods without importing that plugin into the build path. I am trying to create an API that requires plugins using it to 'register' then the API calls methods from the registered plugins.
    Right now I am trying something with the Plugin object but it does not work:
    Code:java
    1. plugin.getClass().getField("info")

    I can get it to work if I cast it to the plugin I am trying to access but because I will not know what the plugins names will be I cannot cast.
    Code:java
    1. MyPlugin api = (MyPlugin)plugin;
    2. //Will not work for me :(

    After trying those I was looking into 'reflection', but I am not an expert at Java so I was wondering if there is an easier way to do this, or if someone could point me in the right direction.
     
  2. Offline

    adam753

    onEnable (and onDisable and onCommand) are overridden from the JavaPlugin class. Just cast it to a JavaPlugin and call onEnable().
     
  3. Offline

    Zach_1919

    electro4fun Well that is what the plugin.yml is for. The plugin.yml contains a field called "name" which has the name of the plugin as its value. When Bukkit loads, it loops through all of the jar files in the plugins folder (using the file.listFiles() method) and looks at the value for its name in the plugin.yml. It then uses that list of names to call methods from individual plugins. Using the way I just talked about, you could end up with a list of strings that represents the names of all of the plugins. You would then do something like this:
    Code:
    for(String s : pluginNames)
    {
      getServer().getPlugin(s).onEnable();
    }
    With that, you could run the onEnable method of each plugin.

    I haven't tested this. In fact, I just kind of made it up based on my knowledge, but it sounds about right...
     
Thread Status:
Not open for further replies.

Share This Page