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,9 +26,12 @@ namespace pocketmine\block\tile;
use pocketmine\block\Air;
use pocketmine\block\Block;
use pocketmine\block\BlockFactory;
use pocketmine\data\bedrock\blockstate\BlockStateDeserializeException;
use pocketmine\data\SavedDataLoadingException;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\world\format\io\GlobalBlockStateHandlers;
/**
* @deprecated
@ -37,25 +40,35 @@ use pocketmine\nbt\tag\ShortTag;
class FlowerPot extends Spawnable{
private const TAG_ITEM = "item";
private const TAG_ITEM_DATA = "mData";
private const TAG_PLANT_BLOCK = "PlantBlock";
private ?Block $plant = null;
public function readSaveData(CompoundTag $nbt) : void{
$blockStateData = null;
if(($itemIdTag = $nbt->getTag(self::TAG_ITEM)) instanceof ShortTag && ($itemMetaTag = $nbt->getTag(self::TAG_ITEM_DATA)) instanceof IntTag){
$blockStateData = GlobalBlockStateHandlers::getLegacyBlockStateMapper()->fromIntIdMeta($itemIdTag->getValue(), $itemMetaTag->getValue());
}elseif(($plantBlockTag = $nbt->getCompoundTag(self::TAG_PLANT_BLOCK)) !== null){
try{
$this->setPlant(BlockFactory::getInstance()->get($itemIdTag->getValue(), $itemMetaTag->getValue()));
}catch(\InvalidArgumentException $e){
//noop
$blockStateData = GlobalBlockStateHandlers::nbtToBlockStateData($plantBlockTag);
}catch(BlockStateDeserializeException $e){
throw new SavedDataLoadingException("Error loading " . self::TAG_PLANT_BLOCK . " tag for flower pot: " . $e->getMessage(), 0, $e);
}
}else{
//TODO: new PlantBlock tag
}
if($blockStateData !== null){
try{
$blockStateId = GlobalBlockStateHandlers::getDeserializer()->deserialize($blockStateData);
}catch(BlockStateDeserializeException $e){
throw new SavedDataLoadingException("Error deserializing plant for flower pot: " . $e->getMessage(), 0, $e);
}
$this->setPlant(BlockFactory::getInstance()->fromFullBlock($blockStateId));
}
}
protected function writeSaveData(CompoundTag $nbt) : void{
if($this->plant !== null){
$nbt->setShort(self::TAG_ITEM, $this->plant->getId());
$nbt->setInt(self::TAG_ITEM_DATA, $this->plant->getMeta());
$nbt->setTag(self::TAG_PLANT_BLOCK, GlobalBlockStateHandlers::getSerializer()->serialize($this->plant->getFullId())->toNbt());
}
}
@ -73,8 +86,7 @@ class FlowerPot extends Spawnable{
protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
if($this->plant !== null){
$nbt->setShort(self::TAG_ITEM, $this->plant->getId());
$nbt->setInt(self::TAG_ITEM_DATA, $this->plant->getMeta());
$nbt->setTag(self::TAG_PLANT_BLOCK, GlobalBlockStateHandlers::getSerializer()->serialize($this->plant->getFullId())->toNbt());
}
}
}