Skip to content

Commit 9f3f029

Browse files
committed
Customies FIX (beta-8)
1 parent 2dcb445 commit 9f3f029

1 file changed

Lines changed: 109 additions & 122 deletions

File tree

Lines changed: 109 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
<?php
2-
declare(strict_types = 1);
2+
declare(strict_types=1);
33

44
namespace imperazim\vendor\customies\item;
55

66
use InvalidArgumentException;
77
use pocketmine\block\Block;
88
use pocketmine\data\bedrock\item\BlockItemIdMap;
99
use pocketmine\data\bedrock\item\SavedItemData;
10-
use pocketmine\inventory\CreativeGroup;
11-
use pocketmine\inventory\CreativeCategory;
1210
use pocketmine\inventory\CreativeInventory;
1311
use pocketmine\item\Item;
14-
use pocketmine\item\ItemTypeIds;
1512
use pocketmine\item\ItemIdentifier;
13+
use pocketmine\item\ItemTypeIds;
1614
use pocketmine\item\StringToItemParser;
17-
use pocketmine\nbt\tag\CompoundTag;
1815
use pocketmine\network\mcpe\convert\TypeConverter;
1916
use pocketmine\network\mcpe\protocol\types\CacheableNbt;
17+
use pocketmine\network\mcpe\protocol\types\ItemComponentPacketEntry;
2018
use pocketmine\network\mcpe\protocol\types\ItemTypeEntry;
2119
use pocketmine\utils\SingletonTrait;
2220
use pocketmine\utils\Utils;
@@ -25,121 +23,110 @@
2523
use function array_values;
2624

