PocketMine-MP 5.25.1 git-694aa17cc916a954b10fe12721c81b1dc73eecd5
Loading...
Searching...
No Matches
CreativeInventory.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\inventory;
25
31use pocketmine\utils\DestructorCallbackTrait;
33use pocketmine\utils\SingletonTrait;
34use Symfony\Component\Filesystem\Path;
35use function array_filter;
36use function array_map;
37
39 use SingletonTrait;
40 use DestructorCallbackTrait;
41
46 private array $creative = [];
47
49 private ObjectSet $contentChangedCallbacks;
50
51 private function __construct(){
52 $this->contentChangedCallbacks = new ObjectSet();
53
54 foreach([
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
63 );
64
65 foreach($groups as $groupData){
66 $icon = $groupData->group_icon === null ? null : CraftingManagerFromDataHelper::deserializeItemStack($groupData->group_icon);
67
68 $group = $icon === null ? null : new CreativeGroup(
69 new Translatable($groupData->group_name),
70 $icon
71 );
72
73 $items = array_filter(array_map(static fn($itemStack) => CraftingManagerFromDataHelper::deserializeItemStack($itemStack), $groupData->items));
74
75 foreach($items as $item){
76 $this->add($item, $categoryEnum, $group);
77 }
78 }
79 }
80 }
81
86 public function clear() : void{
87 $this->creative = [];
88 $this->onContentChange();
89 }
90
95 public function getAll() : array{
96 return array_map(fn(CreativeInventoryEntry $entry) => $entry->getItem(), $this->creative);
97 }
98
103 public function getAllEntries() : array{
104 return $this->creative;
105 }
106
107 public function getItem(int $index) : ?Item{
108 return $this->getEntry($index)?->getItem();
109 }
110
111 public function getEntry(int $index) : ?CreativeInventoryEntry{
112 return $this->creative[$index] ?? null;
113 }
114
115 public function getItemIndex(Item $item) : int{
116 foreach($this->creative as $i => $d){
117 if($d->matchesItem($item)){
118 return $i;
119 }
120 }
121
122 return -1;
123 }
124
129 public function add(Item $item, CreativeCategory $category = CreativeCategory::ITEMS, ?CreativeGroup $group = null) : void{
130 $this->creative[] = new CreativeInventoryEntry($item, $category, $group);
131 $this->onContentChange();
132 }
133
138 public function remove(Item $item) : void{
139 $index = $this->getItemIndex($item);
140 if($index !== -1){
141 unset($this->creative[$index]);
142 $this->onContentChange();
143 }
144 }
145
146 public function contains(Item $item) : bool{
147 return $this->getItemIndex($item) !== -1;
148 }
149
152 return $this->contentChangedCallbacks;
153 }
154
155 private function onContentChange() : void{
156 foreach($this->contentChangedCallbacks as $callback){
157 $callback();
158 }
159 }
160}
static loadJsonArrayOfObjectsFile(string $filePath, string $modelClass)
add(Item $item, CreativeCategory $category=CreativeCategory::ITEMS, ?CreativeGroup $group=null)