mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-19 18:04:07 +00:00
t Merge remote-tracking branch 'origin/next-minor' into next-major
This commit is contained in:
commit
8c651ce4b4
@ -24,8 +24,6 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\inventory;
|
namespace pocketmine\inventory;
|
||||||
|
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\player\Player;
|
|
||||||
use function count;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An inventory which is backed by another inventory, and acts as a proxy to that inventory.
|
* An inventory which is backed by another inventory, and acts as a proxy to that inventory.
|
||||||
@ -37,16 +35,25 @@ class DelegateInventory extends BaseInventory{
|
|||||||
private Inventory $backingInventory
|
private Inventory $backingInventory
|
||||||
){
|
){
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
$weakThis = \WeakReference::create($this);
|
||||||
$this->backingInventory->getListeners()->add($this->inventoryListener = new CallbackInventoryListener(
|
$this->backingInventory->getListeners()->add($this->inventoryListener = new CallbackInventoryListener(
|
||||||
function(Inventory $unused, int $slot, Item $oldItem) : void{
|
static function(Inventory $unused, int $slot, Item $oldItem) use ($weakThis) : void{
|
||||||
$this->onSlotChange($slot, $oldItem);
|
if(($strongThis = $weakThis->get()) !== null){
|
||||||
|
$strongThis->onSlotChange($slot, $oldItem);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
function(Inventory $unused, array $oldContents) : void{
|
static function(Inventory $unused, array $oldContents) use ($weakThis) : void{
|
||||||
$this->onContentChange($oldContents);
|
if(($strongThis = $weakThis->get()) !== null){
|
||||||
|
$strongThis->onContentChange($oldContents);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __destruct(){
|
||||||
|
$this->backingInventory->getListeners()->remove($this->inventoryListener);
|
||||||
|
}
|
||||||
|
|
||||||
public function getSize() : int{
|
public function getSize() : int{
|
||||||
return $this->backingInventory->getSize();
|
return $this->backingInventory->getSize();
|
||||||
}
|
}
|
||||||
@ -66,12 +73,4 @@ class DelegateInventory extends BaseInventory{
|
|||||||
protected function internalSetContents(array $items) : void{
|
protected function internalSetContents(array $items) : void{
|
||||||
$this->backingInventory->setContents($items);
|
$this->backingInventory->setContents($items);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onClose(Player $who) : void{
|
|
||||||
parent::onClose($who);
|
|
||||||
if(count($this->getViewers()) === 0 && count($this->getListeners()->toArray()) === 1){
|
|
||||||
$this->backingInventory->getListeners()->remove($this->inventoryListener);
|
|
||||||
$this->inventoryListener = CallbackInventoryListener::onAnyChange(static function() : void{}); //break cyclic reference
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user