Share objects between plugins?

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

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

    Staartvin

    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:
    Code:
    System.out.print(getServer().getPluginManager("PluginName").getClass().getMethod("MethodName"));
    // This should return a string 'text' with a value: "Hello, can you read me?"
    Thanks in advance,
    Staartvin
     
  2. Offline

    fireblast709

    Code:java
    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

    Staartvin

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

    fireblast709

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

    Armadillo

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

    Staartvin

    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

    fireblast709

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

    Staartvin

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

    Code:

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

    fireblast709

    then you would do:
    Code:java
    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

    Staartvin

    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

    russjr08

    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

    bergerkiller

    Staartvin
    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