diff --git a/src/block/utils/ContainerTrait.php b/src/block/utils/ContainerTrait.php index 90a43cb49..7b6b05795 100644 --- a/src/block/utils/ContainerTrait.php +++ b/src/block/utils/ContainerTrait.php @@ -54,19 +54,25 @@ trait ContainerTrait{ return false; } + abstract protected function getPosition() : Position; + + protected function getTile() : ?ContainerTile{ + $pos = $this->getPosition(); + $tile = $pos->getWorld()->getTile($pos); + return $tile instanceof ContainerTile ? $tile : null; + } + public function canOpenWith(string $key) : bool{ //TODO: maybe we can bring the key to the block in readStateFromWorld()? - $tile = $this->position->getWorld()->getTile($this->position); - return $tile instanceof ContainerTile && $tile->canOpenWith($key); + return $this->getTile()?->canOpenWith($key) ?? false; } public function openToUnchecked(Player $player) : bool{ - $tile = $this->position->getWorld()->getTile($this->position); - return $tile instanceof ContainerTile && $player->setCurrentWindow($this->newMenu($player, $tile->getInventory(), $this->position)); + $tile = $this->getTile(); + return $tile !== null && $player->setCurrentWindow($this->newMenu($player, $tile->getInventory(), $this->getPosition())); } public function getInventory() : ?Inventory{ - $tile = $this->position->getWorld()->getTile($this->position); - return $tile instanceof ContainerTile ? $tile->getInventory() : null; + return $this->getTile()?->getInventory(); } } diff --git a/src/block/utils/MenuAccessorTrait.php b/src/block/utils/MenuAccessorTrait.php index 11eb8414b..2325735fd 100644 --- a/src/block/utils/MenuAccessorTrait.php +++ b/src/block/utils/MenuAccessorTrait.php @@ -50,7 +50,9 @@ trait MenuAccessorTrait{ return false; } + abstract protected function getPosition() : Position; + public function openToUnchecked(Player $player) : bool{ - return $player->setCurrentWindow($this->newMenu($player, $this->position)); + return $player->setCurrentWindow($this->newMenu($player, $this->getPosition())); } }