From cf25df788fe5c7625f196b3ebb01f47fcf291c70 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 12 May 2025 06:22:43 +0300 Subject: [PATCH] fixed block updates not being unscheduled when level is unloaded --- src/API/BlockAPI.php | 55 +++++++++++++++++--------------------------- src/world/Level.php | 15 ++++++------ 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index ddd309d9..9e38ace3 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -430,7 +430,7 @@ class BlockAPI{ $pk->meta = $block->getMetadata(); $player->dataPacket($pk); if($send === true){ - $player->sendInventorySlot($player->slot); + $player->sendInventory(); } return false; } @@ -571,6 +571,18 @@ class BlockAPI{ $this->blockUpdate($pos->getSide(5), $type); } } + + public function removeAllBlockUpdates(Level $level){ + $update = $this->server->query("SELECT x,y,z,level,type FROM blockUpdates WHERE level = '{$level->getName()}';"); + if($update instanceof SQLite3Result){ + while(($up = $update->fetchArray(SQLITE3_ASSOC)) !== false){ + $index = $up["x"] . "." . $up["y"] . "." . $up["z"] . "." . $up["level"] . "." . $up["type"]; + unset($this->scheduledUpdates[$index]); + } + $this->server->query("DELETE FROM blockUpdates WHERE level = '{$level->getName()}';"); + } + } + public function scheduleBlockUpdateXYZ(Level $level, $x, $y, $z, $type = BLOCK_UPDATE_SCHEDULED, $delay = false){ $type = (int) $type; if($delay < 0){ @@ -588,41 +600,9 @@ class BlockAPI{ } public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED){ - $type = (int) $type; - if($delay < 0){ - return false; - } - - $index = $pos->x . "." . $pos->y . "." . $pos->z . "." . $pos->level->getName() . "." . $type; - $delay = microtime(true) + $delay * 0.05; - if(!isset($this->scheduledUpdates[$index])){ - $this->scheduledUpdates[$index] = $pos; - $this->server->query("INSERT INTO blockUpdates (x, y, z, level, type, delay) VALUES (" . $pos->x . ", " . $pos->y . ", " . $pos->z . ", '" . $pos->level->getName() . "', " . $type . ", " . $delay . ");"); - return true; - } - return false; + return $this->scheduleBlockUpdateXYZ($pos->level, $pos->x, $pos->y, $pos->z, $type, $delay); } - public function nextRandomUpdate(Position $pos){ - if(!isset($this->scheduledUpdates[$pos->x . "." . $pos->y . "." . $pos->z . "." . $pos->level->getName() . "." . BLOCK_UPDATE_RANDOM])){ - $time = microtime(true); - $offset = 0; - while(true){ - $t = $offset + Utils::getRandomUpdateTicks() * 0.05; - $update = $this->server->query("SELECT COUNT(*) FROM blockUpdates WHERE level = '" . $pos->level->getName() . "' AND type = " . BLOCK_UPDATE_RANDOM . " AND delay >= " . ($time + $t - 1) . " AND delay <= " . ($time + $t + 1) . ";"); - if($update instanceof SQLite3Result){ - $update = $update->fetchArray(SQLITE3_NUM); - if($update[0] < 3){ - break; - } - }else{ - break; - } - $offset += mt_rand(25, 75); - } - $this->scheduleBlockUpdate($pos, $t / 0.05, BLOCK_UPDATE_RANDOM); - } - } public function blockUpdateTick(){ $time = microtime(true); @@ -644,4 +624,11 @@ class BlockAPI{ } } } + + /** + * @deprecated random updates are handled differently now. + */ + public function nextRandomUpdate(Position $pos){ + + } } diff --git a/src/world/Level.php b/src/world/Level.php index fbec0a71..2f805f98 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -70,7 +70,13 @@ class Level{ } public function close(){ - $this->__destruct(); + if(isset($this->level)){ + $this->save(true, true, true, true); + $this->level->close(); + unset($this->level); + $this->server->api->block->removeAllBlockUpdates($this); + } + unset($this->mobSpawner->level); } public function isTopSolidBlocking($x, $y, $z){ @@ -269,12 +275,7 @@ class Level{ } public function __destruct(){ - if(isset($this->level)){ - $this->save(false, false, false, false); - $this->level->close(); - unset($this->level); - } - unset($this->mobSpawner->level); + $this->close(); } public function isDay(){