Added a way to call custom commands in ConsoleAPI

This commit is contained in:
Shoghi Cervantes Pueyo 2013-01-17 16:10:41 +01:00
parent 1409a1f7b5
commit ed61111aca

View File

@ -150,7 +150,11 @@ class ConsoleAPI{
}
public function alias($alias, $cmd){
if(!isset($this->cmds[$cmd])){
return false;
}
$this->cmds[strtolower(trim($alias))] = &$this->cmds[$cmd];
return true;
}
public function register($cmd, $help, $callback){
@ -161,21 +165,25 @@ class ConsoleAPI{
$this->cmds[$cmd] = $callback;
$this->help[$cmd] = $help;
}
public function run($line = ""){
if($line != ""){
$params = explode(" ", $line);
$cmd = strtolower(array_shift($params));
console("[INFO] Issued server command: /$cmd ".implode(" ", $params));
if(isset($this->cmds[$cmd]) and is_callable($this->cmds[$cmd])){
call_user_func($this->cmds[$cmd], $cmd, $params);
}elseif($this->server->api->dhandle("api.console.command", array("cmd" => $cmd, "params" => $params)) !== false){
$this->defaultCommands($cmd, $params);
}
}
}
public function handle($time){
if($this->loop->line !== false){
$line = trim($this->loop->line);
$this->loop->line = false;
if($line !== ""){
$params = explode(" ", $line);
$cmd = strtolower(array_shift($params));
console("[INFO] Issued server command: /$cmd ".implode(" ", $params));
if(isset($this->cmds[$cmd]) and is_callable($this->cmds[$cmd])){
call_user_func($this->cmds[$cmd], $cmd, $params);
}elseif($this->server->api->dhandle("api.console.command", array("cmd" => $cmd, "params" => $params)) !== false){
$this->defaultCommands($cmd, $params);
}
}
$this->run($line);
}else{
$this->loop->notify();
}