improved commandmanager
This commit is contained in:
parent
c6bafe2abb
commit
67f468161e
@ -18,7 +18,7 @@ class CommandManager implements CallbackListener {
|
|||||||
*/
|
*/
|
||||||
private $maniaControl = null;
|
private $maniaControl = null;
|
||||||
private $commandListeners = array();
|
private $commandListeners = array();
|
||||||
|
private $adminCommandListeners = array();
|
||||||
/**
|
/**
|
||||||
* Construct commands manager
|
* Construct commands manager
|
||||||
*
|
*
|
||||||
@ -35,20 +35,30 @@ class CommandManager implements CallbackListener {
|
|||||||
* @param string $commandName
|
* @param string $commandName
|
||||||
* @param CommandListener $listener
|
* @param CommandListener $listener
|
||||||
* @param string $method
|
* @param string $method
|
||||||
|
* @param bool $adminCommand
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function registerCommandListener($commandName, CommandListener $listener, $method) {
|
public function registerCommandListener($commandName, CommandListener $listener, $method, $adminCommand = false) {
|
||||||
$command = strtolower($commandName);
|
$command = strtolower($commandName);
|
||||||
if (!method_exists($listener, $method)) {
|
if (!method_exists($listener, $method)) {
|
||||||
trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!");
|
trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if($adminCommand){
|
||||||
|
if (!array_key_exists($command, $this->adminCommandListeners) || !is_array($this->adminCommandListeners[$command])) {
|
||||||
|
// Init listeners array
|
||||||
|
$this->adminCommandListeners[$command] = array();
|
||||||
|
}
|
||||||
|
// Register admin command listener
|
||||||
|
array_push($this->adminCommandListeners[$command], array($listener, $method));
|
||||||
|
}else{
|
||||||
if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
|
if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
|
||||||
// Init listeners array
|
// Init listeners array
|
||||||
$this->commandListeners[$command] = array();
|
$this->commandListeners[$command] = array();
|
||||||
}
|
}
|
||||||
// Register command listener
|
// Register command listener
|
||||||
array_push($this->commandListeners[$command], array($listener, $method));
|
array_push($this->commandListeners[$command], array($listener, $method));
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,12 +80,24 @@ class CommandManager implements CallbackListener {
|
|||||||
// Handle command
|
// Handle command
|
||||||
$command = explode(" ", substr($callback[1][2], 1));
|
$command = explode(" ", substr($callback[1][2], 1));
|
||||||
$command = strtolower($command[0]);
|
$command = strtolower($command[0]);
|
||||||
if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
|
|
||||||
|
if(substr($command,0,1) == "/" || $command == "admin"){ //admin command
|
||||||
|
$commandListeners = $this->adminCommandListeners;
|
||||||
|
if($command == "admin"){
|
||||||
|
//TODO make /admin working
|
||||||
|
}
|
||||||
|
$command = substr($command, 1);
|
||||||
|
}else{ //user command
|
||||||
|
$commandListeners = $this->commandListeners;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!array_key_exists($command, $commandListeners) || !is_array($commandListeners[$command])) {
|
||||||
// No command listener registered
|
// No command listener registered
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inform command listeners
|
// Inform command listeners
|
||||||
foreach ($this->commandListeners[$command] as $listener) {
|
foreach ($commandListeners[$command] as $listener) {
|
||||||
call_user_func(array($listener[0], $listener[1]), $callback, $player);
|
call_user_func(array($listener[0], $listener[1]), $callback, $player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,15 +26,15 @@ class PlayerCommands implements CommandListener {
|
|||||||
$this->maniaControl = $maniaControl;
|
$this->maniaControl = $maniaControl;
|
||||||
|
|
||||||
// Register for commands
|
// Register for commands
|
||||||
$this->maniaControl->commandManager->registerCommandListener('/teambalance', $this, 'command_TeamBalance');
|
$this->maniaControl->commandManager->registerCommandListener('teambalance', $this, 'command_TeamBalance',true);
|
||||||
$this->maniaControl->commandManager->registerCommandListener('/autoteambalance', $this, 'command_TeamBalance');
|
$this->maniaControl->commandManager->registerCommandListener('autoteambalance', $this, 'command_TeamBalance',true);
|
||||||
$this->maniaControl->commandManager->registerCommandListener('/kick', $this, 'command_Kick');
|
$this->maniaControl->commandManager->registerCommandListener('kick', $this, 'command_Kick',true);
|
||||||
$this->maniaControl->commandManager->registerCommandListener('/forcespec', $this, 'command_ForceSpectator');
|
$this->maniaControl->commandManager->registerCommandListener('forcespec', $this, 'command_ForceSpectator',true);
|
||||||
$this->maniaControl->commandManager->registerCommandListener('/forcespectator', $this, 'command_ForceSpectator');
|
$this->maniaControl->commandManager->registerCommandListener('forcespectator', $this, 'command_ForceSpectator',true);
|
||||||
$this->maniaControl->commandManager->registerCommandListener('/forceplay', $this, 'command_ForcePlayer');
|
$this->maniaControl->commandManager->registerCommandListener('forceplay', $this, 'command_ForcePlayer',true);
|
||||||
$this->maniaControl->commandManager->registerCommandListener('/forceplayer', $this, 'command_ForcePlayer');
|
$this->maniaControl->commandManager->registerCommandListener('forceplayer', $this, 'command_ForcePlayer',true);
|
||||||
$this->maniaControl->commandManager->registerCommandListener('/addfakeplayers', $this, 'command_AddFakePlayers');
|
$this->maniaControl->commandManager->registerCommandListener('addbots', $this, 'command_AddFakePlayers',true);
|
||||||
$this->maniaControl->commandManager->registerCommandListener('/removefakeplayers', $this, 'command_RemoveFakePlayers');
|
$this->maniaControl->commandManager->registerCommandListener('removebots', $this, 'command_RemoveFakePlayers',true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user