From d69d8d6217bc57774769768c59b063f9bd14bfd0 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 25 Nov 2024 11:28:54 +0000 Subject: [PATCH] EnchantingTableInventoryWindow: cleanup weakref mess this really needs a shorter name --- .../window/EnchantingTableInventoryWindow.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/block/inventory/window/EnchantingTableInventoryWindow.php b/src/block/inventory/window/EnchantingTableInventoryWindow.php index 68ef0084c..294c18189 100644 --- a/src/block/inventory/window/EnchantingTableInventoryWindow.php +++ b/src/block/inventory/window/EnchantingTableInventoryWindow.php @@ -43,8 +43,7 @@ final class EnchantingTableInventoryWindow extends BlockInventoryWindow{ /** @var EnchantingOption[] $options */ private array $options = []; - /** @phpstan-var \WeakReference */ - private \WeakReference $listener; + private InventoryListener $listener; public function __construct( Player $viewer, @@ -54,7 +53,7 @@ final class EnchantingTableInventoryWindow extends BlockInventoryWindow{ /** @phpstan-var \WeakReference $weakThis */ $weakThis = \WeakReference::create($this); - $listener = new CallbackInventoryListener( + $this->listener = new CallbackInventoryListener( onSlotChange: static function(Inventory $_, int $slot) use ($weakThis) : void{ //remaining params unneeded if($slot === self::SLOT_INPUT && ($strongThis = $weakThis->get()) !== null){ $strongThis->regenerateOptions(); @@ -66,16 +65,11 @@ final class EnchantingTableInventoryWindow extends BlockInventoryWindow{ } } ); - $this->inventory->getListeners()->add($listener); - - $this->listener = \WeakReference::create($listener); + $this->inventory->getListeners()->add($this->listener); } public function __destruct(){ - $listener = $this->listener->get(); - if($listener !== null){ - $this->inventory->getListeners()->remove($listener); - } + $this->inventory->getListeners()->remove($this->listener); } private function regenerateOptions() : void{