Consume items from inventory

Discussion in 'Plugin Development' started by xlilcasper, Jun 20, 2011.

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

    xlilcasper

    Any better way to consume say 70 gold from a players inventory then to just loop though there inventory and check each item? I'm not overlooking a inventory.consume command that allows me to put in more then 1 stack right?
     
  2. Offline

    Taco

    As far as I know, you do have to loop through and check every item.
     
  3. Offline

    Trc202

  4. Offline

    NathanWolf

    Hi! That linked thread seemed like a rabbit-hole.

    If you're willing to link to CraftBukkit, this is easy- call CraftInventory.removeItem. (This is the "missing function" you're looking for. Lots of things in Bukkit are not missing, so much as not exposed)

    Pass it an ItemStack- it will remove as much from the inventory as you set the amount of the item stack to.

    So, something like this (written off top of head, may not compile)
    Code:
    ItemStack removeItems = new ItemStack(Material.GOLD_INGOT, 70);
    CraftInventory ci = (CraftInventory)player.getInventory();
    ci.removeItem(removeItems);
    Hope that helps! :)

    Oh, also- you can pass removeItem multiple item stacks- if you want to remove multiple different types of items at once.

    If you want to check to see if there's "enough" first, use contains() as previous people have suggested.
     
  5. Offline

    Taco

    The problem I see with your code there, is that it would return an error if they inventory doesn't contain the item. This could (possibly) be solved by surrounding it with try/catch. Another problem is that you're looking for a stack of 70 ingots, and nothing stacks to 70, so I'm not sure if it would look for more item stacks to make up for the difference.
     
  6. Offline

    NathanWolf

    :D
     
  7. Offline

    Taco

    Ah :p I missed that XD
     
    NathanWolf likes this.
  8. Offline

    xlilcasper

    For anyone else looking here is what I came up with. This is totally ripped from DwarfCraft and hacked to fit my needs.

    Code:
        private boolean CheckItems(Player player, ItemStack costStack)
        {
            //make sure we have enough
            int cost = costStack.getAmount();
            boolean hasEnough=false;
            for (ItemStack invStack : player.getInventory().getContents())
            {
                if(invStack == null)
                    continue;
                if (invStack.getTypeId() == costStack.getTypeId()) {
    
                    int inv = invStack.getAmount();
                    if (cost - inv >= 0) {
                        cost = cost - inv;
                    } else {
                        hasEnough=true;
                        break;
                    }
                }
            }
            return hasEnough;
        }
        private boolean ConsumeItems(Player player, ItemStack costStack)
        {
            if (!CheckItems(player,costStack) return false;
            //Loop though each item and consume as needed. We should of already
            //checked to make sure we had enough with CheckItems.
            for (ItemStack invStack : player.getInventory().getContents())
            {
                if(invStack == null)
                    continue;
    
                if (invStack.getTypeId() == costStack.getTypeId()) {
                    int inv = invStack.getAmount();
                    int cost = costStack.getAmount();
                    if (cost - inv >= 0) {
                        costStack.setAmount(cost - inv);
                        player.getInventory().remove(invStack);
                    } else {
                        costStack.setAmount(0);
                        invStack.setAmount(inv - cost);
                        break;
                    }
                }
            }
            return true;
        }
    
     
    DKDunnings likes this.
Thread Status:
Not open for further replies.

Share This Page