Solved Remove x amount of ItemStack from Player Inventory

Discussion in 'Spigot Plugin Development' started by DinoThePro, Jul 1, 2016.

  1. How would I go about removing let's say 16 of ItemStack from a player's inventory, I've tried with the p.getInventory().remove(itemstack); but it only removes the precies amount, so if a player had 64 of itemstack and I did that method, it wouldn't remove anything...

    Any help appreciated!
     
    • Like Like x 1
  2. p.getInventory().getItem(0).setAmount(amount); ?
     
  3. Code (Text):
                for(ItemStack item : p.getInventory().getContents()) {
                    if (item.getType() == Material.TYPE) {
                        item.setAmount(item.getAmount() - 2);
                    }
                }
    that should work too
     
  4. Wait so i didn't really get your request here can you explain in detail?
     
  5. I don't know what slot it is.

    And what exactly would that do... I still have to do a "huge" calculation to calculate how much to set the amount to. I was thinking isn't there an easier way to do this?


    Well I want to remove x amount of ITEMSTACK from player's inventory, now there is a method for this (p.getInventory.remove(itemstack)) but it removes the precise amount so the player has to have precisely x amount of itemstack if they have more or less it wont work.



    For example:
    Player has 64 of grass with displayname "test" player sells 16, plugin needs to remove 16 grass with name "test" from player's inventory, this would leave the player with 48 Grass with the displayname "test".
     
    • Like Like x 1
  6. This won't work if the amount you want to remove is greater than 64. Do this same looping thing, but keep a counter variable. Set it to the amount you want to remove, the counter is less than 64, just remove that amount. If it's greater than 64, remove the entire ItemStack, decrement the counter, then continue through the loop.

    EDIT:

    Code (Text):
    if(itemstack.getAmount() <= counter) {
                    counter -= itemstack.getAmount();
                    inv.setItem(inv.first(itemstack), new ItemStack(Material.AIR));
    } else {
        itemstack.setAmount(itemstack.getAmount() - counter);
        counter = 0;
    }
    Something like this inside of a loop would work.
     
  7. He wants to remove x amount, so maybe he wants to create some type of shop plugin
     
  8. yeah i did think of that but why would you remove more then 64
     
  9. I am going to try this now:
    Code (Text):
        public void sellItem(PlayerInventory i, ItemStack item)
        {
            int counter = item.getAmount(); // amount I want to remove
            for (ItemStack Item : i.getContents()) // loop through all items
            {
                if (Item.getType().equals(item.getType()) &&
                        Item.getItemMeta().equals(item.getItemMeta()) &&
                        Item.getDurability() == item.getDurability()) // checking so the Item is "equal" to item
                {
                    if (counter - Item.getAmount() < 0) // if the counter - Item.getAmount() < 0 then counter = 0
                    {
                        counter = 0;
                        Item.setAmount(Math.abs(counter - Item.getAmount()));
                    }
                    else// counter - Item.getAmount() > 0
                    {
                        counter = counter - Item.getAmount();
                        Item.setAmount(0);
                    }
                }
            }
        }
    No idea if it'll work though, we'll see in a sec.
     
    • Like Like x 1
  10. Code (Text):
                for(ItemStack item : p.getInventory().getContents()) {
                    if (item.getItemMeta().getDisplayName().equals(x)) {
                        item.setAmount(item.getAmount() - 2);
                    }
                }
     
  11. Code (Text):
        public void sellItem(PlayerInventory i, ItemStack item)
        {
            int counter = item.getAmount(); // amount I want to remove
            for (ItemStack Item : i.getContents()) // loop through all items
            {
                if (Item == null ||
                        Item.getType() == null ||
                        Item.getItemMeta() == null)
                    continue;
                if (Item.getType().equals(item.getType()) &&
                        Item.getItemMeta().equals(item.getItemMeta()) &&
                        Item.getDurability() == item.getDurability()) // checking so the Item is "equal" to item
                {
                    if (counter - Item.getAmount() < 0) // if the counter - Item.getAmount() < 0 then counter = 0
                    {
                        counter = 0;
                        Item.setAmount(Math.abs(counter - Item.getAmount()));
                    }
                    else// counter - Item.getAmount() > 0
                    {
                        counter = counter - Item.getAmount();
                        Item.setAmount(0);
                    }
                }
            }
        }
    Alright this method doesn't work. Maybe I can't set the Item amount beause I'm looping through the contents?
     
    • Like Like x 1
  12. Try making a for loop through every inventory slot, and if the slot's itemStack() is the material you are wanting to subtract from, then setAmount to ItemStack().getAmount - 16, and then exit your loop.
     
  13. Woah woah people, calm down. There's Inventory#removeItem(ItemStack...stacks), use it. And if you are giving advice, ffs, read the documentation properly before giving him arbitrary, complex answers.

    (in essence it iterates over the inventory and tries to remove similar ItemStacks as you provide it, up to the amount of the stack)
     
    • Like Like x 2
  14. Oh my god, where you been all my life?
    I made this method all for nothing then :p
    Code (Text):
        public void sellItem(PlayerInventory pi, ItemStack item)
        {
            int remove = item.getAmount(); // 16
            for (int i = 0; i<35; i++)
            {
                ItemStack Item = pi.getItem(i);
                if (Item == null ||
                        Item.getItemMeta() == null ||
                        Item.getType() == null)
                    continue;
               
                if (Item.getType().equals(item.getType()) &&
                        Item.getItemMeta().equals(item.getItemMeta()) &&
                        Item.getEnchantments().equals(item.getEnchantments()) &&
                        Item.getDurability() == item.getDurability())
                { // Item is item
                    if (remove - Item.getAmount() <= 0)
                    {
                        if (remove - Item.getAmount() == 0)
                        {
                            pi.getItem(i).setType(Material.AIR);
                            remove = 0;
                            break;
                        }
                       
                        pi.getItem(i).setAmount(Math.abs(remove - Item.getAmount()));
                        remove = 0;
                        break;
                    }
                    else // remove - Item.getAmount() > 0
                    {
                        remove = remove - Item.getAmount();
                        Item.setType(Material.AIR);
                    }
                }
            }
        }
    This didn't work 100% tho so I really appreciate that you told me about that method (it works!) I can't believe I missed that method earlier when I was messing around with the Inventory#remove methods... Thanks!
     
    • Like Like x 1
    • Friendly Friendly x 1
  15. Marking thread solved, thanks to everyone who have helped and taken your time to respond.

    Especially thanks to @DarkSeraphim for giving me the final answer!
     
    • Like Like x 1