Raised This Month: $51 Target: $400
 12% 

Easy_Commands - An easier way to deal with Console commands


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
necavi
Veteran Member
Join Date: Sep 2010
Old 01-03-2013 , 00:54   Easy_Commands - An easier way to deal with Console commands
Reply With Quote #1

I realized that having an extra plugin to do this was...redundant and redid this plugin: https://forums.alliedmods.net/showthread.php?t=180761 as a simple include file.

This include's purpose is to make console commands quite a bit easier for plugin authors to add, saving quite a bit of duplicate code. Here is an example, comparing the same function using my include and using the playercommands version:
slap.sp:
PHP Code:
#include <sourcemod>
#include <sdktools>
public Plugin:myinfo =
{
    
name "Player Commands",
    
author "AlliedModders LLC",
    
description "Misc. Player Commands",
    
version SOURCEMOD_VERSION,
    
url "http://www.sourcemod.net/"
};
public 
OnPluginStart()
{
    
RegAdminCmd("sm_slap"Command_SlapADMFLAG_SLAY"sm_slap <#userid|name> [damage]");
}
public 
Action:Command_Slap(clientargs)
{
    if (
args 1)
    {
        
ReplyToCommand(client"[SM] Usage: sm_slap <#userid|name> [damage]");
        return 
Plugin_Handled;
    }

    
decl String:arg[65];
    
GetCmdArg(1argsizeof(arg));

    new 
damage 0;
    if (
args 1)
    {
        
decl String:arg2[20];
        
GetCmdArg(2arg2sizeof(arg2));
        if (
StringToIntEx(arg2damage) == || damage 0)
        {
            
ReplyToCommand(client"[SM] %t""Invalid Amount");
            return 
Plugin_Handled;
        }
    }
    
    
decl String:target_name[MAX_TARGET_LENGTH];
    
decl target_list[MAXPLAYERS], target_countbool:tn_is_ml;
    
    if ((
target_count ProcessTargetString(
            
arg,
            
client,
            
target_list,
            
MAXPLAYERS,
            
COMMAND_FILTER_ALIVE,
            
target_name,
            
sizeof(target_name),
            
tn_is_ml)) <= 0)
    {
        
ReplyToTargetError(clienttarget_count);
        return 
Plugin_Handled;
    }
    
    for (new 
0target_counti++)
    {
        
PerformSlap(clienttarget_list[i], damage);
    }

    if (
tn_is_ml)
    {
        
ShowActivity2(client"[SM] ""%t""Slapped target"target_name);
    }
    else
    {
        
ShowActivity2(client"[SM] ""%t""Slapped target""_s"target_name);
    }

    return 
Plugin_Handled;

My version:
PHP Code:
#include <sourcemod>
#include <easy_commands>
#include <sdktools_functions>
public Plugin:myinfo =
{
    
name "Slap command!",
    
author "necavi",
    
description "Supplies a single command to slap any player.",
    
version "1.0",
    
url "http://necavi.org"
}

public 
OnPluginStart()
{
    
RegEasyAdminCmd("sm_slap2 <playerfilter> [damage]"Command_SlapAdmin_Cheats, { Cmd_FilterCmd_Cell }, 21);
}
public 
Command_Slap(clienttargets[], numTargetsdamage)
{
    if(
damage 0)
    {
        
damage 0;
    }
        
    for(new 
1numTargetsi++)
    {
        
SlapPlayer(idamage);
    }

Obviously this is a bit oversimplified, as the slap command did have a few extra checks to verify various portions for integrity, which I have not added in such a basic, short example.

The commands are as follows:
PHP Code:
stock RegEasyConCmd(String:command[], Function:func_:params[]={0}, totalParams 0reqParams 0filterFlags 0replyNoTargetFound true)
stock RegEasyAdminCmd(String:command[], Function:funcAdminFlag:flag Admin_Generic_:params[] = {0}, totalParams 0reqParams 0filterFlags 0replyNoTargetFound true
Params:
PHP Code:
enum CmdParam
{
    
Cmd_All 0,
    
Cmd_Cell,
    
Cmd_Float,
    
Cmd_String,
    
Cmd_Filter//Note! This parameter requires two on the handler, targets[] and numTargets.
    
Cmd_Filter_Single
}; 
Currently it does not allow for a description, it simply gives the full command string in reply (including parameters).

Benefits:
Deals with filtering (@me,@all,@ct,etc)
Deals with replying to too few arguments
Allows for command handlers with single arguments, instead of the sometimes cryptic (client,args).
Attached Files
File Type: inc easy_commands.inc (5.1 KB, 840 views)

Last edited by necavi; 01-03-2013 at 21:15.
necavi is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 01-03-2013 , 01:16   Re: Easy_Commands - An easier way to deal with Console commands
Reply With Quote #2

Looks cool! Could certainly save a lot of time.

I propose a couple of changes:
  • Add Cmd_Filter_Single or something similar so that single-client targeting (using FindTarget) can be done. This could be paired with a single cell parameter in the callback function instead of the targets[], numTargets parameters used by Cmd_Filter.
  • Add a way to pass command filter flags to Cmd_Filter, so that stuff like COMMAND_FILTER_NO_IMMUNITY or COMMAND_FILTER_ALIVE can be passed.
  • Perhaps remove the PrintToServer call to avoid server console spam.
  • Replace PrintToConsole(client, "Command args incorrect! Format: %s", reply); with ReplyToCommand(client, "[SM] Usage: %s", reply); to maintain consistency with most other plugins.

Edit: Regarding #4, I have a better idea.

Put this at the top of your include file:
PHP Code:
#if !defined INVALID_ARGS_RESPONSE
 #define INVALID_ARGS_RESPONSE "Command args incorrect! Format: %s"
#endif 
And change your response line to:
PHP Code:
ReplyToCommand(clientINVALID_ARGS_RESPONSEreply); 
This would allow plugin developers to change the invalid arguments response by doing the following:
PHP Code:
#define INVALID_ARGS_RESPONSE "\x04[SM] \x01Usage: %s"
#include <easy_commands> 
__________________

Last edited by Dr. McKay; 01-03-2013 at 01:19.
Dr. McKay is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 01-03-2013 , 01:31   Re: Easy_Commands - An easier way to deal with Console commands
Reply With Quote #3

Good points all around! Updated. Basically all I did when I moved this from a plugin to an include was rename variables and switch from native to stock, I just did a general pass for standards. Also I did leave some debug text in >>
necavi is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 01-03-2013 , 01:38   Re: Easy_Commands - An easier way to deal with Console commands
Reply With Quote #4



I'm going to be using this in my plugins from now on.
__________________
Dr. McKay is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 01-03-2013 , 20:57   Re: Easy_Commands - An easier way to deal with Console commands
Reply With Quote #5

Sorry to be a nag, but is there any chance you could change it to it uses ReplyToTargetError when there are no targets returned for Cmd_Filter or Cmd_Filter_Single and have it not call the callback? This would save checking to make sure that the targeting was successful.

You could add a boolean to the end of the RegEasy*Cmd function to enable/disable the automatic targeting check.
__________________
Dr. McKay is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 01-03-2013 , 21:16   Re: Easy_Commands - An easier way to deal with Console commands
Reply With Quote #6

Integrated that change, and really I don't mind, after all, I plan to use this include myself at some point
necavi 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 14:40.


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