From bbf506c8873b0fb10b45fe853ec0329a6da98f9d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 1 Jun 2024 00:57:17 +0300 Subject: [PATCH] invalidated all tile entities on block change --- src/API/TileAPI.php | 24 +++++++++++++++++++++++- src/world/Level.php | 8 +++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/API/TileAPI.php b/src/API/TileAPI.php index 6770c656..a5a48118 100644 --- a/src/API/TileAPI.php +++ b/src/API/TileAPI.php @@ -11,12 +11,34 @@ class TileAPI{ $this->server = ServerAPI::request(); } public function getXYZ(Level $level, $x, $y, $z){ - $tile = $this->server->query("SELECT * FROM tiles WHERE level = '{$level->getName()}' AND x = $z AND y = $y AND z = $z;", true); + $tile = $this->server->query("SELECT * FROM tiles WHERE level = '{$level->getName()}' AND x = $x AND y = $y AND z = $z;", true); if($tile !== false and $tile !== true and ($tile = $this->getByID($tile["ID"])) !== false){ return $tile; } return false; } + + public function invalidateAll(Level $level, $x, $y, $z){ + $x = (int) $x; + $y = (int) $y; + $z = (int) $z; + $tile = $this->server->query("SELECT id FROM tiles WHERE level = '{$level->getName()}' AND x = $x AND y = $y AND z = $z;", false); + $invcnt = 0; + if($tile instanceof SQLite3Result){ + while(($t = $tile->fetchArray(SQLITE3_ASSOC)) !== false){ + $tl = $this->getByID($t["ID"]); + if($tl instanceof Tile){ + ++$invcnt; + $tl->close(); + } + + if($invcnt > 1){ + ConsoleAPI::warn("{$level->getName()}: ($x $y $z) has more than 1 tile entity! Invalidated ID {$t["ID"]} (Total invaliated: $invcnt)"); + } + } + } + } + public function get(Position $pos){ $tile = $this->server->query("SELECT * FROM tiles WHERE level = '" . $pos->level->getName() . "' AND x = {$pos->x} AND y = {$pos->y} AND z = {$pos->z};", true); if($tile !== false and $tile !== true and ($tile = $this->getByID($tile["ID"])) !== false){ diff --git a/src/world/Level.php b/src/world/Level.php index 261b5fe0..840c337c 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -488,8 +488,8 @@ class Level{ $this->level->setBlock($x, $y, $z, $id, $meta); - if($tiles && ($t = $this->server->api->tile->getXYZ($this, $x, $y, $z)) != false){ //TODO rewrite - $t->close(); + if($tiles){ //TODO rewrite + $this->server->api->tile->invalidateAll($this, $x, $y, $z); } if($updateBlocksAround){ self::updateNeighborsAt($x, $y, $z, $oldID); @@ -723,9 +723,7 @@ class Level{ } if($tiles === true){ - if(($t = $this->server->api->tile->get($pos)) !== false){ - $t->close(); - } + $this->server->api->tile->invalidateAll($this, $pos->x, $pos->y, $pos->z); } if($update === true){