From eedc9eaee1b4e838d6e2e8976ecb77616db1f347 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 23 Oct 2019 18:34:47 +0100 Subject: [PATCH] Chunk: move protocol-specific getSubChunkSendCount() to network chunk serializer --- src/network/mcpe/ChunkRequestTask.php | 2 +- .../mcpe/serializer/ChunkSerializer.php | 20 ++++++++++++++++++- src/world/format/Chunk.php | 16 --------------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/network/mcpe/ChunkRequestTask.php b/src/network/mcpe/ChunkRequestTask.php index 1ac04e511..f95d669ab 100644 --- a/src/network/mcpe/ChunkRequestTask.php +++ b/src/network/mcpe/ChunkRequestTask.php @@ -61,7 +61,7 @@ class ChunkRequestTask extends AsyncTask{ public function onRun() : void{ $chunk = FastChunkSerializer::deserialize($this->chunk); - $subCount = $chunk->getSubChunkSendCount(); + $subCount = ChunkSerializer::getSubChunkCount($chunk); $payload = ChunkSerializer::serialize($chunk, $this->tiles); $this->setResult(Zlib::compress(PacketBatch::fromPackets(LevelChunkPacket::withoutCache($this->chunkX, $this->chunkZ, $subCount, $payload))->getBuffer(), $this->compressionLevel)); } diff --git a/src/network/mcpe/serializer/ChunkSerializer.php b/src/network/mcpe/serializer/ChunkSerializer.php index db4258bdf..d7c17a92b 100644 --- a/src/network/mcpe/serializer/ChunkSerializer.php +++ b/src/network/mcpe/serializer/ChunkSerializer.php @@ -35,6 +35,24 @@ final class ChunkSerializer{ //NOOP } + /** + * Returns the number of subchunks that will be sent from the given chunk. + * Chunks are sent in a stack, so every chunk below the top non-empty one must be sent. + * @param Chunk $chunk + * + * @return int + */ + public static function getSubChunkCount(Chunk $chunk) : int{ + for($count = $chunk->getSubChunks()->count(); $count > 0; --$count){ + if($chunk->getSubChunk($count - 1)->isEmptyFast()){ + continue; + } + break; + } + + return $count; + } + /** * @param Chunk $chunk * @@ -44,7 +62,7 @@ final class ChunkSerializer{ */ public static function serialize(Chunk $chunk, ?string $tiles = null) : string{ $stream = new NetworkBinaryStream(); - $subChunkCount = $chunk->getSubChunkSendCount(); + $subChunkCount = self::getSubChunkCount($chunk); for($y = 0; $y < $subChunkCount; ++$y){ $layers = $chunk->getSubChunk($y)->getBlockLayers(); $stream->putByte(8); //version diff --git a/src/world/format/Chunk.php b/src/world/format/Chunk.php index f7fbf4be1..b91263130 100644 --- a/src/world/format/Chunk.php +++ b/src/world/format/Chunk.php @@ -682,22 +682,6 @@ class Chunk{ return $this->subChunks; } - /** - * Returns the count of subchunks that need sending to players - * - * @return int - */ - public function getSubChunkSendCount() : int{ - for($count = $this->subChunks->count(); $count > 0; --$count){ - if($this->subChunks[$count - 1]->isEmptyFast()){ - continue; - } - break; - } - - return $count; - } - /** * Disposes of empty subchunks and frees data where possible */