diff --git a/src/world/World.php b/src/world/World.php index 02c11558c..f649a27d7 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -172,6 +172,9 @@ class World implements ChunkManager{ /** @var Player[][] */ private $playerChunkListeners = []; + /** @var ClientboundPacket[][] */ + private $chunkPackets = []; + /** @var float[] */ private $unloadQueue = []; @@ -522,7 +525,11 @@ class World implements ChunkManager{ * Broadcasts a packet to every player who has the target position within their view distance. */ public function broadcastPacketToViewers(Vector3 $pos, ClientboundPacket $packet) : void{ - $this->server->broadcastPackets($this->getChunkPlayers($pos->getFloorX() >> 4, $pos->getFloorZ() >> 4), [$packet]); + if(!isset($this->chunkPackets[$index = World::chunkHash($pos->getFloorX() >> 4, $pos->getFloorZ() >> 4)])){ + $this->chunkPackets[$index] = [$packet]; + }else{ + $this->chunkPackets[$index][] = $packet; + } } public function registerChunkLoader(ChunkLoader $loader, int $chunkX, int $chunkZ, bool $autoLoad = true) : void{ @@ -768,6 +775,16 @@ class World implements ChunkManager{ if($this->sleepTicks > 0 and --$this->sleepTicks <= 0){ $this->checkSleep(); } + + foreach($this->chunkPackets as $index => $entries){ + World::getXZ($index, $chunkX, $chunkZ); + $chunkPlayers = $this->getChunkPlayers($chunkX, $chunkZ); + if(count($chunkPlayers) > 0){ + $this->server->broadcastPackets($chunkPlayers, $entries); + } + } + + $this->chunkPackets = []; } public function checkSleep() : void{