diff --git a/src/pocketmine/tile/Chest.php b/src/pocketmine/tile/Chest.php index dc3d639cf..ee1f5709a 100644 --- a/src/pocketmine/tile/Chest.php +++ b/src/pocketmine/tile/Chest.php @@ -73,13 +73,6 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ $this->saveItems(); } - /** - * @return int - */ - public function getSize() : int{ - return 27; - } - /** * @return ChestInventory|DoubleChestInventory */ diff --git a/src/pocketmine/tile/Container.php b/src/pocketmine/tile/Container.php index f2c72a08b..fa3f37d23 100644 --- a/src/pocketmine/tile/Container.php +++ b/src/pocketmine/tile/Container.php @@ -29,24 +29,6 @@ use pocketmine\item\Item; interface Container{ public const TAG_ITEMS = "Items"; - /** - * @param int $index - * - * @return Item - */ - public function getItem(int $index) : Item; - - /** - * @param int $index - * @param Item $item - */ - public function setItem(int $index, Item $item); - - /** - * @return int - */ - public function getSize() : int; - /** * @return Inventory */ diff --git a/src/pocketmine/tile/ContainerTrait.php b/src/pocketmine/tile/ContainerTrait.php index 69372df56..cf9558195 100644 --- a/src/pocketmine/tile/ContainerTrait.php +++ b/src/pocketmine/tile/ContainerTrait.php @@ -35,11 +35,6 @@ use pocketmine\nbt\tag\ListTag; */ trait ContainerTrait{ - /** - * @return int - */ - abstract public function getSize() : int; - abstract public function getNBT() : CompoundTag; /** @@ -47,89 +42,23 @@ trait ContainerTrait{ */ abstract public function getRealInventory(); - /** - * @param $index - * - * @return int - */ - protected function getSlotIndex(int $index) : int{ - foreach($this->getNBT()->getListTag(Container::TAG_ITEMS) as $i => $slot){ - /** @var CompoundTag $slot */ - if($slot->getByte("Slot") === $index){ - return (int) $i; - } - } - - return -1; - } - - /** - * This method should not be used by plugins, use the Inventory - * - * @param int $index - * - * @return Item - */ - public function getItem(int $index) : Item{ - $i = $this->getSlotIndex($index); - /** @var CompoundTag|null $itemTag */ - $itemTag = $this->getNBT()->getListTag(Container::TAG_ITEMS)[$i] ?? null; - if($itemTag !== null){ - return Item::nbtDeserialize($itemTag); - } - - return ItemFactory::get(Item::AIR, 0, 0); - } - - /** - * This method should not be used by plugins, use the Inventory - * - * @param int $index - * @param Item $item - */ - public function setItem(int $index, Item $item) : void{ - $i = $this->getSlotIndex($index); - - $d = $item->nbtSerialize($index); - - $items = $this->getNBT()->getListTag(Container::TAG_ITEMS); - assert($items instanceof ListTag); - - if($item->isNull()){ - if($i >= 0){ - unset($items[$i]); - } - }elseif($i < 0){ - for($i = 0; $i <= $this->getSize(); ++$i){ - if(!isset($items[$i])){ - break; - } - } - $items[$i] = $d; - }else{ - $items[$i] = $d; - } - - $this->getNBT()->setTag($items); - } - protected function loadItems() : void{ - if(!$this->getNBT()->hasTag(Container::TAG_ITEMS, ListTag::class)){ - $this->getNBT()->setTag(new ListTag(Container::TAG_ITEMS, [], NBT::TAG_Compound)); - } + if($this->getNBT()->hasTag(Container::TAG_ITEMS, ListTag::class)){ + $inventoryTag = $this->getNBT()->getListTag(Container::TAG_ITEMS); - $inventory = $this->getRealInventory(); - for($i = 0, $size = $this->getSize(); $i < $size; ++$i){ - $inventory->setItem($i, $this->getItem($i)); + $inventory = $this->getRealInventory(); + foreach($inventoryTag as $itemNBT){ + $inventory->setItem($itemNBT->getByte("Slot"), Item::nbtDeserialize($itemNBT)); + } } } protected function saveItems() : void{ - $this->getNBT()->setTag(new ListTag(Container::TAG_ITEMS, [], NBT::TAG_Compound)); - - $inventory = $this->getRealInventory(); - for($i = 0, $size = $this->getSize(); $i < $size; ++$i){ - $this->setItem($i, $inventory->getItem($i)); + $items = []; + foreach($this->getRealInventory()->getContents() as $slot => $item){ + $items[] = $item->nbtSerialize($slot); } + + $this->getNBT()->setTag(new ListTag(Container::TAG_ITEMS, $items, NBT::TAG_Compound)); } } diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index 8cbd0fbb5..d7aa6cc2c 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -104,13 +104,6 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ $this->saveItems(); } - /** - * @return int - */ - public function getSize() : int{ - return 3; - } - /** * @return FurnaceInventory */