Eliminate remaining usages of legacy block ID+meta on disk

flower pots loaded from vanilla worlds should now correctly display the plant inside
This commit is contained in:
Dylan K. Taylor
2022-06-05 21:49:51 +01:00
parent 02568bb049
commit 5c85aa6e58
5 changed files with 98 additions and 69 deletions

View File

@ -26,6 +26,7 @@ namespace pocketmine\entity\object;
use pocketmine\block\Block;
use pocketmine\block\BlockFactory;
use pocketmine\block\utils\Fallable;
use pocketmine\data\bedrock\blockstate\BlockStateDeserializeException;
use pocketmine\data\SavedDataLoadingException;
use pocketmine\entity\Entity;
use pocketmine\entity\EntitySizeInfo;
@ -40,9 +41,11 @@ use pocketmine\network\mcpe\convert\RuntimeBlockMapping;
use pocketmine\network\mcpe\protocol\types\entity\EntityIds;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
use pocketmine\world\format\io\GlobalBlockStateHandlers;
use function abs;
class FallingBlock extends Entity{
private const TAG_FALLING_BLOCK = "FallingBlock"; //TAG_Compound
public static function getNetworkTypeId() : string{ return EntityIds::FALLING_BLOCK; }
@ -60,22 +63,37 @@ class FallingBlock extends Entity{
protected function getInitialGravity() : float{ return 0.04; }
public static function parseBlockNBT(BlockFactory $factory, CompoundTag $nbt) : Block{
$blockId = 0;
//TODO: 1.8+ save format
if(($tileIdTag = $nbt->getTag("TileID")) instanceof IntTag){
$blockId = $tileIdTag->getValue();
}elseif(($tileTag = $nbt->getTag("Tile")) instanceof ByteTag){
$blockId = $tileTag->getValue();
if(($fallingBlockTag = $nbt->getCompoundTag(self::TAG_FALLING_BLOCK)) !== null){
try{
$blockStateData = GlobalBlockStateHandlers::nbtToBlockStateData($fallingBlockTag);
}catch(BlockStateDeserializeException $e){
throw new SavedDataLoadingException($e->getMessage(), 0, $e);
}
}else{
if(($tileIdTag = $nbt->getTag("TileID")) instanceof IntTag){
$blockId = $tileIdTag->getValue();
}elseif(($tileTag = $nbt->getTag("Tile")) instanceof ByteTag){
$blockId = $tileTag->getValue();
}else{
throw new SavedDataLoadingException("Missing legacy falling block info");
}
$damage = $nbt->getByte("Data", 0);
$blockStateData = GlobalBlockStateHandlers::getLegacyBlockStateMapper()->fromIntIdMeta($blockId, $damage);
if($blockStateData === null){
throw new SavedDataLoadingException("Invalid legacy falling block");
}
}
if($blockId === 0){
throw new SavedDataLoadingException("Missing block info from NBT");
try{
$blockStateId = GlobalBlockStateHandlers::getDeserializer()->deserialize($blockStateData);
}catch(BlockStateDeserializeException $e){
throw new SavedDataLoadingException($e->getMessage(), 0, $e);
}
$damage = $nbt->getByte("Data", 0);
return $factory->get($blockId, $damage);
return $factory->fromFullBlock($blockStateId);
}
public function canCollideWith(Entity $entity) : bool{
@ -137,8 +155,7 @@ class FallingBlock extends Entity{
public function saveNBT() : CompoundTag{
$nbt = parent::saveNBT();
$nbt->setInt("TileID", $this->block->getId());
$nbt->setByte("Data", $this->block->getMeta());
$nbt->setTag(self::TAG_FALLING_BLOCK, GlobalBlockStateHandlers::getSerializer()->serialize($this->block->getFullId())->toNbt());
return $nbt;
}