diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 328bc261..f6e63043 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -204,22 +204,27 @@ class EntityAPI{ $this->server->api->dhandle("entity.remove", $this->entities[$eid]); unset($this->entities[$eid]->level->entityList[$eid]); unset($this->entities[$eid]); - $this->server->query("DELETE FROM entities WHERE EID = " . $eid . ";"); } } public function getRadius(Position $center, $radius = 15, $class = false){ - $entities = []; - $l = $this->server->query("SELECT EID FROM entities 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;"); - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->get($e["EID"]); - if($e instanceof Entity){ - $entities[$e->eid] = $e; + $minChunkX = ((int)($center->x - $radius)) >> 4; + $minChunkZ = ((int)($center->z - $radius)) >> 4; + $maxChunkX = ((int)($center->x + $radius)) >> 4; + $maxChunkZ = ((int)($center->z - $radius)) >> 4; + $ents = []; + //TODO also index by chunkY? + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ + $ind = "$chunkX $chunkZ"; + foreach($center->level->entityListPositioned[$ind] ?? [] as $entid){ + if($this->entities[$entid] instanceof Entity && ($class === false || $this->entities[$entid]->class == $class)){ + $ents[$entid] = $this->entities[$entid]; + } } } } - return $entities; + return $ents; } public function heal($eid, $heal, $cause){ diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 938dda3e..d8797ea5 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -32,7 +32,7 @@ class PlayerAPI{ $this->server->api->console->cmdWhitelist("ping"); $this->server->api->console->cmdWhitelist("spawn"); $this->server->api->console->cmdWhitelist("loc"); - $this->server->preparedSQL->selectPlayersToHeal = $this->server->database->prepare("SELECT EID FROM entities WHERE class = " . ENTITY_PLAYER . " AND health < 20;"); + //$this->server->preparedSQL->selectPlayersToHeal = $this->server->database->prepare("SELECT EID FROM entities WHERE class = " . ENTITY_PLAYER . " AND health < 20;"); } public function registerCmd($cmd, $help = ""){ @@ -43,7 +43,7 @@ class PlayerAPI{ switch($event){ case "server.regeneration": if($this->server->difficulty === 0){ - $result = $this->server->preparedSQL->selectPlayersToHeal->execute(); + //$result = $this->server->preparedSQL->selectPlayersToHeal->execute(); if($result !== false){ while(($player = $result->fetchArray()) !== false){ if(($player = $this->server->api->entity->get($player["EID"])) !== false){ @@ -365,6 +365,7 @@ class PlayerAPI{ "z" => $player->level->getSpawn()->z, ]); } + $player->level->players[$CID] = $player; $this->server->query("INSERT OR REPLACE INTO players (CID, ip, port, name) VALUES (" . $player->CID . ", '" . $player->ip . "', " . $player->port . ", '" . strtolower($player->username) . "');"); } } @@ -434,16 +435,8 @@ class PlayerAPI{ public function getAll($level = null){ if($level instanceof Level){ - $clients = []; - $l = $this->server->query("SELECT EID FROM entities WHERE level = '" . $level->getName() . "' AND class = '" . ENTITY_PLAYER . "';"); - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->getByEID($e["EID"]); - if($e instanceof Player){ - $clients[$e->CID] = $e; - } - } - } return $clients; + //safe_var_dump($level->players); + return $level->players; } return $this->server->clients; @@ -487,6 +480,7 @@ class PlayerAPI{ } $this->server->query("DELETE FROM players WHERE name = '" . $player->username . "';"); $this->server->api->entity->remove($player->eid); + unset($player->level->players[$this->CID]); if($player->entity instanceof Entity){ unset($player->entity->player); //unset($player->entity); diff --git a/src/Player.php b/src/Player.php index 49b97b5f..647092d7 100755 --- a/src/Player.php +++ b/src/Player.php @@ -204,7 +204,10 @@ class Player{ } $this->level->freeAllChunks($this); + unset($this->level->players[$this->CID]); $this->level = $pos->level; + + $this->level->players[$this->CID] = $this; $this->chunksLoaded = []; $this->server->api->entity->spawnToAll($this->entity); $this->server->api->entity->spawnAll($this); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 349219ba..3b9c8333 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -729,10 +729,8 @@ class PocketMinecraftServer{ $info["tps"] = $this->getTPS(); $info["memory_usage"] = round((memory_get_usage() / 1024) / 1024, 2) . "MB"; $info["memory_peak_usage"] = round((memory_get_peak_usage() / 1024) / 1024, 2) . "MB"; - $info["entities"] = $this->query("SELECT count(EID) as count FROM entities;", true); - $info["entities"] = $info["entities"]["count"]; - $info["players"] = $this->query("SELECT count(CID) as count FROM players;", true); - $info["players"] = $info["players"]["count"]; + $info["entities"] = count($this->entities); + $info["players"] = count($this->clients); $info["events"] = count($this->eventsID); $info["handlers"] = $this->query("SELECT count(ID) as count FROM handlers;", true); $info["handlers"] = $info["handlers"]["count"]; diff --git a/src/dependencies.php b/src/dependencies.php index d5fb697f..0119e58d 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -99,10 +99,7 @@ require_once(FILE_PATH . "/src/entity/ai/tasks/TaskBase.php"); require_once(FILE_PATH. "/src/entity/ai/tasks/TaskTempt.php"); require_once(FILE_PATH . "/src/entity/Rideable.php"); -require_once(FILE_PATH . "/src/entity/Attachable.php"); -require_once(FILE_PATH . "/src/entity/Damageable.php"); require_once(FILE_PATH . "/src/entity/Breedable.php"); -require_once(FILE_PATH . "/src/entity/Damageable.php"); require_once(FILE_PATH . "/src/entity/Pathfindable.php"); require_once(FILE_PATH . "/src/entity/Entity.php"); require_once(FILE_PATH . "/src/entity/Living.php"); diff --git a/src/entity/Attachable.php b/src/entity/Attachable.php deleted file mode 100644 index b3600271..00000000 --- a/src/entity/Attachable.php +++ /dev/null @@ -1,5 +0,0 @@ -gravity = 0.08; $this->state = $this->data["State"] = isset($this->data["State"]) ? $this->data["State"] : 0; $this->tickCounter = 0; - $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"]) ? (float) $this->data["x"] : 0; $this->y = isset($this->data["y"]) ? (float) $this->data["y"] : 0; $this->z = isset($this->data["z"]) ? (float) $this->data["z"] : 0; @@ -1075,7 +1074,6 @@ class Entity extends Position public function setName($name) { $this->name = $name; - // $this->server->query("UPDATE entities SET name = '".str_replace("'", "", $this->name)."' WHERE EID = ".$this->eid.";"); is this neccessary for database? } public function look($pos2) @@ -1084,7 +1082,6 @@ class Entity extends Position $angle = Utils::angle3D($pos2, $pos); $this->yaw = $angle["yaw"]; $this->pitch = $angle["pitch"]; - $this->server->query("UPDATE entities SET pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); } public function move(Vector3 $pos, $yaw = 0, $pitch = 0) @@ -1096,7 +1093,6 @@ class Entity extends Position $this->yaw %= 360; $this->pitch += $pitch; $this->pitch %= 90; - $this->server->query("UPDATE entities SET x = " . $this->x . ", y = " . $this->y . ", z = " . $this->z . ", pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); $this->updateAABB(); } @@ -1110,14 +1106,12 @@ class Entity extends Position public function updatePosition() { - $this->server->query("UPDATE entities SET level = '" . $this->level->getName() . "', x = " . $this->x . ", y = " . $this->y . ", z = " . $this->z . ", pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); $this->sendMoveUpdate(); $this->updateAABB(); } public function setPosition(Vector3 $pos, $yaw = false, $pitch = false) { - $this->x = $pos->x; $this->y = $pos->y; $this->z = $pos->z; @@ -1127,15 +1121,6 @@ class Entity extends Position if($pitch !== false){ $this->pitch = $pitch; } - $this->server->preparedSQL->entity->setPosition->reset(); - $this->server->preparedSQL->entity->setPosition->clear(); - $this->server->preparedSQL->entity->setPosition->bindValue(":x", $this->x, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":y", $this->y, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":z", $this->z, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":pitch", $this->pitch, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":yaw", $this->yaw, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":eid", $this->eid, SQLITE3_INTEGER); - $this->server->preparedSQL->entity->setPosition->execute(); } public function inBlockNonVector($x, $y, $z, $radius = 0.8) { @@ -1371,7 +1356,6 @@ class Entity extends Position "cause" => $cause )) !== false or $force === true){ $this->health = min(127, max(- 127, $health)); - $this->server->query("UPDATE entities SET health = " . $this->health . " WHERE EID = " . $this->eid . ";"); if($harm === true){ $pk = new EntityEventPacket; $pk->eid = $this->eid; diff --git a/src/entity/Hanging.php b/src/entity/Hanging.php deleted file mode 100644 index 554abb69..00000000 --- a/src/entity/Hanging.php +++ /dev/null @@ -1,5 +0,0 @@ -x >> 4; $newChunkZ = (int)$e->z >> 4; - if($curChunkX != $newChunkX || $curChunkZ != $newChunkZ){ + if($e->level != $this && isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ + unset($this->entityListPositioned["$curChunkX $curChunkZ"][$e->eid]); + }else if($curChunkX != $newChunkX || $curChunkZ != $newChunkZ){ $index = "$curChunkX $curChunkZ"; //while creating index like $curChunkX << 32 | $curChunkZ is faster, placing it inside list is slow $newIndex = "$newChunkX $newChunkZ"; unset($this->entityListPositioned[$index][$e->eid]); $this->entityListPositioned[$newIndex][$e->eid] = $e->eid; //set to e->eid to avoid possible memory leaks } - } + }else if(isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ + unset($this->entityListPositioned["$curChunkX $curChunkZ"][$k]); + } } $this->checkSleep(); @@ -405,22 +409,23 @@ class Level{ $this->queuedBlockUpdates = []; } - public function getEntitiesNearby(Entity $e, $radius = 5){ - $minChunkX = ((int)($e->x - $radius)) >> 4; - $minChunkZ = ((int)($e->z - $radius)) >> 4; - $maxChunkX = ((int)($e->x + $radius)) >> 4; - $maxChunkZ = ((int)($e->z + $radius)) >> 4; + public function getEntitiesInAABB(AxisAlignedBB $bb){ + $minChunkX = ((int)($bb->minX)) >> 4; + $minChunkZ = ((int)($bb->minZ)) >> 4; + $maxChunkX = ((int)($bb->maxX)) >> 4; + $maxChunkZ = ((int)($bb->maxZ)) >> 4; $ents = []; //TODO also index by chunkY? for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ $ind = "$chunkX $chunkZ"; - foreach($this->entityListPositioned[$ind] ?? [] as $entid){ - $ents[$entid] = $this->entityList[$entid]; + foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ + if($this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ + $ents[$entid] = $this->entityList[$entid]; + } } } } - unset($ents[$e->eid]); return $ents; }