diff --git a/.travis.yml b/.travis.yml index 6c4cc803..1c46d5ed 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ before_script: - pecl install channel://pecl.php.net/pthreads-0.0.44 script: - - phpunit src/tests/ + - php src/tests/ServerSuiteTest.php notifications: email: false diff --git a/src/API/MobAPI.php b/src/API/MobAPI.php deleted file mode 100644 index 305aceba..00000000 --- a/src/API/MobAPI.php +++ /dev/null @@ -1,31 +0,0 @@ -server = ServerAPI::request(); - } - - public function init(){ - - } -} diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 54f8cd3b..12381ba7 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -149,7 +149,6 @@ class ServerAPI{ $this->loadAPI("tile", "TileAPI"); $this->loadAPI("player", "PlayerAPI"); $this->loadAPI("time", "TimeAPI"); - $this->loadAPI("mob", "MobAPI"); foreach($this->apiList as $ob){ if(is_callable(array($ob, "init"))){ diff --git a/src/Data.php b/src/Data.php deleted file mode 100644 index eb5486ac..00000000 --- a/src/Data.php +++ /dev/null @@ -1,256 +0,0 @@ - true, - 6 => true, - 30 => true, - 31 => true, - 32 => true, - 37 => true, - 38 => true, - 39 => true, - 40 => true, - 50 => true, - 51 => true, - 55 => true, - 59 => true, - 78 => true, - 105 => true, - ); - static $unbreakable = array( - 0 => true, - 7 => true, - 8 => true, - 9 => true, - 10 => true, - 11 => true, - ); - static $transparent = array( - 0 => true, - 6 => true, - 8 => true, - 9 => true, - 10 => true, - 11 => true, - 18 => true, - 20 => true, - 26 => true, - 30 => true, - 31 => true, - 32 => true, - 37 => true, - 38 => true, - 39 => true, - 40 => true, - 44 => true, - 46 => true, - 50 => true, - 51 => true, - 53 => true, - 59 => true, - 63 => true, - 64 => true, - 65 => true, - 67 => true, - 68 => true, - 71 => true, - 78 => true, - 79 => true, - 83 => true, - 85 => true, - 89 => true, - 96 => true, - 102 => true, - 105 => true, - 107 => true, - 108 => true, - 109 => true, - 114 => true, - 128 => true, - 156 => true, - ); - static $replaceable = array( - 0 => true, - 8 => true, - 9 => true, - 10 => true, - 11 => true, - 31 => true, - 51 => true, - 78 => true, - ); - static $activable = array( - 2 => true, - 3 => true, - 6 => true, - 26 => true, - 31 => true, - //46 => true, - 51 => true, - 54 => true, - 58 => true, - 59 => true, - 61 => true, - 62 => true, - 64 => true, - 71 => true, - 78 => true, - 96 => true, - 105 => true, - 107 => true, - 245 => true, - 247 => true, - ); - static $placeable = array( - 1 => true, - 2 => true, - 3 => true, - 4 => true, - 5 => true, - 6 => true, - //7 => true, - 8 => true, - 9 => true, - 10 => true, - 11 => true, - 12 => true, - 13 => true, - 14 => true, - 15 => true, - 16 => true, - 17 => true, - 18 => true, - 19 => true, - 20 => true, - 21 => true, - 22 => true, - 24 => true, - 355 => 26, - 30 => true, - 35 => true, - 37 => true, - 38 => true, - 39 => true, - 40 => true, - 41 => true, - 42 => true, - 43 => true, - 44 => true, - 45 => true, - 46 => true, - 47 => true, - 48 => true, - 49 => true, - 50 => true, - 53 => true, - 54 => true, - 56 => true, - 59 => true, - 57 => true, - 58 => true, - 295 => 59, - 61 => true, - 324 => 64, - 65 => true, - 67 => true, - 330 => 71, - 73 => true, - 79 => true, - 80 => true, - 81 => true, - 82 => true, - 83 => true, - 85 => true, - 86 => true, - 87 => true, - 88 => true, - 89 => true, - 91 => true, - 96 => true, - 98 => true, - 102 => true, - 103 => true, - 362 => 105, - 107 => true, - 108 => true, - 109 => true, - 112 => true, - 114 => true, - 128 => true, - 155 => true, - 156 => true, - 245 => true, - 246 => true, - 247 => true, - 323 => true, //Special case of signs - 338 => 83, - ); - static $blocks = array( - 0 => "Air", - 1 => "Stone", - 2 => "Grass", - 3 => "Dirt", - 4 => "Cobblestone", - 5 => "Wooden Planks", - 6 => "Sapling", - 7 => "Bedrock", - ); - - -} \ No newline at end of file diff --git a/src/Player.php b/src/Player.php index cdff707f..85bd7cb5 100644 --- a/src/Player.php +++ b/src/Player.php @@ -1100,7 +1100,7 @@ class Player{ $this->close("Incorrect protocol #".$data["protocol1"], false); break; } - if(preg_match('#[^a-zA-Z0-9_]#', $data["username"]) == 0 && $data["username"] != ""){ + if(preg_match('#[^a-zA-Z0-9_]#', $data["username"]) == 0 and $data["username"] != ""){ $this->username = $data["username"]; $this->iusername = strtolower($this->username); }else{ @@ -1631,7 +1631,7 @@ class Player{ } $this->craftingItems = array(); $this->toCraft = array(); - if(trim($data["message"]) != "" and strlen($data["message"]) <= 255 and preg_match('#[^\\x20-\\xff]#', $data["message"]) == 0){ + if(trim($data["message"]) != "" and strlen($data["message"]) <= 255){ $message = $data["message"]; if($message{0} === "/"){ //Command $this->server->api->console->run(substr($message, 1), $this); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index a2919498..e73c176b 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -26,7 +26,9 @@ class PocketMinecraftServer{ private function load(){ $this->version = new VersionString(); - @cli_set_process_title("PocketMine-MP ".MAJOR_VERSION); + if(defined("DEBUG") and DEBUG >= 0){ + @cli_set_process_title("PocketMine-MP ".MAJOR_VERSION); + } if($this->version->isDev()){ console("[INFO] \x1b[31;1mThis is a Development version"); } @@ -64,7 +66,9 @@ class PocketMinecraftServer{ $this->reloadConfig(); $this->stop = false; $this->ticks = 0; - $this->asyncThread = new AsyncMultipleQueue(); + if(!defined("NO_THREADS")){ + $this->asyncThread = new AsyncMultipleQueue(); + } } function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){ @@ -87,7 +91,7 @@ class PocketMinecraftServer{ public function titleTick(){ $time = microtime(true); - if(ENABLE_ANSI === 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"; } $this->interface->bandwidth = array(0, 0, $time); @@ -186,7 +190,10 @@ class PocketMinecraftServer{ $this->stop = true; $this->trigger("server.close", $reason); $this->interface->close(); - @$this->asyncThread->stop = true; + + if(!defined("NO_THREADS")){ + @$this->asyncThread->stop = true; + } } } @@ -204,6 +211,9 @@ class PocketMinecraftServer{ } public function asyncOperation($type, array $data, callable $callable = null){ + if(defined("NO_THREADS")){ + return false; + } $d = ""; $type = (int) $type; switch($type){ @@ -227,6 +237,9 @@ class PocketMinecraftServer{ } public function asyncOperationChecker(){ + if(defined("NO_THREADS")){ + return false; + } if(isset($this->asyncThread->output{5})){ $offset = 0; $ID = Utils::readInt(substr($this->asyncThread->output, $offset, 4)); @@ -552,8 +565,12 @@ class PocketMinecraftServer{ $lastLoop = 0; }else{ ++$lastLoop; - if($lastLoop >= 16){ - usleep(5000); + if($lastLoop < 16){ + usleep(1); + }elseif($lastLoop >= 128){ + usleep(100); + }elseif($lastLoop >= 256){ + usleep(512); } } } diff --git a/src/functions.php b/src/functions.php index 6fd1c954..beb6a457 100644 --- a/src/functions.php +++ b/src/functions.php @@ -122,32 +122,6 @@ function hard_unset(&$var){ } } - -function parseNBTData($data){ - $x = array(); - if(isset($data["value"])){ - return parseNBTData($data["value"]); - } - foreach($data as $d){ - if(!isset($d["value"]) and is_array($d) and count($d) == 1){ - return parseNBTData(array_pop($d)); - }elseif(!isset($d["value"]) and is_array($d)){ - $x[] = parseNBTData($d); - }elseif(is_array($d["value"]) and isset($d["name"])){ - $x[$d["name"]] = parseNBTData($d["value"]); - }elseif(is_array($d["value"]) and $d["type"] == 10){ - return parseNBTData($d["value"]); - }elseif($d["name"] != ""){ - $x[$d["name"]] = $d["value"]; - } - } - if(count($x) == 0){ - $x = $data; - } - return $x; -} - - function arg($name, $default = false){ global $arguments, $argv; if(!isset($arguments)){ diff --git a/src/tests/ServerSuiteTest.php b/src/tests/ServerSuiteTest.php index aa938214..1e1e53c6 100644 --- a/src/tests/ServerSuiteTest.php +++ b/src/tests/ServerSuiteTest.php @@ -1,38 +1,60 @@ assertTrue(true); - $server = ServerAPI::request(); - - - //Everything done! - $server->close(); +/***REM_START***/ + $testErrors = 0; + function testCase($name, $output, $expected){ + global $testErrors; + if($output === $expected){ + console("[TEST] $name: \x1b[32mOk."); + }else{ + console("[TEST] $name: \x1b[31mError."); + console("Expected ".print_r($expected, true).", got ".print_r($output, true)); + ++$testErrors; + } } - - public function testRead(){ + + if(!class_exists("PocketMinecraftServer", false)){ define("NO_THREADS", true); require_once(dirname(__FILE__)."/../dependencies.php"); require_once(FILE_PATH."/src/functions.php"); require_once(FILE_PATH."/src/dependencies.php"); - - //binary things - $this->assertTrue(Utils::readTriad("\x02\x01\x03") === 131331, "Utils::readTriad"); - $this->assertTrue(Utils::readInt("\xff\x02\x01\x03") === -16645885, "Utils::readInt"); - $this->assertTrue(abs(Utils::readFloat("\x49\x02\x01\x03") - 532496.1875) < 0.0001, "Utils::readFloat"); - $this->assertTrue(abs(Utils::readDouble("\x41\x02\x03\x04\x05\x06\x07\x08") - 147552.5024529) < 0.0001, "Utils::readDouble"); - $this->assertTrue(Utils::readLong("\x41\x02\x03\x04\x05\x06\x07\x08") === "4684309878217770760", "Utils::readLong"); - - //PocketMine-MP server startup - $this->server = new ServerAPI(); - $this->server->load(); - $this->assertTrue(is_integer($this->server->event("server.start", array($this, "hook")))); - $this->server->init(); - exit(0); + console("\x1b[36m[TEST] Starting tests"); + testCase("dummy", dummy(), null); + $t = new ServerSuiteTest; + echo PHP_EOL; + if($testErrors === 0){ + console("\x1b[32m[TEST] No errors. Test complete."); + exit(0); + }else{ + console("\x1b[31m[TEST] Errors found."); + exit(1); + } } - } -} \ No newline at end of file + class ServerSuiteTest { + public function __construct(){ + //binary things + testCase("Utils::readTriad", Utils::readTriad("\x02\x01\x03"), 131331); + testCase("Utils::readInt", Utils::readInt("\xff\x02\x01\x03"), -16645885); + testCase("Utils::readFloat", abs(Utils::readFloat("\x49\x02\x01\x03") - 532496.1875) < 0.0001, true); + testCase("Utils::readDouble", abs(Utils::readDouble("\x41\x02\x03\x04\x05\x06\x07\x08") - 147552.5024529) < 0.0001, true); + testCase("Utils::readTriad", Utils::readLong("\x41\x02\x03\x04\x05\x06\x07\x08"), "4684309878217770760"); + + //PocketMine-MP server startup + global $server; + $server = new ServerAPI(); + $server->load(); + testCase("event attached", is_integer($server->event("server.start", array($this, "hook"))), true); + $server->init(); + } + + public function hook(){ + testCase("event fired", true, true); + $server = ServerAPI::request(); + testCase("defaultgamemode", $server->getGamemode(), "survival"); + + + //Everything done! + $server->close(); + } + } +/***REM_END***/ \ No newline at end of file diff --git a/src/tests/phpunit.xml b/src/tests/phpunit.xml deleted file mode 100644 index 2fa40761..00000000 --- a/src/tests/phpunit.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - ./ - - \ No newline at end of file diff --git a/src/world/Tile.php b/src/world/Tile.php index 8a038047..3bf934ff 100644 --- a/src/world/Tile.php +++ b/src/world/Tile.php @@ -103,6 +103,8 @@ class Tile extends Position{ $this->server->api->tile->spawnToAll($this); $this->server->api->tile->spawnToAll($tile); + $this->server->handle("tile.update", $this); + $this->server->handle("tile.update", $tile); } public function unpair(){ @@ -113,9 +115,11 @@ class Tile extends Position{ $tile = $this->getPair(); unset($this->data["pairx"], $this->data["pairz"], $tile->data["pairx"], $tile->data["pairz"]); - $this->server->api->tile->spawnToAll($this); + $this->server->api->tile->spawnToAll($this); + $this->server->handle("tile.update", $this); if($tile instanceof Tile){ $this->server->api->tile->spawnToAll($tile); + $this->server->handle("tile.update", $tile); } } @@ -438,6 +442,7 @@ class Tile extends Position{ $this->data["Text2"] = $line2; $this->data["Text3"] = $line3; $this->data["Text4"] = $line4; + $this->server->api->tile->spawnToAll($this); $this->server->handle("tile.update", $this); return true; } diff --git a/start.cmd b/start.cmd index 83d08496..47ec2390 100644 --- a/start.cmd +++ b/start.cmd @@ -6,6 +6,7 @@ cd /d %~dp0 FOR /F "tokens=*" %%i in ('php -r "echo 1;"') do SET PHPOUTPUT=%%i if not "%PHPOUTPUT%"=="1" ( echo [ERROR] Couldn't find PHP binary in PATH. +echo [INFO] Please use the Windows installer from the homepage ) else ( if exist php.cmd ( if exist bin\ansicon.exe (