Solved Checking if a player has an item

Discussion in 'Plugin Development' started by Hex_27, Jan 3, 2015.

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

    Hex_27

    What I'm using now:
    Code:
    if(p.getInventory().containsAtLeast(new ItemStack(Material.LOG), 5)){
    //codes
    }
    When I use this, it doesn't detect items that are renamed. How can I check if the player has a certain amount of items of a specific type?
     
  2. @Hex_27 One solution would be to iterate over the inventory and check yourself.
     
  3. You need to create a new ItemStack out of the code before checking if the item they have has a name, or specific type.
    Code:
    ItemStack randomItem = new ItemStack(Material.GRASS);
    ItemMeta randomItemMeta = randomItem.getItemMeta();
    randomItemMeta.setDisplayName("Random Item");
    randomItem.setItemMeta(randomItemMeta);
    Then just get their inventory and check if they have that item.
    Code:
    if (player.getInventory().contains(randomItem, 2) {
    // Your code goes here.
    }
     
    Phazerous likes this.
  4. Offline

    Hex_27

    @CodePlaysMinecraft that will work for ONE name for an item. I want it to work of all items.
    @AdamQpzm So... I make an int i = 0 then for each item that is the material i want, i add a number to the integer, then check how much the integer is worth? Would this work? 0.o

    Bump

    would this work?

    Code:
             public boolean hasAmount(Material mat, Inventory inv, int amt){
                
                 int invamt = 0;
                
                     for (ItemStack i : inv) {
                        if(i.getType() == mat){
                            invamt = invamt + i.getAmount();
                        }
                    }
                    
                     if(invamt >= amt){
                         return true;
                     }else{
                return false;
                     }
             }
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 4, 2015
  5. Offline

    teej107

  6. Offline

    Hex_27

    @teej107
    @CodePlaysMinecraft
    @AdamQpzm


    It works... and now i need to make a method to REMOVE a certain material.

    This doesn't work
    Code:
             public void removeItem(Material mat, Inventory inv, int amt){
                
             int amount = amt;
                 for(ItemStack i: inv){
                    
                     if(i != null){
                        if(i.getType() == mat){
                           
                            if(amt > 0){
                            if(i.getAmount() >= amt){
                                i.setAmount(i.getAmount() - amt);
                            }else{
                                amount = amount - i.getAmount();
                                i.setAmount(0);
                            }
                        }
                        }
                       
                        }
                 }
                
                
                
             }
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 4, 2015
  7. Offline

    nj2miami

    Removing an item is done from player.getInventory().remove(ItemStack)

    For instance, if you wanted to reduce/remove ONE from something a player is holding you do this:

    I would do a check on if the getAmount() == 1 or >1 and then

    Code:
    if(count > 1)
    // reduce count
    player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1); 
    else
    // remove last item
    player.getInventory().remove(player.getItemInHand());
     
  8. Offline

    Hex_27

    @nj2miami I need to remove a certain amount of MATERIAL. So, I can't use remove.... And its a crafting table thing, so I can't use p.getItemInHand()
    Using your method, it doesn't remove NAMED items.
     
  9. Offline

    nj2miami

    I don't know what you want to use because you are writing the code :)

    p.getItemInHand() is simply a reference to an ItemStack, it could be ANYTHING. Not sure what you mean by "NAMED" items and you message has been lost in context because you have not provided enough information apparently.

    If you want to remove an Apple, then iterate over the inventory and check if the current item is of type Material.Apple and if so, is the count = 1 or > 1 then either remove 1 by setting its count or remove it entirely by using the command I gave you.

    player.getInventory.remove(ItemStack) <- which you would have by iterating through the Inventory stack.
     
  10. Offline

    Hex_27

    @nj2miami what I'm trying now:
    Code:
             public void removeItem(ItemStack item, Inventory inv, int amt){
               
           
                 for(ItemStack i: inv){
                   
                     if(i != null){
                        if(i == item){
                          
                    if(i.getItemMeta().getDisplayName() == item.getItemMeta().getDisplayName()){
                        i.setAmount(i.getAmount() - amt);
                    }
                          
                        }
                      
                        }
                 }
               
               
               
             }
    Still doesn't work
     
  11. Offline

    Skionz

    @Hex_27 Try using Inventory#getContents(), check if the item has item meta before checking for it's display name, use Object#equals(), or String#equalsIgnoreCase() when comparing Strings.
     
  12. Offline

    Hex_27

    @Skionz I don't needa check because I have another checker where I'm using the method. This isn't working:
    Code:
             public void removeItem(ItemStack item, Inventory inv, int amt){
                
            
                 for(ItemStack i: inv.getContents()){
                
                     if(i != null){
                         int amount = i.getAmount();
                        if(i == item){
                           
                    if(i.getItemMeta().getDisplayName().equalsIgnoreCase(item.getItemMeta().getDisplayName())){
                        i.setAmount(amount - amt);
                    }
                           
                        }
                       
                        }
                 }
                
                
                
             }
     
    Last edited: Jan 4, 2015
  13. Offline

    Skionz

    @Hex_27 What isn't working specifically. Give me a line.
     
  14. Offline

    nj2miami

    OK, last time for you....

    i.setAmount(i.getAmount() - amt); will NOT remove an item.

    The ONLY way to remove an item from an inventory is by calling player.getInventory().remove(ItemStack)

    setAmount can only set the count to a minimum of 1.

    Your code is missing much validation code and will likely break often, but not knowing HOW you are calling this function or knowing your other code, I cannot really offer you additional advice.

    What happens is the user has less than "amt"? What happens is they do not have the item at all? At a minimum your void should return a boolean instead to at least tell whatever called it if it was successful or not.
     
  15. Offline

    Hex_27

    @nj2miami If you would just read the front part.... you would see there was already a check for if the player has enough.... Anyways, I've found another method, so I guess this isn't needed anymore.
     
  16. Offline

    nj2miami

    You should provide HOW you answered your own question so others searching the forums who come across this thread are not left with an unanswered thread and you should mark it as SOLVED since you have apparently "found" a way.
     
  17. Offline

    Hex_27

    @njmiami It IS posted. If you would just scroll up....
     
Thread Status:
Not open for further replies.

Share This Page