Add runtime support for wall connections

this doesn't match the 1.16+ behaviour yet, but it at least recognizes walls that are already in the post-1.16 way and doesn't break them if not interacted with.
This commit is contained in:
Dylan K. Taylor
2022-06-25 15:59:38 +01:00
parent b9542b4908
commit 1da4c45979
9 changed files with 149 additions and 25 deletions

View File

@ -196,12 +196,11 @@ final class BlockStateDeserializerHelper{
/** @throws BlockStateDeserializeException */
public static function decodeWall(Wall $block, BlockStateReader $in) : Wall{
//TODO: our walls don't support the full range of needed states yet
$in->todo(BlockStateNames::WALL_POST_BIT); //TODO
$in->todo(BlockStateNames::WALL_CONNECTION_TYPE_EAST);
$in->todo(BlockStateNames::WALL_CONNECTION_TYPE_NORTH);
$in->todo(BlockStateNames::WALL_CONNECTION_TYPE_SOUTH);
$in->todo(BlockStateNames::WALL_CONNECTION_TYPE_WEST);
$block->setPost($in->readBool(BlockStateNames::WALL_POST_BIT));
$block->setConnection(Facing::NORTH, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_NORTH));
$block->setConnection(Facing::SOUTH, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_SOUTH));
$block->setConnection(Facing::WEST, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_WEST));
$block->setConnection(Facing::EAST, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_EAST));
return $block;
}

View File

@ -27,6 +27,7 @@ use pocketmine\block\utils\BellAttachmentType;
use pocketmine\block\utils\CoralType;
use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\SlabType;
use pocketmine\block\utils\WallConnectionType;
use pocketmine\data\bedrock\block\BlockStateData;
use pocketmine\data\bedrock\block\BlockStateDeserializeException;
use pocketmine\data\bedrock\block\BlockStateNames;
@ -296,6 +297,19 @@ final class BlockStateReader{
};
}
/** @throws BlockStateDeserializeException */
public function readWallConnectionType(string $name) : ?WallConnectionType{
return match($type = $this->readString($name)){
//TODO: this looks a bit confusing due to use of EAST, but the values are the same for all connections
//we need to find a better way to auto-generate the constant names when they are reused
//for now, using these constants is better than nothing since it still gives static analysability
StringValues::WALL_CONNECTION_TYPE_EAST_NONE => null,
StringValues::WALL_CONNECTION_TYPE_EAST_SHORT => WallConnectionType::SHORT(),
StringValues::WALL_CONNECTION_TYPE_EAST_TALL => WallConnectionType::TALL(),
default => throw $this->badValueException($name, $type),
};
}
/**
* Explicitly mark a property as unused, so it doesn't get flagged as an error when debug mode is enabled
*/

View File

@ -48,7 +48,6 @@ use pocketmine\block\Wall;
use pocketmine\block\WallSign;
use pocketmine\block\Wood;
use pocketmine\data\bedrock\block\BlockStateNames;
use pocketmine\data\bedrock\block\BlockStateStringValues;
use pocketmine\data\bedrock\block\BlockTypeNames as Ids;
use pocketmine\data\bedrock\MushroomBlockTypeIdMap;
use pocketmine\math\Axis;
@ -245,13 +244,12 @@ final class BlockStateSerializerHelper{
}
public static function encodeWall(Wall $block, BlockStateWriter $out) : BlockStateWriter{
//TODO: our walls don't support the full range of needed states yet
return $out
->writeBool(BlockStateNames::WALL_POST_BIT, false)
->writeString(BlockStateNames::WALL_CONNECTION_TYPE_EAST, BlockStateStringValues::WALL_CONNECTION_TYPE_EAST_NONE)
->writeString(BlockStateNames::WALL_CONNECTION_TYPE_NORTH, BlockStateStringValues::WALL_CONNECTION_TYPE_NORTH_NONE)
->writeString(BlockStateNames::WALL_CONNECTION_TYPE_SOUTH, BlockStateStringValues::WALL_CONNECTION_TYPE_SOUTH_NONE)
->writeString(BlockStateNames::WALL_CONNECTION_TYPE_WEST, BlockStateStringValues::WALL_CONNECTION_TYPE_WEST_NONE);
->writeBool(BlockStateNames::WALL_POST_BIT, $block->isPost())
->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_EAST, $block->getConnection(Facing::EAST))
->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_NORTH, $block->getConnection(Facing::NORTH))
->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_SOUTH, $block->getConnection(Facing::SOUTH))
->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_WEST, $block->getConnection(Facing::WEST));
}
public static function encodeLegacyWall(Wall $block, string $type) : BlockStateWriter{

View File

@ -28,6 +28,7 @@ use pocketmine\block\utils\CoralType;
use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\SlabType;
use pocketmine\block\utils\TreeType;
use pocketmine\block\utils\WallConnectionType;
use pocketmine\data\bedrock\block\BlockStateData;
use pocketmine\data\bedrock\block\BlockStateNames;
use pocketmine\data\bedrock\block\BlockStateSerializeException;
@ -263,6 +264,17 @@ final class BlockStateWriter{
return $this;
}
/** @return $this */
public function writeWallConnectionType(string $name, ?WallConnectionType $wallConnectionType) : self{
$this->writeString($name, match($wallConnectionType){
null => StringValues::WALL_CONNECTION_TYPE_EAST_NONE,
WallConnectionType::SHORT() => StringValues::WALL_CONNECTION_TYPE_EAST_SHORT,
WallConnectionType::TALL() => StringValues::WALL_CONNECTION_TYPE_EAST_TALL,
default => throw new BlockStateSerializeException("Invalid Wall connection type " . $wallConnectionType->name())
});
return $this;
}
public function getBlockStateData() : BlockStateData{
return new BlockStateData($this->id, $this->states, BlockStateData::CURRENT_VERSION);
}