PocketMine-MP 5.14.2 git-50e2c469a547a16a23b2dc691e70a51d34e29395
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
32use pocketmine\utils\SingletonTrait;
34
36 use SingletonTrait;
37 use DestructorCallbackTrait;
38
40 private array $creative = [];
41
43 private ObjectSet $contentChangedCallbacks;
44
45 private function __construct(){
46 $this->contentChangedCallbacks = new ObjectSet();
48 BedrockDataFiles::CREATIVEITEMS_JSON,
49 ItemStackData::class
50 );
51 foreach($creativeItems as $data){
52 $item = CraftingManagerFromDataHelper::deserializeItemStack($data);
53 if($item === null){
54 //unknown item
55 continue;
56 }
57 $this->add($item);
58 }
59 }
60
65 public function clear() : void{
66 $this->creative = [];
67 $this->onContentChange();
68 }
69
73 public function getAll() : array{
74 return Utils::cloneObjectArray($this->creative);
75 }
76
77 public function getItem(int $index) : ?Item{
78 return isset($this->creative[$index]) ? clone $this->creative[$index] : null;
79 }
80
81 public function getItemIndex(Item $item) : int{
82 foreach($this->creative as $i => $d){
83 if($item->equals($d, true, false)){
84 return $i;
85 }
86 }
87
88 return -1;
89 }
90
95 public function add(Item $item) : void{
96 $this->creative[] = clone $item;
97 $this->onContentChange();
98 }
99
104 public function remove(Item $item) : void{
105 $index = $this->getItemIndex($item);
106 if($index !== -1){
107 unset($this->creative[$index]);
108 $this->onContentChange();
109 }
110 }
111
112 public function contains(Item $item) : bool{
113 return $this->getItemIndex($item) !== -1;
114 }
115
118 return $this->contentChangedCallbacks;
119 }
120
121 private function onContentChange() : void{
122 foreach($this->contentChangedCallbacks as $callback){
123 $callback();
124 }
125 }
126}
static loadJsonArrayOfObjectsFile(string $filePath, string $modelCLass)