From 8a8b1b0b973348f4b9a40e34cb0bad7a30c53d06 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 19 May 2020 18:33:16 +0100 Subject: [PATCH] Remove Position->setWorld() --- src/block/Block.php | 5 +---- src/block/tile/Tile.php | 2 +- src/entity/Entity.php | 11 +++++++++-- src/world/Position.php | 22 +++++----------------- tests/phpstan/configs/gc-hacks.neon | 10 ++++++++++ 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/block/Block.php b/src/block/Block.php index f77607b46..88bb8e64e 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -345,10 +345,7 @@ class Block{ * @internal */ final public function position(World $world, int $x, int $y, int $z) : void{ - $this->pos->x = $x; - $this->pos->y = $y; - $this->pos->z = $z; - $this->pos->world = $world; + $this->pos = new Position($x, $y, $z, $world); } /** diff --git a/src/block/tile/Tile.php b/src/block/tile/Tile.php index 8895b6942..4afcd3f23 100644 --- a/src/block/tile/Tile.php +++ b/src/block/tile/Tile.php @@ -131,8 +131,8 @@ abstract class Tile{ if($this->pos->isValid()){ $this->pos->getWorldNonNull()->removeTile($this); - $this->pos->setWorld(null); } + $this->pos = null; } } } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index b42a62c36..cacca686c 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1486,7 +1486,14 @@ abstract class Entity{ $this->despawnFromAll(); } - $this->location->setWorld($targetWorld); + $this->location = new Location( + $this->location->x, + $this->location->y, + $this->location->z, + $this->location->yaw, + $this->location->pitch, + $targetWorld + ); $this->getWorld()->addEntity($this); $this->chunk = null; @@ -1634,7 +1641,7 @@ abstract class Entity{ */ protected function destroyCycles() : void{ $this->chunk = null; - $this->location->setWorld(null); + $this->location = null; $this->lastDamageCause = null; } diff --git a/src/world/Position.php b/src/world/Position.php index 6ea8a0f19..c295a585f 100644 --- a/src/world/Position.php +++ b/src/world/Position.php @@ -39,7 +39,11 @@ class Position extends Vector3{ */ public function __construct($x = 0, $y = 0, $z = 0, ?World $world = null){ parent::__construct($x, $y, $z); - $this->setWorld($world); + if($world !== null and $world->isClosed()){ + throw new \InvalidArgumentException("Specified world has been unloaded and cannot be used"); + } + + $this->world = $world; } /** @@ -85,22 +89,6 @@ class Position extends Vector3{ return $world; } - /** - * Sets the target world of the position. - * - * @return $this - * - * @throws \InvalidArgumentException if the specified World has been closed - */ - public function setWorld(?World $world){ - if($world !== null and $world->isClosed()){ - throw new \InvalidArgumentException("Specified world has been unloaded and cannot be used"); - } - - $this->world = $world; - return $this; - } - /** * Checks if this object has a valid reference to a loaded world */ diff --git a/tests/phpstan/configs/gc-hacks.neon b/tests/phpstan/configs/gc-hacks.neon index a2cb1cbc3..d5814ad90 100644 --- a/tests/phpstan/configs/gc-hacks.neon +++ b/tests/phpstan/configs/gc-hacks.neon @@ -20,6 +20,16 @@ parameters: count: 1 path: ../../../src/block/tile/Hopper.php + - + message: "#^Property pocketmine\\\\block\\\\tile\\\\Tile\\:\\:\\$pos \\(pocketmine\\\\world\\\\Position\\) does not accept null\\.$#" + count: 1 + path: ../../../src/block/tile/Tile.php + + - + message: "#^Property pocketmine\\\\entity\\\\Entity\\:\\:\\$location \\(pocketmine\\\\entity\\\\Location\\) does not accept null\\.$#" + count: 1 + path: ../../../src/entity/Entity.php + - message: "#^Property pocketmine\\\\entity\\\\Human\\:\\:\\$inventory \\(pocketmine\\\\inventory\\\\PlayerInventory\\) does not accept null\\.$#" count: 1