Plugin Base Command
Description: Allows plugins to build a console-command based menu. Default designs very much resemble the "sm" console command.
Stocks:
PHP Code:
/**
* Initialize the base command.
*/
stock BaseCmd_Init();
/**
* Destroy the base command data.
*/
stock BaseCmd_Destroy();
/**
* Register a sub-command under the base command.
*
* Note: If given non-existant directions they will be automatically created.
*
* @param who For the server or client.
* @param directions The path to the sub-command being registered.
* Ex: "modules" would be the path to "list." Blank if top-level sub-command.
* @param dircount The number of elements (with values) in 'directions.'
* @param subcommand The sub-command to register. (Max length is 16 characters)
* @param callback The function called when the sub-command is used.
*/
BaseCmd_Register(who, const String:directions[][16], dircount, const String:subcommand[], const String:callback[] = "");
/**
* Unregister a registered sub-command under the base command.
*
* @param who For the server or client.
* @param directions The path to the sub-command being unregistered.
* Ex: "modules" would be the path to "list." Blank if top-level sub-command.
* @param dircount The number of elements (with values) in 'directions.'
* @param subcommand The sub-command to unregister.
*
* @return True if the sub-command was unregistered, false if it didn't exist.
*/
stock bool:BaseCmd_Unregister(who, const String:directions[][16], dircount, const String:subcommand[], const String:callback[] = "");
Examples:
This is the default commands registered in BaseCmd_RegisterDefs(). (Feel free to edit them out if you don't want them.)
PHP Code:
// Server commands.
BaseCmd_Register(BASECMD_SERVER, g_dirBlank, 0, "dumpcmdtree", "BaseCmd_DumpCmdTree");
BaseCmd_Register(BASECMD_SERVER, g_dirBlank, 0, "credits", "BaseCmd_Credits");
BaseCmd_Register(BASECMD_SERVER, g_dirBlank, 0, "version", "BaseCmd_Version");
// Client commands.
BaseCmd_Register(BASECMD_CLIENT, g_dirBlank, 0, "credits", "BaseCmd_Credits");
BaseCmd_Register(BASECMD_CLIENT, g_dirBlank, 0, "version", "BaseCmd_Version");
Here's something a bit more complex, to show off the flexibility of the menu:
PHP Code:
BaseCmd_Register(BASECMD_SERVER, g_dirBlank, 0, "servertools");
// This line is optional, the next registrations would automatically create this if it was left out.
new String:dirServerTools[1][16] = {"servertools"};
BaseCmd_Register(BASECMD_SERVER, dirServerTools, 1, "kickafks", "BaseCmd_KickAFKs");
// Server Command: <basecmd> servertools kickafks
BaseCmd_Register(BASECMD_SERVER, dirServerTools, 1, "extras");
new String:dirServerToolsExtras[2][16] = {"servertools", "extras"};
BaseCmd_Register(BASECMD_SERVER, dirServerToolsExtras, 2, "credits", "BaseCmd_Credits");
// Server Command: <basecmd> servertools extras credits
BaseCmd_Register(BASECMD_CLIENT, g_dirBlank, 0, "credits", "BaseCmd_Credits");
// Client command: <basecmd> credits
/**
* Kick all the AFKs.
*
* @param client The client calling this command. 0 if server.
* @param hParams An ADT string array with the arguments given to the command.
* @param argc The number of indexes in hParams.
*/
// Note: The last 2 parameters are optional.
public BaseCmd_KickAFKs(client, Handle:hParams, argc)
{
if (argc < 1)
return; // Not enough arguments.
decl String:somestring[16];
GetArrayString(hParams, 0, somestring, 16); // First parameter.
// The rest of the function.
}
/**
* Base sub-command: "credits"
* Prints credit string to server or client.
*
* @param client The client using the command.
*/
public BaseCmd_Credits(client)
{
// Print credits.
ReplyToCommand(client, "Credits: Andrew \"Greyscale\" Borba for the base command used to print this.");
}
How to use it in your plugin:
- Create a define named "BASE_CMD" anytime before including basecmd.inc. The value of this will be used as the name of the base command. Ex: #define BASE_CMD "zr"
- Call BaseCmd_Init() before any command registering.
- See examples above, or read the code to learn how to register commands.
- Make sure you create translations for each registered command! Examples/defaults are attached.
Translations:
- For every sub-command that leads to more commands, you must create a translation named in the following way:
Code:
"BaseCmd <"server"/"client"> <sub-command name> syntax"
{
"#format" "{1:s}"
"en" "Title to be printed before the list of sub-commands under this one"
}
- For every sub-command registered, you must create a translation of a short description of the command in the following way:
Code:
"BaseCmd <"server"/"client"> <sub-command name> description"
{
"en" "This is printed next to the sub-command when they are listed"
}
Notes:
- The sub-command "dumpcmdtree" registered by default is very useful for debugging registration problems, and for server admins to see all available commands. It dumps a file in keyvalue format of all sub-commands.
Screenshot of it in action in a developing plugin:
[IMG]http://img175.**************/img175/4162/basecmd.jpg[/IMG]