From e0e724bcc385360872d11d3802fb4432b1048523 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Wed, 15 May 2013 23:20:44 +0200 Subject: [PATCH] Saving entities/tiles --- src/API/LevelAPI.php | 30 ----------------- src/API/TileEntityAPI.php | 2 +- src/utils/Config.php | 4 +++ src/world/Level.php | 70 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 73 insertions(+), 33 deletions(-) diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index 1d9db98ff..bf049cd2b 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -143,36 +143,6 @@ class LevelAPI{ return $this->server->spawn; } - /* - if(file_exists(DATA_PATH."worlds/level.dat")){ - console("[NOTICE] Detected unimported map data. Importing..."); - $this->importMap(DATA_PATH."worlds/", true); - } - - $this->server->mapName = $this->getProperty("level-name"); - $this->server->mapDir = DATA_PATH."worlds/".$this->server->mapName."/"; - if($this->server->mapName === false or trim($this->server->mapName) === "" or (!file_exists($this->server->mapDir."chunks.dat") and !file_exists($this->server->mapDir."chunks.dat.gz") and !file_exists($this->server->mapDir."level.pmf"))){ - if($this->server->mapName === false or trim($this->server->mapName) === ""){ - $this->server->mapName = "world"; - } - $this->server->mapDir = DATA_PATH."worlds/".$this->server->mapName."/"; - $generator = "SuperflatGenerator"; - if($this->getProperty("generator") !== false and class_exists($this->getProperty("generator"))){ - $generator = $this->getProperty("generator"); - } - $this->gen = new WorldGenerator($generator, $this->server->seed); - if($this->getProperty("generator-settings") !== false and trim($this->getProperty("generator-settings")) != ""){ - $this->gen->set("preset", $this->getProperty("generator-settings")); - } - $this->gen->init(); - $this->gen->generate(); - $this->gen->save($this->server->mapDir, $this->server->mapName); - $this->setProperty("level-name", $this->server->mapName); - $this->setProperty("gamemode", SURVIVAL); - } - $this->server->loadMap(); - */ - public function loadMap(){ if($this->mapName !== false and trim($this->mapName) !== ""){ if(!file_exists($this->mapDir."level.pmf")){ diff --git a/src/API/TileEntityAPI.php b/src/API/TileEntityAPI.php index 38260e26f..9ce2f89fd 100644 --- a/src/API/TileEntityAPI.php +++ b/src/API/TileEntityAPI.php @@ -71,7 +71,7 @@ class TileEntityAPI{ $l = $this->server->query("SELECT ID FROM tileentities WHERE level = '".$level->getName()."';"); if($l !== false and $l !== true){ while(($t = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $t = $this->get($e["ID"]); + $t = $this->getByID($t["ID"]); if($t instanceof TileEntity){ $tileEntities[$t->id] = $t; } diff --git a/src/utils/Config.php b/src/utils/Config.php index becdff936..223f2ea36 100644 --- a/src/utils/Config.php +++ b/src/utils/Config.php @@ -159,6 +159,10 @@ class Config{ $this->config[$k] = $v; } + public function setAll($v){ + $this->config = $v; + } + public function exists($k){ return isset($this->config[$k]); } diff --git a/src/world/Level.php b/src/world/Level.php index 66524918d..093771ab1 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -36,7 +36,7 @@ class Level{ $this->tileEntities = $tileEntities; $this->startTime = $this->time = (int) $this->level->getData("time"); $this->nextSave = $this->startCheck = microtime(true); - $this->nextSave += 30; + $this->nextSave += 90; $this->server->schedule(15, array($this, "checkThings"), array(), true); $this->server->event("server.close", array($this, "save")); $this->name = $name; @@ -78,7 +78,7 @@ class Level{ if($this->nextSave < $now){ $this->save(); - $this->lastSave = $now + 30; + $this->lastSave = $now + 90; } } @@ -88,6 +88,72 @@ class Level{ } public function save(){ + $entities = array(); + foreach($this->server->api->entity->getAll($this) as $entity){ + if($entity->class === ENTITY_MOB){ + $entities[] = array( + "id" => $entity->type, + "Color" => @$entity->data["Color"], + "Sheared" => @$entity->data["Sheared"], + "Health" => $entity->health, + "Pos" => array( + 0 => $entity->x, + 1 => $entity->y, + 2 => $entity->z, + ), + "Rotation" => array( + 0 => $entity->yaw, + 1 => $entity->pitch, + ), + ); + }elseif($entity->class === ENTITY_OBJECT){ + $entities[] = array( + "id" => $entity->type, + "TileX" => $entity->x, + "TileX" => $entity->y, + "TileX" => $entity->z, + "Health" => $entity->health, + "Motive" => $entity->data["Motive"], + "Pos" => array( + 0 => $entity->x, + 1 => $entity->y, + 2 => $entity->z, + ), + "Rotation" => array( + 0 => $entity->yaw, + 1 => $entity->pitch, + ), + ); + }elseif($entity->class === ENTITY_ITEM){ + $entities[] = array( + "id" => 64, + "Item" => array( + "id" => $entity->type, + "Damage" => $entity->meta, + "Count" => $entity->stack, + ), + "Health" => $entity->health, + "Pos" => array( + 0 => $entity->x, + 1 => $entity->y, + 2 => $entity->z, + ), + "Rotation" => array( + 0 => 0, + 1 => 0, + ), + ); + } + } + $this->entities->setAll($entities); + $this->entities->save(); + $tiles = array(); + foreach($this->server->api->tileentity->getAll($this) as $tile){ + $tiles[] = $tile->data; + } + $this->tileEntities->setAll($tiles); + $this->tileEntities->save(); + $this->level->setData("time", (int) $this->time); $this->level->doSaveRound(); $this->level->saveData();