Paginated help command

This commit is contained in:
Shoghi Cervantes Pueyo 2013-03-20 18:12:06 +01:00
parent 4a9374a58c
commit 5e44ecf63b
2 changed files with 26 additions and 10 deletions

View File

@ -90,7 +90,7 @@ class BanAPI{
$output = ""; $output = "";
switch($cmd){ switch($cmd){
case "sudo": case "sudo":
$target = strtolower($params[0]); $target = strtolower(array_shift($params));
$player = $this->server->api->player->get($target); $player = $this->server->api->player->get($target);
if(!($player instanceof Player)){ if(!($player instanceof Player)){
$output .= "Player not connected.\n"; $output .= "Player not connected.\n";

View File

@ -39,6 +39,14 @@ class ConsoleAPI{
$this->event = $this->server->event("server.tick", array($this, "handle")); $this->event = $this->server->event("server.tick", array($this, "handle"));
$this->loop = new ConsoleLoop; $this->loop = new ConsoleLoop;
$this->loop->start(); $this->loop->start();
$this->register("help", "Show available commands", array($this, "defaultCommands"));
$this->register("status", "Show server TPS and memory usage", array($this, "defaultCommands"));
$this->alias("lag", "status");
$this->register("difficulty", "Changes server difficulty", array($this, "defaultCommands"));
$this->register("invisible", "Changes server visibility", array($this, "defaultCommands"));
$this->register("say", "Broadcast a message", array($this, "defaultCommands"));
$this->register("save-all", "Save pending changes to disk", array($this, "defaultCommands"));
$this->register("stop", "Stops the server gracefully", array($this, "defaultCommands"));
} }
function __destruct(){ function __destruct(){
@ -113,17 +121,24 @@ class ConsoleAPI{
case "save-all": case "save-all":
$this->server->save(); $this->server->save();
break; break;
case "help":
case "?": case "?":
$output .= "/help: Show available commands\n"; if($issuer !== "console"){
$output .= "/status: Show server TPS and memory usage\n"; break;
$output .= "/difficulty: Changes difficulty\n"; }
$output .= "/invisible: Manages server visibility\n"; case "help":
$output .= "/say: Broadcasts mesages\n"; $max = ceil(count($this->help) / 5);
$output .= "/save-all: Saves pending changes\n"; $page = isset($params[0]) ? min($max - 1, max(0, intval($params[0]) - 1)):0;
$output .= "/stop: Stops the server\n"; $output .= "- Showing help page ". ($page + 1) ." of $max (/help <page>) -\n";
$current = 0;
foreach($this->help as $c => $h){ foreach($this->help as $c => $h){
$output .= "/$c: ".$h."\n"; $curpage = (int) ($current / 5);
if($curpage === $page){
$output .= "/$c: ".$h."\n";
}elseif($curpage > $page){
break;
}
++$current;
} }
break; break;
default: default:
@ -145,6 +160,7 @@ class ConsoleAPI{
$cmd = strtolower(trim($cmd)); $cmd = strtolower(trim($cmd));
$this->cmds[$cmd] = $callback; $this->cmds[$cmd] = $callback;
$this->help[$cmd] = $help; $this->help[$cmd] = $help;
ksort($this->help, SORT_NATURAL | SORT_FLAG_CASE);
} }
public function run($line = "", $issuer = false, $alias = false){ public function run($line = "", $issuer = false, $alias = false){