world === null || !$this->world->isLoaded()){ throw new AssumptionFailedError("Position world is null or has been unloaded"); } return $this->world; } /** * Checks if this object has a valid reference to a loaded world */ public function isValid() : bool{ if($this->world !== null && !$this->world->isLoaded()){ $this->world = null; return false; } return $this->world !== null; } public function asVector3() : Vector3{ return new Vector3($this->x, $this->y, $this->z); } public function center() : Vector3{ return new Vector3($this->x + 0.5, $this->y + 0.5, $this->z + 0.5); } public static function fromVector3(Vector3 $vector3, World $world) : self{ return new self($vector3->getFloorX(), $vector3->getFloorY(), $vector3->getFloorZ(), $world); } public function getSide(int $side, int $step = 1) : BlockPosition{ $offset = Facing::OFFSET[$side] ?? throw new \InvalidArgumentException("Invalid side $side"); [$dx, $dy, $dz] = $offset; return new BlockPosition($this->x + ($dx * $step), $this->y + ($dy * $step), $this->z + ($dz * $step), $this->world); } public function add(int $x, int $y, int $z) : BlockPosition{ return new BlockPosition($this->x + $x, $this->y + $y, $this->z + $z, $this->world); } /** * @phpstan-return \Generator */ public function getAllSides() : \Generator{ foreach(Facing::ALL as $facing){ yield $this->getSide($facing); } } /** * @phpstan-return list */ public function getAllSidesArray() : array{ return iterator_to_array($this->getAllSides(), preserve_keys: false); } public function __toString() : string{ $worldName = $this->world?->getFolderName() ?? "???"; return "BlockPosition(x=$this->x,y=$this->y,z=$this->z,world={$worldName}"; } }