Chunk: move protocol-specific getSubChunkSendCount() to network chunk serializer

This commit is contained in:
Dylan K. Taylor 2019-10-23 18:34:47 +01:00
parent 23e2e7c320
commit eedc9eaee1
3 changed files with 20 additions and 18 deletions

View File

@ -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));
}

View File

@ -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

View File

@ -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
*/