2725
final class CustomiesItemFactory {
28-
use SingletonTrait;
29-
30-
/** @var ItemTypeEntry[] */
31-
private array $itemTableEntries = [];
32-
33-
/**
34-
* Identifier in first key of the array and item object in the second
35-
* @var array<array{id: int, id_string: string, item: Item}> $itemRegisteredList
36-
*/
37-
private array $itemRegisteredList = [];
38-
39-
/**
40-
* Get a custom item from its identifier. An exception will be thrown if the item is not registered.
41-
*/
42-
public function get(string $identifier, int $amount = 1): Item {
43-
$item = StringToItemParser::getInstance()->parse($identifier);
44-
45-
if ($item === null) {
46-
throw new InvalidArgumentException("Custom item " . $identifier . " is not registered");
47-
}
48-
return $item->setCount($amount);
49-
}
50-
51-
/**
52-
* Returns the item properties CompoundTag which maps out all custom item properties.
53-
* @return ItemTypeEntry[]
54-
*/
55-
public function getItemComponentEntries(): array {
56-
return $this->itemTableEntries;
57-
}
58-
59-
/**
60-
* Returns custom item entries for the StartGamePacket itemTable property.
61-
* @return ItemTypeEntry[]
62-
*/
63-
public function getItemTableEntries(): array {
64-
return array_values($this->itemTableEntries);
65-
}
66-
67-
/**
68-
* Registers the item to the item factory and assigns it an ID. It also updates the required mappings and stores the
69-
* item components if present.
70-
* @phpstan-param class-string $className
71-
* @phpstan-param class-string $identifier
72-
* @phpstan-param class-string $name
73-
* @phpstan-param CreativeCategory|null $category
74-
* @phpstan-param CreativeGroup|null $group
75-
*/
76-
public function registerItem(string $className, string $identifier, string $name, ?CreativeCategory $category = null, ?CreativeGroup $group = null): void {
77-
if ($className !== Item::class) {
78-
Utils::testValidInstance($className, Item::class);
79-
}
80-
81-
$itemId = ItemTypeIds::newId();
82-
$item = new $className(new ItemIdentifier($itemId), $name);
83-
$this->registerCustomItemMapping($identifier, $itemId);
84-
85-
GlobalItemDataHandlers::getDeserializer()->map($identifier, fn() => clone $item);
86-
GlobalItemDataHandlers::getSerializer()->map($item, fn() => new SavedItemData($identifier));
87-
88-
StringToItemParser::getInstance()->register($identifier, fn() => clone $item);
89-
90-
$this->itemRegisteredList[] = [
91-
"id" => $itemId,
92-
"id_string" => $identifier,
93-
"item" => $item
94-
];
95-
96-
$this->itemTableEntries[$identifier] = new ItemTypeEntry($identifier, $itemId, true, 1,
97-
new CacheableNbt($item->getComponents()
98-
->setInt("id", $itemId)
99-
->setString("name", $identifier)
100-
)
101-
);
102-
if ($category === null) {
103-
$category = CreativeCategory::ITEMS;
104-
}
105-
106-
CreativeInventory::getInstance()->add($item, $category, $group);
107-
}
108-
109-
/**
110-
* Registers a custom item ID to the required mappings in the global ItemTypeDictionary instance.
111-
*/
112-
private function registerCustomItemMapping(string $identifier, int $itemId): void {
113-
$dictionary = TypeConverter::getInstance()->getItemTypeDictionary();
114-
$reflection = new ReflectionClass($dictionary);
115-
116-
$intToString = $reflection->getProperty("intToStringIdMap");
117-
/** @var int[] $value */
118-
$value = $intToString->getValue($dictionary);
119-
$intToString->setValue($dictionary, $value + [$itemId => $identifier]);
120-
121-
$stringToInt = $reflection->getProperty("stringToIntMap");
122-
/** @var int[] $value */
123-
$value = $stringToInt->getValue($dictionary);
124-
$stringToInt->setValue($dictionary, $value + [$identifier => $itemId]);
125-
}
126-
127-
/**
128-
* Registers the required mappings for the block to become an item that can be placed etc. It is assigned an ID that
129-
* correlates to its block ID.
130-
*/
131-
public function registerBlockItem(string $identifier, Block $block): void {
132-
$itemId = $block->getIdInfo()->getBlockTypeId();
133-
$this->registerCustomItemMapping($identifier, $itemId);
134-
StringToItemParser::getInstance()->registerBlock($identifier, fn() => clone $block);
135-
$this->itemTableEntries[] = new ItemTypeEntry($identifier, $itemId, false, 0, new CacheableNbt(new CompoundTag()));
136-
137-
$blockItemIdMap = BlockItemIdMap::getInstance();
138-
$reflection = new ReflectionClass($blockItemIdMap);
139-
140-
$itemToBlockId = $reflection->getProperty("itemToBlockId");
141-
/** @var string[] $value */
142-
$value = $itemToBlockId->getValue($blockItemIdMap);
143-
$itemToBlockId->setValue($blockItemIdMap, $value + [$identifier => $identifier]);
144-
}
26+
use SingletonTrait;
27+
28+
/**
29+
* @var ItemTypeEntry[]
30+
*/
31+
private array $itemTableEntries = [];
32+
/**
33+
* @var ItemComponentPacketEntry[]
34+
*/
35+
private array $itemComponentEntries = [];
36+
37+
/**
38+
* Get a custom item from its identifier. An exception will be thrown if the item is not registered.
39+
*/
40+
public function get(string $identifier, int $amount = 1): ?Item {
41+
$item = StringToItemParser::getInstance()->parse($identifier);
42+
if($item === null) {
43+
return null;
44+
}
45+
return $item->setCount($amount);
46+
}
47+
48+
/**
49+
* Returns the item properties CompoundTag which maps out all custom item properties.
50+
* @return ItemComponentPacketEntry[]
51+
*/
52+
public function getItemComponentEntries(): array {
53+
return $this->itemComponentEntries;
54+
}
55+
56+
/**
57+
* Returns custom item entries for the StartGamePacket itemTable property.
58+
* @return ItemTypeEntry[]
59+
*/
60+
public function getItemTableEntries(): array {
61+
return array_values($this->itemTableEntries);
62+
}
63+
64+
/**
65+
* Registers the item to the item factory and assigns it an ID. It also updates the required mappings and stores the
66+
* item components if present.
67+
* @phpstan-param class-string $className
68+
*/
69+
public function registerItem(string $className, string $identifier, string $name): void {
70+
if($className !== Item::class) {
71+
Utils::testValidInstance($className, Item::class);
72+
}
73+
74+
$itemId = ItemTypeIds::newId();
75+
$item = new $className(new ItemIdentifier($itemId), $name);
76+
$this->registerCustomItemMapping($identifier, $itemId);
77+
78+
GlobalItemDataHandlers::getDeserializer()->map($identifier, fn() => clone $item);
79+
GlobalItemDataHandlers::getSerializer()->map($item, fn() => new SavedItemData($identifier));
80+
81+
StringToItemParser::getInstance()->register($identifier, fn() => clone $item);
82+
83+
if(($componentBased = $item instanceof ItemComponents)) {
84+
$this->itemComponentEntries[$identifier] = new ItemComponentPacketEntry($identifier,
85+
new CacheableNbt($item->getComponents()
86+
->setInt("id", $itemId)
87+
->setString("name", $identifier)
88+
)
89+
);
90+
}
91+
92+
$this->itemTableEntries[$identifier] = new ItemTypeEntry($identifier, $itemId, $componentBased);
93+
CreativeInventory::getInstance()->add($item);
94+
}
95+
96+
/**
97+
* Registers a custom item ID to the required mappings in the global ItemTypeDictionary instance.
98+
*/
99+
private function registerCustomItemMapping(string $identifier, int $itemId): void {
100+
$dictionary = TypeConverter::getInstance()->getItemTypeDictionary();
101+
$reflection = new ReflectionClass($dictionary);
102+
103+
$intToString = $reflection->getProperty("intToStringIdMap");
104+
/** @var int[] $value */
105+
$value = $intToString->getValue($dictionary);
106+
$intToString->setValue($dictionary, $value + [$itemId => $identifier]);
107+
108+
$stringToInt = $reflection->getProperty("stringToIntMap");
109+
/** @var int[] $value */
110+
$value = $stringToInt->getValue($dictionary);
111+
$stringToInt->setValue($dictionary, $value + [$identifier => $itemId]);
112+
}
113+
114+
/**
115+
* Registers the required mappings for the block to become an item that can be placed etc. It is assigned an ID that
116+
* correlates to its block ID.
117+
*/
118+
public function registerBlockItem(string $identifier, Block $block): void {
119+
$itemId = $block->getIdInfo()->getBlockTypeId();
120+
$this->registerCustomItemMapping($identifier, $itemId);
121+
StringToItemParser::getInstance()->registerBlock($identifier, fn() => clone $block);
122+
$this->itemTableEntries[] = new ItemTypeEntry($identifier, $itemId, false);
123+
124+
$blockItemIdMap = BlockItemIdMap::getInstance();
125+
$reflection = new ReflectionClass($blockItemIdMap);
126+
127+
$itemToBlockId = $reflection->getProperty("itemToBlockId");
128+
/** @var string[] $value */
129+
$value = $itemToBlockId->getValue($blockItemIdMap);
130+
$itemToBlockId->setValue($blockItemIdMap, $value + [$identifier => $identifier]);
131+
}
145132
}

0 commit comments

Comments
 (0)