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\player\InventoryWindow;
use pocketmine\player\Player;
use pocketmine\world\Position;
class BlockInventoryWindow extends InventoryWindow{
public function __construct(
Player $viewer,
Inventory $inventory,
protected Block $holder
protected Position $holder
){
parent::__construct($viewer, $inventory);
}
public function getHolder() : Block{ return $this->holder; }
public function getHolder() : Position{ return $this->holder; }
public function onOpen() : void{
parent::onOpen();
if($this->holder instanceof AnimatedContainer){
$this->holder->onContainerOpen();
$block = $this->holder->getWorld()->getBlock($this->holder);
if($block instanceof AnimatedContainer){
$block->onContainerOpen();
}
}
public function onClose() : void{
if($this->holder instanceof AnimatedContainer){
$this->holder->onContainerClose();
$block = $this->holder->getWorld()->getBlock($this->holder);
if($block instanceof AnimatedContainer){
$block->onContainerClose();
}
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
//if the class isn't final, not to mention being inflexible.
if($window instanceof BlockInventoryWindow){
$blockPosition = BlockPosition::fromVector3($window->getHolder()->getPosition());
$blockPosition = BlockPosition::fromVector3($window->getHolder());
$windowType = match(true){
$window instanceof LoomInventoryWindow => WindowTypes::LOOM,
$window instanceof FurnaceInventoryWindow => match($window->getFurnaceType()){