Fixed some places entities/players use their chunk without checking if it is valid

This may be invalid in some cases, such as:
- chunk is not loaded
- entity is not fully constructed
- entity is a Player who has not yet completed the login sequence.
This commit is contained in:
Dylan K. Taylor 2018-01-02 12:57:04 +00:00
parent 9a956692de
commit c9e2e8980f
2 changed files with 20 additions and 14 deletions

View File

@ -2162,7 +2162,9 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
public function handleLevelSoundEvent(LevelSoundEventPacket $packet) : bool{ public function handleLevelSoundEvent(LevelSoundEventPacket $packet) : bool{
//TODO: add events so plugins can change this //TODO: add events so plugins can change this
$this->getLevel()->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $packet); if($this->chunk !== null){
$this->getLevel()->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $packet);
}
return true; return true;
} }

View File

@ -350,7 +350,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
public $passenger = null; public $passenger = null;
public $vehicle = null; public $vehicle = null;
/** @var Chunk */ /** @var Chunk|null */
public $chunk; public $chunk;
/** @var EntityDamageEvent|null */ /** @var EntityDamageEvent|null */
@ -1082,22 +1082,26 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
} }
protected function broadcastMovement(){ protected function broadcastMovement(){
$pk = new MoveEntityPacket(); if($this->chunk !== null){
$pk->entityRuntimeId = $this->id; $pk = new MoveEntityPacket();
$pk->position = $this->getOffsetPosition($this); $pk->entityRuntimeId = $this->id;
$pk->yaw = $this->yaw; $pk->position = $this->getOffsetPosition($this);
$pk->pitch = $this->pitch; $pk->yaw = $this->yaw;
$pk->headYaw = $this->yaw; //TODO $pk->pitch = $this->pitch;
$pk->headYaw = $this->yaw; //TODO
$this->level->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $pk); $this->level->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $pk);
}
} }
protected function broadcastMotion(){ protected function broadcastMotion(){
$pk = new SetEntityMotionPacket(); if($this->chunk !== null){
$pk->entityRuntimeId = $this->id; $pk = new SetEntityMotionPacket();
$pk->motion = $this->getMotion(); $pk->entityRuntimeId = $this->id;
$pk->motion = $this->getMotion();
$this->level->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $pk); $this->level->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $pk);
}
} }
protected function applyDragBeforeGravity() : bool{ protected function applyDragBeforeGravity() : bool{
@ -1879,7 +1883,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
* @param Player $player * @param Player $player
*/ */
public function spawnTo(Player $player){ public function spawnTo(Player $player){
if(!isset($this->hasSpawned[$player->getLoaderId()]) and isset($player->usedChunks[Level::chunkHash($this->chunk->getX(), $this->chunk->getZ())])){ if(!isset($this->hasSpawned[$player->getLoaderId()]) and $this->chunk !== null and isset($player->usedChunks[Level::chunkHash($this->chunk->getX(), $this->chunk->getZ())])){
$this->hasSpawned[$player->getLoaderId()] = $player; $this->hasSpawned[$player->getLoaderId()] = $player;
$this->sendSpawnPacket($player); $this->sendSpawnPacket($player);