API added, fixed server starting issues

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-11 19:33:30 +01:00
parent fe574af170
commit ce078845da
5 changed files with 497 additions and 368 deletions

3
README
View File

@ -28,10 +28,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
\ | /
Github repo: https://github.com/shoghicp/Pocket-Minecraft-PHP
Server (and client) Minecraft Pocket Edition library written in PHP.
Currently a work in progress, and used to document http://www.wiki.vg/Pocket_Minecraft_Protocol
How to connect to internet servers: http://www.minecraftforum.net/topic/1256915-legit-minecraftpe-online-multiplayer/
Video-Tutorial made by ChrisMCMine: http://www.youtube.com/watch?v=GC9MBVaHge0
User-made program to merge server I/O: https://github.com/filfat/MCPES_CPP_INPUT/

241
classes/API/ConsoleAPI.php Normal file
View File

@ -0,0 +1,241 @@
<?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 ConsoleAPI{
private $input, $server, $event;
function __construct($server){
$this->server = $server;
$this->input = fopen(FILE_PATH."console.in", "w+b");
$this->event = $this->server->event("onTick", array($this, "handle"));
}
function __destroy(){
$this->server->deleteEvent("onTick", $this->event);
fclose($this->input);
}
public function handle(){
while(($line = fgets($this->input)) !== false){
$line = trim($line);
if($line === ""){
continue;
}
$params = explode(" ", $line);
$cmd = strtolower(array_shift($params));
console("[INFO] Issued server command: /$cmd ".implode(" ", $params));
switch($cmd){
case "stop":
console("[INFO] Stopping server...");
$this->server->close();
break;
case "banip":
$p = strtolower(array_shift($params));
switch($p){
case "pardon":
case "remove":
$ip = trim(implode($params));
$new = array();
foreach(explode("\n", str_replace(array("\r","\t"), "", file_get_contents(FILE_PATH."banned-ips.txt"))) as $i){
if($i == $ip){
console("[INFO] IP \"$ip\" removed from ban list");
continue;
}
$new[$i] = $i;
}
file_put_contents(FILE_PATH."banned-ips.txt", implode("\r\n", $new));
$this->server->reloadConfig();
break;
case "add":
case "ban":
$ip = trim(implode($params));
file_put_contents(FILE_PATH."banned-ips.txt", "\r\n".$ip, FILE_APPEND);
console("[INFO] IP \"$ip\" added to ban list");
$this->server->reloadConfig();
break;
case "reload":
$this->server->reloadConfig();
break;
case "list":
console("[INFO] IP ban list: ".implode(", ", explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."banned-ips.txt")))));
break;
default:
console("[INFO] Usage: /banip <add | remove | list | reload> [IP]");
break;
}
break;
case "gamemode":
$s = trim(array_shift($params));
if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){
console("[INFO] Usage: /gamemode <0 | 1>");
break;
}
$this->server->api->setProperty("gamemode", (int) $s);
console("[INFO] Gamemode changed to ".$this->server->gamemode);
break;
case "difficulty":
$s = trim(array_shift($params));
if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){
console("[INFO] Usage: /difficulty <0 | 1>");
break;
}
$this->server->api->setProperty("difficulty", (int) $s);
console("[INFO] Difficulty changed to ".$this->server->difficulty);
loadConfig(true);
break;
case "say":
$s = implode(" ", $params);
if(trim($s) == ""){
console("[INFO] Usage: /say <message>");
break;
}
$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){
case "remove":
$user = trim(implode(" ", $params));
$new = array();
foreach(explode("\n", str_replace(array("\r","\t"), "", file_get_contents(FILE_PATH."white-list.txt"))) as $u){
if($u == $user){
console("[INFO] Player \"$user\" removed from white-list");
continue;
}
$new[$u] = $u;
}
file_put_contents(FILE_PATH."white-list.txt", implode("\r\n", $new));
$this->server->reloadConfig();
break;
case "add":
$user = trim(implode(" ", $params));
file_put_contents(FILE_PATH."white-list.txt", "\r\n".$user, FILE_APPEND);
console("[INFO] Player \"$user\" added to white-list");
$this->server->reloadConfig();
break;
case "reload":
$this->server->reloadConfig();
break;
case "list":
console("[INFO] White-list: ".implode(", ", explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt")))));
break;
case "on":
case "true":
case "1":
console("[INFO] White-list turned on");
$this->server->api->setProperty("white-list", true);
break;
case "off":
case "false":
case "0":
console("[INFO] White-list turned off");
$this->server->api->setProperty("white-list", false);
break;
default:
console("[INFO] Usage: /whitelist <on | off | add | reload | list> [username]");
break;
}
break;
case "save-all":
$this->server->save();
break;
case "block":
foreach($this->server->clients as $client){
$b = $this->server->map->getBlock($client->entity->position["x"], $client->entity->position["y"] - 2, $client->entity->position["z"]);
console("[INFO] EID ".$client->eid." is over block ".$b[0].":".$b[1]);
}
break;
case "list":
console("[INFO] Player list:");
foreach($this->server->clients as $c){
console("[INFO] ".$c->username." (".$c->ip.":".$c->port."), ClientID ".$c->clientID);
}
break;
case "help":
case "?":
console("[INFO] /help: Show available commands");
console("[INFO] /gamemode: Changes default gamemode");
console("[INFO] /difficulty: Changes difficulty");
console("[INFO] /say: Broadcasts mesages");
console("[INFO] /time: Manages time");
console("[INFO] /list: Lists online users");
console("[INFO] /save-all: Saves pending changes");
console("[INFO] /whitelist: Manages whitelisting");
console("[INFO] /banip: Manages IP ban");
console("[INFO] /stop: Stops the server");
break;
default:
console("[ERROR] Command doesn't exist! Use /help");
break;
}
}
ftruncate($this->input, 0);
fseek($this->input, 0);
}
}

