How to check if player has enough of item X

Discussion in 'Plugin Development' started by Giant, Jul 18, 2011.

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

    Giant

    Hey,

    Am sort of making a shop plugin like the old SimpleShop, but I ran into a problem... :(

    I am trying to see if a user has enough of the item he is trying to sell, but player.getInventory().contains(itemstack) returns true only when the amount in the itemstack is an exact match to the stack in the inventory...

    So for example:
    user has item stone with amount 6
    stack has item stone with amount 1
    it will return false.
    But when both the user and the stack have the amount 6 it returns true, but when the user has 6 and the stack has 1 it should return true aswell right?

    If not, how should I do this then?

    Thanks in advance! :)
     
  2. Offline

    sunspark

    Try
    Code:
    player.getInventory( ).contains( ItemStack, amount )
    .

    Also,
    Code:
    player.getInventory( ).removeItem( ItemStack ... )
    may come handy if you need to remove a specific amount of items.
     
  3. Offline

    Giant

    So you suggest to move the amount to the contains?
    Code:
    player.getInventory().contains(ItemStack, amount)
    
    And then setting the ItemStack up without an amount? Or set ItemStack amount to 1?
     
  4. Offline

    ItsHarry

    playerInventory.contains is a built in method.
     
  5. Offline

    Giant

    could you care to explain that?
     
  6. Offline

    sunspark

    It doesn't matter how large the amount in your ItemStack is if you use contains(ItemStack, amount).
     
  7. Offline

    Giant

    I have tried that aswell, and when using contains(ItemStack, amount) it seems to only match on a absolute match :(
    I however also found that using just the item id (so no stack or anything) it DOES work, and as it seems that the contains check ignores the data type..........

    I have decided to just use the item id and then do some checks further on! :)

    Thanks for the help though!

    ok, cause am to lazy to make a new topic :oops: I will use this one again...

    I have found that removeItem(ItemStack) ignores the dataType, and just removes any kind of the item....
    So say you have 2 wool, and 2 lime wool and you sell the 2 lime wool.
    removeItem(ItemStack) actually deletes the 2 wool instead of the 2 lime wool.
    Is there a workaround for this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  8. Offline

    nisovin

    Due to problems like this, I ended up writing my own functions for this stuff. Here's what I use:

    Code:
        
        private boolean inventoryContains(Inventory inventory, ItemStack item) {
            int count = 0;
            ItemStack[] items = inventory.getContents();
            for (int i = 0; i < items.length; i++) {
                if (items[i] != null && items[i].getType() == item.getType() && items[i].getDurability() == item.getDurability()) {
                    count += items[i].getAmount();
                }
                if (count >= item.getAmount()) {
                    return true;
                }
            }
            return false;
        }
        
        public void removeFromInventory(Inventory inventory, ItemStack item) {
            int amt = item.getAmount();
            ItemStack[] items = inventory.getContents();
            for (int i = 0; i < items.length; i++) {
                if (items[i] != null && items[i].getType() == item.getType() && items[i].getDurability() == item.getDurability()) {
                    if (items[i].getAmount() > amt) {
                        items[i].setAmount(items[i].getAmount() - amt);
                        break;
                    } else if (items[i].getAmount() == amt) {
                        items[i] = null;
                        break;
                    } else {
                        amt -= items[i].getAmount();
                        items[i] = null;
                    }
                }
            }
            inventory.setContents(items);
        }
    
     
    Giant likes this.
  9. Offline

    Giant

    The removing works great now!! :)
    Thanks alot! :D

    For the contains I already had build my own function hehe.

    Code:
    ArrayList<ItemStack> properStack = new ArrayList<ItemStack>();
    int stackAmt = 0;
    
    HashMap<Integer, ? extends ItemStack> stacky;
    stacky = inventory.all(itemId);
    
    for(Object stacks : stacky.keySet()) {
         ItemStack tmp = stacky.get(stacks);
    
         if(type != null && type.toString().equalsIgnoreCase(tmp.getData().toString())) {
              properStack.add(tmp);
              stackAmt = stackAmt + tmp.getAmount();
         }else if(type == null && tmp.getData() == null) {
              //Items like stone have no dataType so they are null
              properStack.add(tmp);
              stackAmt = stackAmt + tmp.getAmount();
         }
    }
    
    if(stackAmt >= total) {
        //has enough
    }
    
     
Thread Status:
Not open for further replies.

Share This Page