Share objects between plugins?

Discussion in 'Plugin Development' started by Staartvin, Nov 18, 2012.

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


    I'm trying to make a plugin and a 'library plugin'. The library plugin will save all kind of things needed for other plugins I made. But, I can't seem to find a way to share objects/values between plugins. I thought of making a 'getObject' method in the library plugin. How do I call this method from another plugin?

    I tried:
    // This should return a string 'text' with a value: "Hello, can you read me?"
    Thanks in advance,
  2. Offline


    1. [plugin type] plugin = Bukkit.getPluginManager().getPlugin("plugin name");
    2. if(plugin != null) (and thus loaded, as far as I know)
    3. {
    4. plugin.MethodName(); // call the method
    5. }
  3. Offline


    Hmm, what kind of plugin type do I need to use? PluginBase, Plugin, PluginLoader don't seem to work.
  4. Offline


    The mainclass of the other plugin. Also, you would need to use that plugin as a library
  5. Offline


    Could you use Vault for this???
  6. Offline


    How would I use the plugin as a library? Should I download my plugin, and add it to the project in eclipse?

    What I really want is something like Nolagg and BKCommonLib. BKCommonLib is a plugin which has its own API and gets information about certain things. Nolagg uses this for example. If BKCommonLib is not found, Nolagg won't run.

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


    what is the name of the plugin you want to use in this one?
  8. Offline


    It's my own made plugin. It's called LibraryPlugin it's just for a test.


    1. package Staartvin.LibraryPlugin;
    3. import;
    5. public class LibraryPlugin extends JavaPlugin {
    7. public String text = "Hello, can you read me?";
    9. public void onEnable() {
    10. System.out.print("[SLibrary] SLibrary v" + getDescription().getVersion() + " has been enabled!");
    11. }
    13. public void onDisable() {
    14. System.out.print("[SLibrary] SLibrary v" + getDescription().getVersion() + " has been disabled!");
    15. }
    17. public String getStringText() {
    18. return text;
    19. }
    20. }
  9. Offline


    then you would do:
    1. LibraryPlugin plugin = Bukkit.getPluginManager().getPlugin("LibraryPlugin");
    2. if(plugin != null)
    3. {
    4. System.out.println(plugin.getStringText());
    5. }

    in your other plugin
  10. Offline


    LibraryPlugin is the plugin I want to call a method from.

    I have another plugin 'Let's call it A'. Library plugin is 'B'.

    I want to get a certain String. In this case, the string 'text' in plugin B.
    I want to call from A, the method 'getStringText' which is in plugin B. Without having my plugin as a library.
  11. Offline


    Then that's exactly what you want to do (fireblast709's solution). You want a LibraryPlugin object, and you're using Bukkit's getPlugin method to grab it. Once you have LibraryPlugin's object, you can run any method inside of LibraryPlugin you want.
  12. Offline


    You can approach this in many ways. I choose the easy route, that is that I have all of my methods made static. All of the internal handling/listening I turned into events so no plugin has to obtain an instance of the common library plugin.

    One downside to this is that the Bukkit class loader hates this, and static variable instances tend to get screwy and cause all sorts of glitches. If your library deals with a lot of variable instances, I recommend working with non-static functions instead. Or you can mix them. This excludes non-changing static variables...

    Generally, functions that work on their own (math functions or working with inventories) can be made statically.

    In the implementing plugin it is then just a matter of adding a depend in the plugin.yml, add some versioning to notify the admin when the library or plugin needs updating and importing the classes in your library and using the functions.
Thread Status:
Not open for further replies.

Share This Page