PocketMine-MP 5.14.2 git-50e2c469a547a16a23b2dc691e70a51d34e29395
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
28use function assert;
29
30class Position extends Vector3{
31 public ?World $world = null;
32
33 public function __construct(float|int $x, float|int $y, float|int $z, ?World $world){
34 parent::__construct($x, $y, $z);
35 if($world !== null && !$world->isLoaded()){
36 throw new \InvalidArgumentException("Specified world has been unloaded and cannot be used");
37 }
38
39 $this->world = $world;
40 }
41
45 public static function fromObject(Vector3 $pos, ?World $world){
46 return new Position($pos->x, $pos->y, $pos->z, $world);
47 }
48
52 public function asPosition() : Position{
53 return new Position($this->x, $this->y, $this->z, $this->world);
54 }
55
61 public function getWorld() : World{
62 if($this->world === null || !$this->world->isLoaded()){
63 throw new AssumptionFailedError("Position world is null or has been unloaded");
64 }
65
66 return $this->world;
67 }
68
72 public function isValid() : bool{
73 if($this->world !== null && !$this->world->isLoaded()){
74 $this->world = null;
75
76 return false;
77 }
78
79 return $this->world !== null;
80 }
81
87 public function getSide(int $side, int $step = 1){
88 assert($this->isValid());
89
90 return Position::fromObject(parent::getSide($side, $step), $this->world);
91 }
92
93 public function __toString(){
94 return "Position(world=" . ($this->isValid() ? $this->getWorld()->getDisplayName() : "null") . ",x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")";
95 }
96
97 public function equals(Vector3 $v) : bool{
98 if($v instanceof Position){
99 return parent::equals($v) && $v->world === $this->world;
100 }
101 return parent::equals($v);
102 }
103}
getSide(int $side, int $step=1)
Definition: Position.php:87
static fromObject(Vector3 $pos, ?World $world)
Definition: Position.php:45