我正在制作 Minecraft 的插件。
public final static ItemStack WHITE_STAINED_GLASS_PANE = (IS_LEGACY ? new ItemStack(Material.valueOf("STAINED_GLASS_PANE"), 1, (short) 0) : new ItemStack(Material.WHITE_STAINED_GLASS_PANE));
我已经适配了版本。但是当我运行插件时,出现错误:
Fatal error trying to convert ***.class
org.bukkit.plugin.AuthorNagException: No legacy enum constant for WHITE_STAINED_GLASS_PANE. Did you forget to define a modern (1.13+) api-version in your plugin.yml?
我的代码可以顺利运行,但我想删除这个错误信息,我应该插入什么代码?
@Elikill58
更新了代码。
我使用 maven 进行编译。
以下是我认为与此问题相关的代码片段:
// the main class snippet
public static ItemStack createItem(String name, String legacy, short color) {
return IS_LEGACY ? new ItemStack(Material.valueOf(legacy), 1, color) : new ItemStack(Material.valueOf(name));
}
public final static ItemStack WHITE_STAINED_GLASS_PANE = createItem("WHITE_STAINED_GLASS_PANE", "STAINED_GLASS_PANE"), (short) 0);
# src/main/resources/plugin.yml
name: name
author: autext
description: A plugin of Minecraft
main: me.autext.abcde.ABCDE
version: ${project.version}
定义IS_LEGACY
:
class VersionString {
private final int major;
private final int minor;
private final int patch;
public VersionString(String version) {
String[] parts = version.split("\\.");
this.major = Integer.parseInt(parts[0]);
this.minor = Integer.parseInt(parts[1]);
this.patch = parts.length > 2 ? Integer.parseInt(parts[2]) : 0;
}
public boolean isLowerThan(String otherVersion) {
VersionString other = new VersionString(otherVersion);
if (this.major < other.major) {
return true;
}
if (this.major > other.major) {
return false;
}
if (this.minor < other.minor) {
return true;
}
if (this.minor > other.minor) {
return false;
}
return this.patch < other.patch;
}
}
private final static Boolean IS_LEGACY = new VersionString(Bukkit.getVersion().replaceAll("[^0-9.]", "")).isLowerThan("1.13");
您犯了一个 Java 问题。当使用常量作为时
X.A
,即使它们未被使用,它们也会被代码解释。这意味着 Java 将尝试获取变量(在您的情况下,获取枚举常量)WHITE_STAINED_GLASS_PANE
。要修复此问题,您可以
Material.valueOf("WHITE_STAINED_GLASS_PANE")
像修复遗留问题一样直接使用。另一种方式是创建一个选择它的实用方法,例如:
然后像这样使用它:
如果您想要拥有多个彩色玻璃板,这将使您更容易。
关于更新的
IS_LEGACY
变量我测试了您的代码,在某个版本上,
Bukkit.getVersion()
返回的内容如下:git-paper-123456 (MC: 1.12)
。我认为您应该检查“*MC: *”值。因为我个人获取的是版本“16181.12.2”,所以得到的是“git-Paper-1618 (MC: 1.12.2)”。您可以使用这个正则表达式:
MC: (\\d+\\.\\d+(\\.\\d+)?)
。使用此代码,它对我来说很好用:
您可以在此处了解有关该
static {}
关键字的更多信息。您还应该更改您的
plugin.yml
。您必须设置api-version: 1.13
才能修复它。这不会破坏 1.12 及更低版本。最终文件如下: