PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
30use pocketmine\utils\DestructorCallbackTrait;
32use pocketmine\utils\SingletonTrait;
34
36 use SingletonTrait;
37 use DestructorCallbackTrait;
38
43 private array $creative = [];
44
46 private ObjectSet $contentChangedCallbacks;
47
48 private function __construct(){
49 $this->contentChangedCallbacks = new ObjectSet();
51 BedrockDataFiles::CREATIVEITEMS_JSON,
52 ItemStackData::class
53 );
54 foreach($creativeItems as $data){
55 $item = CraftingManagerFromDataHelper::deserializeItemStack($data);
56 if($item === null){
57 //unknown item
58 continue;
59 }
60 $this->add($item);
61 }
62 }
63
68 public function clear() : void{
69 $this->creative = [];
70 $this->onContentChange();
71 }
72
77 public function getAll() : array{
78 return Utils::cloneObjectArray($this->creative);
79 }
80
81 public function getItem(int $index) : ?Item{
82 return isset($this->creative[$index]) ? clone $this->creative[$index] : null;
83 }
84
85 public function getItemIndex(Item $item) : int{
86 foreach($this->creative as $i => $d){
87 if($item->equals($d, true, false)){
88 return $i;
89 }
90 }
91
92 return -1;
93 }
94
99 public function add(Item $item) : void{
100 $this->creative[] = clone $item;
101 $this->onContentChange();
102 }
103
108 public function remove(Item $item) : void{
109 $index = $this->getItemIndex($item);
110 if($index !== -1){
111 unset($this->creative[$index]);
112 $this->onContentChange();
113 }
114 }
115
116 public function contains(Item $item) : bool{
117 return $this->getItemIndex($item) !== -1;
118 }
119
122 return $this->contentChangedCallbacks;
123 }
124
125 private function onContentChange() : void{
126 foreach($this->contentChangedCallbacks as $callback){
127 $callback();
128 }
129 }
130}
static loadJsonArrayOfObjectsFile(string $filePath, string $modelCLass)