Skip to content

Commit

Permalink
Apply Dylan's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
dries-c committed Feb 15, 2025
1 parent 6df2f1d commit 3d2fc31
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 44 deletions.
29 changes: 19 additions & 10 deletions src/inventory/CreativeInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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)

Check failure on line 78 in src/inventory/CreativeInventory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Parameter #3 $icon of static method pocketmine\inventory\data\CreativeGroup::named() expects pocketmine\item\Item, pocketmine\item\Item|null given.

Check failure on line 78 in src/inventory/CreativeInventory.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Parameter #3 $icon of static method pocketmine\inventory\data\CreativeGroup::named() expects pocketmine\item\Item, pocketmine\item\Item|null given.

Check failure on line 78 in src/inventory/CreativeInventory.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Parameter #3 $icon of static method pocketmine\inventory\data\CreativeGroup::named() expects pocketmine\item\Item, pocketmine\item\Item|null given.

Check failure on line 78 in src/inventory/CreativeInventory.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Parameter #3 $icon of static method pocketmine\inventory\data\CreativeGroup::named() expects pocketmine\item\Item, pocketmine\item\Item|null given.

Check failure on line 78 in src/inventory/CreativeInventory.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Parameter #3 $icon of static method pocketmine\inventory\data\CreativeGroup::named() expects pocketmine\item\Item, pocketmine\item\Item|null given.

Check failure on line 78 in src/inventory/CreativeInventory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Parameter #3 $icon of static method pocketmine\inventory\data\CreativeGroup::named() expects pocketmine\item\Item, pocketmine\item\Item|null given.
);
$items = array_filter(array_map(static fn($itemStack) => CraftingManagerFromDataHelper::deserializeItemStack($itemStack), $groupData->items));
Expand Down Expand Up @@ -105,18 +105,23 @@ public function getAll() : array{
}

/**
* Returns the group of every item indexed by the item index.
*
* @return CreativeGroup[]
* @phpstan-return array<int, CreativeGroup>
*/
public function getItemGroup() : array{
public function getItemGroups() : array{
return $this->groups;
}

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;
}

Expand All @@ -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();
}

/**
Expand Down
43 changes: 17 additions & 26 deletions src/inventory/data/CreativeGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check failure on line 31 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $categoryId. Assign it in the constructor.

Check failure on line 31 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $categoryId. Assign it in the constructor.

Check failure on line 31 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $categoryId. Assign it in the constructor.

Check failure on line 31 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $categoryId. Assign it in the constructor.

Check failure on line 31 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $categoryId. Assign it in the constructor.

Check failure on line 31 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $categoryId. Assign it in the constructor.
public readonly Translatable|string $name;

Check failure on line 32 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $name. Assign it in the constructor.

Check failure on line 32 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $name. Assign it in the constructor.

Check failure on line 32 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $name. Assign it in the constructor.

Check failure on line 32 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $name. Assign it in the constructor.

Check failure on line 32 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $name. Assign it in the constructor.

Check failure on line 32 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $name. Assign it in the constructor.
public readonly ?Item $icon;

Check failure on line 33 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $icon. Assign it in the constructor.

Check failure on line 33 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $icon. Assign it in the constructor.

Check failure on line 33 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $icon. Assign it in the constructor.

Check failure on line 33 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $icon. Assign it in the constructor.

Check failure on line 33 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $icon. Assign it in the constructor.

Check failure on line 33 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Class pocketmine\inventory\data\CreativeGroup has an uninitialized readonly property $icon. Assign it in the constructor.

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;

Check failure on line 42 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.

Check failure on line 42 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.

Check failure on line 42 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.

Check failure on line 42 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.

Check failure on line 42 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.

Check failure on line 42 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.
$result->name = "";

Check failure on line 43 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.

Check failure on line 43 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.

Check failure on line 43 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.

Check failure on line 43 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.

Check failure on line 43 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.

Check failure on line 43 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.
$result->icon = null;

Check failure on line 44 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

Check failure on line 44 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

Check failure on line 44 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

Check failure on line 44 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

Check failure on line 44 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

Check failure on line 44 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

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;

Check failure on line 52 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.

Check failure on line 52 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.

Check failure on line 52 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.

Check failure on line 52 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.

Check failure on line 52 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.

Check failure on line 52 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$categoryId is assigned outside of the constructor.
$result->name = $name;

Check failure on line 53 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.

Check failure on line 53 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.

Check failure on line 53 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.

Check failure on line 53 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.

Check failure on line 53 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.

Check failure on line 53 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$name is assigned outside of the constructor.
$result->icon = $icon;

Check failure on line 54 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

Check failure on line 54 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

Check failure on line 54 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

Check failure on line 54 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

Check failure on line 54 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

Check failure on line 54 in src/inventory/data/CreativeGroup.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Readonly property pocketmine\inventory\data\CreativeGroup::$icon is assigned outside of the constructor.

public function setIcon(?Item $icon) : void{
$this->icon = $icon;
return $result;
}
}
12 changes: 6 additions & 6 deletions src/network/mcpe/cache/CreativeInventoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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"))]
);
}

Expand Down
2 changes: 0 additions & 2 deletions tools/generate-bedrock-data-from-packets.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ public function handleItemRegistry(ItemRegistryPacket $packet) : bool{
public function handleCreativeContent(CreativeContentPacket $packet) : bool{
echo "updating creative inventory data\n";

/** @var array<int, ItemStackData[]> $groupItems */
$groupItems = array_reduce($packet->getItems(), function (array $carry, CreativeItemEntry $item) : array{
$carry[$item->getGroupId()][] = self::objectToOrderedArray($this->itemStackToJson($item->getItem()));
return $carry;
Expand All @@ -306,7 +305,6 @@ public function handleCreativeContent(CreativeContentPacket $packet) : bool{
CreativeContentPacket::CATEGORY_ITEMS => "items",
];

/** @var array<int, CreativeGroupData[]> $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()])){
Expand Down

0 comments on commit 3d2fc31

Please sign in to comment.