id = new String("id", Tile::CHEST); parent::__construct($chunk, $nbt); $this->inventory = new ChestInventory($this); if(!isset($this->namedtag->Items) or !($this->namedtag->Items instanceof Enum)){ $this->namedtag->Items = new Enum("Inventory", []); $this->namedtag->Items->setTagType(NBT::TAG_Compound); } for($i = 0; $i < $this->getSize(); ++$i){ $this->inventory->setItem($i, $this->getItem($i)); } $this->checkPairing(); } public function close(){ if($this->closed === false){ foreach($this->getInventory()->getViewers() as $player){ $this->getInventory()->close($player); } foreach($this->getRealInventory()->getViewers() as $player){ $this->getRealInventory()->close($player); } parent::close(); } } public function saveNBT(){ $this->namedtag->Items = new Enum("Items", []); $this->namedtag->Items->setTagType(NBT::TAG_Compound); for($index = 0; $index < $this->getSize(); ++$index){ $this->setItem($index, $this->inventory->getItem($index)); } } /** * @return int */ public function getSize(){ return 27; } /** * @param $index * * @return int */ protected function getSlotIndex($index){ foreach($this->namedtag->Items as $i => $slot){ if($slot["Slot"] === $index){ return $i; } } return -1; } /** * This method should not be used by plugins, use the Inventory * * @param int $index * * @return Item */ public function getItem($index){ $i = $this->getSlotIndex($index); if($i < 0){ return Item::get(Item::AIR, 0, 0); }else{ return Item::get($this->namedtag->Items[$i]["id"], $this->namedtag->Items[$i]["Damage"], $this->namedtag->Items[$i]["Count"]); } } /** * This method should not be used by plugins, use the Inventory * * @param int $index * @param Item $item * * @return bool */ public function setItem($index, Item $item){ $i = $this->getSlotIndex($index); $d = new Compound(false, [ new Byte("Count", $item->getCount()), new Byte("Slot", $index), new Short("id", $item->getID()), new Short("Damage", $item->getDamage()), ]); 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; } /** * @return ChestInventory|DoubleChestInventory */ public function getInventory(){ return $this->doubleInventory instanceof DoubleChestInventory ? $this->doubleInventory : $this->inventory; } /** * @return ChestInventory */ public function getRealInventory(){ return $this->inventory; } protected function checkPairing(){ if(($pair = $this->getPair()) instanceof Chest){ if($this->doubleInventory === null){ if(($pair->x + ($pair->z << 15)) > ($this->x + ($this->z << 15))){ //Order them correctly $this->doubleInventory = new DoubleChestInventory($pair, $this); }else{ $this->doubleInventory = new DoubleChestInventory($this, $pair); } } }else{ $this->doubleInventory = null; unset($this->namedtag->pairx, $this->namedtag->pairz); } } public function isPaired(){ if(!isset($this->namedtag->pairx) or !isset($this->namedtag->pairz)){ return false; } return true; } /** * @return Chest */ public function getPair(){ if($this->isPaired()){ $tile = $this->getLevel()->getTile(Vector3::createVector((int) $this->namedtag["pairx"], $this->y, (int) $this->namedtag["pairz"])); if($tile instanceof Chest){ return $tile; } } return null; } public function pairWith(Chest $tile){ if($this->isPaired() or $tile->isPaired()){ return false; } $this->namedtag->pairx = new Int("pairx", $tile->x); $this->namedtag->pairz = new Int("pairz", $tile->z); $tile->namedtag->pairx = new Int("pairx", $this->x); $tile->namedtag->pairz = new Int("pairz", $this->z); $this->spawnToAll(); $tile->spawnToAll(); $this->checkPairing(); return true; } public function unpair(){ if(!$this->isPaired()){ return false; } $tile = $this->getPair(); unset($this->namedtag->pairx, $this->namedtag->pairz, $tile->namedtag->pairx, $tile->namedtag->pairz); $this->spawnToAll(); $this->checkPairing(); if($tile instanceof Chest){ $tile->spawnToAll(); } return true; } public function getSpawnCompound(){ if($this->isPaired()){ return new Compound("", [ new String("id", Tile::CHEST), new Int("x", (int) $this->x), new Int("y", (int) $this->y), new Int("z", (int) $this->z), new Int("pairx", (int) $this->namedtag["pairx"]), new Int("pairz", (int) $this->namedtag["pairz"]) ]); }else{ return new Compound("", [ new String("id", Tile::CHEST), new Int("x", (int) $this->x), new Int("y", (int) $this->y), new Int("z", (int) $this->z) ]); } } }