Checking how much of an item a player has

Discussion in 'Plugin Development' started by TheAnswerIsMinecraft, Jan 31, 2014.

Thread Status:
Not open for further replies.
  1. I am coding a plugin and I want to check how many of a certain item a player has on death, and how much they have in their hotbar. Thanks!
     
  2. Offline

    ColaCraft

    Code:java
    1. public void onDeath(PlayerDeathEvent e){
    2.  
    3. if(inventory.contains(Material.YOUR_ITEM){
    4.  
    5. doStuff();
    6.  
    7. }
    8.  
    9. }


    Is one way to do it.

    To check if it is in their hot bar specifically? Maybe create a runnable that checks inventory slots 0-8 (0 being the first slot, 8 being the last)
     
  3. Thanks though I am trying to see how many of this item the player has. Thanks!
     
  4. Offline

    xTigerRebornx

    TheAnswerIsMinecraft Could loop through the inventory using a for loop, and then as you come across an ItemStack that has the proper Material, add its size to an int (or other Number) and then after, you'll have the total
     
    Jumla likes this.
  5. how would I add it to an int?
     
  6. Offline

    Bloxcraft


    Code:java
    1. int count = 0;
    2.  
    3. for (ItemStack stack : player.getInventory().getContents()) {
    4. if (stack != null && stack.getType() == /*YOUR MATERIAL HERE*/) {
    5. count += stack.getSize();
    6. }
    7. }
     
Thread Status:
Not open for further replies.

Share This Page