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.
This commit is contained in:
Dylan K. Taylor
2025-08-31 01:39:26 +01:00
parent a9c2e95860
commit 677566bd24
2 changed files with 15 additions and 7 deletions

View File

@ -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();
}
}

View File

@ -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()));
}
}