Unify block serializers (#6769)

This has several advantages:

    Easier to implement new blocks (one less file to modify)
    Easier to adjust serialization of existing blocks
    Guaranteed consistency between serializers and deserializers
    Potentially, exposes more metadata for programmatic analysis, instead of having everything baked inside opaque Closures

There are some exceptions which still use the old approach: big dripleaf, cauldrons, mushroom stems, and pitcher crops. These all have multiple PM block types for a single ID, with relatively complex logic to select which to use. These weren't worth the effort to unify due to their small number. I may revisit this in the future, but I already spent a lot of brainpower on it.
This commit is contained in:
Dylan T.
2025-08-24 14:12:18 +01:00
committed by GitHub
parent 47140cb8d7
commit 7c521b456e
33 changed files with 4072 additions and 3609 deletions

View File

@ -600,6 +600,12 @@ parameters:
count: 1
path: ../../../src/crash/CrashDumpRenderer.php
-
message: '#^Parameter \#1 \$faces of method pocketmine\\block\\Vine\:\:setFaces\(\) expects list\<2\|3\|4\|5\>, array\<int\> given\.$#'
identifier: argument.type
count: 1
path: ../../../src/data/bedrock/block/convert/VanillaBlockMappings.php
-
message: '#^Parameter \#1 \$blockToItemId of class pocketmine\\data\\bedrock\\item\\BlockItemIdMap constructor expects array\<string, string\>, array\<mixed, mixed\> given\.$#'
identifier: argument.type

View File

@ -42,6 +42,8 @@ final class BlockSerializerDeserializerTest extends TestCase{
public function setUp() : void{
$this->deserializer = new BlockStateToObjectDeserializer();
$this->serializer = new BlockObjectToStateSerializer();
$registrar = new BlockSerializerDeserializerRegistrar($this->deserializer, $this->serializer);
VanillaBlockMappings::init($registrar);
}
public function testAllKnownBlockStatesSerializableAndDeserializable() : void{
@ -49,12 +51,12 @@ final class BlockSerializerDeserializerTest extends TestCase{
try{
$blockStateData = $this->serializer->serializeBlock($block);
}catch(BlockStateSerializeException $e){
self::fail($e->getMessage());
self::fail("Failed to serialize " . $block->getName() . ": " . $e->getMessage());
}
try{
$newBlock = $this->deserializer->deserializeBlock($blockStateData);
}catch(BlockStateDeserializeException $e){
self::fail($e->getMessage());
self::fail("Failed to deserialize " . $blockStateData->getName() . ": " . $e->getMessage() . " with data " . $blockStateData->toNbt());
}
if($block->getTypeId() === BlockTypeIds::POTION_CAULDRON){