41 protected int $maxStackSize = Inventory::MAX_STACK;
50 public function __construct(){
56 return $this->maxStackSize;
60 $this->maxStackSize = $size;
63 abstract protected function internalSetItem(
int $index,
Item $item) : void;
67 $item = VanillaItems::AIR();
72 $oldItem = $this->getItem($index);
74 $this->internalSetItem($index, $item);
75 $this->onSlotChange($index, $oldItem);
89 Utils::validateArrayValueType($items, function(
Item $item) : void{});
90 if(count($items) > $this->getSize()){
91 $items = array_slice($items, 0, $this->getSize(),
true);
94 $oldContents = $this->getContents(
true);
96 $listeners = $this->listeners->
toArray();
97 $this->listeners->clear();
99 $this->internalSetContents($items);
101 $this->listeners->add(...$listeners);
103 $this->onContentChange($oldContents);
107 $count = max(1, $item->getCount());
109 for($i = 0, $size = $this->getSize(); $i < $size; $i++){
110 $slotCount = $this->getMatchingItemCount($i, $item, $checkTags);
112 $count -= $slotCount;
125 for($i = 0, $size = $this->getSize(); $i < $size; $i++){
126 if($this->getMatchingItemCount($i, $item, $checkTags) > 0){
127 $slots[$i] = $this->getItem($i);
134 public function first(
Item $item,
bool $exact =
false) : int{
135 $count = $exact ? $item->getCount() : max(1, $item->getCount());
138 for($i = 0, $size = $this->getSize(); $i < $size; $i++){
139 $slotCount = $this->getMatchingItemCount($i, $item, $checkTags);
140 if($slotCount > 0 && ($slotCount === $count || (!$exact && $slotCount > $count))){
149 for($i = 0, $size = $this->getSize(); $i < $size; $i++){
150 if($this->isSlotEmpty($i)){
159 return $this->getAddableItemQuantity($item) === $item->getCount();
163 $count = $item->getCount();
164 $maxStackSize = min($this->getMaxStackSize(), $item->
getMaxStackSize());
166 for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
167 if($this->isSlotEmpty($i)){
168 $count -= $maxStackSize;
170 $slotCount = $this->getMatchingItemCount($i, $item,
true);
171 if($slotCount > 0 && ($diff = $maxStackSize - $slotCount) > 0){
177 return $item->getCount();
181 return $item->getCount() - $count;
188 foreach($slots as $slot){
189 if(!$slot->isNull()){
190 $itemSlots[] = clone $slot;
197 foreach($itemSlots as $item){
198 $leftover = $this->internalAddItem($item);
199 if(!$leftover->isNull()){
200 $returnSlots[] = $leftover;
207 private function internalAddItem(Item $newItem) : Item{
210 $maxStackSize = min($this->getMaxStackSize(), $newItem->getMaxStackSize());
212 for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
213 if($this->isSlotEmpty($i)){
217 $slotCount = $this->getMatchingItemCount($i, $newItem,
true);
218 if($slotCount === 0){
222 if($slotCount < $maxStackSize){
223 $amount = min($maxStackSize - $slotCount, $newItem->getCount());
225 $newItem->setCount($newItem->getCount() - $amount);
226 $slotItem = $this->getItem($i);
227 $slotItem->setCount($slotItem->getCount() + $amount);
228 $this->setItem($i, $slotItem);
229 if($newItem->getCount() <= 0){
236 if(count($emptySlots) > 0){
237 foreach($emptySlots as $slotIndex){
238 $amount = min($maxStackSize, $newItem->getCount());
239 $newItem->setCount($newItem->getCount() - $amount);
240 $slotItem = clone $newItem;
241 $slotItem->setCount($amount);
242 $this->setItem($slotIndex, $slotItem);
243 if($newItem->getCount() <= 0){
252 public function remove(
Item $item) : void{
253 $checkTags = $item->hasNamedTag();
255 for($i = 0, $size = $this->getSize(); $i < $size; $i++){
256 if($this->getMatchingItemCount($i, $item, $checkTags) > 0){
264 foreach($slots as $slot){
265 if(!$slot->isNull()){
266 $searchItems[] = clone $slot;
270 for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
271 if($this->isSlotEmpty($i)){
275 foreach($searchItems as $index => $search){
276 $slotCount = $this->getMatchingItemCount($i, $search, $search->hasNamedTag());
278 $amount = min($slotCount, $search->getCount());
279 $search->setCount($search->getCount() - $amount);
281 $slotItem = $this->getItem($i);
282 $slotItem->setCount($slotItem->getCount() - $amount);
283 $this->setItem($i, $slotItem);
284 if($search->getCount() <= 0){
285 unset($searchItems[$index]);
290 if(count($searchItems) === 0){
298 public function clear(
int $index) : void{
303 $this->setContents([]);
306 public function swap(
int $slot1,
int $slot2) : void{
307 $i1 = $this->getItem($slot1);
308 $i2 = $this->getItem($slot2);
309 $this->setItem($slot1, $i2);
310 $this->setItem($slot2, $i1);
313 protected function onSlotChange(
int $index,
Item $before) : void{
314 foreach($this->listeners as $listener){
315 $listener->onSlotChange($this, $index, $before);
324 foreach($this->listeners as $listener){
325 $listener->onContentChange($this, $itemsBefore);
330 return $slot >= 0 && $slot < $this->getSize();
334 return $this->listeners;
338 return $this->validators;