From 7a1cdf88e8fe962270f9e1ab7950335c7eefd06e Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 30 Oct 2016 19:21:48 +0000 Subject: [PATCH 1/2] Fixed bugs related to #24 such as getName() on null --- src/pocketmine/Player.php | 9 ++-- src/pocketmine/level/Position.php | 2 +- src/pocketmine/level/WeakPosition.php | 78 +++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/pocketmine/level/WeakPosition.php diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 4fc4c1d04..b486f558f 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -86,6 +86,7 @@ use pocketmine\level\Level; use pocketmine\level\Location; use pocketmine\level\Position; use pocketmine\level\sound\LaunchSound; +use pocketmine\level\WeakPosition; use pocketmine\math\AxisAlignedBB; use pocketmine\math\Vector2; use pocketmine\math\Vector3; @@ -228,7 +229,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade protected $viewDistance; protected $chunksPerTick; protected $spawnThreshold; - /** @var null|Position */ + /** @var null|WeakPosition */ private $spawnPosition = null; protected $inAirTicks = 0; @@ -1038,7 +1039,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade }else{ $level = $pos->getLevel(); } - $this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level); + $this->spawnPosition = new WeakPosition($pos->x, $pos->y, $pos->z, $level); $pk = new SetSpawnPositionPacket(); $pk->x = (int) $this->spawnPosition->x; $pk->y = (int) $this->spawnPosition->y; @@ -1678,7 +1679,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade $this->dataPacket(new ResourcePacksInfoPacket()); if($this->spawnPosition === null and isset($this->namedtag->SpawnLevel) and ($level = $this->server->getLevelByName($this->namedtag["SpawnLevel"])) instanceof Level){ - $this->spawnPosition = new Position($this->namedtag["SpawnX"], $this->namedtag["SpawnY"], $this->namedtag["SpawnZ"], $level); + $this->spawnPosition = new WeakPosition($this->namedtag["SpawnX"], $this->namedtag["SpawnY"], $this->namedtag["SpawnZ"], $level); } $spawnPosition = $this->getSpawn(); @@ -3081,7 +3082,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade parent::saveNBT(); if($this->level instanceof Level){ $this->namedtag->Level = new StringTag("Level", $this->level->getName()); - if($this->spawnPosition instanceof Position and $this->spawnPosition->getLevel() instanceof Level){ + if($this->spawnPosition instanceof WeakPosition and $this->spawnPosition->isValid()){ $this->namedtag["SpawnLevel"] = $this->spawnPosition->getLevel()->getName(); $this->namedtag["SpawnX"] = (int) $this->spawnPosition->x; $this->namedtag["SpawnY"] = (int) $this->spawnPosition->y; diff --git a/src/pocketmine/level/Position.php b/src/pocketmine/level/Position.php index d581ab0da..92e0d74fd 100644 --- a/src/pocketmine/level/Position.php +++ b/src/pocketmine/level/Position.php @@ -64,7 +64,7 @@ class Position extends Vector3{ * @return bool */ public function isValid(){ - return $this->level !== null; + return $this->getLevel() !== null; } /** diff --git a/src/pocketmine/level/WeakPosition.php b/src/pocketmine/level/WeakPosition.php new file mode 100644 index 000000000..99064bfe8 --- /dev/null +++ b/src/pocketmine/level/WeakPosition.php @@ -0,0 +1,78 @@ +x = $x; + $this->y = $y; + $this->z = $z; + $this->levelId = ($level !== null ? $level->getId() : -1); + } + + public static function fromObject(Vector3 $pos, Level $level = null){ + return new WeakPosition($pos->x, $pos->y, $pos->z, $level); + } + + /** + * @return Level + */ + public function getLevel(){ + return Server::getInstance()->getLevel($this->levelId); + } + + public function setLevel(Level $level){ + $this->levelId = ($level !== null ? $level->getId() : -1); + return $this; + } + + /** + * Returns a side Vector + * + * @param int $side + * @param int $step + * + * @return WeakPosition + * + * @throws LevelException + */ + public function getSide($side, $step = 1){ + assert($this->isValid()); + + return WeakPosition::fromObject(parent::getSide($side, $step), $this->level); + } + + public function __toString(){ + return "Weak" . parent::__toString(); + } +} \ No newline at end of file From 03003ffa502778e28953a4ead7826bbbf633cc4e Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 31 Oct 2016 14:05:50 +0000 Subject: [PATCH 2/2] Improved invalid spawnpoint checking --- src/pocketmine/Player.php | 13 ++++++++++--- src/pocketmine/level/Position.php | 2 +- src/pocketmine/level/WeakPosition.php | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index b486f558f..2d837414b 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -681,7 +681,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade * @return Position */ public function getSpawn(){ - if($this->spawnPosition instanceof Position and $this->spawnPosition->getLevel() instanceof Level){ + if($this->hasValidSpawnPosition()){ return $this->spawnPosition; }else{ $level = $this->server->getDefaultLevel(); @@ -690,6 +690,13 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade } } + /** + * @return bool + */ + public function hasValidSpawnPosition() : bool{ + return $this->spawnPosition instanceof WeakPosition and $this->spawnPosition->isValid(); + } + public function sendChunk($x, $z, $payload, $ordering = FullChunkDataPacket::ORDER_COLUMNS){ if($this->connected === false){ return; @@ -1678,7 +1685,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade $this->dataPacket(new ResourcePacksInfoPacket()); - if($this->spawnPosition === null and isset($this->namedtag->SpawnLevel) and ($level = $this->server->getLevelByName($this->namedtag["SpawnLevel"])) instanceof Level){ + if(!$this->hasValidSpawnPosition() and isset($this->namedtag->SpawnLevel) and ($level = $this->server->getLevelByName($this->namedtag["SpawnLevel"])) instanceof Level){ $this->spawnPosition = new WeakPosition($this->namedtag["SpawnX"], $this->namedtag["SpawnY"], $this->namedtag["SpawnZ"], $level); } @@ -3082,7 +3089,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade parent::saveNBT(); if($this->level instanceof Level){ $this->namedtag->Level = new StringTag("Level", $this->level->getName()); - if($this->spawnPosition instanceof WeakPosition and $this->spawnPosition->isValid()){ + if($this->hasValidSpawnPosition()){ $this->namedtag["SpawnLevel"] = $this->spawnPosition->getLevel()->getName(); $this->namedtag["SpawnX"] = (int) $this->spawnPosition->x; $this->namedtag["SpawnY"] = (int) $this->spawnPosition->y; diff --git a/src/pocketmine/level/Position.php b/src/pocketmine/level/Position.php index 92e0d74fd..78f7b15f7 100644 --- a/src/pocketmine/level/Position.php +++ b/src/pocketmine/level/Position.php @@ -64,7 +64,7 @@ class Position extends Vector3{ * @return bool */ public function isValid(){ - return $this->getLevel() !== null; + return $this->getLevel() instanceof Level; } /** diff --git a/src/pocketmine/level/WeakPosition.php b/src/pocketmine/level/WeakPosition.php index 99064bfe8..b8808ad48 100644 --- a/src/pocketmine/level/WeakPosition.php +++ b/src/pocketmine/level/WeakPosition.php @@ -45,7 +45,7 @@ class WeakPosition extends Position{ } /** - * @return Level + * @return Level|null */ public function getLevel(){ return Server::getInstance()->getLevel($this->levelId);