Bukkit API 为什么它不能识别这些方块

Bukkit API why won't it recognize these blocks

我使用的是 Spigot API 1.8.6,我把 bukkit 放在标题中是因为它们几乎完全一样。

我有一个配置选择,它从配置中获取项目 ID 及其值。项目 ID 变成 materials。而铁块和煤块的173、42则被跳过。这是我的:

for(String key : plugin.getConfig().getConfigurationSection("sellall"+ranks).getKeys(false))
{
    int id = Integer.valueOf(key);
    Material material = Material.getMaterial(id);
}

然后我检查玩家的物品栏中的 material,找到每个物品 ID BESIDES 42 和 173 的 materials,铁块和煤块。我的问题是为什么他们会跳过它们,我该如何解决。

这是我尝试过的,因为它们被跳过了我试过这个:

String f = key;
Material mat = Material.getMaterial(f.toUpperCase());
if(mat == Material.IRON_BLOCK||mat == Material.COAL_BLOCK)
{
    // continue with code like the else
}
else
{
    // same code as if they are iron block or coal block
}

然而,这样做是一样的,并且会跳过它们。

注意:我已经尝试了多个版本的 spigot

最后一个问题:为什么 Bukkit/Spigot API 跳过铁块和煤块,但不跳过其他所有东西,我该如何解决?

我认为这里的问题都是关于项目 ID。

查看 Material.getMaterial(int) 的 Javadoc:

Deprecated. Magic value

This post 解释了什么是魔法值:

Magic values are values that do not clearly demonstrate what they represent, eg, an item ID. They have deprecated these as minecraft changes could easily break the ID system, and they wish for people to use the bukkit API Enum equivalents that currently exist. For example, using the Material type instead of block ids.

那你应该用Material.getMaterial(String)代替。

您的第一次尝试没有成功,因为 key 是一个数字。 您必须改为保存枚举常量(使用 Enum.name())。

FileConfiguration config; // ...
ConfigurationSection path = config.getConfigurationSection("sellall" + ranks);

Material key = Material.IRON_BLOCK;
Object value; // ...
path.set(key.name(), value);

for (String key : path.getKeys(false)) {
    Material material = Material.getMaterial(key);
}