Added console commands

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-08 03:11:06 +01:00
parent 589c151a4c
commit b1d97809c9
7 changed files with 269 additions and 93 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ data/maps/*
server.properties
white-list.txt
banned-ips.txt
console.in
#################
## Eclipse

View File

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

View File

@ -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(){

View File

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

View File

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

31
input.php Normal file
View File

@ -0,0 +1,31 @@
<?php
/*
-
/ \
/ \
/ POCKET \
/ MINECRAFT PHP \
|\ @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.
*/
$fp = fopen(dirname(__FILE__)."/console.in","wb");
while(true){
echo "/";fwrite($fp, fgets(STDIN));
}

View File

@ -28,6 +28,7 @@ the Free Software Foundation, either version 3 of the License, or
require_once("common/dependencies.php");
require_once("classes/PocketMinecraftServer.class.php");
file_put_contents("packets.log", "");
file_put_contents("console.in", "");
if(!file_exists(FILE_PATH."white-list.txt")){
@ -129,28 +130,185 @@ if(file_exists(FILE_PATH."data/maps/level.dat")){
console("[INFO] Map \"".$level["LevelName"]."\" importing done!");
unset($level, $entities, $nbt);
}
$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"];
$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 <IP>");
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 <message>");
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 <check | set | add | sunrise | day | sunset | night> [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 <on | off | add | reload | list> [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();