'class cannot be cast to the same class Spigot custom API

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?



Solution 1:[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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Levi Heßmann