From 0b2b9126a2164036788a0c90b84239844830139b Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 27 Aug 2017 18:40:18 +0100 Subject: [PATCH] Improved tile spawning There's no need to recreate the spawn packet for every single player, or re-serialize the NBT. --- src/pocketmine/tile/Spawnable.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/pocketmine/tile/Spawnable.php b/src/pocketmine/tile/Spawnable.php index f77e81d71..6a49b642b 100644 --- a/src/pocketmine/tile/Spawnable.php +++ b/src/pocketmine/tile/Spawnable.php @@ -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(){