236
classes/API/ServerAPI.php Normal file
View File

@ -0,0 +1,236 @@
<?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 ServerAPI extends stdClass{ //Yay! I can add anything to this class in runtime!
var $server;
private $config;
function __construct(){
console("[INFO] Starting ServerAPI server handler...");
file_put_contents("packets.log", "");
file_put_contents("console.in", "");
if(!file_exists(FILE_PATH."white-list.txt")){
console("[NOTICE] No white-list.txt found, creating blank file");
file_put_contents(FILE_PATH."white-list.txt", "");
}
if(!file_exists(FILE_PATH."banned-ips.txt")){
console("[NOTICE] No banned-ips.txt found, creating blank file");
file_put_contents(FILE_PATH."banned-ips.txt", "");
}
if(!file_exists(FILE_PATH."server.properties")){
console("[NOTICE] No server.properties found, using default settings");
copy(FILE_PATH."common/default.properties", FILE_PATH."server.properties");
}
console("[DEBUG] Checking data folders...", true, true, 2);
@mkdir(FILE_PATH."data/players/", 0777, true);
@mkdir(FILE_PATH."data/maps/", 0777);
@mkdir(FILE_PATH."data/plugins/", 0777);
console("[DEBUG] Loading server.properties...", true, true, 2);
$this->parseProperties();
define("DEBUG", $this->config["debug"]);
$this->server = new PocketMinecraftServer($this->getProperty("server-name"), $this->getProperty("gamemode"), $this->getProperty("seed"), $this->getProperty("protocol"), $this->getProperty("port"), $this->getProperty("server-id"));
$this->server->api = $this;
if(file_exists(FILE_PATH."data/maps/level.dat")){
console("[NOTICE] Detected unimported map data. Importing...");
$this->importMap(FILE_PATH."data/maps/", true);
}
$this->server->mapName = $this->getProperty("level-name");
$this->server->mapDir = FILE_PATH."data/maps/".$this->getProperty("level-name")."/";
$this->loadProperties();
//Autoload all default APIs
console("[INFO] Loading default APIs");
$dir = dir(FILE_PATH."classes/API/");
while(false !== ($file = $dir->read())){
if($file !== "." and $file !== ".."){
$API = basename($file, ".php");
if(strtolower($API) !== "serverapi"){
$name = strtolower(substr($API, 0, -3));
$this->loadAPI($name, $API);
}
}
}
}
public function loadAPI($name, $class, $dir = false){
if($dir === false){
$dir = FILE_PATH."classes/API/";
}
$file = $dir.$class.".php";
if(!file_exists($file)){
console("[ERROR] API ".$name." [".$class."] in ".$dir." doesn't exist", true, true, 0);
return false;
}
require_once($file);
$this->$name = new $class($this->server);
console("[INFO] API ".$name." [".$class."] loaded");
}
public function importMap($dir, $remove = false){
if(file_exists($dir."level.dat")){
$nbt = new NBT();
$level = parseNBTData($nbt->loadFile($dir."level.dat"));
console("[DEBUG] Importing map \"".$level["LevelName"]."\" gamemode ".$level["GameType"]." with seed ".$level["RandomSeed"], true, true, 2);
unset($level["Player"]);
$lvName = $level["LevelName"]."/";
@mkdir(FILE_PATH."data/maps/".$lvName, 0777);
file_put_contents(FILE_PATH."data/maps/".$lvName."level.dat", serialize($level));
$entities = parseNBTData($nbt->loadFile($dir."entities.dat"));
file_put_contents(FILE_PATH."data/maps/".$lvName."entities.dat", serialize($entities["Entities"]));
if(!isset($entities["TileEntities"])){
$entities["TileEntities"] = array();
}
file_put_contents(FILE_PATH."data/maps/".$lvName."tileEntities.dat", serialize($entities["TileEntities"]));
console("[DEBUG] Imported ".count($entities["Entities"])." Entities and ".count($entities["TileEntities"])." TileEntities", true, true, 2);
if($remove === true){
rename($dir."chunks.dat", FILE_PATH."data/maps/".$lvName."chunks.dat");
unlink($dir."level.dat");
@unlink($dir."level.dat_old");
@unlink($dir."player.dat");
unlink($dir."entities.dat");
}else{
copy($dir."chunks.dat", FILE_PATH."data/maps/".$lvName."chunks.dat");
}
if($this->getProperty("level-name") === false){
console("[INFO] Setting default level to \"".$level["LevelName"]."\"");
$this->setProperty("level-name", $level["LevelName"]);
$this->setProperty("gamemode", $level["GameType"]);
$this->server->seed = $level["RandomSeed"];
$this->setProperty("spawn", array("x" => $level["SpawnX"], "y" => $level["SpawnY"], "z" => $level["SpawnZ"]));
$this->config["spawn"] = array("x" => $level["SpawnX"], "y" => $level["SpawnY"], "z" => $level["SpawnZ"]);
$this->writeProperties();
}
console("[INFO] Map \"".$level["LevelName"]."\" importing done!");
unset($level, $entities, $nbt);
return true;
}
return false;
}
public function getProperty($name){
if(isset($this->config[$name])){
return $this->config[$name];
}
return false;
}
public function setProperty($name, $value){
$this->config[$name] = $value;
$this->writeProperties();
$this->loadProperties();
}
private function loadProperties(){
if(is_object($server)){
$server->setType($this->config["server-type"]);
$server->timePerSecond = $this->config["time-per-second"];
$server->maxClients = $this->config["max-players"];
$server->description = $this->config["description"];
$server->motd = $this->config["motd"];
$server->gamemode = $this->config["gamemode"];
$server->difficulty = $this->config["difficulty"];
$server->spawn = $this->config["spawn"];
$server->whitelist = $this->config["white-list"];
$this->server->reloadConfig();
}
}
private function writeProperties(){
if(is_object($server)){
$this->config["seed"] = $server->seed;
$this->config["server-id"] = $server->serverID;
}
$this->config["regenerate-config"] = "false";
$this->config["white-list"] = $this->config["white-list"] === true ? "true":"false";
$prop = "#Pocket Minecraft PHP server properties\r\n#".date("D M j H:i:s T Y")."\r\n";
foreach($this->config as $n => $v){
if($n == "spawn"){
$v = implode(";", $v);
}
$prop .= $n."=".$v."\r\n";
}
file_put_contents(FILE_PATH."server.properties", $prop);
}
private function parseProperties(){
$prop = file_get_contents(FILE_PATH."server.properties");
$prop = explode("\n", str_replace("\r", "", $prop));
$this->config = array();
foreach($prop as $line){
if(trim($line) == "" or $line{0} == "#"){
continue;
}
$d = explode("=", $line);
$n = strtolower(array_shift($d));
$v = implode("=", $d);
switch($n){
case "protocol":
if(trim($v) == "CURRENT"){
$v = CURRENT_PROTOCOL;
break;
}
case "gamemode":
case "max-players":
case "port":
case "debug":
case "difficulty":
case "time-per-second":
$v = (int) $v;
break;
case "seed":
case "server-id":
$v = trim($v);
$v = $v == "false" ? false:(preg_match("/[^0-9\-]/", $v) > 0 ? Utils::readInt(substr(md5($v, true), 0, 4)):$v);
break;
case "level-name":
$v = trim($v);
$v = $v == "false" ? false:$v;
break;
case "spawn":
$v = explode(";", $v);
$v = array("x" => floatval($v[0]), "y" => floatval($v[1]), "z" => floatval($v[2]));
break;
case "white-list":
case "regenerate-config":
$v = trim($v) == "true" ? true:false;
break;
}
$this->config[$n] = $v;
}
}
public function start(){
$this->server->start();
}
}

