Loop over inventory deleting items

Discussion in 'Plugin Development' started by UltraMC, Apr 28, 2014.

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

    UltraMC

    I loop over inventory with
    Code:java
    1. for (ItemStack i : player.getInventory()) {}


    And want to set currently iterated over item an AIR like
    Code:java
    1. player.setItemInHand(new ItemStack(Material.AIR, 0));


    How to do it right?
     
  2. Offline

    xTigerRebornx

  3. Offline

    UltraMC

    No, as I loop over item's and want to delete all but Maps nad Fishing rods
     
  4. Offline

    Onlineids

    Code:java
    1. for(ItemStack i : e.getPlayer().getInventory().getContents()){
    2. if(!(i.getType().equals(Material.FISHING_ROD)) && !(i.getType().equals(Material.MAP))){
    3. i.setType(Material.AIR);
    4. }
    5. }
     
  5. Offline

    UltraMC

    And how you would include here condition like this (if tool is damaged):

    Code:java
    1. if (itemDUR > 0 && (itemID == 359 || /* Shears */
    2. itemID == 346 || /* Fishing Rod */
    3. itemID == 261 || /* Bow */
    4. itemID == 259 || /* Flint and Steel */
    5. (itemID >= 256 && itemID <= 258) || /* Tools: iron */
    6. (itemID >= 267 && itemID <= 279) || /*
    7. * Swords & Tools: wood,
    8. * stone, diamond
    9. */
    10. (itemID >= 283 && itemID <= 286) || /* Sword & Tools: gold */
    11. (itemID >= 290 && itemID <= 294) || /* Hoes */
    12. (itemID >= 298 && itemID <= 317) /* Armor */
    13. ))


    PS And no, I am not treating you as a free code writer, thank you for posted solution, checking it right now.

    This works:
    Code:java
    1. i.setType(Material.STONE);


    But this not (leaves item as it is):
    Code:java
    1. i.setType(Material.AIR);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page