Skip to content

Commit

Permalink
EnchantingTableInventoryWindow: cleanup weakref mess
Browse files Browse the repository at this point in the history
this really needs a shorter name
  • Loading branch information
dktapps committed Nov 25, 2024
1 parent edf4e9d commit d69d8d6
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/block/inventory/window/EnchantingTableInventoryWindow.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ final class EnchantingTableInventoryWindow extends BlockInventoryWindow{
/** @var EnchantingOption[] $options */
private array $options = [];

/** @phpstan-var \WeakReference<InventoryListener> */
private \WeakReference $listener;
private InventoryListener $listener;

public function __construct(
Player $viewer,
Expand All @@ -54,7 +53,7 @@ public function __construct(

/** @phpstan-var \WeakReference<self> $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();
Expand All @@ -66,16 +65,11 @@ public function __construct(
}
}
);
$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{
Expand Down

0 comments on commit d69d8d6

Please sign in to comment.