PluginAPI

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-23 17:37:53 +01:00
parent 99151981db
commit e0c245d86e
6 changed files with 115 additions and 65 deletions

View File

@ -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();
}
}
}
}

View File

@ -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;
}

View File

@ -166,7 +166,7 @@ class Session{
break;
case "onChat":
$this->dataPacket(MC_CHAT, array(
"message" => $data,
"message" => str_replace("@username", $this->username, $data),
));
break;
}

View File

@ -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

31
example/ExamplePlugin.php Normal file
View File

@ -0,0 +1,31 @@
<?php
/*
__PocketMine Plugin__
name=Example Plugin
version=0.0.1
author=shoghicp
class=ExamplePlugin
api=true
*/
class ExamplePlugin{
private $api;
public function __construct($api, $server = false){
$this->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;
}
}
}

View File

@ -1,45 +0,0 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @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.
*/
require_once("common/dependencies.php");
require_once("classes/PocketMinecraftClient.class.php");
file_put_contents("packets.log", "");
define("DEBUG", 2);
$client = new PocketMinecraftClient("shoghicp");
console("[INFO] Searching servers...");
$list = $client->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");
}