From d1f2f82c6d64251eecc42dbada6ca4bcac6e8f31 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Mon, 3 Jun 2013 12:20:07 +0200 Subject: [PATCH] Fixed #313 Entities are updated depending on near block updates [gh#313] --- src/API/EntityAPI.php | 16 ++++++++++++++++ src/PocketMinecraftServer.php | 2 +- src/world/Entity.php | 2 +- src/world/Level.php | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 510170d5..c53804af 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -43,7 +43,23 @@ class EntityAPI{ } public function init(){ + $this->server->schedule(25, array($this, "updateEntities"), array(), true); + } + public function updateEntities(){ + $l = $this->server->query("SELECT EID FROM entities WHERE hasUpdate = 1;"); + if($l !== false and $l !== true){ + while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ + $e = $this->get($e["EID"]); + if($e instanceof Entity){ + $e->update(); + } + } + } + } + + public function updateRadius(Position $center, $radius = 15, $class = false){ + $this->server->query("UPDATE entities SET hasUpdate = 1 WHERE level = '".$center->level->getName()."' ".($class !== false ? "AND class = $class ":"")."AND abs(x - {$center->x}) <= $radius AND abs(y - {$center->y}) <= $radius AND abs(z - {$center->z}) <= $radius;"); } public function getRadius(Position $center, $radius = 15, $class = false){ diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index f820a026..273fcfa5 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -118,7 +118,7 @@ class PocketMinecraftServer{ $this->query("PRAGMA encoding = \"UTF-8\";"); $this->query("PRAGMA secure_delete = OFF;"); $this->query("CREATE TABLE players (clientID INTEGER PRIMARY KEY, EID NUMERIC, ip TEXT, port NUMERIC, name TEXT UNIQUE COLLATE NOCASE);"); - $this->query("CREATE TABLE entities (EID INTEGER PRIMARY KEY, level TEXT, type NUMERIC, class NUMERIC, name TEXT, x NUMERIC, y NUMERIC, z NUMERIC, yaw NUMERIC, pitch NUMERIC, health NUMERIC);"); + $this->query("CREATE TABLE entities (EID INTEGER PRIMARY KEY, level TEXT, type NUMERIC, class NUMERIC, hasUpdate NUMERIC, name TEXT, x NUMERIC, y NUMERIC, z NUMERIC, yaw NUMERIC, pitch NUMERIC, health NUMERIC);"); $this->query("CREATE TABLE tileentities (ID INTEGER PRIMARY KEY, level TEXT, class TEXT, x NUMERIC, y NUMERIC, z NUMERIC, spawnable NUMERIC);"); $this->query("CREATE TABLE actions (ID INTEGER PRIMARY KEY, interval NUMERIC, last NUMERIC, code TEXT, repeat NUMERIC);"); $this->query("CREATE TABLE handlers (ID INTEGER PRIMARY KEY, name TEXT, priority NUMERIC);"); diff --git a/src/world/Entity.php b/src/world/Entity.php index 626bf40e..661fa8ce 100644 --- a/src/world/Entity.php +++ b/src/world/Entity.php @@ -82,7 +82,7 @@ class Entity extends Position{ $this->closed = false; $this->name = ""; $this->tickCounter = 0; - $this->server->query("INSERT OR REPLACE INTO entities (EID, level, type, class, health) VALUES (".$this->eid.", '".$this->level->getName()."', ".$this->type.", ".$this->class.", ".$this->health.");"); + $this->server->query("INSERT OR REPLACE INTO entities (EID, level, type, class, health, hasUpdate) VALUES (".$this->eid.", '".$this->level->getName()."', ".$this->type.", ".$this->class.", ".$this->health.", 0);"); $this->x = isset($this->data["x"]) ? $this->data["x"]:0; $this->y = isset($this->data["y"]) ? $this->data["y"]:0; $this->z = isset($this->data["z"]) ? $this->data["z"]:0; diff --git a/src/world/Level.php b/src/world/Level.php index b75f3769..98ced637 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -261,6 +261,7 @@ class Level{ if($update === true){ $this->server->api->block->blockUpdateAround($pos, BLOCK_UPDATE_NORMAL, 1); + $this->server->api->entity->updateRadius($pos, 3); } if($tiles === true){ if(($t = $this->server->api->tileentity->get($pos)) !== false){