1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Get count of a specific Item ID?

Discussion in 'Development' started by corytortoise, Jan 9, 2017.

  1. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I'm trying to make a plugin that checks if a player has a specific Item ID in their inventory. If they do, I want remove all of that item from their inventory. Is there a way I can get the amount of a specific ID that a player has in their inventory?(Example: I check and a player has at least 1 Cobblestone in their inventory. How can I remove all of the Cobblestone in their inventory?)
    I have searched the API and haven't found anything yet. Any help is appreciated.
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    try playerinv->getallitems and remove all matching X ID
     
  3. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    Assuming `$player` is a Player object:

    Code:
    $item = Item::get(Item::COBBLESTONE, 0, 1); //item ID, damage value, count
    if($player->getInventory()->contains($item)){
        $player->getInventory()->remove($item); //will remove ALL such items from the inventory, notice that remove() and removeItem() are not the same!
    }
    
    References:
    https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/inventory/BaseInventory.php#L159
    https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/inventory/BaseInventory.php#L188
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Thanks, I never would have noticed this myself!
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.