From 6f59e2eaad9db320507ba3f8cc68b5a1ce883861 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Wed, 15 May 2013 21:20:24 +0200 Subject: [PATCH] Time API working with changes --- src/API/TimeAPI.php | 45 +++++++++++++++++++++++++++++---------------- src/world/Level.php | 6 +++--- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/API/TimeAPI.php b/src/API/TimeAPI.php index c2718bbb9..97209dd88 100644 --- a/src/API/TimeAPI.php +++ b/src/API/TimeAPI.php @@ -45,22 +45,26 @@ class TimeAPI{ $output = ""; switch($cmd){ case "time": + $level = false; + if($issuer instanceof Player){ + $level = $issuer->level; + } $p = strtolower(array_shift($params)); switch($p){ case "check": - $output .= "Time: ".$this->getDate().", ".$this->getPhase()." (".$this->get(true).")\n"; + $output .= "Time: ".$this->getDate($level).", ".$this->getPhase($level)." (".$this->get(true, $level).")\n"; break; case "add": - $output .= "Set the time to ".$this->add(array_shift($params))."\n"; + $output .= "Set the time to ".$this->add(array_shift($params), $level)."\n"; break; case "set": - $output .= "Set the time to ".$this->set(array_shift($params))."\n"; + $output .= "Set the time to ".$this->set(array_shift($params), $level)."\n"; break; case "sunrise": case "day": case "sunset": case "night": - $output .= "Set the time to ".$this->set($p)."\n"; + $output .= "Set the time to ".$this->set($p, $level)."\n"; break; default: $output .= "Usage: /time [time]\n"; @@ -84,22 +88,28 @@ class TimeAPI{ return $this->set("sunset"); } - public function get($raw = false){ - return $raw === true ? $this->server->time:abs($this->server->time) % 19200; + public function get($raw = false, $level = false){ + if(!($level instanceof Level)){ + $level = $this->server->api->level->getDefault(); + } + return $raw === true ? $level->getTime():abs($level->getTime()) % 19200; } - public function add($time){ - $this->server->time += (int) $time; + public function add($time, $level = false){ + if(!($level instanceof Level)){ + $level = $this->server->api->level->getDefault(); + } + $level->setTime($level->getTime() + (int) $time); return $this->server->time; } public function getDate($time = false){ - $time = $time === false ? $this->get():$time; + $time = !is_integer($time) ? $this->get(false, $time):$time; return str_pad(strval((floor($time /800) + 6) % 24), 2, "0", STR_PAD_LEFT).":".str_pad(strval(floor(($time % 800) / 13.33)), 2, "0", STR_PAD_LEFT); } public function getPhase($time = false){ - $time = $time === false ? $this->get():$time; + $time = !is_integer($time) ? $this->get(false, $time):$time; if($time < $this->phase["sunset"]){ $time = "day"; }elseif($time < $this->phase["night"]){ @@ -112,13 +122,16 @@ class TimeAPI{ return $time; } - public function set($time){ - if(is_string($time) and isset($this->phases[$time])){ - $this->server->time = $this->phases[$time]; - }else{ - $this->server->time = (int) $time; + public function set($time, $level = false){ + if(($level instanceof Level)){ + $level = $this->server->api->level->getDefault(); } - return $this->server->time; + if(is_string($time) and isset($this->phases[$time])){ + $level->setTime($this->phases[$time]); + }else{ + $level->setTime((int) $time); + } + return $level->getTime(); } diff --git a/src/world/Level.php b/src/world/Level.php index ec26f1011..af11c1643 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->startCheck = microtime(true); - $this->server->schedule(15, array($this, "checkThings")); + $this->server->schedule(15, array($this, "checkThings"), array(), true); $this->server->event("server.close", array($this, "save")); $this->name = $name; } @@ -55,7 +55,7 @@ class Level{ } public function save(){ - $this->level->setData("time", $this->time); + $this->level->setData("time", (int) $this->time); $this->level->doSaveRound(); $this->level->saveData(); } @@ -138,7 +138,7 @@ class Level{ } public function getTime(){ - return ($this->time); + return (int) ($this->time); } public function getName(){