PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
Loading...
Searching...
No Matches
SlotChangeActionBuilder.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\transaction;
25
32
40
45 private \SplFixedArray $changedSlots;
46
47 public function __construct(
48 private InventoryWindow $inventoryWindow
49 ){
50 parent::__construct();
51 $this->changedSlots = new \SplFixedArray($this->inventoryWindow->getInventory()->getSize());
52 }
53
54 public function getInventoryWindow() : InventoryWindow{
55 return $this->inventoryWindow;
56 }
57
58 protected function internalSetContents(array $items) : void{
59 for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
60 if(!isset($items[$i])){
61 $this->clear($i);
62 }else{
63 $this->setItem($i, $items[$i]);
64 }
65 }
66 }
67
68 protected function internalSetItem(int $index, Item $item) : void{
69 if(!$item->equalsExact($this->inventoryWindow->getInventory()->getItem($index))){
70 $this->changedSlots[$index] = $item->isNull() ? VanillaItems::AIR() : clone $item;
71 }
72 }
73
74 public function getSize() : int{
75 return $this->inventoryWindow->getInventory()->getSize();
76 }
77
78 public function getItem(int $index) : Item{
79 return $this->changedSlots[$index] !== null ? clone $this->changedSlots[$index] : $this->inventoryWindow->getInventory()->getItem($index);
80 }
81
82 public function getContents(bool $includeEmpty = false) : array{
83 $contents = $this->inventoryWindow->getInventory()->getContents($includeEmpty);
84 foreach($this->changedSlots as $index => $item){
85 if($item !== null){
86 if($includeEmpty || !$item->isNull()){
87 $contents[$index] = clone $item;
88 }else{
89 unset($contents[$index]);
90 }
91 }
92 }
93 return $contents;
94 }
95
96 public function getMatchingItemCount(int $slot, Item $test, bool $checkTags) : int{
97 $slotItem = $this->changedSlots[$slot] ?? null;
98 if($slotItem !== null){
99 return $slotItem->equals($test, true, $checkTags) ? $slotItem->getCount() : 0;
100 }
101 return $this->inventoryWindow->getInventory()->getMatchingItemCount($slot, $test, $checkTags);
102 }
103
107 public function generateActions() : array{
108 $result = [];
109 $inventory = $this->inventoryWindow->getInventory();
110 foreach($this->changedSlots as $index => $newItem){
111 if($newItem !== null){
112 $oldItem = $inventory->getItem($index);
113 if(!$newItem->equalsExact($oldItem)){
114 $result[] = new SlotChangeAction($this->inventoryWindow, $index, $oldItem, $newItem);
115 }
116 }
117 }
118 return $result;
119 }
120
121 public function getViewers() : array{
122 return [];
123 }
124
125 public function removeAllWindows() : void{
126 //NOOP
127 }
128
129 public function onOpen(InventoryWindow $window) : void{
130 //NOOP
131 }
132
133 public function onClose(InventoryWindow $window) : void{
134 //NOOP
135 }
136}
setItem(int $index, Item $item)