Skip to content

Commit 0b2b912

Browse files
committed
Improved tile spawning
There's no need to recreate the spawn packet for every single player, or re-serialize the NBT.
1 parent f4f2323 commit 0b2b912

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/pocketmine/tile/Spawnable.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,24 @@
3131

3232
abstract class Spawnable extends Tile{
3333

34-
public function spawnTo(Player $player){
35-
if($this->closed){
36-
return false;
37-
}
38-
34+
public function createSpawnPacket() : BlockEntityDataPacket{
3935
$nbt = new NBT(NBT::LITTLE_ENDIAN);
4036
$nbt->setData($this->getSpawnCompound());
4137
$pk = new BlockEntityDataPacket();
4238
$pk->x = $this->x;
4339
$pk->y = $this->y;
4440
$pk->z = $this->z;
4541
$pk->namedtag = $nbt->write(true);
46-
$player->dataPacket($pk);
42+
43+
return $pk;
44+
}
45+
46+
public function spawnTo(Player $player){
47+
if($this->closed){
48+
return false;
49+
}
50+
51+
$player->dataPacket($this->createSpawnPacket());
4752

4853
return true;
4954
}
@@ -58,11 +63,8 @@ public function spawnToAll(){
5863
return;
5964
}
6065

61-
foreach($this->getLevel()->getChunkPlayers($this->chunk->getX(), $this->chunk->getZ()) as $player){
62-
if($player->spawned === true){
63-
$this->spawnTo($player);
64-
}
65-
}
66+
$pk = $this->createSpawnPacket();
67+
$this->level->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $pk);
6668
}
6769

6870
protected function onChanged(){

0 commit comments

Comments
 (0)