Solved Access methods in different plugin

Discussion in 'Plugin Development' started by Mysticate, Mar 20, 2014.

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

    Mysticate

    I have a custom currency plugin I created, and it has give-money, take money, etc. methods in it. Now, I need to access those methods in another plugin. I know this is possible, but I don't know how. Thanks!
     
  2. Offline

    GameplayJDK

    Mysticate
    If you have the .jar, than use it as library and add it to your build path, just like you did with the craftbukkit.jar
    If you don't have it, you can use java reflection
     
  3. Offline

    Mysticate

    GameplayJDK Yes I know that! I'm not a total noob :p But how would I access those methods?!
     
  4. Offline

    Garris0n

    Get the plugin with PluginManager.getPlugin() and then cast it to whatever the main class of the plugin is.
     
  5. Offline

    Mysticate

    garrison ok, but how would I do that? Can you post some code?
     
  6. Offline

    GameplayJDK

    Mysticate
    You'd import the classes you need and use them
     
  7. Offline

    Garris0n

  8. Offline

    Mysticate

    Garrison :eek:
     
  9. Offline

    GameplayJDK

  10. Offline

    Mysticate

    GameplayJDK I understand how to get a plugin, but how do you cast it to the main class?
     
  11. Offline

    GameplayJDK

    Mysticate
    To call a method you have to use reflection since you do not know if there is a method like the one you are looking for in the plugins main..
    But I prefer using it as library.
    It's easier to call a method like that: de.GameplayJDK.AnyPlugin.PluginMain.theMagicMethod("Hello");
     
  12. Offline

    TryB4

  13. Offline

    DrEinsteinium

    Mysticate
    Code:
     PluginYouWant plugin = (PluginYouWant)PluginManager.getPlugin("PluginYouWantName");
    This is a method in Java called typecasting and it's very useful when working with inheritance. Since the plugin class you are trying to get from the other plugin extends JavaPlugin (or whatever), you are able to typecast .getPlugin(), which typically returns an instance of JavaPlugin, to something that extends JavaPlugin, and in this case, PluginYouWant.class.

    There's a lot of stuff about typecasting on google, but here is something useful
    http://javarevisited.blogspot.com/2012/12/what-is-type-casting-in-java-class-interface-example.html
     
  14. Offline

    Mysticate

    DrEinsteinium But how do I access a method in another class?
     
  15. Offline

    GameplayJDK

    Mysticate
    If it's private you can only access it with reflection (I'm not familiar with reflection..). If it's public you can access it by instantiating the class and then call it "new Theclass.methodYouWant()". If it's public static, just call it "Theclass.methodYouWant()"
     
  16. Offline

    Mysticate

    GameplayJDK
    Sure bit can you post code? I don't really understand where to put what you said or how to use it
     
  17. Offline

    GameplayJDK

    Mysticate
    Code:java
    1. public class MyStuff {
    2.  
    3. // in case you have this:
    4. private void doStuff() {
    5. }
    6. // you can only access doStuff() from within the same class
    7.  
    8. // in case you have this:
    9. public void doStuff() {
    10. }
    11. //you can only access doStuff() like that:
    12. new MyStuff().doStuff();
    13.  
    14. //in case you have this:
    15. public static void doStuff() {
    16. }
    17. // You can access it from eveywhere:
    18. MyStuff.doStuff();
    19.  
    20. }
     
  18. Offline

    Mysticate

    GameplayJDK but can you do that from any plugin, given that you do the get plugin thing from above? And what happens if the plugin you're accessing it from and the plugin it's in have the same name?
     
  19. Offline

    GameplayJDK

    Mysticate
    You can do it limited I think. But in this case I'd use the other plugin as library. It's much easier then to use methods.
     
  20. Offline

    Mysticate

    My methods are to take money, give it, check it, etc. how would I do that in a library GameplayJDK
     
  21. Offline

    GameplayJDK

    Mysticate
    1) In the plugin the methods to give/get/remove/... money are, make sure they are all public (or public static)
    2) Export the plugin
    3) Add it to the build path of the other one (like you did with the craftbukkit.jar)
    4) Now you should be able to access the methods like shown above
     
  22. Offline

    Mysticate

    GameplayJDK so I'd do the import plugin thing that someone mentioned before, then use your methods above?
     
  23. Offline

    GameplayJDK

    Mysticate
    Not exactly the same methods. They are samples for your methods in the other plugin. Samples for the methods you want to call.
    You will call them like the methods from bukkit.
     
  24. Offline

    Mysticate

    Code:java
    1. myexampleplugin plugin = (myexampleplugin)PluginManager.getPlugin("myesampleplugin");
    2. new Money().TakeMoney();
    3.  
    4. ^^ Is that how i'd do it from another plugin if the method takemoney was in Money?
    5.  


    GameplayJDK

    I don't get it; it underlines the whole exampleplugin plugin thing... and when I do the new Money() thing, it just looks for a class inside the plugin i'm accessing it from.

    Anyone?

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

    GameplayJDK

    Mysticate
    Have you imported the class Money?
     
  26. Offline

    Barinade

    ExternalPlugin expl;
    public void onEnable() {
    expl = (ExternalPlugin) getPluginManager().getPlugin("ExternalPluginsName");
    }
     
  27. Offline

    DrEinsteinium

    Barinade GameplayJDK Mysticate You should have a reference to the instance of Money in your main plugin class of your library. Try using code like this:

    LibraryPlugin class (The main class of your library)
    Code:
    private Money money;
     
    @Override
    public void onEnable()
    {
    money = new Money();
    }
     
    public Money getMoneyInstance()
    {
    return this.money;
    }
    
    Your external plugin class (The main class of your plugin that is accessing your library)
    Code:
    LibraryPlugin moneyplugin;
    @Override
    public void onEnable()
    {
      // Here is that example of typecasting.
      moneyplugin = (LibraryPlugin)this.getPluginManager().getPlugin("LibraryPluginName");
      // Now you have 'moneyplugin' as a reference to your library's class you can use it to access getMoneyInstace
      moneyplugin.getMoneyInstance().takeMoney();
    }
    
    I hope this clears things up for you.
     
  28. Offline

    Mysticate

    YESSS IT WORKS!!!!!!!!!!!!
     
    DrEinsteinium likes this.
  29. Offline

    DrEinsteinium

Thread Status:
Not open for further replies.

Share This Page