From 3d2fc31ef2bd3d28859843d68b5775372cf310e5 Mon Sep 17 00:00:00 2001 From: Dries C Date: Sat, 15 Feb 2025 15:13:30 +0100 Subject: [PATCH] Apply Dylan's suggestions --- src/inventory/CreativeInventory.php | 29 ++++++++----- src/inventory/data/CreativeGroup.php | 43 ++++++++----------- .../mcpe/cache/CreativeInventoryCache.php | 12 +++--- tools/generate-bedrock-data-from-packets.php | 2 - 4 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/inventory/CreativeInventory.php b/src/inventory/CreativeInventory.php index 26ed8af50e..0ef5ce2708 100644 --- a/src/inventory/CreativeInventory.php +++ b/src/inventory/CreativeInventory.php @@ -27,6 +27,7 @@ use pocketmine\inventory\data\CreativeGroup; use pocketmine\inventory\json\CreativeGroupData; use pocketmine\item\Item; +use pocketmine\lang\Translatable; use pocketmine\utils\DestructorCallbackTrait; use pocketmine\utils\ObjectSet; use pocketmine\utils\SingletonTrait; @@ -54,11 +55,10 @@ final class CreativeInventory{ /** @phpstan-var ObjectSet<\Closure() : void> */ private ObjectSet $contentChangedCallbacks; - private CreativeGroup $defaultGroup; + private ?CreativeGroup $defaultGroup = null; private function __construct(){ $this->contentChangedCallbacks = new ObjectSet(); - $this->defaultGroup = CreativeGroup::anonymous(CreativeCategory::ITEMS); foreach([ "construction" => CreativeCategory::CONSTRUCTION, @@ -72,9 +72,9 @@ private function __construct(){ ); foreach($groups as $groupData){ - $group = new CreativeGroup( + $group = CreativeGroup::named( $categoryId, - $groupData->group_name, + new Translatable($groupData->group_name), $groupData->group_icon === null ? null : CraftingManagerFromDataHelper::deserializeItemStack($groupData->group_icon) ); $items = array_filter(array_map(static fn($itemStack) => CraftingManagerFromDataHelper::deserializeItemStack($itemStack), $groupData->items)); @@ -105,10 +105,12 @@ public function getAll() : array{ } /** + * Returns the group of every item indexed by the item index. + * * @return CreativeGroup[] * @phpstan-return array */ - public function getItemGroup() : array{ + public function getItemGroups() : array{ return $this->groups; } @@ -116,7 +118,10 @@ public function getItem(int $index) : ?Item{ return isset($this->items[$index]) ? clone $this->items[$index] : null; } - public function getGroup(int $index) : ?CreativeGroup{ + /** + * @see CreativeInventory::getItemIndex() + */ + public function getItemGroupByIndex(int $index) : ?CreativeGroup{ return $this->groups[$index] ?? null; } @@ -136,12 +141,16 @@ public function getItemIndex(Item $item) : int{ */ public function add(Item $item, ?CreativeGroup $group = null) : void{ $this->items[] = $item; - $this->groups[] = $group ?? $this->defaultGroup; - $this->onContentChange(); - if($group !== null){ // We need to create a new default group if another group is used. - $this->defaultGroup = CreativeGroup::anonymous(CreativeCategory::ITEMS); + if($group === null){ + $this->defaultGroup ??= CreativeGroup::anonymous(CreativeCategory::ITEMS); + $this->groups[] = $this->defaultGroup; + }else{ + $this->defaultGroup = null; + $this->groups[] = $group; } + + $this->onContentChange(); } /** diff --git a/src/inventory/data/CreativeGroup.php b/src/inventory/data/CreativeGroup.php index b7fe9efeca..21cecf8f1f 100644 --- a/src/inventory/data/CreativeGroup.php +++ b/src/inventory/data/CreativeGroup.php @@ -25,43 +25,34 @@ use pocketmine\inventory\CreativeCategory; use pocketmine\item\Item; +use pocketmine\lang\Translatable; final class CreativeGroup{ - private CreativeCategory $categoryId; - private string $name; - private ?Item $icon; + public readonly CreativeCategory $categoryId; + public readonly Translatable|string $name; + public readonly ?Item $icon; - public function __construct(CreativeCategory $categoryId, string $name, ?Item $icon = null){ - $this->categoryId = $categoryId; - $this->name = $name; - $this->icon = $icon; + private function __construct(){ + //NOOP } public static function anonymous(CreativeCategory $categoryId) : self{ - return new self($categoryId, ""); - } - - public function getCategoryId() : CreativeCategory{ - return $this->categoryId; - } + $result = new self(); - public function setCategoryId(CreativeCategory $categoryId) : void{ - $this->categoryId = $categoryId; - } + $result->categoryId = $categoryId; + $result->name = ""; + $result->icon = null; - public function getName() : string{ - return $this->name; + return $result; } - public function setName(string $name) : void{ - $this->name = $name; - } + public static function named(CreativeCategory $categoryId, Translatable|string $name, Item $icon) : self{ + $result = new self(); - public function getIcon() : ?Item{ - return $this->icon; - } + $result->categoryId = $categoryId; + $result->name = $name; + $result->icon = $icon; - public function setIcon(?Item $icon) : void{ - $this->icon = $icon; + return $result; } } diff --git a/src/network/mcpe/cache/CreativeInventoryCache.php b/src/network/mcpe/cache/CreativeInventoryCache.php index 7149afb717..b5de0719e6 100644 --- a/src/network/mcpe/cache/CreativeInventoryCache.php +++ b/src/network/mcpe/cache/CreativeInventoryCache.php @@ -26,6 +26,7 @@ use pocketmine\inventory\CreativeCategory; use pocketmine\inventory\CreativeInventory; use pocketmine\inventory\data\CreativeGroup; +use pocketmine\lang\Translatable; use pocketmine\network\mcpe\convert\TypeConverter; use pocketmine\network\mcpe\protocol\CreativeContentPacket; use pocketmine\network\mcpe\protocol\types\inventory\CreativeGroupEntry; @@ -70,22 +71,21 @@ private function buildCreativeInventoryCache(CreativeInventory $inventory) : Cre $typeConverter = TypeConverter::getInstance(); $index = 0; - $mappedGroups = array_reduce($inventory->getItemGroup(), function (array $carry, CreativeGroup $group) use ($typeConverter, &$index, &$groups) : array{ + $mappedGroups = array_reduce($inventory->getItemGroups(), function (array $carry, CreativeGroup $group) use ($typeConverter, &$index, &$groups) : array{ if (!isset($carry[$id = spl_object_id($group)])) { $carry[$id] = $index++; - $categoryId = match($group->getCategoryId()){ + $categoryId = match($group->categoryId){ CreativeCategory::CONSTRUCTION => CreativeContentPacket::CATEGORY_CONSTRUCTION, CreativeCategory::NATURE => CreativeContentPacket::CATEGORY_NATURE, CreativeCategory::EQUIPMENT => CreativeContentPacket::CATEGORY_EQUIPMENT, CreativeCategory::ITEMS => CreativeContentPacket::CATEGORY_ITEMS }; - $groupIcon = $group->getIcon(); $groups[] = new CreativeGroupEntry( $categoryId, - $group->getName(), - $groupIcon === null ? ItemStack::null() : $typeConverter->coreItemStackToNet($groupIcon) + $group->name instanceof Translatable ? $group->name->getText() : (string) $group->name, + $group->icon === null ? ItemStack::null() : $typeConverter->coreItemStackToNet($group->icon) ); } return $carry; @@ -96,7 +96,7 @@ private function buildCreativeInventoryCache(CreativeInventory $inventory) : Cre $items[] = new CreativeItemEntry( $k, $typeConverter->coreItemStackToNet($item), - $mappedGroups[spl_object_id($inventory->getGroup($k) ?? throw new \AssertionError("Item group not found"))] + $mappedGroups[spl_object_id($inventory->getItemGroupByIndex($k) ?? throw new \AssertionError("Item group not found"))] ); } diff --git a/tools/generate-bedrock-data-from-packets.php b/tools/generate-bedrock-data-from-packets.php index 3460d5a541..9beb47bae6 100644 --- a/tools/generate-bedrock-data-from-packets.php +++ b/tools/generate-bedrock-data-from-packets.php @@ -291,7 +291,6 @@ public function handleItemRegistry(ItemRegistryPacket $packet) : bool{ public function handleCreativeContent(CreativeContentPacket $packet) : bool{ echo "updating creative inventory data\n"; - /** @var array $groupItems */ $groupItems = array_reduce($packet->getItems(), function (array $carry, CreativeItemEntry $item) : array{ $carry[$item->getGroupId()][] = self::objectToOrderedArray($this->itemStackToJson($item->getItem())); return $carry; @@ -306,7 +305,6 @@ public function handleCreativeContent(CreativeContentPacket $packet) : bool{ CreativeContentPacket::CATEGORY_ITEMS => "items", ]; - /** @var array $groupCategories */ $groupCategories = array_reduce(array_keys($packet->getGroups()), function (array $carry, int $groupIndex) use ($typeMap, $groups, $groupItems) : array{ $group = $groups[$groupIndex]; if(!isset($typeMap[$group->getCategoryId()])){