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()){
 
 
   87        $stream = new ByteBufferWriter();
 
   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);
 
  103        Byte::writeUnsigned($stream, 0); 
 
  107            $stream->writeByteArray($tiles);
 
  109            $stream->writeByteArray(self::serializeTiles($chunk));
 
  111        return $stream->getData();
 
 
  114    public static function serializeSubChunk(SubChunk $subChunk, BlockTranslator $blockTranslator, ByteBufferWriter $stream, 
bool $persistentBlockStates) : void{
 
  115        $layers = $subChunk->getBlockLayers();
 
  116        Byte::writeUnsigned($stream, 8); 
 
  118        Byte::writeUnsigned($stream, count($layers));
 
  120        $blockStateDictionary = $blockTranslator->getBlockStateDictionary();
 
  122        foreach($layers as $blocks){
 
  123            $bitsPerBlock = $blocks->getBitsPerBlock();
 
  124            $words = $blocks->getWordArray();
 
  125            Byte::writeUnsigned($stream, ($bitsPerBlock << 1) | ($persistentBlockStates ? 0 : 1));
 
  126            $stream->writeByteArray($words);
 
  127            $palette = $blocks->getPalette();
 
  129            if($bitsPerBlock !== 0){
 
  130                VarInt::writeSignedInt($stream, count($palette)); 
 
  132            if($persistentBlockStates){
 
  133                $nbtSerializer = 
new NetworkNbtSerializer();
 
  134                foreach($palette as $p){
 
  136                    $state = $blockStateDictionary->generateDataFromStateId($blockTranslator->internalIdToNetworkId($p));
 
  138                        $state = $blockTranslator->getFallbackStateData();
 
  141                    $stream->writeByteArray($nbtSerializer->write(
new TreeRoot($state->toNbt())));
 
  146                foreach($palette as $p){
 
  147                    VarInt::writeSignedInt($stream, $blockTranslator->internalIdToNetworkId($p));
 
  153    private static function serializeBiomePalette(PalettedBlockArray $biomePalette, LegacyBiomeIdToStringIdMap $biomeIdMap, ByteBufferWriter $stream) : void{
 
  154        $biomePaletteBitsPerBlock = $biomePalette->getBitsPerBlock();
 
  155        Byte::writeUnsigned($stream, ($biomePaletteBitsPerBlock << 1) | 1); 
 
  156        $stream->writeByteArray($biomePalette->getWordArray());
 
  158        $biomePaletteArray = $biomePalette->getPalette();
 
  159        if($biomePaletteBitsPerBlock !== 0){
 
  160            VarInt::writeSignedInt($stream, count($biomePaletteArray));
 
  163        foreach($biomePaletteArray as $p){
 
  166            VarInt::writeSignedInt($stream, $biomeIdMap->legacyToString($p) !== 
null ? $p : BiomeIds::OCEAN);
 
  170    public static function serializeTiles(Chunk $chunk) : string{
 
  171        $stream = new ByteBufferWriter();
 
  172        foreach($chunk->getTiles() as $tile){
 
  173            if($tile instanceof Spawnable){
 
  174                $stream->writeByteArray($tile->getSerializedSpawnCompound()->getEncodedNbt());
 
  178        return $stream->getData();