diff --git a/classes/API/PlayerAPI.php b/classes/API/PlayerAPI.php new file mode 100644 index 000000000..3ba282985 --- /dev/null +++ b/classes/API/PlayerAPI.php @@ -0,0 +1,132 @@ +server = $server; + } + + public function init(){ + $this->server->api->console->register("list", "Shows connected player list", array($this, "commandHandler")); + $this->server->api->console->register("kill", "Kills a player", array($this, "commandHandler")); + } + + public function commandHandler($cmd, $params){ + switch($cmd){ + case "kill": + $player = $this->get(implode(" ", $params)); + if($player !== false){ + $this->server->trigger("onHealthChange", array("eid" => $player->eid, "health" => -1, "cause" => "console")); + }else{ + console("[INFO] Usage: /kill "); + } + 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; + } + } + + public function get($name){ + $CID = $this->server->query("SELECT ip,port FROM players WHERE name = '".str_replace("'", "", $name)."';", true); + $CID = $this->server->clientID($CID["ip"], $CID["port"]); + if(isset($this->server->clients[$CID])){ + return $this->server->clients[$CID]; + } + return false; + } + + public function getByEID($eid){ + $eid = (int) $eid; + $CID = $this->server->query("SELECT ip,port FROM players WHERE EID = '".$eid."';", true); + $CID = $this->server->clientID($CID["ip"], $CID["port"]); + if(isset($this->server->clients[$CID])){ + return $this->server->clients[$CID]; + } + return false; + } + + public function online(){ + $o = array(); + foreach($this->server->clients as $p){ + if($p->auth === true){ + $o[] = $p->username; + } + } + return $o; + } + + public function add($CID){ + if(isset($this->server->clients[$CID])){ + $player = $this->server->clients[$CID]; + console("[INFO] Player \"".$player->username."\" connected from ".$player->ip.":".$player->port); + $player->data = $this->getOffline($player->username); + $this->server->query("INSERT OR REPLACE INTO players (clientID, EID, ip, port, name) VALUES (".$player->clientID.", ".$player->eid.", '".$player->ip."', ".$player->port.", '".$player->username."');"); + } + } + + public function remove($CID){ + if(isset($this->server->clients[$CID])){ + $player = $this->server->clients[$CID]; + if(is_object($player->entity)){ + $player->entity->close(); + } + $this->saveOffline($player->username, $player->data); + $this->server->query("DELETE FROM players WHERE name = '".$player->username."';"); + unset($this->server->entities[$player->eid]); + unset($this->server->clients[$player->CID]); + } + } + + public function getOffline($name){ + if(!file_exists(FILE_PATH."data/players/".$name.".dat")){ + console("[NOTICE] Player data not found for \"".$name."\", creating new"); + $data = array( + "spawn" => array( + "x" => $this->server->spawn["x"], + "y" => $this->server->spawn["y"], + "z" => $this->server->spawn["z"], + ), + "health" => 20, + "lastIP" => "", + "lastID" => "", + ); + $this->saveOffline($name, $data); + }else{ + $data = unserialize(file_get_contents(FILE_PATH."data/players/".$name.".dat")); + } + return $data; + } + + public function saveOffline($name, $data){ + file_put_contents(FILE_PATH."data/players/".str_replace("/", "", $name).".dat", serialize($data)); + } +} \ No newline at end of file diff --git a/classes/API/ServerAPI.php b/classes/API/ServerAPI.php index fbc0daf55..d2a696949 100644 --- a/classes/API/ServerAPI.php +++ b/classes/API/ServerAPI.php @@ -27,7 +27,7 @@ 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; - private $config; + private $config, $apiList = array(); function __construct(){ console("[INFO] Starting ServerAPI server handler..."); file_put_contents("packets.log", ""); @@ -92,6 +92,15 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run } } } + foreach($this->apiList as $a){ + if(method_exists($this->$a, "init")){ + $this->$a->init(); + } + } + } + + public function getList(){ + return $this->apiList; } public function loadAPI($name, $class, $dir = false){ @@ -105,9 +114,8 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run } require_once($file); $this->$name = new $class($this->server); - + $this->apiList[] = $name; console("[INFO] API ".$name." [".$class."] loaded"); - } public function importMap($dir, $remove = false){ @@ -142,7 +150,6 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run $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!"); diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index 1ca84f3df..56a2b0603 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -85,17 +85,17 @@ class PocketMinecraftServer extends stdClass{ $this->action(5000000, '$this->trigger("onHealthRegeneration", 1);'); $this->action(1000000 * 60, '$this->reloadConfig();'); $this->action(1000000 * 60 * 10, '$this->custom = array();'); - $this->action(1000000 * 80, '$list = ""; foreach($this->clients as $c){$list .= ", ".$c->username;}$this->chat(false, count($this->clients)."/".$this->maxClients." online: ".substr($list, 2));'); + $this->action(1000000 * 80, '$this->chat(false, count($this->clients)."/".$this->maxClients." online: ".implode(", ",$this->api->player->online()));'); $this->action(1000000 * 75, '$this->debugInfo(true);'); } public function startDatabase(){ $this->database = new SQLite3(":memory:"); - $this->query("CREATE TABLE clients (clientID INTEGER PRIMARY KEY, EID NUMERIC, ip TEXT, port NUMERIC, name TEXT);"); + $this->query("CREATE TABLE players (clientID INTEGER PRIMARY KEY, EID NUMERIC, ip TEXT, port NUMERIC, name TEXT);"); $this->query("CREATE TABLE entities (EID INTEGER PRIMARY KEY, type NUMERIC, class NUMERIC, name TEXT, x NUMERIC, y NUMERIC, z NUMERIC, yaw NUMERIC, pitch NUMERIC, health NUMERIC);"); $this->query("CREATE TABLE metadata (EID INTEGER PRIMARY KEY, name TEXT, value TEXT);"); $this->query("CREATE TABLE actions (ID INTEGER PRIMARY KEY, interval NUMERIC, last NUMERIC, code TEXT, repeat NUMERIC);"); - $this->query("CREATE TABLE events (ID INTEGER PRIMARY KEY, eventName TEXT, disabled INTEGER);"); + $this->query("CREATE TABLE events (ID INTEGER PRIMARY KEY, eventName TEXT, priority NUMERIC, disabled INTEGER);"); } public function query($sql, $fetch = false){ @@ -366,6 +366,7 @@ class PocketMinecraftServer extends stdClass{ $this->responses[$evn["ID"]] = call_user_func($ev[0], $data, $event, $this); } } + return true; } public function response($eid){ diff --git a/classes/Session.class.php b/classes/Session.class.php index 5776b6b52..3747f64d7 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -28,7 +28,7 @@ the Free Software Foundation, either version 3 of the License, or class Session{ private $server, $serverID, $timeout, $connected, $evid; - var $clientID, $ip, $port, $counter, $username, $eid, $data, $entity; + var $clientID, $ip, $port, $counter, $username, $eid, $data, $entity, $auth; function __construct($server, $clientID, $eid, $ip, $port){ $this->server = $server; $this->clientID = $clientID; @@ -43,8 +43,9 @@ class Session{ $this->evid = array(); $this->evid[] = array("onTick", $this->server->event("onTick", array($this, "checkTimeout"))); $this->evid[] = array("onClose", $this->server->event("onClose", array($this, "close"))); - console("[DEBUG] New Session started with ".$ip.":".$port.". Client GUID ".$this->clientID, true, true, 2); + console("[DEBUG] New Session started with ".$ip.":".$port.". Client ID ".$this->clientID, true, true, 2); $this->connected = true; + $this->auth = false; $this->counter = array(0, 0); } @@ -66,15 +67,11 @@ class Session{ "z" => $this->entity->position["z"], ); } - file_put_contents(FILE_PATH."data/players/".str_replace("/", "", $this->username).".dat", serialize($this->data)); } public function close($reason = "", $msg = true){ $reason = $reason == "" ? "server stop":$reason; $this->save(); - if(is_object($this->entity)){ - $this->entity->close(); - } foreach($this->evid as $ev){ $this->server->deleteEvent($ev[0], $ev[1]); } @@ -83,9 +80,8 @@ class Session{ if($msg === true){ $this->server->trigger("onChat", $this->username." left the game"); } - console("[INFO] Session with ".$this->ip.":".$this->port." closed due to ".$reason); - unset($this->server->entities[$this->eid]); - unset($this->server->clients[$this->CID]); + console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason); + $this->server->api->player->remove($this->CID); } public function eventHandler($data, $event){ @@ -262,24 +258,10 @@ class Session{ $this->close("\"".$this->username."\" not being on white-list", false); break; } - console("[INFO] Player \"".$this->username."\" connected from ".$this->ip.":".$this->port); - if(!file_exists(FILE_PATH."data/players/".$this->username.".dat")){ - console("[NOTICE] Player data not found for \"".$this->username."\", creating new"); - $this->data = array( - "spawn" => array( - "x" => $this->server->spawn["x"], - "y" => $this->server->spawn["y"], - "z" => $this->server->spawn["z"], - ), - "health" => 20, - "lastIP" => $this->ip, - "lastID" => $this->clientID, - ); - }else{ - $this->data = unserialize(file_get_contents(FILE_PATH."data/players/".str_replace("/", "", $this->username).".dat")); - $this->data["lastIP"] = $this->ip; - $this->data["lastID"] = $this->clientID; - } + $this->server->api->player->add($this->CID); + $this->auth = true; + $this->data["lastIP"] = $this->ip; + $this->data["lastID"] = $this->clientID; $this->evid[] = array("onTimeChange", $this->server->event("onTimeChange", array($this, "eventHandler"))); $this->evid[] = array("onChat", $this->server->event("onChat", array($this, "eventHandler"))); $this->evid[] = array("onDeath", $this->server->event("onDeath", array($this, "eventHandler")));