Raised This Month: $ Target: $400
 0% 

[INC] Plugin Base Command


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 03-04-2010 , 21:11   [INC] Plugin Base Command
Reply With Quote #1

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_SERVERg_dirBlank0"dumpcmdtree""BaseCmd_DumpCmdTree");
BaseCmd_Register(BASECMD_SERVERg_dirBlank0"credits""BaseCmd_Credits");
BaseCmd_Register(BASECMD_SERVERg_dirBlank0"version""BaseCmd_Version");
    
// Client commands.
BaseCmd_Register(BASECMD_CLIENTg_dirBlank0"credits""BaseCmd_Credits");
BaseCmd_Register(BASECMD_CLIENTg_dirBlank0"version""BaseCmd_Version"); 
Here's something a bit more complex, to show off the flexibility of the menu:

PHP Code:
BaseCmd_Register(BASECMD_SERVERg_dirBlank0"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_SERVERdirServerTools1"kickafks""BaseCmd_KickAFKs");
// Server Command: <basecmd> servertools kickafks


BaseCmd_Register(BASECMD_SERVERdirServerTools1"extras");

new 
String:dirServerToolsExtras[2][16] = {"servertools""extras"};
BaseCmd_Register(BASECMD_SERVERdirServerToolsExtras2"credits""BaseCmd_Credits");
// Server Command: <basecmd> servertools extras credits

BaseCmd_Register(BASECMD_CLIENTg_dirBlank0"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(clientHandle:hParamsargc)
{
    if (
argc 1)
        return;  
// Not enough arguments.
    
decl String:somestring[16];
    
GetArrayString(hParams0somestring16);  // 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]
Attached Files
File Type: txt pluginname.basecmd.phrases.txt (961 Bytes, 186 views)
File Type: inc basecmd.inc (10.6 KB, 191 views)
File Type: sp Get Plugin or Get Source (basecmdtest.sp - 220 views - 167 Bytes)
__________________

Last edited by Greyscale; 03-04-2010 at 21:21.
Greyscale is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 03-04-2010 , 21:24   Re: [INC] Plugin Base Command
Reply With Quote #2

(Reserved)

I know the plugin won't compile, it's because it's trying to include basecmd.inc. To use it you will need to compile it yourself.
__________________
Greyscale is offline
exvel
SourceMod Donor
Join Date: Jun 2006
Location: Russia
Old 03-04-2010 , 23:51   Re: [INC] Plugin Base Command
Reply With Quote #3

Pretty useful.
How did you printed modules list in "zr modules" menu? Is that something built into "Plugin Base Command" or "Modules System"?
__________________
For admins: My plugins

For developers: Colors library
exvel is offline
Send a message via ICQ to exvel
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 03-05-2010 , 00:11   Re: [INC] Plugin Base Command
Reply With Quote #4

Well this base command is built into the Project Base and I took it out and released it separately here.

The module system is built in modulemanager.inc of the project base. The project base isn't officially released, but it is functional. We're developing ZR on it so I can fix the base's weaknesses or bugs.

I want ZR and the base to be the last things I release for SM.
__________________
Greyscale is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:44.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode