我使用的是 Spigot API 1.8.6,我将 bukkit 放在标题中,因为它们实际上几乎完全相同。
我有一个配置选择,可以从配置中获取项目 ID 及其值。项目 ID 会转化为 Material 。然而,173和42个铁 block 和煤 block 被跳过。这就是我所拥有的:
for(String key : plugin.getConfig().getConfigurationSection("sellall"+ranks).getKeys(false))
{
int id = Integer.valueOf(key);
Material material = Material.getMaterial(id);
}
然后我检查玩家的库存中是否有 Material ,除了 42 和 173、铁 block 和煤 block 之外,每个元素都找到了 Material 。我的问题是他们为什么跳过它们以及我该如何修复它。
这是我尝试过的,因为它们被跳过,所以我尝试了这个:
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 会跳过铁 block 和煤 block ,但不会跳过其他所有内容,我该如何修复它?
最佳答案
我认为这里的问题全部与项目 ID 有关。
查看 Javadoc Material.getMaterial(int)
:
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);
}
关于java - Bukkit API 为什么不能识别这些 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31195846/