1

I recently made a custom api for my spigot plugin to manage the database connection in an easier way. I had to get the plugin and cast it to the class of my API I imported.

public GameAPI api;

@Override
public void onEnable() {
    this.api = (GameAPI) Bukkit.getPluginManager().getPlugin("GameAPI");
...

The plugin file is the same file as the library, but I get this log every time

[22:18:52] [Server thread/INFO]: [Bedwars] Enabling Bedwars v1.0
[22:18:52] [Server thread/ERROR]: Error occurred while enabling Bedwars v1.0 (Is it up to date?)
java.lang.ClassCastException: class org.example.GameAPI cannot be cast to class org.example.GameAPI (org.example.GameAPI is in unnamed module of loader org.bukkit.plugin.java.PluginClassLoader @b7cef6c; org.example.GameAPI is in unnamed module of loader org.bukkit.plugin.java.PluginClassLoader @228d6e56)
        at org.example.Bedwars.onEnable(Bedwars.java:85) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:342) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.craftbukkit.v1_18_R1.CraftServer.enablePlugin(CraftServer.java:521) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
        at org.bukkit.craftbukkit.v1_18_R1.CraftServer.enablePlugins(CraftServer.java:435) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
        at org.bukkit.craftbukkit.v1_18_R1.CraftServer.reload(CraftServer.java:920) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
        at org.bukkit.Bukkit.reload(Bukkit.java:789) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.craftbukkit.v1_18_R1.CraftServer.dispatchCommand(CraftServer.java:829) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
        at org.bukkit.craftbukkit.v1_18_R1.CraftServer.dispatchServerCommand(CraftServer.java:814) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
        at net.minecraft.server.dedicated.DedicatedServer.bf(DedicatedServer.java:453) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
        at net.minecraft.server.dedicated.DedicatedServer.b(DedicatedServer.java:429) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
        at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:1206) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1034) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
        at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:304) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
        at java.lang.Thread.run(Thread.java:833) [?:?]

How can I fix that?

  • Can you print the class name for Bukkit.getPluginManager().getPlugin("GameAPI");? You can simply type `System.out.println(Bukkit.getPluginManager().getPlugin("GameAPI").getClass().getSimpleName()); ` check if the output is `GameApi` or a subclass of `GameApi` – Demir Apr 22 '22 at 20:50
  • 5
    According to the stack trace, the two classes are loaded by different class loaders. Maybe that gives you a hint...? – Sweeper Apr 22 '22 at 20:52
  • @Demir the output is "GameAPI" – Johannes Becker Apr 23 '22 at 07:29
  • @Sweeper and how can I fix this? – Johannes Becker Apr 23 '22 at 07:30
  • I would suggest you, whenever you try to do downcasting, that is casting the Parent Object to a Child Object given that the Child object actually extends the Parent, use the if(Child instance of Parent) condition to prevent any Exception to be thrown since downcasting is explicit. Here in your code you're forcing the compiler to believe that GameAPI is a superclass of PluginClassLoader. You should verify their relationship – Pawan.Java Apr 23 '22 at 07:51
  • I think the problem is that your plugin *contains* the `.class` file for `GameAPI`. That is probably provided by Bukkit and shouldn't be shipped as part of your plugin: make sure you don't include those classes with your plugin. – Joachim Sauer May 09 '22 at 08:19

1 Answers1

1

Why would you do that like this? The objects you're trying to get is this.

private static GameAPI instance;

@Override
public void onEnable() {
    instance = this;
}

public static void getInstance() {
   return instance;
}

There is no need to get it by the plugin manager.

Levi Heßmann
  • 140
  • 1
  • 6