Solved Creating an API?

Discussion in 'Spigot Plugin Development' started by ChickenSaysBak, Jun 4, 2017.

  1. Surprisingly, Google wasn't much help. I tried numerous searches but I didn't get the right results. Anyways, I am trying to use a method from one of my other plugins into another plugin. I've added the other plugin as an external jar, and the method I am trying to use is public but when I try to use it in the other plugin, it doesn't give an option to import or anything... Is there something that I need to do to make the method useable on other plugins?
     
  2. Is your method static or do have to create a new instance of your class first?
     
  3. Are you in Eclipse? You might need to add the project to the project build path, not the JAR, otherwise it will say the import doesn't exist.
     
  4. Mas

    Mas

    #4 Mas, Jun 4, 2017
    Last edited: Jun 4, 2017
  5. How do you do the instance thing? I've been hearing that a lot.
     
    • Agree Agree x 1
  6. Mas

    Mas

  7. I added
    Bukkit.getPluginManager().getPlugin("cpClothes");
    to the onEnable method and it's still not working. What else needs to be done?
     
  8. At first you need to show us some more of your code, so we may be able to help you.
     
  9. Code (Text):
    package me.ChickenSaysBak.APITest;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
       
        @Override
        public void onEnable() {
           
            Bukkit.getPluginManager().getPlugin("cpClothes");
           
        }
       
        @Override
        public void onDisable() {
           
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (cmd.getName().equalsIgnoreCase("test")) {
               
                test();
               
            }
           
            return true;
           
        }
       
    }
     
  10. What does the method do?
     
  11. How did you expect this to work out?
    This only gets the "other" plugin and does nothing with it.
    Is this method defined by the other plugin? So keep an reference to the other plugin received by #onEnable as private field then use #test by using this field.
    Learn OOP...
     
  12. 1. Idk xD
    2. Yeah, I don't know where that goes..
    3. Yes, that is defined in the other plugin and it just broadcasts a message saying "Test"
    And how do I do this field thing you speak of?
     
  13. Code (Java):
    public class Main extends JavaPlugin {
         private WhateverYourOtherClassIsCalled otherPlugin;
         @Override
         public void onEnable() {
             this.otherPlugin = this.getServer().getPluginManager().getPlugin("cpClothes");
         }
         @Override
         public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
             // you don't need to check cmd#getName, if this command is the only one of your plugin
             this.otherPlugin.test();
             return true;
         }
    }
     
  14. Solved, both plugin's class names were the same xD Thankyou chris (who found this thread and msged me on skype) for that!
     
  15. In case someone else got the same problem, you should post the final solution.
    Same class names can be handled by using the full qualified name of the other plugin when declaring or casting it.