Blocks are ephemeral objects, InventoryWindow might exceed its lifetime

This commit is contained in:
Dylan K. Taylor 2025-04-30 19:44:10 +01:00
parent 310ca684fa
commit 1245ee2575
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 10 additions and 7 deletions

View File

@ -28,29 +28,32 @@ use pocketmine\block\utils\AnimatedContainer;
use pocketmine\inventory\Inventory; use pocketmine\inventory\Inventory;
use pocketmine\player\InventoryWindow; use pocketmine\player\InventoryWindow;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\world\Position;
class BlockInventoryWindow extends InventoryWindow{ class BlockInventoryWindow extends InventoryWindow{
public function __construct( public function __construct(
Player $viewer, Player $viewer,
Inventory $inventory, Inventory $inventory,
protected Block $holder protected Position $holder
){ ){
parent::__construct($viewer, $inventory); parent::__construct($viewer, $inventory);
} }
public function getHolder() : Block{ return $this->holder; } public function getHolder() : Position{ return $this->holder; }
public function onOpen() : void{ public function onOpen() : void{
parent::onOpen(); parent::onOpen();
if($this->holder instanceof AnimatedContainer){ $block = $this->holder->getWorld()->getBlock($this->holder);
$this->holder->onContainerOpen(); if($block instanceof AnimatedContainer){
$block->onContainerOpen();
} }
} }
public function onClose() : void{ public function onClose() : void{
if($this->holder instanceof AnimatedContainer){ $block = $this->holder->getWorld()->getBlock($this->holder);
$this->holder->onContainerClose(); if($block instanceof AnimatedContainer){
$block->onContainerClose();
} }
parent::onClose(); parent::onClose();
} }

View File

@ -382,7 +382,7 @@ class InventoryManager implements InventoryListener{
//TODO: we should be using some kind of tagging system to identify the types. Instanceof is flaky especially //TODO: we should be using some kind of tagging system to identify the types. Instanceof is flaky especially
//if the class isn't final, not to mention being inflexible. //if the class isn't final, not to mention being inflexible.
if($window instanceof BlockInventoryWindow){ if($window instanceof BlockInventoryWindow){
$blockPosition = BlockPosition::fromVector3($window->getHolder()->getPosition()); $blockPosition = BlockPosition::fromVector3($window->getHolder());
$windowType = match(true){ $windowType = match(true){
$window instanceof LoomInventoryWindow => WindowTypes::LOOM, $window instanceof LoomInventoryWindow => WindowTypes::LOOM,
$window instanceof FurnaceInventoryWindow => match($window->getFurnaceType()){ $window instanceof FurnaceInventoryWindow => match($window->getFurnaceType()){