PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
Loading...
Searching...
No Matches
Position.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\world;
25
29use function assert;
30
31readonly class Position extends Vector3{
32 public ?World $world;
33
34 public function __construct(float|int $x, float|int $y, float|int $z, ?World $world){
35 parent::__construct($x, $y, $z);
36 if($world !== null && !$world->isLoaded()){
37 throw new \InvalidArgumentException("Specified world has been unloaded and cannot be used");
38 }
39
40 $this->world = $world;
41 }
42
46 public static function fromObject(Vector3 $pos, ?World $world){
47 return new Position($pos->x, $pos->y, $pos->z, $world);
48 }
49
53 public function asPosition() : Position{
54 return new Position($this->x, $this->y, $this->z, $this->world);
55 }
56
62 public function getWorld() : World{
63 if($this->world === null || !$this->world->isLoaded()){
64 throw new AssumptionFailedError("Position world is null or has been unloaded");
65 }
66
67 return $this->world;
68 }
69
73 public function isValid() : bool{
74 if($this->world !== null && !$this->world->isLoaded()){
75 return false;
76 }
77
78 return $this->world !== null;
79 }
80
86 public function getSide(Facing $side, int $step = 1){
87 assert($this->isValid());
88
89 return Position::fromObject(parent::getSide($side, $step), $this->world);
90 }
91
92 public function __toString(){
93 return "Position(world=" . ($this->isValid() ? $this->getWorld()->getDisplayName() : "null") . ",x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")";
94 }
95
96 public function equals(Vector3 $v) : bool{
97 if($v instanceof Position){
98 return parent::equals($v) && $v->world === $this->world;
99 }
100 return parent::equals($v);
101 }
102}
getSide(Facing $side, int $step=1)
Definition Position.php:86
static fromObject(Vector3 $pos, ?World $world)
Definition Position.php:46