22declare(strict_types=1);
24namespace pocketmine\network\mcpe\serializer;
37use pocketmine\world\format\PalettedBlockArray;
42 private function __construct(){
55 return match($dimensionId){
56 DimensionIds::OVERWORLD => [-4, 19],
57 DimensionIds::NETHER => [0, 7],
58 DimensionIds::THE_END => [0, 15],
59 default =>
throw new \InvalidArgumentException(
"Unknown dimension ID $dimensionId"),
72 [$minSubChunkIndex, $maxSubChunkIndex] = self::getDimensionChunkBounds($dimensionId);
73 for($y = $maxSubChunkIndex, $count = $maxSubChunkIndex - $minSubChunkIndex + 1; $y >= $minSubChunkIndex; --$y, --$count){
74 if($chunk->getSubChunk($y)->isEmptyFast()){
89 $subChunkCount = self::getSubChunkCount($chunk, $dimensionId);
92 [$minSubChunkIndex, $maxSubChunkIndex] = self::getDimensionChunkBounds($dimensionId);
93 for($y = $minSubChunkIndex; $writtenCount < $subChunkCount; ++$y, ++$writtenCount){
94 self::serializeSubChunk($chunk->getSubChunk($y), $blockTranslator, $stream,
false);
97 $biomeIdMap = LegacyBiomeIdToStringIdMap::getInstance();
99 for($y = $minSubChunkIndex; $y <= $maxSubChunkIndex; ++$y){
100 self::serializeBiomePalette($chunk->getSubChunk($y)->getBiomeArray(), $biomeIdMap, $stream);
107 $stream->put($tiles);
109 $stream->put(self::serializeTiles($chunk));
111 return $stream->getBuffer();
114 public static function serializeSubChunk(SubChunk $subChunk, BlockTranslator $blockTranslator, PacketSerializer $stream,
bool $persistentBlockStates) : void{
115 $layers = $subChunk->getBlockLayers();
118 $stream->putByte(count($layers));
120 $blockStateDictionary = $blockTranslator->getBlockStateDictionary();
122 foreach($layers as $blocks){
123 $bitsPerBlock = $blocks->getBitsPerBlock();
124 $words = $blocks->getWordArray();
125 $stream->putByte(($bitsPerBlock << 1) | ($persistentBlockStates ? 0 : 1));
126 $stream->put($words);
127 $palette = $blocks->getPalette();
129 if($bitsPerBlock !== 0){
133 $stream->putUnsignedVarInt(count($palette) << 1);
135 if($persistentBlockStates){
136 $nbtSerializer =
new NetworkNbtSerializer();
137 foreach($palette as $p){
139 $state = $blockStateDictionary->generateDataFromStateId($blockTranslator->internalIdToNetworkId($p));
141 $state = $blockTranslator->getFallbackStateData();
144 $stream->put($nbtSerializer->write(
new TreeRoot($state->toNbt())));
147 foreach($palette as $p){
148 $stream->put(Binary::writeUnsignedVarInt($blockTranslator->internalIdToNetworkId($p) << 1));
154 private static function serializeBiomePalette(PalettedBlockArray $biomePalette, LegacyBiomeIdToStringIdMap $biomeIdMap, PacketSerializer $stream) : void{
155 $biomePaletteBitsPerBlock = $biomePalette->getBitsPerBlock();
156 $stream->putByte(($biomePaletteBitsPerBlock << 1) | 1);
157 $stream->put($biomePalette->getWordArray());
162 $biomePaletteArray = $biomePalette->getPalette();
163 if($biomePaletteBitsPerBlock !== 0){
164 $stream->putUnsignedVarInt(count($biomePaletteArray) << 1);
167 foreach($biomePaletteArray as $p){
168 if($biomeIdMap->legacyToString($p) ===
null){
170 $p = BiomeIds::OCEAN;
172 $stream->put(Binary::writeUnsignedVarInt($p << 1));
176 public static function serializeTiles(Chunk $chunk) : string{
177 $stream = new BinaryStream();
178 foreach($chunk->getTiles() as $tile){
179 if($tile instanceof Spawnable){
180 $stream->put($tile->getSerializedSpawnCompound()->getEncodedNbt());
184 return $stream->getBuffer();
static serializeFullChunk(Chunk $chunk, int $dimensionId, BlockTranslator $blockTranslator, ?string $tiles=null)
static getSubChunkCount(Chunk $chunk, int $dimensionId)
static getDimensionChunkBounds(int $dimensionId)