Improved tile spawning

There's no need to recreate the spawn packet for every single player, or re-serialize the NBT.
This commit is contained in:
Dylan K. Taylor 2017-08-27 18:40:18 +01:00
parent f4f2323518
commit 0b2b9126a2

View File

@ -31,11 +31,7 @@ use pocketmine\Player;
abstract class Spawnable extends Tile{
public function spawnTo(Player $player){
if($this->closed){
return false;
}
public function createSpawnPacket() : BlockEntityDataPacket{
$nbt = new NBT(NBT::LITTLE_ENDIAN);
$nbt->setData($this->getSpawnCompound());
$pk = new BlockEntityDataPacket();
@ -43,7 +39,16 @@ abstract class Spawnable extends Tile{
$pk->y = $this->y;
$pk->z = $this->z;
$pk->namedtag = $nbt->write(true);
$player->dataPacket($pk);
return $pk;
}
public function spawnTo(Player $player){
if($this->closed){
return false;
}
$player->dataPacket($this->createSpawnPacket());
return true;
}
@ -58,11 +63,8 @@ abstract class Spawnable extends Tile{
return;
}
foreach($this->getLevel()->getChunkPlayers($this->chunk->getX(), $this->chunk->getZ()) as $player){
if($player->spawned === true){
$this->spawnTo($player);
}
}
$pk = $this->createSpawnPacket();
$this->level->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $pk);
}
protected function onChanged(){