Files
PocketMine-MP/src/world/format/io/region/McRegion.php
2020-07-05 21:01:13 +01:00

118 lines
3.8 KiB
PHP

<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\world\format\io\region;
use pocketmine\block\BlockLegacyIds;
use pocketmine\nbt\BigEndianNbtSerializer;
use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\ByteArrayTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntArrayTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\world\format\BiomeArray;
use pocketmine\world\format\Chunk;
use pocketmine\world\format\io\ChunkUtils;
use pocketmine\world\format\io\exception\CorruptedChunkException;
use pocketmine\world\format\io\SubChunkConverter;
use pocketmine\world\format\SubChunk;
use function str_repeat;
use function zlib_decode;
class McRegion extends RegionWorldProvider{
/**
* @throws \RuntimeException
*/
protected function serializeChunk(Chunk $chunk) : string{
throw new \RuntimeException("Unsupported");
}
/**
* @throws CorruptedChunkException
*/
protected function deserializeChunk(string $data) : Chunk{
$decompressed = @zlib_decode($data);
if($decompressed === false){
throw new CorruptedChunkException("Failed to decompress chunk NBT");
}
$nbt = new BigEndianNbtSerializer();
try{
$chunk = $nbt->read($decompressed)->mustGetCompoundTag();
}catch(NbtDataException $e){
throw new CorruptedChunkException($e->getMessage(), 0, $e);
}
$chunk = $chunk->getTag("Level");
if(!($chunk instanceof CompoundTag)){
throw new CorruptedChunkException("'Level' key is missing from chunk NBT");
}
$subChunks = [];
$fullIds = $chunk->hasTag("Blocks", ByteArrayTag::class) ? $chunk->getByteArray("Blocks") : str_repeat("\x00", 32768);
$fullData = $chunk->hasTag("Data", ByteArrayTag::class) ? $chunk->getByteArray("Data") : str_repeat("\x00", 16384);
for($y = 0; $y < 8; ++$y){
$subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, [SubChunkConverter::convertSubChunkFromLegacyColumn($fullIds, $fullData, $y)]);
}
$makeBiomeArray = function(string $biomeIds) : BiomeArray{
try{
return new BiomeArray($biomeIds);
}catch(\InvalidArgumentException $e){
throw new CorruptedChunkException($e->getMessage(), 0, $e);
}
};
$biomeIds = null;
if($chunk->hasTag("BiomeColors", IntArrayTag::class)){
$biomeIds = $makeBiomeArray(ChunkUtils::convertBiomeColors($chunk->getIntArray("BiomeColors"))); //Convert back to original format
}elseif($chunk->hasTag("Biomes", ByteArrayTag::class)){
$biomeIds = $makeBiomeArray($chunk->getByteArray("Biomes"));
}
$result = new Chunk(
$chunk->getInt("xPos"),
$chunk->getInt("zPos"),
$subChunks,
($entitiesTag = $chunk->getTag("Entities")) instanceof ListTag ? self::getCompoundList("Entities", $entitiesTag) : [],
($tilesTag = $chunk->getTag("TileEntities")) instanceof ListTag ? self::getCompoundList("TileEntities", $tilesTag) : [],
$biomeIds
);
$result->setPopulated($chunk->getByte("TerrainPopulated", 0) !== 0);
$result->setGenerated(true);
return $result;
}
protected static function getRegionFileExtension() : string{
return "mcr";
}
protected static function getPcWorldFormatVersion() : int{
return 19132;
}
public function getWorldHeight() : int{
//TODO: add world height options
return 128;
}
}