Veteran Member
Join Date: Oct 2006
Location: Poland
|
04-25-2010
, 09:35
Re: Plugin like df_responses (executes server commands)
|
#37
|
I added possibility to pass arguments from chat to execute command.
PHP Code:
#include < amxmodx > #include < amxmisc > #include < celltrie >
#define MAX_ARGS 5 #define MAX_ARG_LEN 64
new Trie:g_tArrayPos; new Array:g_aCommands; new Array:g_aFlags; new Array:g_aHideCmd;
public plugin_init( ) { register_plugin( "Say Command Macro", "0.0.2", "Exolent" ); register_clcmd( "say", "CmdSay" ); g_tArrayPos = TrieCreate( ); g_aCommands = ArrayCreate( 128 ); g_aFlags = ArrayCreate( 1 ); g_aHideCmd = ArrayCreate( 1 ); LoadCommands( ); }
public plugin_end( ) { TrieDestroy( g_tArrayPos ); ArrayDestroy( g_aCommands ); ArrayDestroy( g_aFlags ); ArrayDestroy( g_aHideCmd ); }
LoadCommands( ) { new szFilename[ 128 ]; get_configsdir( szFilename, 127 ); add( szFilename, 127, "/say_macros.ini" ); if( !file_exists( szFilename) ) { new iFile = fopen( szFilename, "wt" ); if( iFile ) { fclose( iFile ); } return; } new iFile = fopen( szFilename, "rt" ); if( !iFile ) { return; } new szData[ 512 ], szCommand[ 32 ], szExecute[ 128 ], szFlags[ 27 ], szHide[ 2 ], iPos; while( !feof( iFile ) ) { fgets( iFile, szData, 511 ); trim( szData ); if( !szData[ 0 ] || szData[ 0 ] == ';' || szData[ 0 ] == '/' && szData[ 1 ] == '/' ) { continue; } parse( szData, szCommand, 31, szExecute, 127, szFlags, 26, szHide, 1 ); strtolower( szCommand ); TrieSetCell( g_tArrayPos, szCommand, iPos ); ArrayPushString( g_aCommands, szExecute ); ArrayPushCell( g_aFlags, read_flags( szFlags ) ); ArrayPushCell( g_aHideCmd, str_to_num( szHide ) ); iPos++; } fclose( iFile ); }
public CmdSay( client ) { static szMessage[ 194 ], len, i, j, k, szTmp[ 5 ]; static szArg[MAX_ARGS][MAX_ARG_LEN], argsCnt; read_args( szMessage, 193 );
len = strlen(szMessage)-1 argsCnt = i = j = 0 k = -1 while(++i<len){ if(k>=0){ if(szMessage[i] == '^"'){ szArg[argsCnt++][k] = '^0'; k=-1; j += formatex( szMessage[j], 193-j, "$%d", argsCnt ); }else{ if(k<MAX_ARG_LEN) szArg[argsCnt][k++] = szMessage[i]; } }else{ if(szMessage[i] == '^"'){ if(argsCnt<MAX_ARGS) k=0; }else{ szMessage[j++] = szMessage[i]; } } } szMessage[j] = '^0'; if(k!=-1){ szArg[argsCnt++][k] = '^0'; } strtolower( szMessage );
static iArrayPos; if( TrieGetCell( g_tArrayPos, szMessage, iArrayPos ) ) { if( !access( client, ArrayGetCell( g_aFlags, iArrayPos ) ) ) { client_print( client, print_chat, "* You have no access to that command!" ); } else { static szExecute[ 128 ]; ArrayGetString( g_aCommands, iArrayPos, szExecute, 127 ); static szName[ 32 ]; get_user_name( client, szName, 31 ); replace_all( szName, 31, "'", "" ); replace_all( szName, 31, "^"", "" ); static szAuthid[ 35 ]; get_user_authid( client, szAuthid, 34 ); static szIP[ 32 ]; get_user_ip( client, szIP, 31, 1 ); static szUserid[ 6 ]; formatex( szUserid, 5, "#%i", get_user_userid( client ) ); replace_all( szExecute, 127, "''", "^"" ); replace_all( szExecute, 127, "$name", szName ); replace_all( szExecute, 127, "$steamid", szAuthid ); replace_all( szExecute, 127, "$ip", szIP ); replace_all( szExecute, 127, "$userid", szUserid ); for( i=0; i<argsCnt; i++ ) { format( szTmp, 4, "$%d", i+1); replace_all( szExecute, 127, szTmp, szArg[i] ); }
server_cmd( "%s", szExecute ); if( ArrayGetCell( g_aHideCmd, iArrayPos ) ) { return PLUGIN_HANDLED; } } } return PLUGIN_CONTINUE; }
Config file (save it in addons/amxmodx/configs/say_macros.ini):
Code:
; "say command" "execute command" "admin flags" "hide command from chat"
;
; Example:
; "menu" "amxmodxmenu" "u" "1"
; This will work when you say:
; say "menu"
; And it will hide your text "menu" from chat
;
; To not hide the text, leave that part out, or set it to 0
; Example:
; "menu" "amxmodxmenu" "u"
; "kickmenu" "amx_kickmeu" "u" "0"
;
; In the execute command, you can use these to pass yourself into the execute:
; - $name = your name
; - $steamid = your steamid
; - $ip = your ip
; - $userid = your userid
;
; Example:
; "godon" "amx_godmode $userid 1" "m"
; "godoff" "amx_godmode $userid 0" "m"
;
; You can also use '' (two apostrophes) to represent quotes inside of the execute command,
; since (example) "amx_nick "name1" "name2"" would cause errors.
;
; Example:
; "gametype hns" "amx_gametype ''HideNSeek Mod''; exec ''addons/amxmodx/configs/hidenseek.cfg''; sv_restart 1" "a"
;
; If you are using the $name, $ip, or $steamid in a way that needs quotes,
; you will need to add them yourself.
;
; Example:
; "ban me" "amx_ban ''$steamid'' 0 ''lol banned myself :D''" ""
;
;
; Passing arguments from chat to execute command:
; before comparing chat input to defined say commands, all "CONTENT" are replaced with $1, $2, $3
; after that all $number in execute command are replaced with adequate CONTENT
;
; Example1:
; "csay $1 color $2" "amx_csay $2 $name said $1 using $2 color" ""
; now if player named Player1 write in chat: csay "some message" color "green"
; then it will trigger in server console: "amx_csay green Player1 said some message using green color"
;
; Example2;
; "!nick $1 $2" "amx_nick ''$1'' ''$2''" ""
; now writing in chat: !nick "player1" "test nick"
; will trigger in server console: "amx_nick ^"Player1^" ^"test nick^""
;
; More examples (you can use them all at the same time):
; "!kick $1" "amx_kick ''$1''" "c" ;kick without reason
; "!kick $1 $2" "amx_kick ''$1'' ''$2''" "c" ;kick with reason
; "!ban5 $1" "amx_ban ''$1'' 5" "d" ;short ban without reason
; "!ban5 $1 $2" "amx_ban ''$1'' 5 ''$2''" "d" ;short ban with reason
; "!pban $1 $2" "amx_ban ''$1'' 0 ''$2''" "d" ;permanent ban with reason
; "!hostname $1" "hostname ''$1''" "a" ;set hostname
__________________
Impossible is Nothing
Last edited by Sylwester; 10-31-2011 at 02:49.
|
|