From 00379bb2d1f7dfab4b1f03180cb4a8558b68e669 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Wed, 2 Jan 2013 22:48:37 +0100 Subject: [PATCH] Added command alias --- src/API/ConsoleAPI.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 9751da2c1..f82a34812 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -26,9 +26,10 @@ the Free Software Foundation, either version 3 of the License, or */ class ConsoleAPI{ - private $loop, $server, $event; + private $loop, $server, $event, $help, $cmds; function __construct(PocketMinecraftServer $server){ $this->help = array(); + $this->cmds = array(); $this->server = $server; $this->last = microtime(true); } @@ -216,7 +217,7 @@ class ConsoleAPI{ console("[INFO] /stop: Stops the server"); //console("[INFO] /restart: Restarts the server"); foreach($this->help as $c => $h){ - console("[INFO] /$c: ".$h[0]); + console("[INFO] /$c: ".$h); } break; default: @@ -225,11 +226,17 @@ class ConsoleAPI{ } } - public function register($cmd, $help,$callback){ + public function alias($alias, $cmd){ + $this->cmds[strtolower(trim($alias))] = &$this->cmds[$cmd]; + } + + public function register($cmd, $help, $callback){ if(!is_callable($callback)){ return false; } - $this->help[strtolower(trim($cmd))] = array($help, $callback); + $cmd = strtolower(trim($cmd)); + $this->cmds[$cmd] = $callback; + $this->help[$cmd] = $help; } public function handle($time){ @@ -240,8 +247,8 @@ class ConsoleAPI{ $params = explode(" ", $line); $cmd = strtolower(array_shift($params)); console("[INFO] Issued server command: /$cmd ".implode(" ", $params)); - if(isset($this->help[$cmd]) and is_callable($this->help[$cmd][1])){ - call_user_func($this->help[$cmd][1], $cmd, $params); + if(isset($this->cmds[$cmd]) and is_callable($this->cmds[$cmd])){ + call_user_func($this->cmds[$cmd], $cmd, $params); }elseif($this->server->trigger("api.console.command", array("cmd" => $cmd, "params" => $params)) !== false){ $this->defaultCommands($cmd, $params); }