pocketmine\inventory\BaseInventory::removeItem PHP Метод

removeItem() публичный Метод

public removeItem ( $slots )
    public function removeItem(...$slots)
    {
        /** @var Item[] $itemSlots */
        /** @var Item[] $slots */
        $itemSlots = [];
        foreach ($slots as $slot) {
            if (!$slot instanceof Item) {
                throw new \InvalidArgumentException("Expected Item[], got " . gettype($slot));
            }
            if ($slot->getId() !== 0 and $slot->getCount() > 0) {
                $itemSlots[] = clone $slot;
            }
        }
        for ($i = 0; $i < $this->getSize(); ++$i) {
            $item = $this->getItem($i);
            if ($item->getId() === Item::AIR or $item->getCount() <= 0) {
                continue;
            }
            foreach ($itemSlots as $index => $slot) {
                if ($slot->equals($item, $slot->getDamage() === null ? false : true, $slot->getCompoundTag() === null ? false : true)) {
                    $amount = min($item->getCount(), $slot->getCount());
                    $slot->setCount($slot->getCount() - $amount);
                    $item->setCount($item->getCount() - $amount);
                    $this->setItem($i, $item);
                    if ($slot->getCount() <= 0) {
                        unset($itemSlots[$index]);
                    }
                }
            }
            if (count($itemSlots) === 0) {
                break;
            }
        }
        return $itemSlots;
    }

Usage Example

Пример #1
0
 public function removeItem(...$slots)
 {
     $result = parent::removeItem(...$slots);
     if ($this->getHolder() instanceof Player) {
         if ($this->getHolder()->isSurvival()) {
             $this->sendContents($this->getHolder());
         }
     }
     return $result;
 }