11<?php
2- declare (strict_types= 1 );
2+ declare (strict_types = 1 );
33
44namespace imperazim \vendor \customies \item ;
55
66use InvalidArgumentException ;
77use pocketmine \block \Block ;
88use pocketmine \data \bedrock \item \BlockItemIdMap ;
99use pocketmine \data \bedrock \item \SavedItemData ;
10+ use pocketmine \inventory \CreativeGroup ;
11+ use pocketmine \inventory \CreativeCategory ;
1012use pocketmine \inventory \CreativeInventory ;
1113use pocketmine \item \Item ;
12- use pocketmine \item \ItemIdentifier ;
1314use pocketmine \item \ItemTypeIds ;
15+ use pocketmine \item \ItemIdentifier ;
1416use pocketmine \item \StringToItemParser ;
17+ use pocketmine \nbt \tag \CompoundTag ;
1518use pocketmine \network \mcpe \convert \TypeConverter ;
1619use pocketmine \network \mcpe \protocol \types \CacheableNbt ;
17- use pocketmine \network \mcpe \protocol \types \ItemComponentPacketEntry ;
1820use pocketmine \network \mcpe \protocol \types \ItemTypeEntry ;
1921use pocketmine \utils \SingletonTrait ;
2022use pocketmine \utils \Utils ;
2325use function array_values ;
2426
2527final class CustomiesItemFactory {
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- }
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+
103+ if ($ category === null ) {
104+ $ category = CreativeCategory::ITEMS ;
105+ }
106+
107+ CreativeInventory::getInstance ()->add ($ item , $ category , $ group );
108+ }
109+
110+ /**
111+ * Registers a custom item ID to the required mappings in the global ItemTypeDictionary instance.
112+ */
113+ private function registerCustomItemMapping (string $ identifier , int $ itemId ): void {
114+ $ dictionary = TypeConverter::getInstance ()->getItemTypeDictionary ();
115+ $ reflection = new ReflectionClass ($ dictionary );
116+
117+ $ intToString = $ reflection ->getProperty ("intToStringIdMap " );
118+ /** @var int[] $value */
119+ $ value = $ intToString ->getValue ($ dictionary );
120+ $ intToString ->setValue ($ dictionary , $ value + [$ itemId => $ identifier ]);
121+
122+ $ stringToInt = $ reflection ->getProperty ("stringToIntMap " );
123+ /** @var int[] $value */
124+ $ value = $ stringToInt ->getValue ($ dictionary );
125+ $ stringToInt ->setValue ($ dictionary , $ value + [$ identifier => $ itemId ]);
126+ }
127+
128+ /**
129+ * Registers the required mappings for the block to become an item that can be placed etc. It is assigned an ID that
130+ * correlates to its block ID.
131+ */
132+ public function registerBlockItem (string $ identifier , Block $ block ): void {
133+ $ itemId = $ block ->getIdInfo ()->getBlockTypeId ();
134+ $ this ->registerCustomItemMapping ($ identifier , $ itemId );
135+ StringToItemParser::getInstance ()->registerBlock ($ identifier , fn () => clone $ block );
136+ $ this ->itemTableEntries [] = new ItemTypeEntry ($ identifier , $ itemId , false , 0 , new CacheableNbt (new CompoundTag ()));
137+
138+ $ blockItemIdMap = BlockItemIdMap::getInstance ();
139+ $ reflection = new ReflectionClass ($ blockItemIdMap );
140+
141+ $ itemToBlockId = $ reflection ->getProperty ("itemToBlockId " );
142+ /** @var string[] $value */
143+ $ value = $ itemToBlockId ->getValue ($ blockItemIdMap );
144+ $ itemToBlockId ->setValue ($ blockItemIdMap , $ value + [$ identifier => $ identifier ]);
145+ }
132146}
0 commit comments