PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
Loading...
Searching...
No Matches
SimpleInventory.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 function array_map;
31use function spl_object_id;
32
41 protected \SplFixedArray $slots;
42
47 protected array $windows = [];
48
49 public function __construct(int $size){
50 $this->slots = new \SplFixedArray($size);
51 parent::__construct();
52 }
53
57 public function getSize() : int{
58 return $this->slots->getSize();
59 }
60
61 public function getItem(int $index) : Item{
62 return $this->slots[$index] !== null ? clone $this->slots[$index] : VanillaItems::AIR();
63 }
64
65 protected function internalSetItem(int $index, Item $item) : void{
66 $this->slots[$index] = $item->isNull() ? null : $item;
67 }
68
73 public function getContents(bool $includeEmpty = false) : array{
74 $contents = [];
75
76 foreach($this->slots as $i => $slot){
77 if($slot !== null){
78 $contents[$i] = clone $slot;
79 }elseif($includeEmpty){
80 $contents[$i] = VanillaItems::AIR();
81 }
82 }
83
84 return $contents;
85 }
86
87 protected function internalSetContents(array $items) : void{
88 for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
89 if(!isset($items[$i]) || $items[$i]->isNull()){
90 $this->slots[$i] = null;
91 }else{
92 $this->slots[$i] = clone $items[$i];
93 }
94 }
95 }
96
97 public function getMatchingItemCount(int $slot, Item $test, bool $checkTags) : int{
98 $slotItem = $this->slots[$slot];
99 return $slotItem !== null && $slotItem->equals($test, true, $checkTags) ? $slotItem->getCount() : 0;
100 }
101
102 public function isSlotEmpty(int $index) : bool{
103 return $this->slots[$index] === null || $this->slots[$index]->isNull();
104 }
105
109 public function getViewers() : array{
110 $result = [];
111 //this can't use array_map - the result needs to be keyed by spl_object_id(player), not spl_object_id(window)
112 foreach($this->windows as $window){
113 $player = $window->getViewer();
114 $result[spl_object_id($player)] = $player;
115 }
116 return $result;
117 }
118
122 public function removeAllWindows() : void{
123 foreach($this->windows as $hash => $window){
124 $viewer = $window->getViewer();
125 if($window->getViewer()->getCurrentWindow() === $window){
126 $viewer->removeCurrentWindow();
127 }
128 unset($this->windows[$hash]);
129 }
130 }
131
132 public function onOpen(InventoryWindow $window) : void{
133 $this->windows[spl_object_id($window)] = $window;
134 }
135
136 public function onClose(InventoryWindow $window) : void{
137 unset($this->windows[spl_object_id($window)]);
138 }
139}
getContents(bool $includeEmpty=false)
getMatchingItemCount(int $slot, Item $test, bool $checkTags)