pocketmine\Player::addWindow PHP Method

addWindow() public method

Returns the created/existing window id
public addWindow ( pocketmine\inventory\Inventory $inventory, integer $forceId = null ) : integer
$inventory pocketmine\inventory\Inventory
$forceId integer
return integer
    public function addWindow(Inventory $inventory, $forceId = null) : int
    {
        if ($this->windows->contains($inventory)) {
            return $this->windows[$inventory];
        }
        if ($forceId === null) {
            $this->windowCnt = $cnt = max(2, ++$this->windowCnt % 99);
        } else {
            $cnt = (int) $forceId;
        }
        $this->windowIndex[$cnt] = $inventory;
        $this->windows->attach($inventory, $cnt);
        if ($inventory->open($this)) {
            return $cnt;
        } else {
            $this->removeWindow($inventory);
            return -1;
        }
    }

Usage Example

示例#1
1
 public function onActivate(Item $item, Player $player = null)
 {
     if (!$this->getLevel()->getServer()->enchantingTableEnabled) {
         return true;
     }
     if ($player instanceof Player) {
         if ($player->isCreative() and $player->getServer()->limitedCreative) {
             return true;
         }
         $tile = $this->getLevel()->getTile($this);
         $enchantTable = null;
         if ($tile instanceof EnchantTable) {
             $enchantTable = $tile;
         } else {
             $this->getLevel()->setBlock($this, $this, true, true);
             $nbt = new CompoundTag("", [new StringTag("id", Tile::ENCHANT_TABLE), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z)]);
             if ($item->hasCustomName()) {
                 $nbt->CustomName = new StringTag("CustomName", $item->getCustomName());
             }
             if ($item->hasCustomBlockData()) {
                 foreach ($item->getCustomBlockData() as $key => $v) {
                     $nbt->{$key} = $v;
                 }
             }
             /** @var EnchantTable $enchantTable */
             $enchantTable = Tile::createTile(Tile::ENCHANT_TABLE, $this->getLevel()->getChunk($this->x >> 4, $this->z >> 4), $nbt);
         }
         $player->addWindow(new EnchantInventory($this));
         $player->craftingType = Player::CRAFTING_ENCHANT;
     }
     return true;
 }
All Usage Examples Of pocketmine\Player::addWindow
Player