From e0c245d86e02c2176f9aac102054fbfa7489dcc1 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sun, 23 Dec 2012 17:37:53 +0100 Subject: [PATCH] PluginAPI --- classes/API/PluginAPI.php | 63 ++++++++++++++++++++++++++++++++++++++- classes/API/ServerAPI.php | 36 +++++++++++----------- classes/Session.class.php | 2 +- common/default.properties | 3 +- example/ExamplePlugin.php | 31 +++++++++++++++++++ example/client.php | 45 ---------------------------- 6 files changed, 115 insertions(+), 65 deletions(-) create mode 100644 example/ExamplePlugin.php delete mode 100644 example/client.php diff --git a/classes/API/PluginAPI.php b/classes/API/PluginAPI.php index cb62f2f37..fc627a781 100644 --- a/classes/API/PluginAPI.php +++ b/classes/API/PluginAPI.php @@ -25,6 +25,67 @@ the Free Software Foundation, either version 3 of the License, or */ -class PluginAPI{ +class PluginAPI extends stdClass{ + private $server, $plugins; + public function __construct($server){ + $this->server = $server; + $this->plugins = array(); + } + private function load($file){ + $content = file_get_contents($file); + $info = strstr($content, "*/", true); + $content = substr(strstr($content, "*/"),2); + if(preg_match_all('#([a-zA-Z0-9\-_]*)=([^\r\n]*)#u', $info, $matches) == 0){ //false or 0 matches + console("[ERROR] [PluginAPI] Failed parsing of ".basename($file)); + return false; + } + $info = array(); + foreach($matches[1] as $k => $i){ + $v = $matches[2][$k]; + switch(strtolower($v)){ + case "on": + case "true": + case "yes": + $v = true; + break; + case "off": + case "false": + case "no": + $v = false; + break; + } + $info[$i] = $v; + } + if(!isset($info["name"]) or !isset($info["version"]) or !isset($info["class"])){ + console("[ERROR] [PluginAPI] Failed parsing of ".basename($file)); + } + console("[INFO] [PluginAPI] Loading plugin \"".$info["name"]."\" ".$info["version"]); + if(eval($content) === false or !class_exists($info["class"])){ + console("[ERROR] [PluginAPI] Failed loading plugin"); + } + $className = trim($info["class"]); + if(isset($info["api"]) and $info["api"] !== true){ + console("[NOTICE] [PluginAPI] Plugin \"".$info["name"]."\" got raw access to Server methods"); + } + $object = new $className($this->server->api, ((isset($info["api"]) and $info["api"] !== true) ? $this->server:false)); + $this->plugins[$className] = array($object, $info); + } + + public function init(){ + console("[INFO] Loading Plugins..."); + $dir = dir(FILE_PATH."data/plugins/"); + while(false !== ($file = $dir->read())){ + if($file !== "." and $file !== ".."){ + if(strtolower(substr($file, -3)) === "php"){ + $this->load(FILE_PATH."data/plugins/" . $file); + } + } + } + foreach($this->plugins as $p){ + if(method_exists($p[0], "init")){ + $p[0]->init(); + } + } + } } \ No newline at end of file diff --git a/classes/API/ServerAPI.php b/classes/API/ServerAPI.php index 5c4127688..ae3eb4db3 100644 --- a/classes/API/ServerAPI.php +++ b/classes/API/ServerAPI.php @@ -26,8 +26,8 @@ the Free Software Foundation, either version 3 of the License, or */ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in runtime! - var $server, $restart = false; - private $config, $apiList = array(); + var $restart = false; + private $server, $config, $apiList = array(); function __construct(){ console("[INFO] Starting ServerAPI server handler..."); file_put_contents(FILE_PATH."packets.log", ""); @@ -291,9 +291,21 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run $d = explode("=", $line); $n = strtolower(array_shift($d)); $v = implode("=", $d); + switch(strtolower(trim($v))){ + case "on": + case "true": + case "yes": + $v = true; + break; + case "off": + case "false": + case "no": + $v = false; + break; + } switch($n){ case "last-update": - if(trim($v) == "false"){ + if($v === false){ $v = time(); }else{ $v = (int) $v; @@ -308,10 +320,7 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run $v = (int) $v; break; case "seed": - $v = trim($v); - if($v == "false"){ - $v = false; - }elseif(preg_match("/[^0-9\-]/", $v) > 0){ + if($v !== false and preg_match("/[^0-9\-]/", $v) > 0){ $str = new Java_String($v); $v = $str->hashCode(); }else{ @@ -319,21 +328,14 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run } break; 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; + if($v !== false){ + $v = preg_match("/[^0-9\-]/", $v) > 0 ? Utils::readInt(substr(md5($v, true), 0, 4)):$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 "invisible": - $v = trim($v) == "true" ? true:false; - break; } $this->config[$n] = $v; } diff --git a/classes/Session.class.php b/classes/Session.class.php index bf3b27f2a..74fdae3a9 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -166,7 +166,7 @@ class Session{ break; case "onChat": $this->dataPacket(MC_CHAT, array( - "message" => $data, + "message" => str_replace("@username", $this->username, $data), )); break; } diff --git a/common/default.properties b/common/default.properties index b59fee473..d80e2017a 100644 --- a/common/default.properties +++ b/common/default.properties @@ -1,4 +1,4 @@ -#Pocket Minecraft PHP server properties +#PocketMine-MP default server properties server-name=A Minecraft Server description=Server made using PocketMine-MP motd=Welcome @username to this server! @@ -13,6 +13,7 @@ server-type=normal time-per-second=20 gamemode=1 difficulty=1 +generator-settings= seed=false level-name=false server-id=false diff --git a/example/ExamplePlugin.php b/example/ExamplePlugin.php new file mode 100644 index 000000000..1663c9aa6 --- /dev/null +++ b/example/ExamplePlugin.php @@ -0,0 +1,31 @@ +api = $api; + } + + public function init(){ + $this->api->console->register("example", "Example command", array($this, "handleCommand")); + } + + public function handleCommand($cmd, $arg){ + switch($cmd){ + case "example": + console("EXAMPLE!!!"); + break; + } + } + +} \ No newline at end of file diff --git a/example/client.php b/example/client.php deleted file mode 100644 index bf97ba587..000000000 --- a/example/client.php +++ /dev/null @@ -1,45 +0,0 @@ -getServerList(); -foreach($list as $i => $info){ - console("[Server] #".$i." ".$info["ip"]." ".$info["username"]); -} -console("[Select Server] #", false, false); -$i = (int) trim(fgets(STDIN)); -if(isset($list[$i])){ - $client->start($list[$i]["ip"]); -}else{ - console("[Error] Unknown ID"); -} \ No newline at end of file