Added player commands, better console API and implemented future player chat

This commit is contained in:
Shoghi Cervantes Pueyo 2013-02-07 09:23:37 +01:00
parent 72388fa3cb
commit c2e55941d8
6 changed files with 186 additions and 131 deletions

View File

@ -45,21 +45,26 @@ class BanAPI{
$this->server->api->console->register("whitelist", "Manages White-listing", array($this, "commandHandler")); $this->server->api->console->register("whitelist", "Manages White-listing", array($this, "commandHandler"));
} }
public function commandHandler($cmd, $params){ public function commandHandler($cmd, $params, $issuer){
$output = "";
switch($cmd){ switch($cmd){
case "kick": case "kick":
if(!isset($params[0])){ if(!isset($params[0])){
console("[INFO] Usage: /kick <playername> [reason]"); $output .= "Usage: /kick <playername> [reason]\n";
}else{ }else{
$name = array_shift($params); $name = array_shift($params);
$player = $this->server->api->player->get($name); $player = $this->server->api->player->get($name);
if($player === false){ if($player === false){
console("[ERROR] Player \"".$name."\" does not exist"); $output .= "Player \"".$name."\" does not exist\n";
}else{ }else{
$reason = implode(" ", $params); $reason = implode(" ", $params);
$reason = $reason == "" ? "No reason":$reason; $reason = $reason == "" ? "No reason":$reason;
$player->close("You have been kicked: ".$reason); $player->close("You have been kicked: ".$reason);
console("[INFO] Player \"".$player->username."\" has been kicked: $reason"); if($issuer instanceof Player){
$this->server->api->chat->broadcast($player->username." has been kicked by ".$issuer->username.": $reason\n");
}else{
$this->server->api->chat->broadcast($player->username." has been kicked: $reason\n");
}
} }
} }
break; break;
@ -70,34 +75,34 @@ class BanAPI{
$user = trim(implode(" ", $params)); $user = trim(implode(" ", $params));
$this->whitelist->remove($user); $this->whitelist->remove($user);
$this->whitelist->save(); $this->whitelist->save();
console("[INFO] Player \"$user\" removed from white-list"); $output .= "Player \"$user\" removed from white-list\n";
break; break;
case "add": case "add":
$user = trim(implode(" ", $params)); $user = trim(implode(" ", $params));
$this->whitelist->set($user); $this->whitelist->set($user);
$this->whitelist->save(); $this->whitelist->save();
console("[INFO] Player \"$user\" added to white-list"); $output .= "Player \"$user\" added to white-list\n";
break; break;
case "reload": case "reload":
$this->whitelist = $this->load(FILE_PATH."white-list.txt", CONFIG_LIST); $this->whitelist = $this->load(FILE_PATH."white-list.txt", CONFIG_LIST);
break; break;
case "list": case "list":
console("[INFO] White-list: ".implode(", ", $this->whitelist->getAll(true))); $output .= "White-list: ".implode(", ", $this->whitelist->getAll(true))."\n";
break; break;
case "on": case "on":
case "true": case "true":
case "1": case "1":
console("[INFO] White-list turned on"); $output .= "White-list turned on\n";
$this->server->api->setProperty("white-list", true); $this->server->api->setProperty("white-list", true);
break; break;
case "off": case "off":
case "false": case "false":
case "0": case "0":
console("[INFO] White-list turned off"); $output .= "White-list turned off\n";
$this->server->api->setProperty("white-list", false); $this->server->api->setProperty("white-list", false);
break; break;
default: default:
console("[INFO] Usage: /whitelist <on | off | add | remove | reload | list> [username]"); $output .= "Usage: /whitelist <on | off | add | remove | reload | list> [username]\n";
break; break;
} }
break; break;
@ -109,23 +114,23 @@ class BanAPI{
$ip = trim(implode($params)); $ip = trim(implode($params));
$this->bannedIPs->remove($ip); $this->bannedIPs->remove($ip);
$this->bannedIPs->save(); $this->bannedIPs->save();
console("[INFO] IP \"$ip\" removed from ban list"); $output .= "IP \"$ip\" removed from ban list\n";
break; break;
case "add": case "add":
case "ban": case "ban":
$ip = trim(implode($params)); $ip = trim(implode($params));
$this->bannedIPs->set($ip); $this->bannedIPs->set($ip);
$this->bannedIPs->save(); $this->bannedIPs->save();
console("[INFO] IP \"$ip\" added to ban list"); $output .= "IP \"$ip\" added to ban list\n";
break; break;
case "reload": case "reload":
$this->bannedIPs = new Config(FILE_PATH."banned-ips.txt", CONFIG_LIST); $this->bannedIPs = new Config(FILE_PATH."banned-ips.txt", CONFIG_LIST);
break; break;
case "list": case "list":
console("[INFO] IP ban list: ".implode(", ", $this->bannedIPs->getAll(true))); $output .= "IP ban list: ".implode(", ", $this->bannedIPs->getAll(true))."\n";
break; break;
default: default:
console("[INFO] Usage: /banip <add | remove | list | reload> [IP]"); $output .= "Usage: /banip <add | remove | list | reload> [IP]\n";
break; break;
} }
break; break;
@ -137,7 +142,7 @@ class BanAPI{
$user = trim(implode($params)); $user = trim(implode($params));
$this->banned->remove($user); $this->banned->remove($user);
$this->banned->save(); $this->banned->save();
console("[INFO] Player \"$user\" removed from ban list"); $output .= "Player \"$user\" removed from ban list\n";
break; break;
case "add": case "add":
case "ban": case "ban":
@ -149,20 +154,26 @@ class BanAPI{
$player->close("You have been banned"); $player->close("You have been banned");
} }
$this->server->api->chat->broadcast("$user has been banned"); $this->server->api->chat->broadcast("$user has been banned");
console("[INFO] Player \"$user\" added to ban list"); if($issuer instanceof Player){
$this->server->api->chat->broadcast($user." has been banned by ".$issuer->username."\n");
}else{
$this->server->api->chat->broadcast($user." has been banned\n");
}
$output .= "Player \"$user\" added to ban list\n";
break; break;
case "reload": case "reload":
$this->banned = new Config(FILE_PATH."banned.txt", CONFIG_LIST); $this->banned = new Config(FILE_PATH."banned.txt", CONFIG_LIST);
break; break;
case "list": case "list":
console("[INFO] Ban list: ".implode(", ", $this->banned->getAll(true))); $output .= "Ban list: ".implode(", ", $this->banned->getAll(true))."\n";
break; break;
default: default:
console("[INFO] Usage: /ban <add | remove | list | reload> [player]"); $output .= "Usage: /ban <add | remove | list | reload> [player]\n";
break; break;
} }
break; break;
} }
return $output;
} }
public function ban($username){ public function ban($username){

View File

@ -39,15 +39,19 @@ class ChatAPI{
$this->send(false, $message); $this->send(false, $message);
} }
public function sendTo($owner, $text, $username){ public function sendTo($owner, $text, $player){
$this->send($owner, $text, array($username)); $this->send($owner, $text, array($player));
} }
public function send($owner, $text, $whitelist = false, $blacklist = false){ public function send($owner, $text, $whitelist = false, $blacklist = false){
$message = ""; $message = "";
if($owner !== false){ if($owner !== false){
if($owner instanceof Player){
$message = "<".$owner->username."> ";
}else{
$message = "<".$owner."> "; $message = "<".$owner."> ";
} }
}
$message .= $text; $message .= $text;
console("[CHAT] ".$message); console("[CHAT] ".$message);
$this->server->handle("server.chat", new Container($message, $whitelist, $blacklist)); $this->server->handle("server.chat", new Container($message, $whitelist, $blacklist));

View File

@ -47,7 +47,8 @@ class ConsoleAPI{
$this->loop->join(); $this->loop->join();
} }
public function defaultCommands($cmd, $params){ public function defaultCommands($cmd, $params, $issuer){
$output = "";
switch($cmd){ switch($cmd){
case "crash": //Crashes the server to generate an report case "crash": //Crashes the server to generate an report
$this->callNotDefinedMethodCrash(); $this->callNotDefinedMethodCrash();
@ -60,17 +61,17 @@ class ConsoleAPI{
case "on": case "on":
case "true": case "true":
case "1": case "1":
console("[INFO] Server is invisible"); $output .= "Server is invisible\n";
$this->server->api->setProperty("invisible", true); $this->server->api->setProperty("invisible", true);
break; break;
case "off": case "off":
case "false": case "false":
case "0": case "0":
console("[INFO] Server is visible"); $output .= "Server is visible\n";
$this->server->api->setProperty("invisible", false); $this->server->api->setProperty("invisible", false);
break; break;
default: default:
console("[INFO] Usage: /invisible <on | off>"); $output .= "Usage: /invisible <on | off>\n";
break; break;
} }
break; break;
@ -78,39 +79,39 @@ class ConsoleAPI{
case "lag": case "lag":
$this->server->debugInfo(true); $this->server->debugInfo(true);
$info = $this->server->debugInfo(); $info = $this->server->debugInfo();
console("[INFO] TPS: ".$info["tps"].", Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"].")"); $output .= "TPS: ".$info["tps"].", Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"].")\n";
break; break;
case "update-done": case "update-done":
$this->server->api->setProperty("last-update", time()); $this->server->api->setProperty("last-update", time());
break; break;
case "stop": case "stop":
$this->loop->stop = true; $this->loop->stop = true;
console("[INFO] Stopping the server..."); $output .= "Stopping the server\n";
$this->server->close(); $this->server->close();
break; break;
case "gamemode": case "gamemode":
$s = trim(array_shift($params)); $s = trim(array_shift($params));
if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1 and ((int) $s) !== 2)){ if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1 and ((int) $s) !== 2)){
console("[INFO] Usage: /gamemode <0 | 1 | 2>"); $output .= "Usage: /gamemode <0 | 1 | 2>\n";
break; break;
} }
$this->server->api->setProperty("gamemode", (int) $s); $this->server->api->setProperty("gamemode", (int) $s);
console("[INFO] Gamemode changed to ".$this->server->getGamemode()); $output .= "Gamemode changed to ".$this->server->getGamemode()."\n";
break; break;
case "difficulty": case "difficulty":
$s = trim(array_shift($params)); $s = trim(array_shift($params));
if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){ if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){
console("[INFO] Usage: /difficulty <0 | 1>"); $output .= "Usage: /difficulty <0 | 1>\n";
break; break;
} }
$this->server->api->setProperty("difficulty", (int) $s); $this->server->api->setProperty("difficulty", (int) $s);
console("[INFO] Difficulty changed to ".$this->server->difficulty); $output .= "Difficulty changed to ".$this->server->difficulty."\n";
loadConfig(true); loadConfig(true);
break; break;
case "say": case "say":
$s = implode(" ", $params); $s = implode(" ", $params);
if(trim($s) == ""){ if(trim($s) == ""){
console("[INFO] Usage: /say <message>"); $output .= "Usage: /say <message>\n";
break; break;
} }
$this->server->api->chat->broadcast($s); $this->server->api->chat->broadcast($s);
@ -120,23 +121,23 @@ class ConsoleAPI{
break; break;
case "help": case "help":
case "?": case "?":
console("[INFO] /help: Show available commands"); $output .= "/help: Show available commands\n";
console("[INFO] /status: Show server TPS and memory usage"); $output .= "/status: Show server TPS and memory usage\n";
console("[INFO] /gamemode: Changes default gamemode"); $output .= "/gamemode: Changes default gamemode\n";
console("[INFO] /difficulty: Changes difficulty"); $output .= "/difficulty: Changes difficulty\n";
console("[INFO] /invisible: Manages server visibility"); $output .= "/invisible: Manages server visibility\n";
console("[INFO] /say: Broadcasts mesages"); $output .= "/say: Broadcasts mesagesv";
console("[INFO] /save-all: Saves pending changes"); $output .= "/save-all: Saves pending changes\n";
console("[INFO] /stop: Stops the server"); $output .= "/stop: Stops the server\n";
//console("[INFO] /restart: Restarts the server");
foreach($this->help as $c => $h){ foreach($this->help as $c => $h){
console("[INFO] /$c: ".$h); $output .= "/$c: ".$h."\n";
} }
break; break;
default: default:
console("[ERROR] Command doesn't exist! Use /help"); $output .= "Command doesn't exist! Use /help\n";
break; break;
} }
return $output;
} }
public function alias($alias, $cmd){ public function alias($alias, $cmd){
@ -156,15 +157,33 @@ class ConsoleAPI{
$this->help[$cmd] = $help; $this->help[$cmd] = $help;
} }
public function run($line = ""){ public function run($line = "", $issuer = false){
if($line != ""){ if($line != ""){
$params = explode(" ", $line); $params = explode(" ", $line);
$cmd = strtolower(array_shift($params)); $cmd = strtolower(array_shift($params));
if($issuer instanceof Player){
console("[INFO] \"".$issuer->username."\" issued server command: /$cmd ".implode(" ", $params));
}else{
console("[INFO] Issued server command: /$cmd ".implode(" ", $params)); console("[INFO] Issued server command: /$cmd ".implode(" ", $params));
}
if($this->server->api->dhandle("console.command.".$cmd, array("cmd" => $cmd, "parameters" => $params, "issuer" => $issuer)) === false
or $this->server->api->dhandle("console.command", array("cmd" => $cmd, "parameters" => $params, "issuer" => $issuer)) === false){
$output = "You don't have permissions\n";
}else{
if(isset($this->cmds[$cmd]) and is_callable($this->cmds[$cmd])){ if(isset($this->cmds[$cmd]) and is_callable($this->cmds[$cmd])){
call_user_func($this->cmds[$cmd], $cmd, $params); $output = @call_user_func($this->cmds[$cmd], $cmd, $params, $issuer);
}elseif($this->server->api->dhandle("api.console.command", array("cmd" => $cmd, "params" => $params)) !== false){ }elseif($this->server->api->dhandle("console.command.unknown", array("cmd" => $cmd, "params" => $params, "issuer" => $issuer)) !== false){
$this->defaultCommands($cmd, $params); $output = $this->defaultCommands($cmd, $params, $issuer);
}
}
if($output != "" and ($issuer instanceof Player)){
$issuer->sendChat($output);
}elseif($output != ""){
$mes = explode("\n", $output);
foreach($mes as $m){
console($m);
}
} }
} }
} }

View File

@ -107,19 +107,20 @@ class PlayerAPI{
} }
} }
public function commandHandler($cmd, $params){ public function commandHandler($cmd, $params, $issuer){
$output = "";
switch($cmd){ switch($cmd){
case "tp": case "tp":
$name = array_shift($params); $name = array_shift($params);
$target = array_shift($params); $target = array_shift($params);
if($name == null or $target == null){ if($name == null or $target == null){
console("[INFO] Usage: /tp <player> <target>"); $output .= "Usage: /tp <player> <target>\n";
break; break;
} }
if($this->teleport($name, $target)){ if($this->teleport($name, $target)){
console("[INFO] \"$name\" teleported to \"$target\""); $output .= "\"$name\" teleported to \"$target\"\n";
}else{ }else{
console("[ERROR] Couldn't teleport"); $output .= "Couldn't teleport\n";
} }
break; break;
case "tppos": case "tppos":
@ -128,13 +129,13 @@ class PlayerAPI{
$x = array_pop($params); $x = array_pop($params);
$name = implode(" ", $params); $name = implode(" ", $params);
if($name == null or $x === null or $y === null or $z === null){ if($name == null or $x === null or $y === null or $z === null){
console("[INFO] Usage: /tp <player> <x> <y> <z>"); $output .= "Usage: /tp <player> <x> <y> <z>\n";
break; break;
} }
if($this->tppos($name, $x, $y, $z)){ if($this->tppos($name, $x, $y, $z)){
console("[INFO] \"$name\" teleported to ($x, $y, $z)"); $output .= "\"$name\" teleported to ($x, $y, $z)\n";
}else{ }else{
console("[ERROR] Couldn't teleport"); $output .= "Couldn't teleport\n";
} }
break; break;
case "kill": case "kill":
@ -142,7 +143,7 @@ class PlayerAPI{
if($player !== false){ if($player !== false){
$this->server->api->entity->harm($player->eid, 20, "console", true); $this->server->api->entity->harm($player->eid, 20, "console", true);
}else{ }else{
console("[INFO] Usage: /kill <player>"); $output .= "Usage: /kill <player>\n";
} }
break; break;
case "harm": case "harm":
@ -151,16 +152,17 @@ class PlayerAPI{
if($player !== false){ if($player !== false){
$this->server->api->entity->harm($player->eid, $dmg, "console", true); $this->server->api->entity->harm($player->eid, $dmg, "console", true);
}else{ }else{
console("[INFO] Usage: /harm <damage> <player>"); $output .= "Usage: /harm <damage> <player>\n";
} }
break; break;
case "list": case "list":
console("[INFO] Player list:"); $output .= "Player list:\n";
foreach($this->server->clients as $c){ foreach($this->server->clients as $c){
console("[INFO] ".$c->username." (".$c->ip.":".$c->port."), ClientID ".$c->clientID.", (".round($c->entity->x, 2).", ".round($c->entity->y, 2).", ".round($c->entity->z, 2).")"); $output .= $c->username." (".$c->ip.":".$c->port."), ClientID ".$c->clientID.", (".round($c->entity->x, 2).", ".round($c->entity->y, 2).", ".round($c->entity->z, 2).")\n";
} }
break; break;
} }
return $output;
} }
public function teleport($name, $target){ public function teleport($name, $target){

View File

@ -41,13 +41,14 @@ class TimeAPI{
$this->server->api->console->register("time", "Manages server time", array($this, "commandHandler")); $this->server->api->console->register("time", "Manages server time", array($this, "commandHandler"));
} }
public function commandHandler($cmd, $params){ public function commandHandler($cmd, $params, $issuer){
$output = "";
switch($cmd){ switch($cmd){
case "time": case "time":
$p = strtolower(array_shift($params)); $p = strtolower(array_shift($params));
switch($p){ switch($p){
case "check": case "check":
console("[INFO] Time: ".$this->getDate().", ".$this->getPhase()." (".$this->get(true).")"); $output .= "Time: ".$this->getDate().", ".$this->getPhase()." (".$this->get(true).")\n";
break; break;
case "add": case "add":
$this->add(array_shift($params)); $this->add(array_shift($params));
@ -68,11 +69,12 @@ class TimeAPI{
$this->night(); $this->night();
break; break;
default: default:
console("[INFO] Usage: /time <check | set | add | sunrise | day | sunset | night> [time]"); $output .= "Usage: /time <check | set | add | sunrise | day | sunset | night> [time]\n";
break; break;
} }
break; break;
} }
return $output;
} }
public function night(){ public function night(){

View File

@ -374,13 +374,20 @@ class Player{
}else{ }else{
$message = (string) $data; $message = (string) $data;
} }
$this->dataPacket(MC_CHAT, array( $this->sendChat($message);
"message" => str_replace("@username", $this->username, $message),
));
break; break;
} }
} }
public function sendChat($message){
$mes = explode("\n", $message);
foreach($mes as $m){
$this->dataPacket(MC_CHAT, array(
"message" => str_replace("@username", $this->username, $m),
));
}
}
public function handle($pid, $data){ public function handle($pid, $data){
if($this->connected === true){ if($this->connected === true){
$this->timeout = microtime(true) + 20; $this->timeout = microtime(true) + 20;
@ -724,6 +731,16 @@ class Player{
} }
} }
break; break;
case MC_CHAT:
$message = $data["message"];
if($message{0} === "/"){ //Command
$this->server->api->console->run(substr($message, 1), $this);
}else{
if($this->server->api->dhandle("player.chat", array("player" => $this, "message" => $message)) !== false){
$this->server->api->send($this, $message);
}
}
break;
default: default:
console("[DEBUG] Unhandled 0x".dechex($data["id"])." Data Packet for Client ID ".$this->clientID.": ".print_r($data, true), true, true, 2); console("[DEBUG] Unhandled 0x".dechex($data["id"])." Data Packet for Client ID ".$this->clientID.": ".print_r($data, true), true, true, 2);
break; break;