pocketmine\inventory\PlayerInventory::setItem PHP Метод

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

public setItem ( $index, Item $item, $send = true )
$item pocketmine\item\Item
    public function setItem($index, Item $item, $send = true)
    {
        if ($index < 0 or $index >= $this->size) {
            return false;
        } elseif ($item->getId() === 0 or $item->getCount() <= 0) {
            return $this->clear($index, $send);
        }
        if ($index >= $this->getSize()) {
            //Armor change
            Server::getInstance()->getPluginManager()->callEvent($ev = new EntityArmorChangeEvent($this->getHolder(), $this->getItem($index), $item, $index));
            if ($ev->isCancelled() and $this->getHolder() instanceof Human) {
                $this->sendArmorSlot($index, $this->getViewers());
                return false;
            }
            $item = $ev->getNewItem();
        } else {
            Server::getInstance()->getPluginManager()->callEvent($ev = new EntityInventoryChangeEvent($this->getHolder(), $this->getItem($index), $item, $index));
            if ($ev->isCancelled()) {
                $this->sendSlot($index, $this->getViewers());
                return false;
            }
            $item = $ev->getNewItem();
        }
        $old = $this->getItem($index);
        $this->slots[$index] = clone $item;
        $this->onSlotChange($index, $old, $send);
        return true;
    }

Usage Example

Пример #1
0
 protected function initEntity()
 {
     $this->setDataFlag(self::DATA_PLAYER_FLAGS, self::DATA_PLAYER_FLAG_SLEEP, false);
     $this->setDataProperty(self::DATA_PLAYER_BED_POSITION, self::DATA_TYPE_POS, [0, 0, 0]);
     $this->inventory = new PlayerInventory($this);
     if ($this instanceof Player) {
         $this->addWindow($this->inventory, 0);
     }
     if (!$this instanceof Player) {
         if (isset($this->namedtag->NameTag)) {
             $this->setNameTag($this->namedtag["NameTag"]);
         }
         if (isset($this->namedtag->Skin) and $this->namedtag->Skin instanceof Compound) {
             $this->setSkin($this->namedtag->Skin["Data"], $this->namedtag->Skin["Slim"] > 0, $this->namedtag->Skin["Transparent"] > 0);
         }
         $this->uuid = UUID::fromData($this->getId(), $this->getSkinData(), $this->getNameTag());
     }
     if (isset($this->namedtag->Inventory) and $this->namedtag->Inventory instanceof Enum) {
         foreach ($this->namedtag->Inventory as $item) {
             if ($item["Slot"] >= 0 and $item["Slot"] < 9) {
                 //Hotbar
                 $this->inventory->setHotbarSlotIndex($item["Slot"], isset($item["TrueSlot"]) ? $item["TrueSlot"] : -1);
             } elseif ($item["Slot"] >= 100 and $item["Slot"] < 104) {
                 //Armor
                 $this->inventory->setItem($this->inventory->getSize() + $item["Slot"] - 100, NBT::getItemHelper($item));
             } else {
                 $this->inventory->setItem($item["Slot"] - 9, NBT::getItemHelper($item));
             }
         }
     }
     parent::initEntity();
 }
All Usage Examples Of pocketmine\inventory\PlayerInventory::setItem