View File

@ -27,11 +27,12 @@ the Free Software Foundation, either version 3 of the License, or
require_once("classes/Session.class.php");
class PocketMinecraftServer{
class PocketMinecraftServer extends stdClass{
var $seed, $protocol, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $timePerSecond, $responses, $spawn, $entities, $mapDir, $mapName, $map, $level, $tileEntities;
private $database, $interface, $cnt, $events, $version, $serverType, $lastTick;
function __construct($name, $gamemode = 1, $seed = false, $protocol = CURRENT_PROTOCOL, $port = 19132, $serverID = false, $version = CURRENT_VERSION){
$this->port = (int) $port;
console("[INFO] Pocket-Minecraft-PHP by @shoghicp, LGPL License. http://bit.ly/RE7uaW", true, true, 0);
console("[INFO] Starting Minecraft PE Server at *:".$this->port);
console("[INFO] Loading database...");
$this->startDatabase();
@ -212,16 +213,18 @@ class PocketMinecraftServer{
}else{
$this->map->loadMap();
}
console("[INFO] Loading entities...");
$entities = unserialize(file_get_contents($this->mapDir."entities.dat"));
foreach($entities as $entity){
$this->entities[$this->eidCnt] = new Entity($this->eidCnt, ENTITY_MOB, $entity["id"], $this);
$this->entities[$this->eidCnt]->setPosition($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2], $entity["Rotation"][0], $entity["Rotation"][1]);
$this->entities[$this->eidCnt]->setHealth($entity["Health"]);
++$this->eidCnt;
if($this->map !== false){
console("[INFO] Loading entities...");
$entities = unserialize(file_get_contents($this->mapDir."entities.dat"));
foreach($entities as $entity){
$this->entities[$this->eidCnt] = new Entity($this->eidCnt, ENTITY_MOB, $entity["id"], $this);
$this->entities[$this->eidCnt]->setPosition($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2], $entity["Rotation"][0], $entity["Rotation"][1]);
$this->entities[$this->eidCnt]->setHealth($entity["Health"]);
++$this->eidCnt;
}
console("[DEBUG] Loaded ".count($this->entities)." Entities", true, true, 2);
$this->action(1000000 * 60 * 15, '$this->chat(false, "Forcing save...");$this->save();$this->chat(false, "Done");');
}
console("[DEBUG] Loaded ".count($this->entities)." Entities", true, true, 2);
$this->action(1000000 * 60 * 15, '$this->chat(false, "Forcing save...");$this->save();$this->chat(false, "Done");');
}else{
console("[INFO] Time: ".$this->time);
console("[INFO] Seed: ".$this->seed);

View File

@ -27,363 +27,9 @@ the Free Software Foundation, either version 3 of the License, or
require_once("common/dependencies.php");
require_once("classes/PocketMinecraftServer.class.php");
file_put_contents("packets.log", "");
file_put_contents("console.in", "");
require_once("classes/API/ServerAPI.php");
$server = new ServerAPI();
//You can add simple things here
if(!file_exists(FILE_PATH."white-list.txt")){
console("[NOTICE] No white-list.txt found, creating blank file");
file_put_contents(FILE_PATH."white-list.txt", "");
}
if(!file_exists(FILE_PATH."banned-ips.txt")){
console("[NOTICE] No banned-ips.txt found, creating blank file");
file_put_contents(FILE_PATH."banned-ips.txt", "");
}
if(!file_exists(FILE_PATH."server.properties")){
console("[NOTICE] No server.properties found, using default settings");
copy(FILE_PATH."common/default.properties", FILE_PATH."server.properties");
}
@mkdir(FILE_PATH."data/players/", 0777, true);
@mkdir(FILE_PATH."data/maps/", 0777);
$prop = file_get_contents(FILE_PATH."server.properties");
$prop = explode("\n", str_replace("\r", "", $prop));
$config = array();
foreach($prop as $line){
if(trim($line) == "" or $line{0} == "#"){
continue;
}
$d = explode("=", $line);
$n = strtolower(array_shift($d));
$v = implode("=", $d);
switch($n){
case "protocol":
if(trim($v) == "CURRENT"){
$v = CURRENT_PROTOCOL;
break;
}
case "gamemode":
case "max-players":
case "port":
case "debug":
case "difficulty":
case "time-per-second":
$v = (int) $v;
break;
case "seed":
case "server-id":
$v = trim($v);
$v = $v == "false" ? false:(preg_match("/[^0-9\-]/", $v) > 0 ? Utils::readInt(substr(md5($v, true), 0, 4)):$v);
break;
case "level-name":
$v = trim($v);
$v = $v == "false" ? false:$v;
break;
case "spawn":
$v = explode(";", $v);
$v = array("x" => floatval($v[0]), "y" => floatval($v[1]), "z" => floatval($v[2]));
break;
case "white-list":
case "regenerate-config":
$v = trim($v) == "true" ? true:false;
break;
}
$config[$n] = $v;
}
define("DEBUG", $config["debug"]);
$server = new PocketMinecraftServer($config["server-name"], $config["gamemode"], $config["seed"], $config["protocol"], $config["port"], $config["server-id"]);
if(file_exists(FILE_PATH."data/maps/level.dat")){
console("[NOTICE] Detected unimported map data. Importing...");
$nbt = new NBT();
$level = parseNBTData($nbt->loadFile(FILE_PATH."data/maps/level.dat"));
console("[DEBUG] Importing map \"".$level["LevelName"]."\" gamemode ".$level["GameType"]." with seed ".$level["RandomSeed"], true, true, 2);
unset($level["Player"]);
$lvName = $level["LevelName"]."/";
@mkdir(FILE_PATH."data/maps/".$lvName, 0777);
file_put_contents(FILE_PATH."data/maps/".$lvName."level.dat", serialize($level));
$entities = parseNBTData($nbt->loadFile(FILE_PATH."data/maps/entities.dat"));
file_put_contents(FILE_PATH."data/maps/".$lvName."entities.dat", serialize($entities["Entities"]));
if(!isset($entities["TileEntities"])){
$entities["TileEntities"] = array();
}
file_put_contents(FILE_PATH."data/maps/".$lvName."tileEntities.dat", serialize($entities["TileEntities"]));
console("[DEBUG] Imported ".count($entities["Entities"])." Entities and ".count($entities["TileEntities"])." TileEntities", true, true, 2);
rename(FILE_PATH."data/maps/chunks.dat", FILE_PATH."data/maps/".$lvName."chunks.dat");
unlink(FILE_PATH."data/maps/level.dat");
@unlink(FILE_PATH."data/maps/level.dat_old");
@unlink(FILE_PATH."data/maps/player.dat");
unlink(FILE_PATH."data/maps/entities.dat");
if($config["level-name"] === false){
console("[INFO] Setting default level to \"".$level["LevelName"]."\"");
$config["level-name"] = $level["LevelName"];
$config["gamemode"] = $level["GameType"];
$server->gamemode = $config["gamemode"];
$server->seed = $level["RandomSeed"];
$config["spawn"] = array("x" => $level["SpawnX"], "y" => $level["SpawnY"], "z" => $level["SpawnZ"]);
$config["regenerate-config"] = true;
}
console("[INFO] Map \"".$level["LevelName"]."\" importing done!");
unset($level, $entities, $nbt);
}
$server->mapName = $config["level-name"];
$server->mapDir = FILE_PATH."data/maps/".$config["level-name"]."/";
loadConfig();
function loadConfig($regenerate = false){
global $server, $config;
$server->setType($config["server-type"]);
$server->timePerSecond = $config["time-per-second"];
$server->maxClients = $config["max-players"];
$server->description = $config["description"];
$server->motd = $config["motd"];
$server->spawn = $config["spawn"];
$server->whitelist = $config["white-list"];
if($config["regenerate-config"] == true or $regenerate === true){
$config["seed"] = $server->seed;
$config["server-id"] = $server->serverID;
$config["regenerate-config"] = "false";
$config["white-list"] = $config["white-list"] === true ? "true":"false";
$prop = "#Pocket Minecraft PHP server properties\r\n#".date("D M j H:i:s T Y")."\r\n";
foreach($config as $n => $v){
if($n == "spawn"){
$v = implode(";", $v);
}
$prop .= $n."=".$v."\r\n";
}
file_put_contents(FILE_PATH."server.properties", $prop);
}
$server->reloadConfig();
}
$server->event("onTick", "serverCommands");
$commands = fopen(FILE_PATH."console.in", "w+b");
function serverCommands(){
global $server, $commands, $config;
while(($line = fgets($commands)) !== false){
$line = trim($line);
if($line === ""){
continue;
}
$params = explode(" ", $line);
$cmd = strtolower(array_shift($params));
console("[INFO] Issued server command: /$cmd ".implode(" ", $params));
switch($cmd){
case "stop":
console("[INFO] Stopping server...");
$server->close();
break;
case "banip":
$p = strtolower(array_shift($params));
switch($p){
case "pardon":
case "remove":
$ip = trim(implode($params));
$new = array();
foreach(explode("\n", str_replace(array("\r","\t"), "", file_get_contents(FILE_PATH."banned-ips.txt"))) as $i){
if($i == $ip){
console("[INFO] IP \"$ip\" removed from ban list");
continue;
}
$new[$i] = $i;
}
file_put_contents(FILE_PATH."banned-ips.txt", implode("\r\n", $new));
loadConfig();
break;
case "add":
case "ban":
$ip = trim(implode($params));
file_put_contents(FILE_PATH."banned-ips.txt", "\r\n".$ip, FILE_APPEND);
console("[INFO] IP \"$ip\" added to ban list");
loadConfig();
break;
case "reload":
loadConfig();
break;
case "list":
console("[INFO] IP ban list: ".implode(", ", explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."banned-ips.txt")))));
break;
default:
console("[INFO] Usage: /banip <add | remove | list | reload> [IP]");
break;
}
break;
case "gamemode":
$s = trim(array_shift($params));
if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){
console("[INFO] Usage: /gamemode <0 | 1>");
break;
}
$config["gamemode"] = (int) $s;
$server->gamemode = $config["gamemode"];
console("[INFO] Gamemode changed to ".$server->gamemode);
loadConfig(true);
break;
case "difficulty":
$s = trim(array_shift($params));
if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){
console("[INFO] Usage: /difficulty <0 | 1>");
break;
}
$config["difficulty"] = (int) $s;
$server->difficulty = $config["difficulty"];
console("[INFO] Difficulty changed to ".$server->difficulty);
loadConfig(true);
break;
case "say":
$s = implode(" ", $params);
if(trim($s) == ""){
console("[INFO] Usage: /say <message>");
break;
}
$server->chat(false, $s);
break;
case "time":
$p = strtolower(array_shift($params));
switch($p){
case "check":
$time = abs($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 (".$server->time.")");
break;
case "add":
$t = (int) array_shift($params);
$server->time += $t;
break;
case "set":
$t = (int) array_shift($params);
$server->time = $t;
break;
case "sunrise":
$server->time = 17800;
break;
case "day":
$server->time = 0;
break;
case "sunset":
$server->time = 9500;
break;
case "night":
$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){
case "remove":
$user = trim(implode(" ", $params));
$new = array();
foreach(explode("\n", str_replace(array("\r","\t"), "", file_get_contents(FILE_PATH."white-list.txt"))) as $u){
if($u == $user){
console("[INFO] Player \"$user\" removed from white-list");
continue;
}
$new[$u] = $u;
}
file_put_contents(FILE_PATH."white-list.txt", implode("\r\n", $new));
loadConfig();
break;
case "add":
$user = trim(implode(" ", $params));
file_put_contents(FILE_PATH."white-list.txt", "\r\n".$user, FILE_APPEND);
console("[INFO] Player \"$user\" added to white-list");
loadConfig();
break;
case "reload":
loadConfig(true);
break;
case "list":
console("[INFO] White-list: ".implode(", ", explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt")))));
break;
case "on":
case "true":
case "1":
console("[INFO] White-list turned on");
$config["white-list"] = true;
loadConfig(true);
break;
case "off":
case "false":
case "0":
console("[INFO] White-list turned off");
$config["white-list"] = false;
loadConfig(true);
break;
default:
console("[INFO] Usage: /whitelist <on | off | add | reload | list> [username]");
break;
}
break;
case "save-all":
$server->save();
break;
case "spawnplayer":
foreach($server->clients as $client){
$entity = $client->entity;
$server->trigger("onPlayerAdd", array(
"clientID" => Utils::readLong(Utils::getRandomBytes(8)),
"username" => Utils::strToHex(Utils::getRandomBytes(6)),
"eid" => $server->eidCnt++,
"x" => $entity->position["x"],
"y" => $entity->position["y"],
"z" => $entity->position["z"],
));
}
break;
case "block":
foreach($server->clients as $client){
$b = $server->map->getBlock($client->entity->position["x"], $client->entity->position["y"] - 2, $client->entity->position["z"]);
console("[INFO] EID ".$client->eid." is over block ".$b[0].":".$b[1]);
}
break;
case "list":
console("[INFO] Player list:");
foreach($server->clients as $c){
console("[INFO] ".$c->username." (".$c->ip.":".$c->port."), ClientID ".$c->clientID);
}
break;
case "help":
case "?":
console("[INFO] /help: Show available commands");
console("[INFO] /gamemode: Changes default gamemode");
console("[INFO] /difficulty: Changes difficulty");
console("[INFO] /say: Broadcasts mesages");
console("[INFO] /time: Manages time");
console("[INFO] /list: Lists online users");
console("[INFO] /save-all: Saves pending changes");
console("[INFO] /whitelist: Manages whitelisting");
console("[INFO] /banip: Manages IP ban");
console("[INFO] /stop: Stops the server");
break;
default:
console("[ERROR] Command doesn't exist! Use /help");
break;
}
}
ftruncate($commands, 0);
fseek($commands, 0);
}
$server->start();
$server->start();