Plugin Help Removing Items from the Players Inventory

Discussion in 'Plugin Help/Development/Requests' started by TheDiamond06, Dec 26, 2014.

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

    TheDiamond06

    I am trying to removing Items from a players inventory. I am trying to remove a custom item"
    Code:
            ItemStack CommonCrate = new ItemStack(Material.CHEST);
            ItemMeta im = CommonCrate.getItemMeta();
            im.setDisplayName("§b§lCommon Crate");
            List<String> lore = new ArrayList<String>(); 
           lore.add("§b§l§oRight Click to Open " + ChatColor.RED + "[Can Contain Rarities 25-30]"); 
           im.setLore(lore); 
            CommonCrate.setItemMeta(im);
    
    What I am trying to do is this:
    Code:
                    p.getInventory().remove(CommonCrate);
    
    When I do this I can remove it IF there is only one in my hand.
    If there is a stack in my hand this does not work.
    If I have multiple slots with one inside of it it removes ALL of them.
    I need to know how to fix all of those issues above.
    Thanks if you can help!
     
  2. Offline

    mrCookieSlime

    @TheDiamond06
    Use .removeItem() instead of .remove()
    .remove() will remove the exact same ItemStack from the Players Inventory.
    .removeItem() however will also look for stacked Items and decrease the ItemStack by the amount of the specified Item.
     
  3. Offline

    TheDiamond06

    I did that, however now when I right click a stack of it, it still does not work. When I have one in my hand and a stack somewhere else in my inventory it removes them all.
    Any idea how to fix it?
    Code:
                    p.getInventory().removeItem(LegendaryCrate);
    
     
  4. Offline

    Josh014

    @TheDiamond06
    Set the amount in your ItemStack to 1.

    Code:
    ItemStack CommonCrate = new ItemStack(Material.CHEST, 1);
    And after removing the item you need to update the inventory.

    Code:
    player.updateInventory();
    This will work. I used it for myself too.
     
Thread Status:
Not open for further replies.

Share This Page