How to load id list from config for player Inventory? [Bukkit]

廉价感情. 提交于 2020-01-07 07:56:10

问题


I have this code:

public boolean hasItem(Player player){
    int empty = 0;
    int armors = 0;

    for (String str : config.denyBlocks) {
         for (ItemStack item : player.getInventory().getContents()) {
            if (item == null || item.getType() == Material.getMaterial(str)) empty++;
          }
     }


    for (ItemStack armor : player.getInventory().getArmorContents()) {
            if (armor == null || armor.getType() == Material.AIR) armors++;
    }

    return empty == player.getInventory().getContents().length && armors == player.getInventory().getArmorContents().length;
}

but for some reason it does not work. The tests did not work, if not the first cycle, the test runs.

This code works:

public boolean hasItem(Player player){
    int empty = 0;
    int armors = 0;

         for (ItemStack item : player.getInventory().getContents()) {
            if (item == null || item.getType() == Material.getMaterial(4)) empty++;
          }



    for (ItemStack armor : player.getInventory().getArmorContents()) {
            if (armor == null || armor.getType() == Material.AIR) armors++;
    }

    return empty == player.getInventory().getContents().length && armors == player.getInventory().getArmorContents().length;
}

Where 4 - id block Minecraft. Please help!!


回答1:


In the first case, you are using String for getting Material by ID, but the Bukkit expects Material name. Instead of that you should convert it to Integer. Use Integer.parseInt(string).

for (String str : config.denyBlocks) {
     for (ItemStack item : player.getInventory().getContents()) {
        if (item == null || item.getType() == Material.getMaterial(Integer.parseInt(str))) empty++;
     }
}


来源:https://stackoverflow.com/questions/30965375/how-to-load-id-list-from-config-for-player-inventory-bukkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!