Solved Problem creating my custom API for my plugins

Discussion in 'Spigot Plugin Development' started by JuanDouCore_, Jul 25, 2018.

  1. Look, recently I started making attempts to create my own api to use among my own plugins (own use, not public).
    And what happens is the following:

    Well, first I create the API class in the plugin where it will come from. In the same I also put the constructor to be able to have access to the main:
    Code (Java):
    public class API {

    public main pl;
    public API(main main) { = main;}

    public boolean Check(Player p) {
       if(p.hasPermission("check") {
            return true;
       return false;

    Then I go to the main class and do the following things:
    Code (Java):
    public class Main extends JavaPlugin {

    public API api;

    public void onEnable() {
    api = new API(this);

    After having all this ready. I export the plugin and add it to the buildpath of another plugin. In the other plugin in your main I'll do this:

    Code (Java):
    public class OtherPluginMain extends JavaPlugin{

    public API api = (API) Bukkit.getServer().getPluginManager().getPlugin("PluginAPI");

    public void onEnable() {


    Good. After doing that in the main of that plugin, I create another class in that same plugin and use the api:
    Code (Java):
    public class ClassForUseApi() implements Listener{

    public OhterPluginMain pl;
    public ClassForUseApi(OtherPluginMain pl) { = pl;}

    public void onJoin(PlayerJoinEvent e) {

    if( {
    All these codes are illustrative (that is, an example). But it is as I use it. Now
    what happens is that when I load the plugin that will use that api jumps this error:
    Caused by: java.lang.ClassCastException: com.mediacraftgames.beneficios.home cannot be cast to com.mediacraftgames.beneficios.API
    at com.mediacrafgames.staff.home.<init>( ~[?:?]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_144]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance( ~[?:1.8.0_144]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance( ~[?:1.8.0_144]
    at java.lang.reflect.Constructor.newInstance( ~[?:1.8.0_144]
    at java.lang.Class.newInstance( ~[?:1.8.0_144]
    at<init>( ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]
    at ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]
    ... 13 more
    I do not understand what is the error between all this, I would help
  2. What's on line 40 in com.mediacrafgames.staff.home? Seems like some wrong casting
    #2 iDoomfull, Jul 25, 2018
    Last edited: Jul 25, 2018
  3. That's where the:
    Code (Java):
    public API api = (API) Bukkit.getServer().getPluginManager().getPlugin("MediaCraftBeneficios");
  4. Because Bukkit.getServer().getPluginManager().getPlugin("MediaCraftBeneficios") returns the plugin instance, not the api instance.(Therefore you can't cast it to "API"). Add a getter, which returns the api instance, to your API plugin's main class. Now simply call that getter after you get the plugin instance.

    Code (Java):
    //Check that the plugin instance is not null!
    public API api = Bukkit.getServer().getPluginManager().getPlugin("MediaCraftBeneficios").getApi();
  5. Now that I see it. In those getters that you showed me. getPlugin returns a Plugin object. Not a class
    It means that with that I only have access to the Plugin methods (like getConfig for example). No to the methods that are within the main class
  6. You have to cast it on MediaCraftBeneficios first. Or You can use static access that would do same job. Also you should check/ensure first if your plugin is really installed on your server and it is running. There are several ways to do it like using plugin.yml dependency option.
  7. What RiotSeb means is that in your example above, the getPlugin("PluginAPI") would return the "Main" class that contains the API object. You can only cast the result of getPlugin() to a class that 'extends JavaPlugin' (i.e., your Main class), this doesn't include objects within that class.

    So, in 'OtherPluginMain', you would instead need to do something like
    Code (Java):
    public Main mainPlugin = (Main) Bukkit.getServer().getPluginManager().getPlugin("PluginAPI");
    API api = mainPlugin.api;
    • Agree Agree x 1
    • Optimistic Optimistic x 1
  8. Effectively, this way I can work. Thank you very much for your contribution and help.