class 无法转换为相同的 class Spigot 自定义 API
class cannot be cast to the same class Spigot custom API
我最近为我的 spigot 插件定制了一个 api 来以更简单的方式管理数据库连接。我必须获取插件并将其转换为我导入的 API 的 class。
public GameAPI api;
@Override
public void onEnable() {
this.api = (GameAPI) Bukkit.getPluginManager().getPlugin("GameAPI");
...
插件文件和库是同一个文件,但是我每次都得到这个日志
[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[=14=](MinecraftServer.java:304) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
at java.lang.Thread.run(Thread.java:833) [?:?]
我该如何解决?
你为什么要这样做?您要获取的对象是 this
.
private static GameAPI instance;
@Override
public void onEnable() {
instance = this;
}
public static void getInstance() {
return instance;
}
不需要插件管理器获取。
我最近为我的 spigot 插件定制了一个 api 来以更简单的方式管理数据库连接。我必须获取插件并将其转换为我导入的 API 的 class。
public GameAPI api;
@Override
public void onEnable() {
this.api = (GameAPI) Bukkit.getPluginManager().getPlugin("GameAPI");
...
插件文件和库是同一个文件,但是我每次都得到这个日志
[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[=14=](MinecraftServer.java:304) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
at java.lang.Thread.run(Thread.java:833) [?:?]
我该如何解决?
你为什么要这样做?您要获取的对象是 this
.
private static GameAPI instance;
@Override
public void onEnable() {
instance = this;
}
public static void getInstance() {
return instance;
}
不需要插件管理器获取。