/**
* This source file is part of WZ Hybrid Bots
* For the latest information, see http://sourceforge.net/projects/wzhybridbots
*
* Copyright (c) 2006 - WzCtf
* For more information about WzCtf, see http://www.wzctf.net
* Also see the license in license.txt
*/
package wzhybridbots.tools.command;
/** The imported classes/interfaces */
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import hybrid.core.consts.MessageTypes;
import frontend.alphaspawn.ASConnection;
import frontend.alphaspawn.tools.command.Command;
import frontend.alphaspawn.tools.command.CommandManager;
/**
* Help command used to list commands understood by the bot and to
* obtain specific help about a command.
*
* @author Witlospock
* @version 1.0
*/
public class Help extends Command {
/** A connection object used to track and handle commands */
protected ASConnection objConnection;
/**
* The class constructor. Initialize the command object.
*
* @param conn A fully functionnal connection object.
*/
public Help ( ASConnection conn ) {
// Initialize the class
super( "!help <command>", "Brings this help menu or explains the command" );
// Checks for valid parameter
if ( conn == null ) { throw new NullPointerException(); }
// Sets the field
this.objConnection = conn;
}
/**
* Returns true if the player has sufficient access level,
* false otherwise.
*
* @param playerName The name of the player to check access.
*
* @return see above.
*/
public boolean checkAccess ( String playerName ) {
// No Access level required to call this command
return true;
}
/**
* Returns a bit field representing the command type this command can be fired from.
*
* @return see above.
*/
public int getMessageTypes () {
return (1 << MessageTypes.Private) | (1 << MessageTypes.Public);
}
/**
* Called when a player with the proper access level execute a command
* that belongs to this Command object.
*
* @param playerName The name of the player executing the command.
* @param command The command to be execute.
* @param args Additionnal arguments beside the command .
*/
public void handleCommand( String playerName, String command, String args ) {
boolean hasArgs = (args != null && args.trim().length() > 0);
// Checks if the player is requesting help for a specific command
// or to obtain the list of available commands
if ( hasArgs ) {
// The player is asking for a specific command's help
// TODO Print help's for a specific command
}
else {
// The player wants to obtain a list of all available commands
// Retrieve the command manager
// TODO Print a beautiful help gui
CommandManager cmdMgr = CommandManager.getInstance();
// Variables use to display the menu
SortedMap cmdDisplay = new TreeMap();
int longestCmd = 0;
// Retrieve a list of all commands
List cmdList = cmdMgr.getCommands();
// Display list of commands
for ( Iterator iter = cmdList.iterator() ; iter.hasNext() ; ) {
Command cmd = (Command)iter.next();
// Checks if the player has the right to see the command
if ( cmd.checkAccess( playerName ) ) {
String cmdName = cmd.getName().trim();
if ( cmdName.length() > longestCmd ) { longestCmd = cmdName.length(); }
// Memorize the command name
cmdDisplay.put( cmdName, cmd );
}
}
// Display the help menu to the player
String spaces = " ";
for ( Iterator iter = cmdDisplay.entrySet().iterator() ; iter.hasNext() ; ) {
Command cmd = (Command)( (Map.Entry)iter.next() ).getValue();
// Format the output string
StringBuffer output = new StringBuffer( cmd.getName().trim() );
// Fill the output with some space
for ( int i = output.length() ; i < longestCmd ; i++ ) {
output.append( ' ' );
}
// Add the description to the output
output.append( spaces ).append( cmd.getDescription().trim() );
// Display the command + description to the player
this.objConnection.sendPrivateMessage( playerName, output.toString() );
}
}
}
/**
* Called when a player with insufficient permission want to execute
* a command that belongs to this Command object.
*
* @param playerName The name of the player executing the command.
* @param command The command to be execute.
* @param args Additionnal arguments beside the command.
*/
public void ignoreCommand( String playerName, String command, String args ) {
}
}