Best way to check if player has something in Inv?

Discussion in 'Plugin Development' started by Specops343, Jul 27, 2011.

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

    Specops343

    How would i do this? And also, how might I subtract a material after its used?
     
  2. Offline

    pixelDepth

    Hi,

    Am completely new to writing Bukkit Plugins, and very new to Java, but was looking at doing something similar myself, so thought I would share what I have learned about modifying the inventory. I'll share my code...

    Code:
          if(player != null){
                PlayerInventory inv = player.getInventory();
    
                // Item ID 5 is Wood
                // Check that inventory contains the item, this
                // is so we can prevent unneeded looping
    
                if(inv.contains(5)){
    
                    // Here we loop over all items in the inventory
                    // getSize returns total items in the inventory
    
                    for(int i = 0; i < inv.getSize(); i ++){
    
                        // Here get an item at a specific index and
                        // check it's type id is equal to Wood
    
                        if(inv.getItem(i).getTypeId() == 5){
    
                            // Here it sets all matched items to an
                            // amount of 1, so if you have 2 items (stacks)
                            // of Wood, both will be set to 1
    
                            inv.getItem(i).setAmount(1);
                        }
                    }
                }
            }
    Have added in some comments. I tested it, seems to work as expected, hopefully that will set you on the right track.

    Things to note is that if you had 2 stacks, it will subtract away from both, so you may want to total it up and them remove the amount from the total instead of each stack.

    No idea if it's the best way to do it, so I welcome some advice / tips.

    :)
     
    Specops343 likes this.
  3. Offline

    Specops343

    And how would I total it?
     
  4. Offline

    pixelDepth

Thread Status:
Not open for further replies.

Share This Page