Iterate over inventory slots?

Discussion in 'Plugin Development' started by daviga404, Apr 8, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    daviga404

    Hi,
    Is it possible to iterate over the inventory slots (not just the contents of the inventory as an ItemStack[] as in Inventory#getContents()). I have already tried a for loop starting from 0 and looping until it hits 44 (taken from here) but I get an out of bounds exception when it gets to 4. From looking at the JavaDocs, I can't see an obvious method to loop over every item slot while maintaining the slot id as a variable. Does anyone know of a way to achieve this?
     
  2. Offline

    LucasEmanuel

    Have you tried
    Code:
    for(int i = 0 ; i < inventory.getSize() ; i++) {
        ItemStack item = inventory.getItem(i);
    }
     
  3. Offline

    daviga404

    Thanks, this seems to work, but it doesn't include the armor slots. Do you happen to have any idea of the slot numbers of the armor?
     
  4. Offline

    cnaude

    You can do this:

    Code:
    ItemStack[] invArmour  = player.getInventory().getArmorContents();
     
    for (int i = 0; i < invArmour.length; i++) {
     
        // do stuff
     
    }
     
Thread Status:
Not open for further replies.

Share This Page