Removing X amount of custom items

Discussion in 'Plugin Development' started by TheMagzuz, Jan 4, 2016.

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

    TheMagzuz

    If I want to remove X amount of custom items from a players inventory (e.g Removing 7 pieces of dirt, named "Test"), regardless of size of the itemstack?
     
  2. Offline

    xXJustiinXx

    Well, I think you want help. So where is your problem? Show us your code
     
  3. Offline

    Zombie_Striker

    @TheMagzuz
    • Loop through every item in the players inventory
    • Check to make sure the item is not null.
    • If the itemstack's type is that of what you're testing for
    • Get the amount
    • If the amount is greater than the amount you want to remove
    • set the amount to be (amount-amount you want to take away)
    • else (If it's less than or equal to the amount you want to take)
    • Remove the item, (amount you need to take a way minus amount that was taken away), and keep looping.
     
    87pen likes this.
  4. Offline

    MoeMix

    Use these methods:
    Code:
    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);
        }
       
        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;
        }
     
  5. Offline

    Zombie_Striker

    @MoeMix
    Looks like your work here is done. Captain Spoonfeeder, away!
     
    87pen likes this.
  6. Offline

    MoeMix

    @Zombie_Striker
    I found them within 30 seconds of googling. So technically I'm just redirecting him to a resource :p
     
  7. Offline

    Zombie_Striker

    @MoeMix
    It seems you are. But you should have posted the link instead (the resource would most likely have a tutorial/ some form of documentation saying what everything does and why)
     
    Last edited: Jan 4, 2016
  8. Offline

    87pen

    Going for the full Pseudo make sure to check if the item has ItemMeta and if it's customDisplayName equals the one you are searching for.
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page