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

763 views Asked by At

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

There are 1 answers

0
Genhis On

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++;
     }
}