diff --git a/classes/API/ConsoleAPI.php b/classes/API/ConsoleAPI.php index d2811603d..f73e38e1f 100644 --- a/classes/API/ConsoleAPI.php +++ b/classes/API/ConsoleAPI.php @@ -110,48 +110,6 @@ class ConsoleAPI{ } $this->server->chat(false, $s); break; - case "time": - $p = strtolower(array_shift($params)); - switch($p){ - case "check": - $time = abs($this->server->time) % 19200; - $hour = 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); - if($time < 9500){ - $time = "daytime"; - }elseif($time < 10900){ - $time = "sunset"; - }elseif($time < 17800){ - $time = "night"; - }else{ - $time = "sunrise"; - } - console("[INFO] Time: $hour, $time (".$this->server->time.")"); - break; - case "add": - $t = (int) array_shift($params); - $this->server->time += $t; - break; - case "set": - $t = (int) array_shift($params); - $this->server->time = $t; - break; - case "sunrise": - $this->server->time = 17800; - break; - case "day": - $this->server->time = 0; - break; - case "sunset": - $this->server->time = 9500; - break; - case "night": - $this->server->time = 10900; - break; - default: - console("[INFO] Usage: /time [time]"); - break; - } - break; case "whitelist": $p = strtolower(array_shift($params)); switch($p){ @@ -212,7 +170,6 @@ class ConsoleAPI{ console("[INFO] /gamemode: Changes default gamemode"); console("[INFO] /difficulty: Changes difficulty"); console("[INFO] /say: Broadcasts mesages"); - console("[INFO] /time: Manages time"); console("[INFO] /save-all: Saves pending changes"); console("[INFO] /whitelist: Manages whitelisting"); console("[INFO] /banip: Manages IP ban"); diff --git a/classes/API/TimeAPI.php b/classes/API/TimeAPI.php new file mode 100644 index 000000000..6ad1ca45e --- /dev/null +++ b/classes/API/TimeAPI.php @@ -0,0 +1,127 @@ + 0, + "sunset" => 9500, + "night" => 10900, + "sunrise" => 17800, + ); + private $server; + function __construct($server){ + $this->server = $server; + } + + public function init(){ + $this->server->api->console->register("time", "Manages server time", array($this, "commandHandler")); + } + + public function commandHandler($cmd, $params){ + switch($cmd){ + case "time": + $p = strtolower(array_shift($params)); + switch($p){ + case "check": + console("[INFO] Time: ".$this->getDate().", ".$this->getPhase()." (".$this->get(true).")"); + break; + case "add": + $this->add(array_shift($params)); + break; + case "set": + $this->set(array_shift($params)); + break; + case "sunrise": + $this->sunrise(); + break; + case "day": + $this->day(); + break; + case "sunset": + $this->sunset(); + break; + case "night": + $this->night(); + break; + default: + console("[INFO] Usage: /time [time]"); + break; + } + break; + } + } + + public function night(){ + $this->set("night"); + } + public function day(){ + $this->set("day"); + } + public function sunrise(){ + $this->set("sunrise"); + } + public function sunset(){ + $this->set("sunset"); + } + + public function get($raw = false){ + return $raw === true ? $this->server->time:abs($this->server->time) % 19200; + } + + public function add($time){ + $this->server->time += (int) $time; + } + + public function getDate(){ + $time = $this->get(); + 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 = $this->get(); + if($time < $this->phase["sunset"]){ + $time = "day"; + }elseif($time < $this->phase["night"]){ + $time = "sunset"; + }elseif($time < $this->phase["sunrise"]){ + $time = "night"; + }else{ + $time = "sunrise"; + } + 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; + } + } + + +} \ No newline at end of file