From 5eeaeb6c3e1665dfa8b5ffa11277eff2ddded5ca Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 22 Sep 2018 13:40:50 +0100 Subject: [PATCH] Level: Bail on trying to unload a level during level tick (#2435) --- src/pocketmine/level/Level.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 5ad9d1686..ee7fddb1e 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -241,6 +241,9 @@ class Level implements ChunkManager, Metadatable{ /** @var int */ public $tickRateCounter = 0; + /** @var bool */ + private $doingTick = false; + /** @var string|Generator */ private $generator; @@ -528,8 +531,12 @@ class Level implements ChunkManager, Metadatable{ * @param bool $force default false, force unload of default level * * @return bool + * @throws \InvalidStateException if trying to unload a level during level tick */ public function unload(bool $force = false) : bool{ + if($this->doingTick and !$force){ + throw new \InvalidStateException("Cannot unload a level during level tick"); + } $ev = new LevelUnloadEvent($this); @@ -697,7 +704,16 @@ class Level implements ChunkManager, Metadatable{ } $this->timings->doTick->startTiming(); + $this->doingTick = true; + try{ + $this->actuallyDoTick($currentTick); + }finally{ + $this->doingTick = false; + $this->timings->doTick->stopTiming(); + } + } + protected function actuallyDoTick(int $currentTick) : void{ $this->checkTime(); $this->sunAnglePercentage = $this->computeSunAnglePercentage(); //Sun angle depends on the current time @@ -818,8 +834,6 @@ class Level implements ChunkManager, Metadatable{ } $this->chunkPackets = []; - - $this->timings->doTick->stopTiming(); } public function checkSleep(){