Improved invalid spawnpoint checking

This commit is contained in:
Dylan K. Taylor 2016-10-31 14:05:50 +00:00
parent 7a1cdf88e8
commit 03003ffa50
3 changed files with 12 additions and 5 deletions

View File

@ -681,7 +681,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
* @return Position * @return Position
*/ */
public function getSpawn(){ public function getSpawn(){
if($this->spawnPosition instanceof Position and $this->spawnPosition->getLevel() instanceof Level){ if($this->hasValidSpawnPosition()){
return $this->spawnPosition; return $this->spawnPosition;
}else{ }else{
$level = $this->server->getDefaultLevel(); $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){ public function sendChunk($x, $z, $payload, $ordering = FullChunkDataPacket::ORDER_COLUMNS){
if($this->connected === false){ if($this->connected === false){
return; return;
@ -1678,7 +1685,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->dataPacket(new ResourcePacksInfoPacket()); $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); $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(); parent::saveNBT();
if($this->level instanceof Level){ if($this->level instanceof Level){
$this->namedtag->Level = new StringTag("Level", $this->level->getName()); $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["SpawnLevel"] = $this->spawnPosition->getLevel()->getName();
$this->namedtag["SpawnX"] = (int) $this->spawnPosition->x; $this->namedtag["SpawnX"] = (int) $this->spawnPosition->x;
$this->namedtag["SpawnY"] = (int) $this->spawnPosition->y; $this->namedtag["SpawnY"] = (int) $this->spawnPosition->y;

View File

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

View File

@ -45,7 +45,7 @@ class WeakPosition extends Position{
} }
/** /**
* @return Level * @return Level|null
*/ */
public function getLevel(){ public function getLevel(){
return Server::getInstance()->getLevel($this->levelId); return Server::getInstance()->getLevel($this->levelId);