mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-10 21:45:35 +00:00
Updated to support new tag storage format
This commit is contained in:
parent
169a3217de
commit
bc46e148df
@ -38,6 +38,7 @@ use function array_map;
|
|||||||
use function count;
|
use function count;
|
||||||
use function file_get_contents;
|
use function file_get_contents;
|
||||||
use function get_class;
|
use function get_class;
|
||||||
|
use function get_debug_type;
|
||||||
use function gettype;
|
use function gettype;
|
||||||
use function implode;
|
use function implode;
|
||||||
use function is_int;
|
use function is_int;
|
||||||
@ -89,36 +90,28 @@ final class BlockStateUpgradeSchemaUtils{
|
|||||||
return implode("\n", $lines);
|
return implode("\n", $lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function tagToJsonModel(Tag $tag) : BlockStateUpgradeSchemaModelTag{
|
public static function tagToJsonModel(Tag $tag) : BlockStateUpgradeSchemaModelTag{
|
||||||
$type = match(get_class($tag)){
|
$model = new BlockStateUpgradeSchemaModelTag();
|
||||||
IntTag::class => "int",
|
if($tag instanceof IntTag){
|
||||||
StringTag::class => "string",
|
$model->int = $tag->getValue();
|
||||||
ByteTag::class => "byte",
|
}elseif($tag instanceof StringTag){
|
||||||
default => throw new \UnexpectedValueException()
|
$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{
|
private static function jsonModelToTag(BlockStateUpgradeSchemaModelTag $model) : Tag{
|
||||||
if($model->type === "int"){
|
return match(true){
|
||||||
if(!is_int($model->value)){
|
isset($model->byte) && !isset($model->int) && !isset($model->string) => new ByteTag($model->byte),
|
||||||
throw new \UnexpectedValueException("Value for type int must be an int");
|
!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),
|
||||||
return new IntTag($model->value);
|
default => throw new \UnexpectedValueException("Malformed JSON model tag, expected exactly one of 'byte', 'int' or 'string' properties")
|
||||||
}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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromJsonModel(BlockStateUpgradeSchemaModel $model) : BlockStateUpgradeSchema{
|
public static function fromJsonModel(BlockStateUpgradeSchemaModel $model) : BlockStateUpgradeSchema{
|
||||||
|
@ -24,17 +24,7 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\data\bedrock\blockstate\upgrade\model;
|
namespace pocketmine\data\bedrock\blockstate\upgrade\model;
|
||||||
|
|
||||||
final class BlockStateUpgradeSchemaModelTag{
|
final class BlockStateUpgradeSchemaModelTag{
|
||||||
|
public int $byte;
|
||||||
/** @required */
|
public int $int;
|
||||||
public string $type;
|
public string $string;
|
||||||
/**
|
|
||||||
* @required
|
|
||||||
* @var mixed JsonMapper doesn't support mixed type :(
|
|
||||||
*/
|
|
||||||
public $value;
|
|
||||||
|
|
||||||
public function __construct(string $type, mixed $value){
|
|
||||||
$this->type = $type;
|
|
||||||
$this->value = $value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user