From b1d97809c973218d05a6de1be9b68a7a4807f37c Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sat, 8 Dec 2012 03:11:06 +0100 Subject: [PATCH] Added console commands --- .gitignore | 1 + classes/CustomPacketHandler.class.php | 6 +- classes/Entity.class.php | 16 +- classes/PocketMinecraftServer.class.php | 4 +- classes/Session.class.php | 110 ++++++-------- input.php | 31 ++++ server.php | 194 +++++++++++++++++++++--- 7 files changed, 269 insertions(+), 93 deletions(-) create mode 100644 input.php diff --git a/.gitignore b/.gitignore index 59aae3fba..a2caf794e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ data/maps/* server.properties white-list.txt banned-ips.txt +console.in ################# ## Eclipse diff --git a/classes/CustomPacketHandler.class.php b/classes/CustomPacketHandler.class.php index e091937f9..37266ff94 100644 --- a/classes/CustomPacketHandler.class.php +++ b/classes/CustomPacketHandler.class.php @@ -194,8 +194,7 @@ class CustomPacketHandler{ $this->data["z"] = Utils::readFloat($this->get(4)); $this->data["yaw"] = Utils::readFloat($this->get(4)); $this->data["pitch"] = Utils::readFloat($this->get(4)); - $this->data["block"] = Utils::readShort($this->get(2), false); - $this->data["meta"] = Utils::readShort($this->get(2), false); + $this->data["metadata"] = $this->get(true); }else{ $this->raw .= Utils::writeLong($this->data["clientID"]); $this->raw .= Utils::writeShort(strlen($this->data["username"])).$this->data["username"]; @@ -205,8 +204,7 @@ class CustomPacketHandler{ $this->raw .= Utils::writeFloat($this->data["z"]); $this->raw .= Utils::writeFloat($this->data["yaw"]); $this->raw .= Utils::writeFloat($this->data["pitch"]); - $this->raw .= Utils::writeShort($this->data["block"]); - $this->raw .= Utils::writeShort($this->data["meta"]); + $this->raw .= "\x21\x2c\x01\x10"."\x7f"; } break; case MC_REMOVE_ENTITY: diff --git a/classes/Entity.class.php b/classes/Entity.class.php index 81d2b398d..20c8ff8a5 100644 --- a/classes/Entity.class.php +++ b/classes/Entity.class.php @@ -33,7 +33,7 @@ define("ENTITY_ITEM", 3); define("ENTITY_PAINTING", 4); class Entity{ - var $eid, $type, $name, $position, $dead, $metadata, $class, $attach; + var $eid, $type, $name, $position, $dead, $metadata, $class, $attach, $data, $closed; protected $health, $client; function __construct($eid, $class, $type = 0, $server){ //$type = 0 ---> player @@ -42,9 +42,12 @@ class Entity{ $this->type = (int) $type; $this->class = (int) $class; $this->attach = false; + $this->data = array(); $this->status = 0; $this->health = 20; $this->dead = false; + $this->closed = false; + $this->name = ""; $this->server->query("INSERT OR REPLACE INTO entities (EID, type, class, health) VALUES (".$this->eid.", ".$this->type.", ".$this->class.", ".$this->health.");"); $this->metadata = array(); /*include("misc/entities.php"); @@ -62,9 +65,16 @@ class Entity{ }*/ } + public function close(){ + if($this->closed === false){ + $this->server->query("DELETE FROM entities WHERE EID = ".$this->eid.";"); + $this->server->trigger("onEntityRemove", $this->eid); + $this->closed = true; + } + } + public function __destruct(){ - $this->server->query("DELETE FROM entities WHERE EID = ".$this->eid.";"); - $this->server->trigger("onEntityRemove", $this->eid); + $this->close(); } public function getEID(){ diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index a9938d866..d89c14199 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -76,7 +76,7 @@ class PocketMinecraftServer{ $this->event("onChat", "eventHandler", true); - $this->action(1000000, '$this->time += $this->timePerSecond;$this->trigger("onTimeChange", $this->time);'); + $this->action(100000, '$this->time += ceil($this->timePerSecond / 10);$this->trigger("onTimeChange", $this->time);'); $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));'); @@ -103,7 +103,7 @@ class PocketMinecraftServer{ public function reloadConfig(){ if($this->whitelist === true or is_array($this->whitelist)){ - $this->whitelist = explode("\n", str_replace(array(" ","\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt"))); + $this->whitelist = explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt"))); } $this->bannedIPs = explode("\n", str_replace(array(" ","\t","\r"), "", file_get_contents(FILE_PATH."banned-ips.txt"))); } diff --git a/classes/Session.class.php b/classes/Session.class.php index 60d5bb0c2..6ec7defd1 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -70,6 +70,10 @@ class Session{ } public function close($reason = "server stop", $msg = true){ + $this->save(); + if(is_object($this->entity)){ + $this->entity->close(); + } foreach($this->evid as $ev){ $this->server->deleteEvent($ev[0], $ev[1]); } @@ -77,10 +81,6 @@ class Session{ if($msg === true){ $this->server->trigger("onChat", $this->username." left the game"); } - $this->save(); - if(is_object($this->entity)){ - $this->entity->__destruct(); - } console("[INFO] Session with ".$this->ip.":".$this->port." closed due to ".$reason); unset($this->server->entities[$this->eid]); unset($this->server->clients[$this->CID]); @@ -102,8 +102,8 @@ class Session{ "x" => $entity->position["x"], "y" => $entity->position["y"], "z" => $entity->position["z"], - "yaw" => 0, - "pitch" => 0, + "yaw" => $entity->position["yaw"], + "pitch" => $entity->position["pitch"], ), )); ++$this->counter[0]; @@ -129,7 +129,7 @@ class Session{ if($data["eid"] === $this->eid){ break; } - /*$this->send(0x84, array( + $this->send(0x84, array( $this->counter[0], 0x00, array( @@ -142,42 +142,23 @@ class Session{ "z" => $data["z"], "yaw" => $data["yaw"], "pitch" => $data["pitch"], - "block" => $data["block"], - "meta" => $data["meta"], - ), - )); - ++$this->counter[0]; - */ - $this->send(0x84, array( - $this->counter[0], - 0x00, - array( - "id" => MC_ADD_ITEM_ENTITY, - "eid" => $data["eid"], - "x" => $data["x"], - "y" => $data["y"], - "z" => $data["z"], - "block" => 10, - "meta" => 0, - "stack" => 1, ), )); ++$this->counter[0]; break; case "onEntityRemove": if($data === $this->eid){ - $this->close("despawn"); - }else{ - $this->send(0x84, array( - $this->counter[0], - 0x00, - array( - "id" => MC_ENTITY_REMOVE, - "eid" => $data, - ), - )); - ++$this->counter[0]; + break; } + $this->send(0x84, array( + $this->counter[0], + 0x00, + array( + "id" => MC_ENTITY_REMOVE, + "eid" => $data, + ), + )); + ++$this->counter[0]; break; case "onTimeChange": $this->send(0x84, array( @@ -277,7 +258,7 @@ class Session{ $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("onPlayerAdd", $this->server->event("onPlayerAdd", array($this, "eventHandler"))); - $this->evid[] = array("onEntityDespawn", $this->server->event("onEntityDespawn", array($this, "eventHandler"))); + $this->evid[] = array("onEntityRemove", $this->server->event("onEntityRemove", array($this, "eventHandler"))); $this->evid[] = array("onEntityMove", $this->server->event("onEntityMove", array($this, "eventHandler"))); $this->evid[] = array("onHealthChange", $this->server->event("onHealthChange", array($this, "eventHandler"))); $this->send(0x84, array( @@ -313,6 +294,7 @@ class Session{ console("[DEBUG] Player with EID ".$this->eid." \"".$this->username."\" spawned!", true, true, 2); $this->entity = new Entity($this->eid, ENTITY_PLAYER, 0, $this->server); $this->entity->setName($this->username); + $this->entity->data["clientID"] = $this->clientID; $this->server->entities[$this->eid] = &$this->entity; $this->server->trigger("onPlayerAdd", array( "clientID" => $this->clientID, @@ -323,49 +305,45 @@ class Session{ "z" => $this->data["spawn"]["z"], "yaw" => $this->data["spawn"]["yaw"], "pitch" => $this->data["spawn"]["pitch"], - "block" => 0, - "meta" => 0, )); foreach($this->server->entities as $entity){ if($entity->eid !== $this->eid){ - $this->send(0x84, array( - $this->counter[0], - 0x00, - array( - "id" => MC_ADD_ITEM_ENTITY, + if($entity->class === 0){ + $this->server->trigger("onPlayerAdd", array( + "clientID" => $entity->data["clientID"], + "username" => $entity->name, "eid" => $entity->eid, "x" => $entity->position["x"], "y" => $entity->position["y"], "z" => $entity->position["z"], - "block" => 10, - "meta" => 0, - "stack" => 1, - ), - )); - ++$this->counter[0]; + "yaw" => $entity->position["yaw"], + "pitch" => $entity->position["pitch"], + )); + }else{ + $this->send(0x84, array( + $this->counter[0], + 0x00, + array( + "id" => MC_ADD_ITEM_ENTITY, + "eid" => $entity->eid, + "x" => $entity->position["x"], + "y" => $entity->position["y"], + "z" => $entity->position["z"], + "block" => 10, + "meta" => 0, + "stack" => 1, + ), + )); + ++$this->counter[0]; + } } } $this->eventHandler($this->server->motd, "onChat"); - $this->server->trigger("onChat", $this->username." joined the game"); + //$this->server->trigger("onChat", $this->username." joined the game"); break; case MC_MOVE_PLAYER: $this->entity->setPosition($data["x"], $data["y"], $data["z"], $data["yaw"], $data["pitch"]); $this->server->trigger("onEntityMove", $this->eid); - $this->send(0x84, array( - $this->counter[0], - 0x00, - array( - "id" => MC_ADD_ITEM_ENTITY, - "eid" => $this->server->eidCnt++, - "x" => $data["x"], - "y" => $data["y"], - "z" => $data["z"], - "block" => 7, - "meta" => 0, - "stack" => 1, - ), - )); - ++$this->counter[0]; break; case MC_PLAYER_EQUIPMENT: console("[DEBUG] EID ".$this->eid." has now ".$data["block"].":".$data["meta"]." in their hands!", true, true, 2); diff --git a/input.php b/input.php new file mode 100644 index 000000000..7c17ce734 --- /dev/null +++ b/input.php @@ -0,0 +1,31 @@ +setType($config["server-type"]); -$server->timePerSecond = $config["time-per-second"]; -$server->maxClients = $config["max-players"]; -$server->description = $config["description"]; -$server->motd = $config["motd"]; -$server->spawn = $config["spawn"]; -$server->whitelist = $config["white-list"]; + + $server->mapName = $config["level-name"]; $server->mapDir = FILE_PATH."data/maps/".$config["level-name"]."/"; -$server->reloadConfig(); +loadConfig(); -if($config["regenerate-config"] == true){ - $config["seed"] = $server->seed; - $config["server-id"] = $server->serverID; - $config["regenerate-config"] = "false"; - $config["white-list"] = $config["whitelist"] === true ? "true":"false"; - $config["spawn"] = implode(";", $config["spawn"]); - $prop = "#Pocket Minecraft PHP server properties\r\n#".date("D M j H:i:s T Y")."\r\n"; - foreach($config as $n => $v){ - $prop .= $n."=".$v."\r\n"; + +function loadConfig($regenerate = false){ + global $server, $config; + $server->setType($config["server-type"]); + $server->timePerSecond = $config["time-per-second"]; + $server->maxClients = $config["max-players"]; + $server->description = $config["description"]; + $server->motd = $config["motd"]; + $server->spawn = $config["spawn"]; + $server->whitelist = $config["white-list"]; + if($config["regenerate-config"] == true or $regenerate === true){ + $config["seed"] = $server->seed; + $config["server-id"] = $server->serverID; + $config["regenerate-config"] = "false"; + $config["white-list"] = $config["whitelist"] === true ? "true":"false"; + $config["spawn"] = implode(";", $config["spawn"]); + $prop = "#Pocket Minecraft PHP server properties\r\n#".date("D M j H:i:s T Y")."\r\n"; + foreach($config as $n => $v){ + $prop .= $n."=".$v."\r\n"; + } + file_put_contents(FILE_PATH."server.properties", $prop); } - file_put_contents(FILE_PATH."server.properties", $prop); + $server->reloadConfig(); } +$server->event("onTick", "serverCommands"); +$commands = fopen(FILE_PATH."console.in", "w+b"); +function serverCommands(){ + global $server, $commands, $config; + while(($line = fgets($commands)) !== false){ + $line = trim($line); + if($line === ""){ + continue; + } + $params = explode(" ", $line); + $cmd = strtolower(array_shift($params)); + console("[INFO] Issued server command: /$cmd ".implode(" ", $params)); + switch($cmd){ + case "stop": + console("[INFO] Stopping server..."); + $server->close(); + break; + case "banip": + $s = implode(" ", $params); + if(trim($s) == ""){ + console("[INFO] Usage: /banip "); + break; + } + file_put_contents(FILE_PATH."banned-ips.txt", "\r\n".$s, FILE_APPEND); + console("[INFO] IP \"$s\" added to IP ban list"); + loadConfig(); + break; + break; + case "gamemode": + $s = trim(array_shift($params)); + if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){ + console("[INFO] Usage: /gamemode <0 | 1>"); + break; + } + $config["gamemode"] = (int) $s; + $server->gamemode = $config["gamemode"]; + console("[INFO] Gamemode changed to ".$server->gamemode); + loadConfig(); + break; + case "say": + $s = implode(" ", $params); + if(trim($s) == ""){ + console("[INFO] Usage: /say "); + break; + } + $server->chat(false, $s); + break; + case "time": + $p = strtolower(array_shift($params)); + switch($p){ + case "check": + $time = abs($server->time) % 14400; + $hour = str_pad(strval((floor($time / 600) + 6) % 24), 2, "0", STR_PAD_LEFT).":".str_pad(strval(floor(($time % 600) / 10)), 2, "0", STR_PAD_LEFT); + if($time < 7200){ + $time = "daytime"; + }elseif($time < 8280){ + $time = "sunset"; + }elseif($time < 13320){ + $time = "night"; + }else{ + $time = "sunrise"; + } + console("[INFO] Time: $hour, $time (".$server->time.")"); + break; + case "add": + $t = (int) array_shift($params); + $server->time += $t; + break; + case "set": + $t = (int) array_shift($params); + $server->time = $t; + break; + case "sunrise": + $server->time = 13320; + break; + case "day": + $server->time = 0; + break; + case "sunset": + $server->time = 7200; + break; + case "night": + $server->time = 8280; + break; + default: + console("[INFO] Usage: /time [time]"); + break; + } + break; + case "whitelist": + $p = strtolower(array_shift($params)); + switch($p){ + case "add": + $user = trim(implode(" ", $params)); + file_put_contents(FILE_PATH."white-list.txt", "\r\n".$user, FILE_APPEND); + console("[INFO] Player \"$user\" added to white-list"); + loadConfig(); + break; + case "on": + case "true": + case "1": + console("[INFO] White-list turned on"); + $config["white-list"] = true; + loadConfig(true); + break; + case "off": + case "false": + case "0": + console("[INFO] White-list turned off"); + $config["white-list"] = false; + loadConfig(true); + break; + case "reload": + loadConfig(true); + break; + case "list": + console("[INFO] White-list: ".implode(", ", explode("\n", str_replace(array("\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt"))))); + break; + default: + console("[INFO] Usage: /whitelist [username]"); + break; + } + break; + case "list": + $list = ""; + foreach($server->clients as $c){ + $list .= ", ".$c->username; + } + console("[INFO] Online: ".substr($list, 2)); + break; + case "help": + console("[INFO] /help: Show available commands"); + console("[INFO] /gamemode: Changes default gamemode"); + console("[INFO] /say: Broadcasts mesages"); + console("[INFO] /time: Manages time"); + console("[INFO] /list: Lists online users"); + console("[INFO] /whitelist: Manages whitelisting"); + console("[INFO] /banip: Bans an IP"); + console("[INFO] /stop: Stops the server"); + break; + default: + console("[ERROR] Command doesn't exist!"); + break; + } + } + ftruncate($commands, 0); + fseek($commands, 0); +} + + $server->start(); \ No newline at end of file