diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index e74a68dad..d6aaeb5e5 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -128,7 +128,7 @@ class ConsoleAPI{ $max = ceil(count($cmds) / 5); $page = (int) (isset($params[0]) ? min($max, max(1, intval($params[0]))):1); - $output .= "- Showing help page $page of $max (/help ) -\n"; + $output .= "\x1b[31;1m-\x1b[0m Showing help page $page of $max (/help ) \x1b[31;1m-\x1b[0m\n"; $current = 1; foreach($cmds as $c => $h){ $curpage = (int) ceil($current / 5); @@ -175,9 +175,9 @@ class ConsoleAPI{ return $this->run($this->alias[$cmd] . ($params !== "" ? " " .$params:""), $issuer, $cmd); } if($issuer instanceof Player){ - console("[DEBUG] \x1b[33m".$issuer->username."\x1b[0m issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2); + console("[DEBUG] ".FORMAT_AQUA.$issuer->username.FORMAT_RESET." issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2); }else{ - console("[DEBUG] \x1b[33m*".$issuer."\x1b[0m issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2); + console("[DEBUG] ".FORMAT_YELLOW."*".$issuer.FORMAT_RESET." issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2); } if(preg_match_all('#@([@a-z]{1,})#', $params, $matches, PREG_OFFSET_CAPTURE) > 0){ diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index 552ca1a92..b9bdb069d 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -79,7 +79,7 @@ class PluginAPI extends stdClass{ console("[ERROR] Failed parsing of ".basename($file)); return false; } - console("[INFO] Loading plugin \"\x1b[32m".$info["name"]."\x1b[0m\" \x1b[35m".$info["version"]." \x1b[0mby \x1b[36m".$info["author"]."\x1b[0m"); + console("[INFO] Loading plugin \"".FORMAT_GREEN.$info["name"].FORMAT_RESET."\" ".FORMAT_AQUA.$info["version"].FORMAT_RESET." by ".FORMAT_AQUA.$info["author"].FORMAT_RESET); if($info["class"] !== "none" and class_exists($info["class"])){ console("[ERROR] Failed loading plugin: class already exists"); return false; @@ -98,7 +98,7 @@ class PluginAPI extends stdClass{ if($info["class"] !== "none"){ $object = new $className($this->server->api, false); if(!($object instanceof Plugin)){ - console("[ERROR] Plugin \"\x1b[36m".$info["name"]."\x1b[0m\" doesn't use the Plugin Interface"); + console("[ERROR] Plugin \"".$info["name"]."\" doesn't use the Plugin Interface"); if(method_exists($object, "__destruct")){ $object->__destruct(); } diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index e7c894514..cf16dd7bc 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -48,7 +48,7 @@ class ServerAPI{ @mkdir(DATA_PATH."worlds/", 0755); @mkdir(DATA_PATH."plugins/", 0755); $version = new VersionString(); - console("[INFO] Starting Minecraft PE server version \x1b[36;1m".CURRENT_MINECRAFT_VERSION); + console("[INFO] Starting Minecraft PE server version ".FORMAT_AQUA.CURRENT_MINECRAFT_VERSION); console("[INFO] Loading properties..."); $this->config = new Config(DATA_PATH . "server.properties", CONFIG_PROPERTIES, array( @@ -92,7 +92,7 @@ class ServerAPI{ $this->server = new PocketMinecraftServer($this->getProperty("server-name"), $this->getProperty("gamemode"), ($seed = $this->getProperty("level-seed")) != "" ? (int) $seed:false, $this->getProperty("server-port"), ($ip = $this->getProperty("server-ip")) != "" ? $ip:"0.0.0.0"); $this->server->api = $this; self::$serverRequest = $this->server; - console("[INFO] This server is running PocketMine-MP version ".($version->isDev() ? "\x1b[31;1m":"").MAJOR_VERSION."\x1b[0m (MCPE: ".CURRENT_MINECRAFT_VERSION.") (API ".CURRENT_API_VERSION.")", true, true, 0); + console("[INFO] This server is running PocketMine-MP version ".($version->isDev() ? FORMAT_YELLOW:"").MAJOR_VERSION.FORMAT_RESET." (MCPE: ".CURRENT_MINECRAFT_VERSION.") (API ".CURRENT_API_VERSION.")", true, true, 0); console("[INFO] PocketMine-MP is distibuted under the LGPL License", true, true, 0); if($this->getProperty("upnp-forwarding") === true){ @@ -102,7 +102,7 @@ class ServerAPI{ if($this->getProperty("last-update") === false or ($this->getProperty("last-update") + 3600) < time()){ console("[INFO] Checking for new server version"); - console("[INFO] Last check: \x1b[36m".date("Y-m-d H:i:s", $this->getProperty("last-update"))."\x1b[0m"); + console("[INFO] Last check: ".FORMAT_AQUA.date("Y-m-d H:i:s", $this->getProperty("last-update"))."\x1b[0m"); $info = json_decode(Utils::curl_get("http://www.pocketmine.net/latest"), true); if($this->server->version->isDev()){ if($info === false or !isset($info["development"])){ @@ -110,13 +110,13 @@ class ServerAPI{ }else{ $last = $info["development"]["date"]; if($last >= $this->getProperty("last-update") and $this->getProperty("last-update") !== false and GIT_COMMIT != $info["development"]["commit"]){ - console("[NOTICE] \x1b[33mA new DEVELOPMENT version of PocketMine-MP has been released"); - console("[NOTICE] \x1b[33mVersion \"".$info["development"]["version"]."\" [".substr($info["development"]["commit"], 0, 10)."]"); - console("[NOTICE] \x1b[36mGet it at PocketMine.net or ".$info["development"]["download"]); + console("[NOTICE] ".FORMAT_YELLOW."A new DEVELOPMENT version of PocketMine-MP has been released"); + console("[NOTICE] ".FORMAT_YELLOW."Version \"".$info["development"]["version"]."\" [".substr($info["development"]["commit"], 0, 10)."]"); + console("[NOTICE] ".FORMAT_YELLOW."Get it at PocketMine.net or ".$info["development"]["download"]); console("[NOTICE] This message will dissapear after issuing the command \"/update-done\""); }else{ $this->setProperty("last-update", time()); - console("[INFO] \x1b[36mThis is the latest DEVELOPMENT version"); + console("[INFO] ".FORMAT_AQUA."This is the latest DEVELOPMENT version"); } } }else{ @@ -128,13 +128,13 @@ class ServerAPI{ $update = new VersionString($info["stable"]["version"]); $updateN = $update->getNumber(); if($updateN > $newestN){ - console("[NOTICE] \x1b[33mA new STABLE version of PocketMine-MP has been released"); - console("[NOTICE] \x1b[36mVersion \"".$info["stable"]["version"]."\" #".$updateN); + console("[NOTICE] ".FORMAT_GREEN."A new STABLE version of PocketMine-MP has been released"); + console("[NOTICE] ".FORMAT_GREEN."Version \"".$info["stable"]["version"]."\" #".$updateN); console("[NOTICE] Get it at PocketMine.net or ".$info["stable"]["download"]); console("[NOTICE] This message will dissapear as soon as you update"); }else{ $this->setProperty("last-update", time()); - console("[INFO] \x1b[36mThis is the latest STABLE version"); + console("[INFO] ".FORMAT_AQUA."This is the latest STABLE version"); } } } diff --git a/src/Player.php b/src/Player.php index db26c7afc..a83887c26 100644 --- a/src/Player.php +++ b/src/Player.php @@ -280,7 +280,7 @@ class Player{ if($msg === true and $this->username != ""){ $this->server->api->chat->broadcast($this->username." left the game"); } - console("[INFO] \x1b[33m".$this->username."\x1b[0m[/".$this->ip.":".$this->port."] logged out due to ".$reason); + console("[INFO] ".FORMAT_AQUA.$this->username.FORMAT_RESET."[/".$this->ip.":".$this->port."] logged out due to ".$reason); $this->windows = array(); $this->armor = array(); $this->inventory = array(); @@ -1265,7 +1265,7 @@ class Player{ $this->evid[] = $this->server->event("tile.update", array($this, "eventHandler")); $this->lastMeasure = microtime(true); $this->server->schedule(50, array($this, "measureLag"), array(), true); - console("[INFO] \x1b[33m".$this->username."\x1b[0m[/".$this->ip.":".$this->port."] logged in with entity id ".$this->eid." at (".$this->entity->level->getName().", ".round($this->entity->x, 2).", ".round($this->entity->y, 2).", ".round($this->entity->z, 2).")"); + console("[INFO] ".FORMAT_AQUA.$this->username.FORMAT_RESET."[/".$this->ip.":".$this->port."] logged in with entity id ".$this->eid." at (".$this->entity->level->getName().", ".round($this->entity->x, 2).", ".round($this->entity->y, 2).", ".round($this->entity->z, 2).")"); break; case MC_READY: if($this->loggedIn === false){ @@ -1284,7 +1284,7 @@ class Player{ $this->server->api->entity->spawnToAll($this->entity); $this->server->schedule(5, array($this->entity, "update"), array(), true); - $this->server->schedule(2, array($this->entity, "checkMovement"), array(), true); + $this->server->schedule(2, array($this->entity, "updateMovement"), array(), true); $this->sendArmor(); $this->sendChat($this->server->motd."\n"); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index f7a430823..4870e3b14 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -26,9 +26,9 @@ class PocketMinecraftServer{ private function load(){ $this->version = new VersionString(); - if(defined("DEBUG") and DEBUG >= 0){ + /*if(defined("DEBUG") and DEBUG >= 0){ @cli_set_process_title("PocketMine-MP ".MAJOR_VERSION); - } + }*/ console("[INFO] Starting Minecraft PE server on ".($this->serverip === "0.0.0.0" ? "*":$this->serverip).":".$this->port); define("BOOTUP_RANDOM", Utils::getRandomBytes(16)); $this->serverID = $this->serverID === false ? Utils::readLong(Utils::getRandomBytes(8, false)):$this->serverID; @@ -89,7 +89,7 @@ class PocketMinecraftServer{ public function titleTick(){ $time = microtime(true); if(defined("DEBUG") and DEBUG >= 0 and ENABLE_ANSI === true){ - echo "\x1b]0;PocketMine-MP ".MAJOR_VERSION." | Online ". count($this->clients)."/".$this->maxClients." | RAM ".round((memory_get_usage() / 1024) / 1024, 2)."MB | U ".round(($this->interface->bandwidth[1] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2)." D ".round(($this->interface->bandwidth[0] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2)." kB/s | TPS ".$this->getTPS()."\x07"; + cli_set_process_title("PocketMine-MP ".MAJOR_VERSION." | Online ". count($this->clients)."/".$this->maxClients." | RAM ".round((memory_get_usage() / 1024) / 1024, 2)."MB | U ".round(($this->interface->bandwidth[1] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2)." D ".round(($this->interface->bandwidth[0] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2)." kB/s | TPS ".$this->getTPS()); } $this->interface->bandwidth = array(0, 0, $time); } diff --git a/src/dependencies.php b/src/dependencies.php index 22c021a03..c0a4de1cd 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -21,6 +21,7 @@ /***REM_START***/ require_once(dirname(__FILE__)."/config.php"); +require_once(FILE_PATH."/src/utils/TextFormat.php"); require_once(FILE_PATH."/src/functions.php"); /***REM_END***/ define(DATA_PATH, realpath(arg("data-path", FILE_PATH))."/"); diff --git a/src/functions.php b/src/functions.php index 9434072a4..cdd90d58b 100644 --- a/src/functions.php +++ b/src/functions.php @@ -182,7 +182,7 @@ function arguments ( $args ){ function console($message, $EOL = true, $log = true, $level = 1){ if(!defined("DEBUG") or DEBUG >= $level){ $message .= $EOL === true ? PHP_EOL:""; - $time = (ENABLE_ANSI === true ? "\x1b[36m".date("H:i:s")."\x1b[0m":date("H:i:s")) . " "; + $time = (ENABLE_ANSI === true ? FORMAT_AQUA . date("H:i:s") . FORMAT_RESET:date("H:i:s")) . " "; $replaced = preg_replace('/\x1b\[[0-9;]*m/', "", $time . $message); if($log === true and (!defined("LOG") or LOG === true)){ logg(date("Y-m-d")." ".$replaced, "console", false, $level); @@ -190,27 +190,26 @@ function console($message, $EOL = true, $log = true, $level = 1){ if(ENABLE_ANSI === true){ $add = ""; if(preg_match("/\[([a-zA-Z0-9]*)\]/", $message, $matches) > 0){ - $add .= "\x1b"; switch($matches[1]){ case "ERROR": - $add .= "[31;1m"; + $add .= FORMAT_RED; break; case "INTERNAL": case "DEBUG": - $add .= "[30;1m"; + $add .= FORMAT_GRAY; break; case "WARNING": - $add .= "[33;1m"; + $add .= FORMAT_YELLOW; break; case "NOTICE": - $add .= "[37;1m"; + $add .= FORMAT_AQUA; break; default: $add = ""; break; } } - $message = $time . $add . $message . "\x1b[0m"; + $message = TextFormat::toANSI($time . $add . $message . FORMAT_RESET); }else{ $message = $replaced; } diff --git a/src/tests/ServerSuiteTest.php b/src/tests/ServerSuiteTest.php index 1e1e53c6a..4ef030f75 100644 --- a/src/tests/ServerSuiteTest.php +++ b/src/tests/ServerSuiteTest.php @@ -4,9 +4,9 @@ function testCase($name, $output, $expected){ global $testErrors; if($output === $expected){ - console("[TEST] $name: \x1b[32mOk."); + console("[TEST] $name: ".FORMAT_GREEN."Ok."); }else{ - console("[TEST] $name: \x1b[31mError."); + console("[TEST] $name: ".FORMAT_RED."Error."); console("Expected ".print_r($expected, true).", got ".print_r($output, true)); ++$testErrors; } @@ -17,15 +17,15 @@ require_once(dirname(__FILE__)."/../dependencies.php"); require_once(FILE_PATH."/src/functions.php"); require_once(FILE_PATH."/src/dependencies.php"); - console("\x1b[36m[TEST] Starting tests"); + console(FORMAT_GREEN . "[TEST] Starting tests"); testCase("dummy", dummy(), null); $t = new ServerSuiteTest; echo PHP_EOL; if($testErrors === 0){ - console("\x1b[32m[TEST] No errors. Test complete."); + console(FORMAT_GREEN . "[TEST] No errors. Test complete."); exit(0); }else{ - console("\x1b[31m[TEST] Errors found."); + console(FORMAT_RED . "[TEST] Errors found."); exit(1); } } diff --git a/src/utils/TextFormat.php b/src/utils/TextFormat.php new file mode 100644 index 000000000..a15f09df0 --- /dev/null +++ b/src/utils/TextFormat.php @@ -0,0 +1,137 @@ +