From e63677a23d34c9ee61dca207817b7761846e8e6f Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Wed, 15 May 2013 21:03:48 +0200 Subject: [PATCH] Level saving --- src/API/LevelAPI.php | 2 +- src/PocketMinecraftServer.php | 1 - src/pmf/Level.php | 66 +++++++++++++++++------------------ src/world/Level.php | 8 +++-- 4 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index 948bf754c..1d9db98ff 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -100,7 +100,7 @@ class LevelAPI{ $level = new PMFLevel($path."level.pmf"); $entities = new Config($path."entities.yml", CONFIG_YAML); $tileEntities = new Config($path."tileEntities.yml", CONFIG_YAML); - $this->levels[$name] = new Level($level, $entities, $tileEntities); + $this->levels[$name] = new Level($level, $entities, $tileEntities, $name); foreach($entities->getAll() as $entity){ if(!isset($entity["id"])){ break; diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index d4c70e3c1..df19cdd7b 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -169,7 +169,6 @@ class PocketMinecraftServer{ $this->api->chat->broadcast("Stopping server..."); } } - $this->save(true); $this->stop = true; $this->trigger("server.close", $reason); $this->interface->close(); diff --git a/src/pmf/Level.php b/src/pmf/Level.php index 0483eb3e0..215ead729 100644 --- a/src/pmf/Level.php +++ b/src/pmf/Level.php @@ -180,7 +180,7 @@ class PMFLevel extends PMF{ @ftruncate($this->fp, $this->payloadOffset); $this->seek($this->payloadOffset); for($index = 0; $index < $cnt; ++$index){ - $this->write(Utils::writeShort($this->locationTable[$index])); + $this->write(Utils::writeShort($this->locationTable[$index][0])); } } @@ -246,36 +246,6 @@ class PMFLevel extends PMF{ return true; } - public function getBlock($x, $y, $z){ - $X = $x >> 4; - $Z = $z >> 4; - $Y = $y >> 4; - if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"]){ - return array(AIR, 0); - } - $index = $this->getIndex($X, $Z); - if($this->chunks[$index] === false){ - if($this->loadChunk($X, $Z) === false){ - return array(AIR, 0); - } - }elseif($this->chunks[$index][$Y] === false){ - return array(AIR, 0); - } - $aX = $x - ($X << 4); - $aZ = $z - ($Z << 4); - $aY = $y - ($Y << 4); - $bindex = $aY + ($aX << 5) + ($aZ << 9); - $mindex = ($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9); - $b = ord($this->chunks[$index][$Y]{$bindex}); - $m = ord($this->chunks[$index][$Y]{$mindex}); - if(($y & 1) === 0){ - $m = $m & 0x0F; - }else{ - $m = $m >> 4; - } - return array($b, $m); - } - protected function fillMiniChunk($X, $Z, $Y){ if($this->isChunkLoaded($X, $Z) === false){ return false; @@ -311,13 +281,43 @@ class PMFLevel extends PMF{ return true; } + public function getBlock($x, $y, $z){ + $X = $x >> 4; + $Z = $z >> 4; + $Y = $y >> 4; + if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){ + return array(AIR, 0); + } + $index = $this->getIndex($X, $Z); + if($this->chunks[$index] === false){ + if($this->loadChunk($X, $Z) === false){ + return array(AIR, 0); + } + }elseif($this->chunks[$index][$Y] === false){ + return array(AIR, 0); + } + $aX = $x - ($X << 4); + $aZ = $z - ($Z << 4); + $aY = $y - ($Y << 4); + $bindex = $aY + ($aX << 5) + ($aZ << 9); + $mindex = ($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9); + $b = ord($this->chunks[$index][$Y]{$bindex}); + $m = ord($this->chunks[$index][$Y]{$mindex}); + if(($y & 1) === 0){ + $m = $m & 0x0F; + }else{ + $m = $m >> 4; + } + return array($b, $m); + } + public function setBlock($x, $y, $z, $block, $meta = 0){ $X = $x >> 4; $Z = $z >> 4; $Y = $y >> 4; $block &= 0xFF; $meta &= 0x0F; - if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"]){ + if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){ return false; } $index = $this->getIndex($X, $Z); @@ -333,7 +333,7 @@ class PMFLevel extends PMF{ $aY = $y - ($Y << 4); $bindex = $aY + ($aX << 5) + ($aZ << 9); $mindex = ($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9); - $old_b = $this->chunks[$index][$Y]{$bindex}; + $old_b = $this->chunks[$index][$Y]{$bindex}; $old_m = $this->chunks[$index][$Y]{$mindex}; if(($y & 1) === 0){ $old_m = $old_m & 0x0F; diff --git a/src/world/Level.php b/src/world/Level.php index b61000315..a5875cb98 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -27,9 +27,9 @@ the Free Software Foundation, either version 3 of the License, or class Level{ public $entities, $tileEntities; - private $level, $time, $startCheck, $startTime, $server; + private $level, $time, $startCheck, $startTime, $server, $name; - public function __construct(PMFLevel $level, Config $entities, Config $tileEntities){ + public function __construct(PMFLevel $level, Config $entities, Config $tileEntities, $name){ $this->server = ServerAPI::request(); $this->level = $level; $this->entities = $entities; @@ -37,6 +37,8 @@ class Level{ $this->startTime = $this->time = (int) $this->level->getData("time"); $this->startCheck = microtime(true); $this->server->schedule(15, array($this, "checkThings")); + $this->server->event("server.close", array($this, "save")); + $this->name = $name; } public function __destruct(){ @@ -132,7 +134,7 @@ class Level{ } public function getName(){ - return $this->level->getData("name"); + return $this->name;//return $this->level->getData("name"); } public function setTime($time){