diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index 307cd464c..8d4ed82c9 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -541,10 +541,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ public function close(){ if(!$this->closed){ if($this->inventory !== null){ - foreach($this->inventory->getViewers() as $viewer){ - $viewer->removeWindow($this->inventory); - } - + $this->inventory->removeAllViewers(true); $this->inventory = null; } parent::close(); diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index 22c83197d..c9190a8ff 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -349,6 +349,17 @@ abstract class BaseInventory implements Inventory{ return $this->viewers; } + /** + * Removes the inventory window from all players currently viewing it. + * @param bool $force Force removal of permanent windows such as the player's own inventory. Used internally. + */ + public function removeAllViewers(bool $force = false) : void{ + foreach($this->viewers as $hash => $viewer){ + $viewer->removeWindow($this, $force); + unset($this->viewers[$hash]); + } + } + public function getHolder(){ return $this->holder; } diff --git a/src/pocketmine/tile/Chest.php b/src/pocketmine/tile/Chest.php index 42ae5aa94..f9834d2ed 100644 --- a/src/pocketmine/tile/Chest.php +++ b/src/pocketmine/tile/Chest.php @@ -59,15 +59,10 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ public function close(){ if($this->closed === false){ - foreach($this->getInventory()->getViewers() as $player){ - $player->removeWindow($this->getInventory()); - } - - foreach($this->getInventory()->getViewers() as $player){ - $player->removeWindow($this->getRealInventory()); - } + $this->inventory->removeAllViewers(true); if($this->doubleInventory !== null){ + $this->doubleInventory->removeAllViewers(true); $this->doubleInventory->invalidate(); $this->doubleInventory = null; } diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index 04aaee2d3..31a5c5247 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -97,10 +97,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ public function close(){ if($this->closed === false){ - foreach($this->getInventory()->getViewers() as $player){ - $player->removeWindow($this->getInventory()); - } - + $this->inventory->removeAllViewers(true); $this->inventory = null; parent::close();