From 81eafde0744455e5ad7b717207d7a7a8bf3c598f Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 24 May 2022 15:07:38 +0100 Subject: [PATCH] Hacks for banners and coral fans --- src/data/bedrock/item/ItemSerializer.php | 4 +- src/item/CoralFan.php | 61 ++++++++++++++++++++++++ src/item/Item.php | 7 ++- src/item/ItemFactory.php | 21 ++------ src/item/ItemIdentifier.php | 2 +- src/item/ItemIdentifierFlattened.php | 41 ++++++++++++++++ 6 files changed, 115 insertions(+), 21 deletions(-) create mode 100644 src/item/CoralFan.php create mode 100644 src/item/ItemIdentifierFlattened.php diff --git a/src/data/bedrock/item/ItemSerializer.php b/src/data/bedrock/item/ItemSerializer.php index d5bbcb149..e8d73e60f 100644 --- a/src/data/bedrock/item/ItemSerializer.php +++ b/src/data/bedrock/item/ItemSerializer.php @@ -68,7 +68,7 @@ final class ItemSerializer{ if($item->hasAnyDamageValue()){ throw new \InvalidArgumentException("Cannot serialize a recipe wildcard"); } - $index = ($item->getId() << 16) | $item->getMeta(); + $index = $item->getTypeId(); if(isset($this->serializers[$index])){ //TODO: REMOVE ME throw new AssumptionFailedError("Registering the same item twice!"); @@ -89,7 +89,7 @@ final class ItemSerializer{ if($item instanceof ItemBlock){ $data = self::standardBlock($item->getBlock()); }else{ - $index = ($item->getId() << 16) | ($item instanceof Durable ? 0 : $item->getMeta()); + $index = $item->getTypeId(); $locatedSerializer = $this->serializers[$index][get_class($item)] ?? null; if($locatedSerializer === null){ diff --git a/src/item/CoralFan.php b/src/item/CoralFan.php new file mode 100644 index 000000000..7cf055823 --- /dev/null +++ b/src/item/CoralFan.php @@ -0,0 +1,61 @@ +identifierFlattened, VanillaBlocks::CORAL_FAN()->getName()); + } + + public function getId() : int{ + return $this->dead ? $this->identifierFlattened->getAdditionalIds()[0] : $this->identifierFlattened->getId(); + } + + public function getMeta() : int{ + return CoralTypeIdMap::getInstance()->toId($this->coralType); + } + + public function getBlock(?int $clickedFace = null) : Block{ + $block = $clickedFace !== null && Facing::axis($clickedFace) !== Axis::Y ? VanillaBlocks::WALL_CORAL_FAN() : VanillaBlocks::CORAL_FAN(); + + return $block->setCoralType($this->coralType)->setDead($this->dead); + } + + public function getFuelTime() : int{ + return $this->getBlock()->getFuelTime(); + } + + public function getMaxStackSize() : int{ + return $this->getBlock()->getMaxStackSize(); + } +} \ No newline at end of file diff --git a/src/item/Item.php b/src/item/Item.php index eed7b0e20..a84d2b4e6 100644 --- a/src/item/Item.php +++ b/src/item/Item.php @@ -439,7 +439,12 @@ class Item implements \JsonSerializable{ return VanillaBlocks::AIR(); } - final public function getId() : int{ + final public function getTypeId() : int{ + //don't use Item::getMeta(), since it might be overridden for non-type information (e.g. durability) + return ($this->identifier->getId() << 16) | $this->identifier->getMeta(); + } + + public function getId() : int{ return $this->identifier->getId(); } diff --git a/src/item/ItemFactory.php b/src/item/ItemFactory.php index c3e6a1cfc..3cc27ca88 100644 --- a/src/item/ItemFactory.php +++ b/src/item/ItemFactory.php @@ -82,23 +82,10 @@ class ItemFactory{ $this->register(new Clownfish(new ItemIdentifier(ItemIds::CLOWNFISH, 0), "Clownfish")); $this->register(new Coal(new ItemIdentifier(ItemIds::COAL, 0), "Coal")); - foreach([ - 0 => CoralType::TUBE(), - 1 => CoralType::BRAIN(), - 2 => CoralType::BUBBLE(), - 3 => CoralType::FIRE(), - 4 => CoralType::HORN() - ] as $meta => $coralType){ - $this->register(new ItemBlockWallOrFloor( - new ItemIdentifier(ItemIds::CORAL_FAN, $meta), - VanillaBlocks::CORAL_FAN()->setCoralType($coralType)->setDead(false), - VanillaBlocks::WALL_CORAL_FAN()->setCoralType($coralType)->setDead(false) - ), true); - $this->register(new ItemBlockWallOrFloor( - new ItemIdentifier(ItemIds::CORAL_FAN_DEAD, $meta), - VanillaBlocks::CORAL_FAN()->setCoralType($coralType)->setDead(true), - VanillaBlocks::WALL_CORAL_FAN()->setCoralType($coralType)->setDead(true) - ), true); + $identifier = new ItemIdentifierFlattened(ItemIds::CORAL_FAN, 0, [ItemIds::CORAL_FAN_DEAD]); + foreach(CoralType::getAll() as $coralType){ + $this->register((new CoralFan($identifier))->setCoralType($coralType)->setDead(false), true); + $this->register((new CoralFan($identifier))->setCoralType($coralType)->setDead(true), true); } $this->register(new Coal(new ItemIdentifier(ItemIds::COAL, 1), "Charcoal")); diff --git a/src/item/ItemIdentifier.php b/src/item/ItemIdentifier.php index b24c586a9..e7f440c0b 100644 --- a/src/item/ItemIdentifier.php +++ b/src/item/ItemIdentifier.php @@ -23,7 +23,7 @@ declare(strict_types=1); namespace pocketmine\item; -final class ItemIdentifier{ +class ItemIdentifier{ private int $id; private int $meta; diff --git a/src/item/ItemIdentifierFlattened.php b/src/item/ItemIdentifierFlattened.php new file mode 100644 index 000000000..0690ece14 --- /dev/null +++ b/src/item/ItemIdentifierFlattened.php @@ -0,0 +1,41 @@ +additionalIds; } + + public function getAllIds() : array{ + return [$this->getId(), ...$this->additionalIds]; + } +} \ No newline at end of file