42 private const FLAG_POPULATED = 1 << 1;
44 private function __construct(){
48 private static function serializePalettedArray(
BinaryStream $stream, PalettedBlockArray $array) :
void{
49 $wordArray = $array->getWordArray();
50 $palette = $array->getPalette();
52 $stream->putByte($array->getBitsPerBlock());
53 $stream->put($wordArray);
54 $serialPalette = pack(
"L*", ...$palette);
55 $stream->putInt(strlen($serialPalette));
56 $stream->put($serialPalette);
66 ($chunk->isPopulated() ? self::FLAG_POPULATED : 0)
71 $count = count($subChunks);
72 $stream->putByte($count);
74 foreach($subChunks as $y => $subChunk){
76 $stream->putInt($subChunk->getEmptyBlockId());
77 $layers = $subChunk->getBlockLayers();
78 $stream->putByte(count($layers));
79 foreach($layers as $blocks){
80 self::serializePalettedArray($stream, $blocks);
82 self::serializePalettedArray($stream, $subChunk->getBiomeArray());
86 return $stream->getBuffer();
89 private static function deserializePalettedArray(BinaryStream $stream) : PalettedBlockArray{
90 $bitsPerBlock = $stream->getByte();
91 $words = $stream->get(PalettedBlockArray::getExpectedWordArraySize($bitsPerBlock));
93 $unpackedPalette = unpack(
"L*", $stream->get($stream->getInt()));
94 $palette = array_values($unpackedPalette);
96 return PalettedBlockArray::fromData($bitsPerBlock, $words, $palette);
106 $terrainPopulated = (bool) ($flags & self::FLAG_POPULATED);
110 $count = $stream->getByte();
111 for($subCount = 0; $subCount < $count; ++$subCount){
112 $y = Binary::signByte($stream->getByte());
113 $airBlockId = $stream->getInt();
117 for($i = 0, $layerCount = $stream->getByte(); $i < $layerCount; ++$i){
118 $layers[] = self::deserializePalettedArray($stream);
120 $biomeArray = self::deserializePalettedArray($stream);
121 $subChunks[$y] =
new SubChunk($airBlockId, $layers, $biomeArray);
124 return new Chunk($subChunks, $terrainPopulated);