ÂBlock: use Facing::OFFSET in getSide()

instead of the comically inefficient getBlock() + throwaway Position->getSide()
This improved the function's performance by 2.3x.
This commit is contained in:
Dylan K. Taylor 2023-10-19 13:25:32 +01:00
parent ccd2cdd324
commit 6a3ec70c72
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -747,8 +747,14 @@ class Block{
* @return Block
*/
public function getSide(int $side, int $step = 1){
if($this->position->isValid()){
return $this->position->getWorld()->getBlock($this->position->getSide($side, $step));
$position = $this->position;
if($position->isValid()){
[$dx, $dy, $dz] = Facing::OFFSET[$side] ?? throw new \InvalidArgumentException("Unknown side $side");
return $position->getWorld()->getBlockAt(
$position->x + ($dx * $step),
$position->y + ($dy * $step),
$position->z + ($dz * $step)
);
}
throw new \LogicException("Block does not have a valid world");