From 83016a97bd7249fe1e5aae18e34a95178d921fb5 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 22 Jul 2021 19:13:26 +0100 Subject: [PATCH] Added getMushroomBlockType() / setMushroomBlockType() APIs to Red/BrownMushroomBlock --- src/block/RedMushroomBlock.php | 33 ++++++--- src/block/utils/MushroomBlockType.php | 63 ++++++++++++++++++ src/data/bedrock/MushroomBlockTypeIdMap.php | 74 +++++++++++++++++++++ 3 files changed, 160 insertions(+), 10 deletions(-) create mode 100644 src/block/utils/MushroomBlockType.php create mode 100644 src/data/bedrock/MushroomBlockTypeIdMap.php diff --git a/src/block/RedMushroomBlock.php b/src/block/RedMushroomBlock.php index 955514bca..a41b031f5 100644 --- a/src/block/RedMushroomBlock.php +++ b/src/block/RedMushroomBlock.php @@ -23,32 +23,45 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\InvalidBlockStateException; +use pocketmine\block\utils\MushroomBlockType; +use pocketmine\data\bedrock\MushroomBlockTypeIdMap; use pocketmine\item\Item; use function mt_rand; class RedMushroomBlock extends Opaque{ - /** - * @var int - * In PC they have blockstate properties for each of the sides (pores/not pores). Unfortunately, we can't support - * that because we can't serialize 2^6 combinations into a 4-bit metadata value, so this has to stick with storing - * the legacy crap for now. - * TODO: change this once proper blockstates are implemented - */ - protected int $rotationData = 0; + protected MushroomBlockType $mushroomBlockType; + + public function __construct(BlockIdentifier $idInfo, string $name, BlockBreakInfo $breakInfo){ + $this->mushroomBlockType = MushroomBlockType::PORES(); + parent::__construct($idInfo, $name, $breakInfo); + } protected function writeStateToMeta() : int{ - return $this->rotationData; + return MushroomBlockTypeIdMap::getInstance()->toId($this->mushroomBlockType); } public function readStateFromData(int $id, int $stateMeta) : void{ - $this->rotationData = $stateMeta; + $type = MushroomBlockTypeIdMap::getInstance()->fromId($stateMeta); + if($type === null){ + throw new InvalidBlockStateException("No such mushroom variant $stateMeta"); + } + $this->mushroomBlockType = $type; } public function getStateBitmask() : int{ return 0b1111; } + public function getMushroomBlockType() : MushroomBlockType{ return $this->mushroomBlockType; } + + /** @return $this */ + public function setMushroomBlockType(MushroomBlockType $mushroomBlockType) : self{ + $this->mushroomBlockType = $mushroomBlockType; + return $this; + } + public function getDropsForCompatibleTool(Item $item) : array{ return [ VanillaBlocks::RED_MUSHROOM()->asItem()->setCount(mt_rand(0, 2)) diff --git a/src/block/utils/MushroomBlockType.php b/src/block/utils/MushroomBlockType.php new file mode 100644 index 000000000..413c27370 --- /dev/null +++ b/src/block/utils/MushroomBlockType.php @@ -0,0 +1,63 @@ + + */ + private array $idToEnum = []; + /** + * @var int[] + * @phpstan-var array + */ + private array $enumToId = []; + + public function __construct(){ + $this->register(LegacyMeta::MUSHROOM_BLOCK_ALL_PORES, MushroomBlockType::PORES()); + $this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_NORTHWEST_CORNER, MushroomBlockType::CAP_NORTHWEST()); + $this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_NORTH_SIDE, MushroomBlockType::CAP_NORTH()); + $this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_NORTHEAST_CORNER, MushroomBlockType::CAP_NORTHEAST()); + $this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_WEST_SIDE, MushroomBlockType::CAP_WEST()); + $this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_TOP_ONLY, MushroomBlockType::CAP_MIDDLE()); + $this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_EAST_SIDE, MushroomBlockType::CAP_EAST()); + $this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_SOUTHWEST_CORNER, MushroomBlockType::CAP_SOUTHWEST()); + $this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_SOUTH_SIDE, MushroomBlockType::CAP_SOUTH()); + $this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_SOUTHEAST_CORNER, MushroomBlockType::CAP_SOUTHEAST()); + $this->register(LegacyMeta::MUSHROOM_BLOCK_ALL_CAP, MushroomBlockType::ALL_CAP()); + } + + public function register(int $id, MushroomBlockType $type) : void{ + $this->idToEnum[$id] = $type; + $this->enumToId[$type->id()] = $id; + } + + public function fromId(int $id) : ?MushroomBlockType{ + return $this->idToEnum[$id] ?? null; + } + + public function toId(MushroomBlockType $type) : int{ + if(!array_key_exists($type->id(), $this->enumToId)){ + throw new \InvalidArgumentException("Mushroom block type does not have a mapped ID"); //this should never happen + } + return $this->enumToId[$type->id()]; + } +}