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

Get item slot

Discussion in 'Development' started by RoyalMCPE, May 7, 2017.

  1. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    How would I be able to get the slot that an item is in, and remove it?
     
    DanielYTK likes this.
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    This is possibly a good example to use in your code
    PHP:
    public function matchItem(Item $neededItem) {
        
    $items $player->getInventory()->getContents();
        foreach(
    $items as $key => $item) {
            if(
    $item->equals($neededItem)) {
                return 
    $key;
            }
            return 
    false;
        }
    }
    Note that unless the other parameters are set properly, the Item will only check for an exact match in the NBT tree and Item damage. https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/item/Item.php#L948
     
    Last edited: May 18, 2017
    Muqsit likes this.
  3. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    Doesn't work
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    But it "should" work. Or you are doing something wrong.
    There's an alternative way if you want to check "deepEquals".
    PHP:
    /** @var BaseInventory $inv */?
    /** @var Item $item */
    $contents $inv->getContents();
    $slot array_search($item$contents);
    if(
    $slot === false){//don't use "==" here!
        //Item not found.
    }else{
        
    //Item found and slot is $slot
    }
     
    corytortoise likes this.
  5. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    If you copied it, it won't work. You need to edit my example to your needs for it to work.
     
    corytortoise likes this.
  6. TestDevelopment

    TestDevelopment Spider Jockey

    Messages:
    41
    GitHub:
    non
    ist $slot an int
     
  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.