fixed block updates not being unscheduled when level is unloaded

This commit is contained in:
GameHerobrine 2025-05-12 06:22:43 +03:00
parent c4b7012fe9
commit cf25df788f
2 changed files with 29 additions and 41 deletions

View file

@ -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){
}
}

View file

@ -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(){