Fixed bugs related to #24 such as getName() on null

This commit is contained in:
Dylan K. Taylor 2016-10-30 19:21:48 +00:00
parent 4856dbd1c6
commit 7a1cdf88e8
3 changed files with 84 additions and 5 deletions

View File

@ -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;

View File

@ -64,7 +64,7 @@ class Position extends Vector3{
* @return bool
*/
public function isValid(){
return $this->level !== null;
return $this->getLevel() !== null;
}
/**

View File

@ -0,0 +1,78 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\level;
use pocketmine\math\Vector3;
use pocketmine\Server;
use pocketmine\utils\LevelException;
class WeakPosition extends Position{
/**
* @param int $x
* @param int $y
* @param int $z
* @param Level $level
*/
public function __construct($x = 0, $y = 0, $z = 0, Level $level = null){
$this->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();
}
}