Time API!

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-11 23:47:55 +01:00
parent 667590c0e7
commit f137841d3e
2 changed files with 127 additions and 43 deletions

View File

@ -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 <check | set | add | sunrise | day | sunset | night> [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");

127
classes/API/TimeAPI.php Normal file
View File

@ -0,0 +1,127 @@
<?php
/*
-
/ \
/ \
/ POCKET \
/ MINECRAFT PHP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
class TimeAPI{
var $phases = array(
"day" => 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 <check | set | add | sunrise | day | sunset | night> [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;
}
}
}