pocketmine\tile\Chest::setItem PHP Method

setItem() public method

This method should not be used by plugins, use the Inventory
public setItem ( integer $index, Item $item ) : boolean
$index integer
$item pocketmine\item\Item
return boolean
    public function setItem($index, Item $item)
    {
        $i = $this->getSlotIndex($index);
        $d = NBT::putItemHelper($item, $index);
        if ($item->getId() === Item::AIR or $item->getCount() <= 0) {
            if ($i >= 0) {
                unset($this->namedtag->Items[$i]);
            }
        } elseif ($i < 0) {
            for ($i = 0; $i <= $this->getSize(); ++$i) {
                if (!isset($this->namedtag->Items[$i])) {
                    break;
                }
            }
            $this->namedtag->Items[$i] = $d;
        } else {
            $this->namedtag->Items[$i] = $d;
        }
        return true;
    }

Usage Example

Example #1
0
 public function refillChest(Chest $chest, $refillPair = true)
 {
     $replaceSlots = [];
     $chest->clearAll();
     if (count($replaceSlots) >= $chest->getSize() / 2) {
         array_slice($replaceSlots, $chest->getSize() / 2 - 1);
     }
     foreach ($replaceSlots as $slot) {
         $item = null;
         // TODO: Decide items to put in chest.
         $chest->setItem($slot, $item);
     }
     if ($chest->isPaired() && $refillPair) {
         // Large chests
         $this->refillChest($chest->getPair(), false);
     }
 }