De-spaghettify the hierarchy for chest inventories

This commit is contained in:
Dylan K. Taylor 2021-04-30 13:44:39 +01:00
parent d37016f43b
commit f538440bce
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 17 additions and 13 deletions

View File

@ -23,12 +23,11 @@ declare(strict_types=1);
namespace pocketmine\block\inventory; namespace pocketmine\block\inventory;
use pocketmine\inventory\BaseInventory;
use pocketmine\inventory\InventoryHolder; use pocketmine\inventory\InventoryHolder;
use pocketmine\item\Item; 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 */ /** @var ChestInventory */
private $left; private $left;
/** @var ChestInventory */ /** @var ChestInventory */
@ -37,20 +36,13 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
public function __construct(ChestInventory $left, ChestInventory $right){ public function __construct(ChestInventory $left, ChestInventory $right){
$this->left = $left; $this->left = $left;
$this->right = $right; $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(){ public function getInventory(){
return $this; return $this;
} }
/**
* @return Position
*/
public function getHolder(){
return $this->left->getHolder();
}
public function getItem(int $index) : Item{ public function getItem(int $index) : Item{
return $index < $this->left->getSize() ? $this->left->getItem($index) : $this->right->getItem($index - $this->left->getSize()); 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; return $result;
} }
protected function getOpenSound() : Sound{ return $this->left->getOpenSound(); }
protected function getCloseSound() : Sound{ return $this->left->getCloseSound(); }
protected function animateBlock(bool $isOpen) : void{ protected function animateBlock(bool $isOpen) : void{
$this->left->animateBlock($isOpen); $this->left->animateBlock($isOpen);
$this->right->animateBlock($isOpen); $this->right->animateBlock($isOpen);

View File

@ -23,15 +23,16 @@ declare(strict_types=1);
namespace pocketmine\block\inventory; namespace pocketmine\block\inventory;
use pocketmine\network\mcpe\protocol\BlockEventPacket;
use pocketmine\world\Position; use pocketmine\world\Position;
use pocketmine\world\sound\EnderChestCloseSound; use pocketmine\world\sound\EnderChestCloseSound;
use pocketmine\world\sound\EnderChestOpenSound; use pocketmine\world\sound\EnderChestOpenSound;
use pocketmine\world\sound\Sound; use pocketmine\world\sound\Sound;
class EnderChestInventory extends ChestInventory{ class EnderChestInventory extends AnimatedBlockInventory{
public function __construct(){ 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{ public function setHolderPosition(Position $pos) : void{
@ -45,4 +46,11 @@ class EnderChestInventory extends ChestInventory{
protected function getCloseSound() : Sound{ protected function getCloseSound() : Sound{
return new EnderChestCloseSound(); 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()));
}
} }