From bc46e148dfb5de931f8c56c91b4218d6e845658a Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 11 Feb 2022 21:13:28 +0000 Subject: [PATCH] Updated to support new tag storage format --- .../upgrade/BlockStateUpgradeSchemaUtils.php | 45 ++++++++----------- .../model/BlockStateUpgradeSchemaModelTag.php | 16 ++----- 2 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/data/bedrock/blockstate/upgrade/BlockStateUpgradeSchemaUtils.php b/src/data/bedrock/blockstate/upgrade/BlockStateUpgradeSchemaUtils.php index dfe3b922ab..ea6033aa3d 100644 --- a/src/data/bedrock/blockstate/upgrade/BlockStateUpgradeSchemaUtils.php +++ b/src/data/bedrock/blockstate/upgrade/BlockStateUpgradeSchemaUtils.php @@ -38,6 +38,7 @@ use function array_map; use function count; use function file_get_contents; use function get_class; +use function get_debug_type; use function gettype; use function implode; use function is_int; @@ -89,36 +90,28 @@ final class BlockStateUpgradeSchemaUtils{ return implode("\n", $lines); } - private static function tagToJsonModel(Tag $tag) : BlockStateUpgradeSchemaModelTag{ - $type = match(get_class($tag)){ - IntTag::class => "int", - StringTag::class => "string", - ByteTag::class => "byte", - default => throw new \UnexpectedValueException() - }; + public static function tagToJsonModel(Tag $tag) : BlockStateUpgradeSchemaModelTag{ + $model = new BlockStateUpgradeSchemaModelTag(); + if($tag instanceof IntTag){ + $model->int = $tag->getValue(); + }elseif($tag instanceof StringTag){ + $model->string = $tag->getValue(); + }elseif($tag instanceof ByteTag){ + $model->byte = $tag->getValue(); + }else{ + throw new \UnexpectedValueException("Unexpected value type " . get_debug_type($tag)); + } - return new BlockStateUpgradeSchemaModelTag($type, $tag->getValue()); + return $model; } private static function jsonModelToTag(BlockStateUpgradeSchemaModelTag $model) : Tag{ - if($model->type === "int"){ - if(!is_int($model->value)){ - throw new \UnexpectedValueException("Value for type int must be an int"); - } - return new IntTag($model->value); - }elseif($model->type === "byte"){ - if(!is_int($model->value)){ - throw new \UnexpectedValueException("Value for type byte must be an int"); - } - return new ByteTag($model->value); - }elseif($model->type === "string"){ - if(!is_string($model->value)){ - throw new \UnexpectedValueException("Value for type string must be a string"); - } - return new StringTag($model->value); - }else{ - throw new \UnexpectedValueException("Unknown blockstate value type $model->type"); - } + return match(true){ + isset($model->byte) && !isset($model->int) && !isset($model->string) => new ByteTag($model->byte), + !isset($model->byte) && isset($model->int) && !isset($model->string) => new IntTag($model->int), + !isset($model->byte) && !isset($model->int) && isset($model->string) => new StringTag($model->string), + default => throw new \UnexpectedValueException("Malformed JSON model tag, expected exactly one of 'byte', 'int' or 'string' properties") + }; } public static function fromJsonModel(BlockStateUpgradeSchemaModel $model) : BlockStateUpgradeSchema{ diff --git a/src/data/bedrock/blockstate/upgrade/model/BlockStateUpgradeSchemaModelTag.php b/src/data/bedrock/blockstate/upgrade/model/BlockStateUpgradeSchemaModelTag.php index f34c489282..2e0d24cacf 100644 --- a/src/data/bedrock/blockstate/upgrade/model/BlockStateUpgradeSchemaModelTag.php +++ b/src/data/bedrock/blockstate/upgrade/model/BlockStateUpgradeSchemaModelTag.php @@ -24,17 +24,7 @@ declare(strict_types=1); namespace pocketmine\data\bedrock\blockstate\upgrade\model; final class BlockStateUpgradeSchemaModelTag{ - - /** @required */ - public string $type; - /** - * @required - * @var mixed JsonMapper doesn't support mixed type :( - */ - public $value; - - public function __construct(string $type, mixed $value){ - $this->type = $type; - $this->value = $value; - } + public int $byte; + public int $int; + public string $string; }