40 use DestructorCallbackTrait;
46 private array $creative = [];
49 private ObjectSet $contentChangedCallbacks;
51 private function __construct(){
52 $this->contentChangedCallbacks =
new ObjectSet();
55 "construction" => CreativeCategory::CONSTRUCTION,
56 "nature" => CreativeCategory::NATURE,
57 "equipment" => CreativeCategory::EQUIPMENT,
58 "items" => CreativeCategory::ITEMS,
59 ] as $categoryId => $categoryEnum){
61 Path::join(BedrockDataFiles::CREATIVE, $categoryId .
".json"),
62 CreativeGroupData::class
65 foreach($groups as $groupData){
66 $icon = $groupData->group_icon ===
null ? null : CraftingManagerFromDataHelper::deserializeItemStack($groupData->group_icon);
73 $items = array_filter(array_map(
static fn($itemStack) => CraftingManagerFromDataHelper::deserializeItemStack($itemStack), $groupData->items));
75 foreach($items as $item){
76 $this->
add($item, $categoryEnum, $group);
88 $this->onContentChange();
104 return $this->creative;
107 public function getItem(
int $index) : ?
Item{
108 return $this->getEntry($index)?->getItem();
111 public function getEntry(
int $index) : ?CreativeInventoryEntry{
112 return $this->creative[$index] ?? null;
115 public function getItemIndex(Item $item) : int{
116 foreach($this->creative as $i => $d){
117 if($d->matchesItem($item)){
131 $this->onContentChange();
138 public function remove(
Item $item) : void{
139 $index = $this->getItemIndex($item);
141 unset($this->creative[$index]);
142 $this->onContentChange();
146 public function contains(Item $item) : bool{
147 return $this->getItemIndex($item) !== -1;
152 return $this->contentChangedCallbacks;
155 private function onContentChange() : void{
156 foreach($this->contentChangedCallbacks as $callback){