Skip to content

Commit 677566b

Browse files
committed
Abstract getPosition() in traits
we don't technically need to do this, but I suppose it's nicer for people who don't use a static analyser.
1 parent a9c2e95 commit 677566b

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/block/utils/ContainerTrait.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,25 @@ public function isOpeningObstructed() : bool{
5454
return false;
5555
}
5656

57+
abstract protected function getPosition() : Position;
58+
59+
protected function getTile() : ?ContainerTile{
60+
$pos = $this->getPosition();
61+
$tile = $pos->getWorld()->getTile($pos);
62+
return $tile instanceof ContainerTile ? $tile : null;
63+
}
64+
5765
public function canOpenWith(string $key) : bool{
5866
//TODO: maybe we can bring the key to the block in readStateFromWorld()?
59-
$tile = $this->position->getWorld()->getTile($this->position);
60-
return $tile instanceof ContainerTile && $tile->canOpenWith($key);
67+
return $this->getTile()?->canOpenWith($key) ?? false;
6168
}
6269

6370
public function openToUnchecked(Player $player) : bool{
64-
$tile = $this->position->getWorld()->getTile($this->position);
65-
return $tile instanceof ContainerTile && $player->setCurrentWindow($this->newMenu($player, $tile->getInventory(), $this->position));
71+
$tile = $this->getTile();
72+
return $tile !== null && $player->setCurrentWindow($this->newMenu($player, $tile->getInventory(), $this->getPosition()));
6673
}
6774

6875
public function getInventory() : ?Inventory{
69-
$tile = $this->position->getWorld()->getTile($this->position);
70-
return $tile instanceof ContainerTile ? $tile->getInventory() : null;
76+
return $this->getTile()?->getInventory();
7177
}
7278
}

src/block/utils/MenuAccessorTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public function isOpeningObstructed() : bool{
5050
return false;
5151
}
5252

53+
abstract protected function getPosition() : Position;
54+
5355
public function openToUnchecked(Player $player) : bool{
54-
return $player->setCurrentWindow($this->newMenu($player, $this->position));
56+
return $player->setCurrentWindow($this->newMenu($player, $this->getPosition()));
5557
}
5658
}

0 commit comments

Comments
 (0)