PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
DelegateInventory.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
27
32 private InventoryListener $inventoryListener;
33 private bool $backingInventoryChanging = false;
34
35 public function __construct(
36 private Inventory $backingInventory
37 ){
38 parent::__construct();
39 $weakThis = \WeakReference::create($this);
40 $this->backingInventory->getListeners()->add($this->inventoryListener = new CallbackInventoryListener(
41 static function(Inventory $unused, int $slot, Item $oldItem) use ($weakThis) : void{
42 if(($strongThis = $weakThis->get()) !== null){
43 $strongThis->backingInventoryChanging = true;
44 try{
45 $strongThis->onSlotChange($slot, $oldItem);
46 }finally{
47 $strongThis->backingInventoryChanging = false;
48 }
49 }
50 },
51 static function(Inventory $unused, array $oldContents) use ($weakThis) : void{
52 if(($strongThis = $weakThis->get()) !== null){
53 $strongThis->backingInventoryChanging = true;
54 try{
55 $strongThis->onContentChange($oldContents);
56 }finally{
57 $strongThis->backingInventoryChanging = false;
58 }
59 }
60 }
61 ));
62 }
63
64 public function __destruct(){
65 $this->backingInventory->getListeners()->remove($this->inventoryListener);
66 }
67
68 public function getSize() : int{
69 return $this->backingInventory->getSize();
70 }
71
72 public function getItem(int $index) : Item{
73 return $this->backingInventory->getItem($index);
74 }
75
76 protected function internalSetItem(int $index, Item $item) : void{
77 $this->backingInventory->setItem($index, $item);
78 }
79
80 public function getContents(bool $includeEmpty = false) : array{
81 return $this->backingInventory->getContents($includeEmpty);
82 }
83
84 protected function internalSetContents(array $items) : void{
85 $this->backingInventory->setContents($items);
86 }
87
88 public function isSlotEmpty(int $index) : bool{
89 return $this->backingInventory->isSlotEmpty($index);
90 }
91
92 protected function onSlotChange(int $index, Item $before) : void{
93 if($this->backingInventoryChanging){
94 parent::onSlotChange($index, $before);
95 }
96 }
97
98 protected function onContentChange(array $itemsBefore) : void{
99 if($this->backingInventoryChanging){
100 parent::onContentChange($itemsBefore);
101 }
102 }
103}