Communication Between Plugins

Discussion in 'Plugin Development' started by AndrewM16921, Oct 20, 2011.

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

    AndrewM16921

    I am working on a plugin that all of my other plugins can use to help manage a particular feature. I want these other plugins to be able to query this plugin, and receive a response. I don't really know how to go about doing this, or what the best way would be. I have two vague ideas.

    1. Create a 'Sender' type entity for each plugin, and have them actually send messages to each other in game.
    2. Somehow make a class called "attachment" or something, and each plugin can create an instance of it (so far very doable), but no idea how to send a reference of that instance to the plugin that manages it.

    ...ideas?
     
  2. Offline

    Jogy34

    I think that when someone makes a plugin thats an addon of another plugin like the addons to citizens they import the citizens package. You could try doing that to transfer information back and forth between your plugins.
     
  3. Offline

    AndrewM16921

    Yeah, it can import it, easy enough. But how can I send information back and forth? If I make an instance of something in package A, how does something in package B send it information or even know it exists?

    Is there some kind of getPluginInstance method in bukkit? :/

    EDIT:

    Oh, derp... there's a plugin manager. There must be something in there worth looking at. *goes to read*

    Okay, I came up with something like this as a general idea. Does it look stupid, or might it just work out quite well?

    Code:java
    1.  
    2. package nubcraft.util.plugins;
    3.  
    4. import java.util.HashMap;
    5. import org.bukkit.plugin.PluginManager;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. @SuppressWarnings("unused")
    9. public abstract class PluginMessager
    10. {
    11. private PluginManager pm;
    12. private JavaPlugin plugin;
    13. private HashMap<String, PluginMessager> plugins;
    14. public PluginMessager(PluginManager pm, JavaPlugin instance)
    15. {
    16. this.pm = pm;
    17. plugin = instance;
    18. plugins = new HashMap<String, PluginMessager>();
    19. }
    20. public void register(String name, PluginMessager plugin)
    21. {
    22. plugins.put(name, plugin);
    23. }
    24. public void unregister(String name)
    25. {
    26. plugins.remove(name);
    27. }
    28. public void broadcast(String msg)
    29. {
    30. for(String name : plugins.keySet())
    31. {
    32. plugins.get(name).msg(msg);
    33. }
    34. }
    35. public void send(PluginMessager pm, String msg)
    36. {
    37. plugins.get(pm).msg(msg);
    38. }
    39. public void send(PluginMessager pm, String msg, boolean b)
    40. {
    41. plugins.get(pm).msg(msg, b);
    42. }
    43. public void send(PluginMessager pm, int code)
    44. {
    45. plugins.get(pm).msg(code);
    46. }
    47. public void send(PluginMessager pm, int code, boolean b)
    48. {
    49. plugins.get(pm).msg(code, b);
    50. }
    51. //Subclass must implement the msg methods to do something based on the message
    52. public abstract boolean msg(String msg);
    53. public abstract boolean msg(String msg, boolean b);
    54. public abstract boolean msg(int code);
    55. public abstract boolean msg(int code, boolean b);
    56. }
    57.  
    58.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  4. Offline

    nisovin

    The PluginManager has a getPlugin() method that will return the instance of a Plugin by its name. You can then use this to directly call another plugin's methods, rather than using some weird messaging system. Alternatively, since each Plugin is essentially a singleton, you could just add a static variable to each Plugin class that stores the instance of the Plugin, and use those to allow access between plugins.
     
Thread Status:
Not open for further replies.

Share This Page