Solved How way to clear a slot in a players inventory

Discussion in 'Plugin Development' started by Scullyking, Jul 2, 2015.

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

    Scullyking

    At the moment I'm iterating through the player's inventory and attempting to clear a particular slot using slot.setAmount(0) and slot.setType(Material.AIR) but this doesn't do anything.

    How should this be done?
     
  2. Offline

    Bram0101

    when iterating, I recommend to do it with a for loop with an int that loops from 0 to the size of the inventory, and then get the itemstack with inv.getItem(int) and then set the item in the inventory with inv.setItem(int, itemstack). And when you're done with the loop use player.updateInventory() to actually update the inventory.
     
    Scullyking likes this.
  3. Offline

    Ruptur

    @Scullyking
    There are plenty of ways to do this

    Did you update the player inventory?
    Code:
    player.getInventory().getItem(index).setType(Material.AIR);
    
    player.updateInventory();
    
    You can use the setItem(index, itemstack) method
    Code:
    player.getInventory().setItem(index, null);
    
    Then there is a no recommended deprecated method #remove(index)
    Code:
    player.getInventory().remove(index);
    
    Leave a like if you feel i helped :)
     
  4. Offline

    Scullyking

    @Bram0101

    Thank you the setItem() method worked great. It must be a bug because I tested setType(Material.WOOD) and it worked fine but setType(Material.AIR) did nothing. Oh well cheers :)
     
  5. Offline

    bwfcwalshy Retired Staff

    @Scullyking If you are removing with setItem use null not Material.AIR
     
Thread Status:
Not open for further replies.

Share This Page