From f538440bce92a0bfe4cbb9578563017d075620af Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 30 Apr 2021 13:44:39 +0100 Subject: [PATCH] De-spaghettify the hierarchy for chest inventories --- src/block/inventory/DoubleChestInventory.php | 18 +++++++----------- src/block/inventory/EnderChestInventory.php | 12 ++++++++++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/block/inventory/DoubleChestInventory.php b/src/block/inventory/DoubleChestInventory.php index 978d132b0..cab0c2acb 100644 --- a/src/block/inventory/DoubleChestInventory.php +++ b/src/block/inventory/DoubleChestInventory.php @@ -23,12 +23,11 @@ declare(strict_types=1); namespace pocketmine\block\inventory; -use pocketmine\inventory\BaseInventory; use pocketmine\inventory\InventoryHolder; use pocketmine\item\Item; -use pocketmine\world\Position; +use pocketmine\world\sound\Sound; -class DoubleChestInventory extends ChestInventory implements InventoryHolder{ +class DoubleChestInventory extends AnimatedBlockInventory implements InventoryHolder{ /** @var ChestInventory */ private $left; /** @var ChestInventory */ @@ -37,20 +36,13 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{ public function __construct(ChestInventory $left, ChestInventory $right){ $this->left = $left; $this->right = $right; - BaseInventory::__construct($this->left->getSize() + $this->right->getSize()); + parent::__construct($this->left->getHolder(), $this->left->getSize() + $this->right->getSize()); } public function getInventory(){ return $this; } - /** - * @return Position - */ - public function getHolder(){ - return $this->left->getHolder(); - } - public function getItem(int $index) : Item{ return $index < $this->left->getSize() ? $this->left->getItem($index) : $this->right->getItem($index - $this->left->getSize()); } @@ -72,6 +64,10 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{ return $result; } + protected function getOpenSound() : Sound{ return $this->left->getOpenSound(); } + + protected function getCloseSound() : Sound{ return $this->left->getCloseSound(); } + protected function animateBlock(bool $isOpen) : void{ $this->left->animateBlock($isOpen); $this->right->animateBlock($isOpen); diff --git a/src/block/inventory/EnderChestInventory.php b/src/block/inventory/EnderChestInventory.php index 1e822aa28..b176997d9 100644 --- a/src/block/inventory/EnderChestInventory.php +++ b/src/block/inventory/EnderChestInventory.php @@ -23,15 +23,16 @@ declare(strict_types=1); namespace pocketmine\block\inventory; +use pocketmine\network\mcpe\protocol\BlockEventPacket; use pocketmine\world\Position; use pocketmine\world\sound\EnderChestCloseSound; use pocketmine\world\sound\EnderChestOpenSound; use pocketmine\world\sound\Sound; -class EnderChestInventory extends ChestInventory{ +class EnderChestInventory extends AnimatedBlockInventory{ public function __construct(){ - parent::__construct(new Position(0, 0, 0, null)); + parent::__construct(new Position(0, 0, 0, null), 27); } public function setHolderPosition(Position $pos) : void{ @@ -45,4 +46,11 @@ class EnderChestInventory extends ChestInventory{ protected function getCloseSound() : Sound{ return new EnderChestCloseSound(); } + + protected function animateBlock(bool $isOpen) : void{ + $holder = $this->getHolder(); + + //event ID is always 1 for a chest + $holder->getWorld()->broadcastPacketToViewers($holder, BlockEventPacket::create(1, $isOpen ? 1 : 0, $holder->asVector3())); + } }