Inter-plugin Communication: Complete.

Discussion in 'Plugin Development' started by Dinnerbone, Jan 11, 2011.

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

    Dinnerbone Bukkit Team Member

    Plugins can now directly call other plugins. Finally! :D

    What's better, is that there is literally no special code to do so. You just call the plugin. Nice and easy. Here's a nice example!

    Code:
    public void doSomethingFancy(Server server, Player player) {
        Plugin tester = server.getPluginManager().getPlugin("Super Plugin");
        if (tester == null) {
            player.sendMessage("You don't have Super Plugin, I can't do any fancy stuff with it!");
        } else {
            try {
                SuperPlugin plugin = (SuperPlugin)tester;
                if (plugin.canDoSomethingFancy(player)) {
                    plugin.doSomethingReallyFancy(player);
                }
            } catch (ClassCastException ex) {
                player.sendMessage("There's a plugin disguised as Super Plugin! It's not the one I was expecting!");
            }
        }
    }
    
    Note that if a plugin is updated at runtime, your own plugin may need to be reloaded too or it'll use old code. We'll probably do this automatically when we get to plugin dependancies.
     
  2. Offline

    Firestar

    way to go team :D, keep up the great work.
     
  3. Offline

    Afforess

    Sweet. Dependencies that lock a plugin from launching if the required plugins (possibly specified in the plugin.yml) would be sweeter.
     
  4. Offline

    Dinnerbone Bukkit Team Member

    Yep, in the plan.
     
  5. Offline

    Raphfrk

    So you have to add an interface for the other plugin to your java path?

    Is SuperPlugin a bukkit class or the name of the hypothetical other plugin :) ?

    How does Java know that the methods are valid?
     
  6. Offline

    TotempaaltJ

    Whaaat? No, you can just call the other plugin's functions from your plugin by including it using a Bukkit function.

    Hypothetical other plugin.

    What do you mean exactly?
     
  7. Offline

    Raphfrk

    Well if I do something like

    SuperPlugin plugin = (SuperPlugin)tester;

    then the compiler needs to know about the SuperPlugin interface, right?

    In the above example, I would need to add an additional file to my source:

    Code:
    public interface SuperPlugin {
    
        Boolean canDoSomethingFancy( Player player );
    
    }
    
    ... or am I over thinking it? :) (also, I should probably just create a project and test it :) ).
     
  8. Offline

    Dinnerbone Bukkit Team Member

    You add the other plugin as a project reference in your favourite IDE, like you do for Bukkit or any other library you may be using.
     
  9. Offline

    TotempaaltJ

    The guy-with-way-more-Java-experience-than-me has spoken (Dinnerbone), listen to him.
     
  10. Offline

    Raphfrk

    Right ofc :), guess I was right ... about over thinking it.
     
  11. Offline

    feverdream

    Very nice, this just makes it even more valuable that people release sources and get github accounts.
     
  12. Offline

    Shados

    As suggested, I'm reposting this from here:

     
  13. Great! First I justed linked the packages, way easier now :)
     
  14. Offline

    LRFLEW

    sticky? This really needs to be sticky'd.
     
  15. Offline

    Raphfrk

    Is it possible to use classes from other plugins with this?

    I want to extend a class that is defined in a another plugin. It is giving me a class not defined error. I assume that is because the base class is being loaded by the loader after the class that is trying to do the extending.
     
  16. Offline

    mindless728

    if you add the class files to the jar it should be fine
     
  17. Offline

    Raphfrk

    I have added a pull request for this.

    This adds a new field to the yml file called "depend". With this you can list .jar files that you want included by the class loader.
    --- merged: Feb 26, 2011 10:46 PM ---
    However, that means that it wouldn't update when the other plugin updates, you would be locked to the old classes?
     
Thread Status:
Not open for further replies.

Share This Page