76 protected const FINALISATION_NEEDS_INSTATICKING = 0;
77 protected const FINALISATION_NEEDS_POPULATION = 1;
78 protected const FINALISATION_DONE = 2;
80 protected const ENTRY_FLAT_WORLD_LAYERS =
"game_flatworldlayers";
82 protected const CURRENT_LEVEL_CHUNK_VERSION = WorldDataVersions::CHUNK;
83 protected const CURRENT_LEVEL_SUBCHUNK_VERSION = WorldDataVersions::SUBCHUNK;
85 private const CAVES_CLIFFS_EXPERIMENTAL_SUBCHUNK_KEY_OFFSET = 4;
87 protected \LevelDB $db;
89 private static function checkForLevelDBExtension() :
void{
90 if(!extension_loaded(
'leveldb')){
94 if(!defined(
'LEVELDB_ZLIB_RAW_COMPRESSION')){
102 private static function createDB(
string $path) : \
LevelDB{
103 return new \LevelDB(Path::join($path,
"db"), [
104 "compression" => LEVELDB_ZLIB_RAW_COMPRESSION,
105 "block_size" => 64 * 1024
109 public function __construct(
string $path, \
Logger $logger){
110 self::checkForLevelDBExtension();
111 parent::__construct($path, $logger);
114 $this->db = self::createDB($path);
115 }
catch(\LevelDBException $e){
133 public static function isValid(
string $path) : bool{
134 return file_exists(Path::join($path,
"level.dat")) && is_dir(Path::join($path,
"db"));
137 public static function generate(
string $path,
string $name,
WorldCreationOptions $options) : void{
138 self::checkForLevelDBExtension();
140 $dbPath = Path::join($path,
"db");
141 if(!file_exists($dbPath)){
142 mkdir($dbPath, 0777,
true);
145 BedrockWorldData::generate($path, $name, $options);
152 $bitsPerBlock = $stream->getByte() >> 1;
155 $words = $stream->
get(PalettedBlockArray::getExpectedWordArraySize($bitsPerBlock));
156 }
catch(\InvalidArgumentException $e){
159 $nbt =
new LittleEndianNbtSerializer();
162 if($bitsPerBlock === 0){
175 $offset = $stream->getOffset();
177 $stream->setOffset($offset);
179 if($byte1 !== NBT::TAG_Compound){
180 $susLength = $stream->
getLInt();
181 if($susLength !== 1){
184 $logger->
error(
"Unexpected palette size for 0 bpb palette");
187 $paletteSize = $stream->
getLInt();
190 $blockDecodeErrors = [];
192 for($i = 0; $i < $paletteSize; ++$i){
194 $offset = $stream->getOffset();
195 $blockStateNbt = $nbt->read($stream->getBuffer(), $offset)->mustGetCompoundTag();
196 $stream->setOffset($offset);
197 }
catch(NbtDataException $e){
199 throw new CorruptedChunkException(
"Invalid blockstate NBT at offset $i in paletted storage: " . $e->getMessage(), 0, $e);
204 $blockStateData = $this->blockDataUpgrader->upgradeBlockStateNbt($blockStateNbt);
205 }
catch(BlockStateDeserializeException $e){
207 $blockDecodeErrors[] =
"Palette offset $i / Upgrade error: " . $e->getMessage() .
", NBT: " . $blockStateNbt->toString();
208 $palette[] = $this->blockStateDeserializer->deserialize(GlobalBlockStateHandlers::getUnknownBlockStateData());
212 $palette[] = $this->blockStateDeserializer->deserialize($blockStateData);
213 }
catch(UnsupportedBlockStateException $e){
214 $blockDecodeErrors[] =
"Palette offset $i / " . $e->getMessage();
215 $palette[] = $this->blockStateDeserializer->deserialize(GlobalBlockStateHandlers::getUnknownBlockStateData());
216 }
catch(BlockStateDeserializeException $e){
217 $blockDecodeErrors[] =
"Palette offset $i / Deserialize error: " . $e->getMessage() .
", NBT: " . $blockStateNbt->toString();
218 $palette[] = $this->blockStateDeserializer->deserialize(GlobalBlockStateHandlers::getUnknownBlockStateData());
222 if(count($blockDecodeErrors) > 0){
223 $logger->
error(
"Errors decoding blocks:\n - " . implode(
"\n - ", $blockDecodeErrors));
227 return PalettedBlockArray::fromData($bitsPerBlock, $words, $palette);
230 private function serializeBlockPalette(BinaryStream $stream, PalettedBlockArray $blocks) : void{
231 $stream->putByte($blocks->getBitsPerBlock() << 1);
232 $stream->put($blocks->getWordArray());
234 $palette = $blocks->getPalette();
235 if($blocks->getBitsPerBlock() !== 0){
236 $stream->putLInt(count($palette));
239 foreach($palette as $p){
240 $tags[] = new TreeRoot($this->blockStateSerializer->serialize($p)->toNbt());
243 $stream->put((
new LittleEndianNbtSerializer())->writeMultiple($tags));
249 private static function getExpected3dBiomesCount(
int $chunkVersion) : int{
251 $chunkVersion >= ChunkVersion::v1_18_30 => 24,
252 $chunkVersion >= ChunkVersion::v1_18_0_25_beta => 25,
253 $chunkVersion >= ChunkVersion::v1_18_0_24_beta => 32,
254 $chunkVersion >= ChunkVersion::v1_18_0_22_beta => 65,
255 $chunkVersion >= ChunkVersion::v1_17_40_20_beta_experimental_caves_cliffs => 32,
256 default =>
throw new CorruptedChunkException(
"Chunk version $chunkVersion should not have 3D biomes")
263 private static function deserializeBiomePalette(BinaryStream $stream,
int $bitsPerBlock) : PalettedBlockArray{
265 $words = $stream->get(PalettedBlockArray::getExpectedWordArraySize($bitsPerBlock));
266 }
catch(\InvalidArgumentException $e){
267 throw new CorruptedChunkException(
"Failed to deserialize paletted biomes: " . $e->getMessage(), 0, $e);
270 $paletteSize = $bitsPerBlock === 0 ? 1 : $stream->getLInt();
272 for($i = 0; $i < $paletteSize; ++$i){
273 $palette[] = $stream->getLInt();
277 return PalettedBlockArray::fromData($bitsPerBlock, $words, $palette);
280 private static function serializeBiomePalette(BinaryStream $stream, PalettedBlockArray $biomes) : void{
281 $stream->putByte($biomes->getBitsPerBlock() << 1);
282 $stream->put($biomes->getWordArray());
284 $palette = $biomes->getPalette();
285 if($biomes->getBitsPerBlock() !== 0){
286 $stream->putLInt(count($palette));
288 foreach($palette as $p){
289 $stream->putLInt($p);
298 private static function deserialize3dBiomes(BinaryStream $stream,
int $chunkVersion, \
Logger $logger) : array{
301 $nextIndex = Chunk::MIN_SUBCHUNK_INDEX;
303 $expectedCount = self::getExpected3dBiomesCount($chunkVersion);
304 for($i = 0; $i < $expectedCount; ++$i){
306 $bitsPerBlock = $stream->getByte() >> 1;
307 if($bitsPerBlock === 127){
308 if($previous ===
null){
309 throw new CorruptedChunkException(
"Serialized biome palette $i has no previous palette to copy from");
311 $decoded = clone $previous;
313 $decoded = self::deserializeBiomePalette($stream, $bitsPerBlock);
315 $previous = $decoded;
316 if($nextIndex <= Chunk::MAX_SUBCHUNK_INDEX){
317 $result[$nextIndex++] = $decoded;
318 }elseif($stream->feof()){
320 $logger->
error(
"Wrong number of 3D biome palettes for this chunk version: expected $expectedCount, but got " . ($i + 1) .
" - this is not a problem, but may indicate a corrupted chunk");
323 }
catch(BinaryDataException $e){
324 throw new CorruptedChunkException(
"Failed to deserialize biome palette $i: " . $e->getMessage(), 0, $e);
327 if(!$stream->feof()){
329 $logger->
error(
"Unexpected trailing data after 3D biomes data");
338 private static function serialize3dBiomes(BinaryStream $stream, array $subChunks) : void{
340 for($y = Chunk::MIN_SUBCHUNK_INDEX; $y <= Chunk::MAX_SUBCHUNK_INDEX; $y++){
343 self::serializeBiomePalette($stream, $subChunks[$y]->getBiomeArray());
354 $x = ($key >> 12) & 0xf;
355 $z = ($key >> 8) & 0xf;
358 $x = ($key >> 11) & 0xf;
359 $z = ($key >> 7) & 0xf;
368 if(($extraRawData = $this->db->get($index .
ChunkDataKey::LEGACY_BLOCK_EXTRA_DATA)) === false || $extraRawData ===
""){
373 $extraDataLayers = [];
375 $count = $binaryStream->getLInt();
377 for($i = 0; $i < $count; ++$i){
378 $key = $binaryStream->getLInt();
379 $value = $binaryStream->getLShort();
381 self::deserializeExtraDataKey($chunkVersion, $key, $x, $fullY, $z);
383 $ySub = ($fullY >> SubChunk::COORD_BIT_SIZE);
384 $y = $key & SubChunk::COORD_MASK;
386 $blockId = $value & 0xff;
387 $blockData = ($value >> 8) & 0xf;
389 $blockStateData = $this->blockDataUpgrader->upgradeIntIdMeta($blockId, $blockData);
393 $logger->
error(
"Failed to upgrade legacy extra block: " . $e->getMessage() .
" ($blockId:$blockData)");
397 $blockStateId = $this->blockStateDeserializer->deserialize($blockStateData);
399 if(!isset($extraDataLayers[$ySub])){
400 $extraDataLayers[$ySub] =
new PalettedBlockArray(Block::EMPTY_STATE_ID);
402 $extraDataLayers[$ySub]->set($x, $y, $z, $blockStateId);
405 return $extraDataLayers;
408 private function readVersion(
int $chunkX,
int $chunkZ) : ?int{
409 $index = self::chunkIndex($chunkX, $chunkZ);
410 $chunkVersionRaw = $this->db->get($index . ChunkDataKey::NEW_VERSION);
411 if($chunkVersionRaw ===
false){
412 $chunkVersionRaw = $this->db->get($index . ChunkDataKey::OLD_VERSION);
413 if($chunkVersionRaw ===
false){
418 return ord($chunkVersionRaw);
428 private function deserializeLegacyTerrainData(
string $index,
int $chunkVersion, \
Logger $logger) : array{
429 $convertedLegacyExtraData = $this->deserializeLegacyExtraData($index, $chunkVersion, $logger);
431 $legacyTerrain = $this->db->get($index . ChunkDataKey::LEGACY_TERRAIN);
432 if($legacyTerrain ===
false){
433 throw new CorruptedChunkException(
"Missing expected LEGACY_TERRAIN tag for format version $chunkVersion");
435 $binaryStream =
new BinaryStream($legacyTerrain);
437 $fullIds = $binaryStream->get(32768);
438 $fullData = $binaryStream->get(16384);
439 $binaryStream->get(32768);
440 }
catch(BinaryDataException $e){
441 throw new CorruptedChunkException($e->getMessage(), 0, $e);
445 $binaryStream->get(256);
447 $unpackedBiomeArray = unpack(
"N*", $binaryStream->get(1024));
448 $biomes3d = ChunkUtils::extrapolate3DBiomes(ChunkUtils::convertBiomeColors(array_values($unpackedBiomeArray)));
449 }
catch(BinaryDataException $e){
450 throw new CorruptedChunkException($e->getMessage(), 0, $e);
452 if(!$binaryStream->feof()){
453 $logger->
error(
"Unexpected trailing data in legacy terrain data");
457 for($yy = 0; $yy < 8; ++$yy){
458 $storages = [$this->palettizeLegacySubChunkFromColumn($fullIds, $fullData, $yy,
new \
PrefixedLogger($logger,
"Subchunk y=$yy"))];
459 if(isset($convertedLegacyExtraData[$yy])){
460 $storages[] = $convertedLegacyExtraData[$yy];
462 $subChunks[$yy] =
new SubChunk(Block::EMPTY_STATE_ID, $storages, clone $biomes3d);
466 for($yy = Chunk::MIN_SUBCHUNK_INDEX; $yy <= Chunk::MAX_SUBCHUNK_INDEX; ++$yy){
467 if(!isset($subChunks[$yy])){
468 $subChunks[$yy] =
new SubChunk(Block::EMPTY_STATE_ID, [], clone $biomes3d);
478 private function deserializeNonPalettedSubChunkData(BinaryStream $binaryStream,
int $chunkVersion, ?PalettedBlockArray $convertedLegacyExtraData, PalettedBlockArray $biomePalette, \
Logger $logger) : SubChunk{
480 $blocks = $binaryStream->get(4096);
481 $blockData = $binaryStream->get(2048);
482 }
catch(BinaryDataException $e){
483 throw new CorruptedChunkException($e->getMessage(), 0, $e);
486 if($chunkVersion < ChunkVersion::v1_1_0){
488 $binaryStream->get(4096);
489 if(!$binaryStream->feof()){
490 $logger->
error(
"Unexpected trailing data in legacy subchunk data");
492 }
catch(BinaryDataException $e){
493 $logger->
error(
"Failed to read legacy subchunk light info: " . $e->getMessage());
497 $storages = [$this->palettizeLegacySubChunkXZY($blocks, $blockData, $logger)];
498 if($convertedLegacyExtraData !==
null){
499 $storages[] = $convertedLegacyExtraData;
502 return new SubChunk(Block::EMPTY_STATE_ID, $storages, $biomePalette);
511 private function deserializeSubChunkData(BinaryStream $binaryStream,
int $chunkVersion,
int $subChunkVersion, ?PalettedBlockArray $convertedLegacyExtraData, PalettedBlockArray $biomePalette, \
Logger $logger) : SubChunk{
512 switch($subChunkVersion){
513 case SubChunkVersion::CLASSIC:
514 case SubChunkVersion::CLASSIC_BUG_2:
515 case SubChunkVersion::CLASSIC_BUG_3:
516 case SubChunkVersion::CLASSIC_BUG_4:
517 case SubChunkVersion::CLASSIC_BUG_5:
518 case SubChunkVersion::CLASSIC_BUG_6:
519 case SubChunkVersion::CLASSIC_BUG_7:
520 return $this->deserializeNonPalettedSubChunkData($binaryStream, $chunkVersion, $convertedLegacyExtraData, $biomePalette, $logger);
521 case SubChunkVersion::PALETTED_SINGLE:
522 $storages = [$this->deserializeBlockPalette($binaryStream, $logger)];
523 if($convertedLegacyExtraData !==
null){
524 $storages[] = $convertedLegacyExtraData;
526 return new SubChunk(Block::EMPTY_STATE_ID, $storages, $biomePalette);
527 case SubChunkVersion::PALETTED_MULTI:
528 case SubChunkVersion::PALETTED_MULTI_WITH_OFFSET:
531 $storageCount = $binaryStream->getByte();
532 if($subChunkVersion >= SubChunkVersion::PALETTED_MULTI_WITH_OFFSET){
534 $binaryStream->getByte();
538 for($k = 0; $k < $storageCount; ++$k){
539 $storages[] = $this->deserializeBlockPalette($binaryStream, $logger);
541 return new SubChunk(Block::EMPTY_STATE_ID, $storages, $biomePalette);
544 throw new CorruptedChunkException(
"don't know how to decode LevelDB subchunk format version $subChunkVersion");
548 private static function hasOffsetCavesAndCliffsSubChunks(
int $chunkVersion) : bool{
549 return $chunkVersion >= ChunkVersion::v1_16_220_50_unused && $chunkVersion <= ChunkVersion::v1_16_230_50_unused;
565 private function deserializeAllSubChunkData(
string $index,
int $chunkVersion,
bool &$hasBeenUpgraded, array $convertedLegacyExtraData, array $biomeArrays, \
Logger $logger) : array{
568 $subChunkKeyOffset = self::hasOffsetCavesAndCliffsSubChunks($chunkVersion) ? self::CAVES_CLIFFS_EXPERIMENTAL_SUBCHUNK_KEY_OFFSET : 0;
569 for($y = Chunk::MIN_SUBCHUNK_INDEX; $y <= Chunk::MAX_SUBCHUNK_INDEX; ++$y){
570 if(($data = $this->db->get($index . ChunkDataKey::SUBCHUNK . chr($y + $subChunkKeyOffset))) ===
false){
571 $subChunks[$y] =
new SubChunk(Block::EMPTY_STATE_ID, [], $biomeArrays[$y]);
575 $binaryStream =
new BinaryStream($data);
576 if($binaryStream->feof()){
577 throw new CorruptedChunkException(
"Unexpected empty data for subchunk $y");
579 $subChunkVersion = $binaryStream->getByte();
580 if($subChunkVersion < self::CURRENT_LEVEL_SUBCHUNK_VERSION){
581 $hasBeenUpgraded =
true;
584 $subChunks[$y] = $this->deserializeSubChunkData(
588 $convertedLegacyExtraData[$y] ??
null,
603 private function deserializeBiomeData(
string $index,
int $chunkVersion, \
Logger $logger) : array{
605 if(($maps2d = $this->db->get($index . ChunkDataKey::HEIGHTMAP_AND_2D_BIOMES)) !==
false){
606 $binaryStream =
new BinaryStream($maps2d);
609 $binaryStream->get(512);
610 $biomes3d = ChunkUtils::extrapolate3DBiomes($binaryStream->get(256));
611 if(!$binaryStream->feof()){
612 $logger->
error(
"Unexpected trailing data after 2D biome data");
614 }
catch(BinaryDataException $e){
615 throw new CorruptedChunkException($e->getMessage(), 0, $e);
617 for($i = Chunk::MIN_SUBCHUNK_INDEX; $i <= Chunk::MAX_SUBCHUNK_INDEX; ++$i){
618 $biomeArrays[$i] = clone $biomes3d;
620 }elseif(($maps3d = $this->db->get($index . ChunkDataKey::HEIGHTMAP_AND_3D_BIOMES)) !==
false){
621 $binaryStream =
new BinaryStream($maps3d);
624 $binaryStream->get(512);
625 $biomeArrays = self::deserialize3dBiomes($binaryStream, $chunkVersion, $logger);
626 }
catch(BinaryDataException $e){
627 throw new CorruptedChunkException($e->getMessage(), 0, $e);
630 $logger->
error(
"Missing biome data, using default ocean biome");
631 for($i = Chunk::MIN_SUBCHUNK_INDEX; $i <= Chunk::MAX_SUBCHUNK_INDEX; ++$i){
632 $biomeArrays[$i] =
new PalettedBlockArray(BiomeIds::OCEAN);
643 $index =
LevelDB::chunkIndex($chunkX, $chunkZ);
645 $chunkVersion = $this->readVersion($chunkX, $chunkZ);
646 if($chunkVersion ===
null){
653 $logger = new \PrefixedLogger($this->logger,
"Loading chunk x=$chunkX z=$chunkZ v$chunkVersion");
655 $hasBeenUpgraded = $chunkVersion < self::CURRENT_LEVEL_CHUNK_VERSION;
657 switch($chunkVersion){
658 case ChunkVersion::v1_21_40:
660 case ChunkVersion::v1_18_30:
661 case ChunkVersion::v1_18_0_25_beta:
662 case ChunkVersion::v1_18_0_24_unused:
663 case ChunkVersion::v1_18_0_24_beta:
664 case ChunkVersion::v1_18_0_22_unused:
665 case ChunkVersion::v1_18_0_22_beta:
666 case ChunkVersion::v1_18_0_20_unused:
667 case ChunkVersion::v1_18_0_20_beta:
668 case ChunkVersion::v1_17_40_unused:
669 case ChunkVersion::v1_17_40_20_beta_experimental_caves_cliffs:
670 case ChunkVersion::v1_17_30_25_unused:
671 case ChunkVersion::v1_17_30_25_beta_experimental_caves_cliffs:
672 case ChunkVersion::v1_17_30_23_unused:
673 case ChunkVersion::v1_17_30_23_beta_experimental_caves_cliffs:
674 case ChunkVersion::v1_16_230_50_unused:
675 case ChunkVersion::v1_16_230_50_beta_experimental_caves_cliffs:
676 case ChunkVersion::v1_16_220_50_unused:
677 case ChunkVersion::v1_16_220_50_beta_experimental_caves_cliffs:
678 case ChunkVersion::v1_16_210:
679 case ChunkVersion::v1_16_100_57_beta:
680 case ChunkVersion::v1_16_100_52_beta:
681 case ChunkVersion::v1_16_0:
682 case ChunkVersion::v1_16_0_51_beta:
684 case ChunkVersion::v1_12_0_unused2:
685 case ChunkVersion::v1_12_0_unused1:
686 case ChunkVersion::v1_12_0_4_beta:
687 case ChunkVersion::v1_11_1:
688 case ChunkVersion::v1_11_0_4_beta:
689 case ChunkVersion::v1_11_0_3_beta:
690 case ChunkVersion::v1_11_0_1_beta:
691 case ChunkVersion::v1_9_0:
692 case ChunkVersion::v1_8_0:
693 case ChunkVersion::v1_2_13:
694 case ChunkVersion::v1_2_0:
695 case ChunkVersion::v1_2_0_2_beta:
696 case ChunkVersion::v1_1_0_converted_from_console:
697 case ChunkVersion::v1_1_0:
699 case ChunkVersion::v1_0_0:
700 $convertedLegacyExtraData = $this->deserializeLegacyExtraData($index, $chunkVersion, $logger);
701 $biomeArrays = $this->deserializeBiomeData($index, $chunkVersion, $logger);
702 $subChunks = $this->deserializeAllSubChunkData($index, $chunkVersion, $hasBeenUpgraded, $convertedLegacyExtraData, $biomeArrays, $logger);
704 case ChunkVersion::v0_9_5:
705 case ChunkVersion::v0_9_2:
706 case ChunkVersion::v0_9_0:
707 $subChunks = $this->deserializeLegacyTerrainData($index, $chunkVersion, $logger);
713 $nbt =
new LittleEndianNbtSerializer();
716 if(($entityData = $this->db->get($index . ChunkDataKey::ENTITIES)) !==
false && $entityData !==
""){
718 $entities = array_map(fn(TreeRoot $root) => $root->mustGetCompoundTag(), $nbt->readMultiple($entityData));
719 }
catch(NbtDataException $e){
725 if(($tileData = $this->db->get($index . ChunkDataKey::BLOCK_ENTITIES)) !==
false && $tileData !==
""){
727 $tiles = array_map(fn(TreeRoot $root) => $root->mustGetCompoundTag(), $nbt->readMultiple($tileData));
728 }
catch(NbtDataException $e){
729 throw new CorruptedChunkException($e->getMessage(), 0, $e);
733 $finalisationChr = $this->db->get($index . ChunkDataKey::FINALIZATION);
734 if($finalisationChr !==
false){
735 $finalisation = ord($finalisationChr);
736 $terrainPopulated = $finalisation === self::FINALISATION_DONE;
738 $terrainPopulated =
true;
743 return new LoadedChunkData(
744 data:
new ChunkData($subChunks, $terrainPopulated, $entities, $tiles),
745 upgraded: $hasBeenUpgraded,
746 fixerFlags: LoadedChunkData::FIXER_FLAG_ALL
751 $index =
LevelDB::chunkIndex($chunkX, $chunkZ);
753 $write = new \LevelDBWriteBatch();
755 $write->put($index . ChunkDataKey::NEW_VERSION, chr(self::CURRENT_LEVEL_CHUNK_VERSION));
756 $write->put($index . ChunkDataKey::PM_DATA_VERSION, Binary::writeLLong(VersionInfo::WORLD_DATA_VERSION));
760 if(($dirtyFlags & Chunk::DIRTY_FLAG_BLOCKS) !== 0){
762 foreach($subChunks as $y => $subChunk){
763 $key = $index . ChunkDataKey::SUBCHUNK . chr($y);
764 if($subChunk->isEmptyAuthoritative()){
765 $write->delete($key);
768 $subStream->putByte(self::CURRENT_LEVEL_SUBCHUNK_VERSION);
770 $layers = $subChunk->getBlockLayers();
771 $subStream->putByte(count($layers));
772 foreach($layers as $blocks){
773 $this->serializeBlockPalette($subStream, $blocks);
776 $write->put($key, $subStream->getBuffer());
781 if(($dirtyFlags & Chunk::DIRTY_FLAG_BIOMES) !== 0){
782 $write->delete($index . ChunkDataKey::HEIGHTMAP_AND_2D_BIOMES);
783 $stream =
new BinaryStream();
784 $stream->put(str_repeat(
"\x00", 512));
785 self::serialize3dBiomes($stream, $subChunks);
786 $write->put($index . ChunkDataKey::HEIGHTMAP_AND_3D_BIOMES, $stream->getBuffer());
790 $write->put($index . ChunkDataKey::FINALIZATION, chr($chunkData->isPopulated() ? self::FINALISATION_DONE : self::FINALISATION_NEEDS_POPULATION));
792 $this->writeTags($chunkData->
getTileNBT(), $index . ChunkDataKey::BLOCK_ENTITIES, $write);
793 $this->writeTags($chunkData->
getEntityNBT(), $index . ChunkDataKey::ENTITIES, $write);
795 $write->delete($index . ChunkDataKey::HEIGHTMAP_AND_2D_BIOME_COLORS);
796 $write->delete($index . ChunkDataKey::LEGACY_TERRAIN);
798 $this->db->write($write);
804 private function writeTags(array $targets,
string $index, \LevelDBWriteBatch $write) : void{
805 if(count($targets) > 0){
806 $nbt =
new LittleEndianNbtSerializer();
807 $write->put($index, $nbt->writeMultiple(array_map(fn(CompoundTag $tag) =>
new TreeRoot($tag), $targets)));
809 $write->delete($index);
813 public function getDatabase() : \LevelDB{
817 public static function chunkIndex(
int $chunkX,
int $chunkZ) : string{
818 return Binary::writeLInt($chunkX) . Binary::writeLInt($chunkZ);
825 public function close() : void{
830 foreach($this->db->getIterator() as $key => $_){
831 if(strlen($key) === 9 && ($key[8] === ChunkDataKey::NEW_VERSION || $key[8] === ChunkDataKey::OLD_VERSION)){
832 $chunkX = Binary::readLInt(substr($key, 0, 4));
833 $chunkZ = Binary::readLInt(substr($key, 4, 4));
835 if(($chunk = $this->loadChunk($chunkX, $chunkZ)) !==
null){
836 yield [$chunkX, $chunkZ] => $chunk;
842 if($logger !==
null){
843 $logger->
error(
"Skipped corrupted chunk $chunkX $chunkZ (" . $e->getMessage() .
")");
852 foreach($this->db->getIterator() as $key => $_){
853 if(strlen($key) === 9 && ($key[8] === ChunkDataKey::NEW_VERSION || $key[8] === ChunkDataKey::OLD_VERSION)){