Allow remapped oldState and newState to accept null, to make it easier for third-party tools to use the schemas

since PHP emits empty JSON objects as arrays, this makes it pretty annoying to work with the schemas in other languages. However, nullability is something most languages understand pretty easily.

This should continue to support old schemas.
This commit is contained in:
Dylan K. Taylor 2023-03-25 19:41:40 +00:00
parent 341a9b78b5
commit 0818388bd5
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 12 additions and 11 deletions

View File

@ -150,9 +150,9 @@ final class BlockStateUpgradeSchemaUtils{
foreach(Utils::stringifyKeys($model->remappedStates ?? []) as $oldBlockName => $remaps){ foreach(Utils::stringifyKeys($model->remappedStates ?? []) as $oldBlockName => $remaps){
foreach($remaps as $remap){ foreach($remaps as $remap){
$result->remappedStates[$oldBlockName][] = new BlockStateUpgradeSchemaBlockRemap( $result->remappedStates[$oldBlockName][] = new BlockStateUpgradeSchemaBlockRemap(
array_map(fn(BlockStateUpgradeSchemaModelTag $tag) => self::jsonModelToTag($tag), $remap->oldState), array_map(fn(BlockStateUpgradeSchemaModelTag $tag) => self::jsonModelToTag($tag), $remap->oldState ?? []),
$remap->newName, $remap->newName,
array_map(fn(BlockStateUpgradeSchemaModelTag $tag) => self::jsonModelToTag($tag), $remap->newState), array_map(fn(BlockStateUpgradeSchemaModelTag $tag) => self::jsonModelToTag($tag), $remap->newState ?? []),
); );
} }
} }

View File

@ -23,24 +23,25 @@ declare(strict_types=1);
namespace pocketmine\data\bedrock\block\upgrade\model; namespace pocketmine\data\bedrock\block\upgrade\model;
use function count;
final class BlockStateUpgradeSchemaModelBlockRemap{ final class BlockStateUpgradeSchemaModelBlockRemap{
/** /**
* @var BlockStateUpgradeSchemaModelTag[] * @var BlockStateUpgradeSchemaModelTag[]|null
* @phpstan-var array<string, BlockStateUpgradeSchemaModelTag> * @phpstan-var array<string, BlockStateUpgradeSchemaModelTag>|null
* @required * @required
*/ */
public array $oldState; public ?array $oldState;
/** @required */ /** @required */
public string $newName; public string $newName;
/** /**
* @var BlockStateUpgradeSchemaModelTag[] * @var BlockStateUpgradeSchemaModelTag[]|null
* @phpstan-var array<string, BlockStateUpgradeSchemaModelTag> * @phpstan-var array<string, BlockStateUpgradeSchemaModelTag>|null
* @required
*/ */
public array $newState; public ?array $newState;
/** /**
* @param BlockStateUpgradeSchemaModelTag[] $oldState * @param BlockStateUpgradeSchemaModelTag[] $oldState
@ -49,8 +50,8 @@ final class BlockStateUpgradeSchemaModelBlockRemap{
* @phpstan-param array<string, BlockStateUpgradeSchemaModelTag> $newState * @phpstan-param array<string, BlockStateUpgradeSchemaModelTag> $newState
*/ */
public function __construct(array $oldState, string $newName, array $newState){ public function __construct(array $oldState, string $newName, array $newState){
$this->oldState = $oldState; $this->oldState = count($oldState) === 0 ? null : $oldState;
$this->newName = $newName; $this->newName = $newName;
$this->newState = $newState; $this->newState = count($newState) === 0 ? null : $newState;
} }
} }