Raised This Month: $32 Target: $400
 8% 

[L4D2] OnClientSayCommand_Post Help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
fdxx
Member
Join Date: Oct 2020
Location: 0xdeadbeef
Old 12-12-2020 , 22:23   [L4D2] OnClientSayCommand_Post Help
Reply With Quote #1

I want the player to directly enter: 6ht, 7ht, 8ht .... in the chat to change the hunter's limit, but this does not work

PHP Code:
#include <sourcemod>

new Handle:h_HunterLimit;
new 
HunterLimit;

public 
void OnAllPluginsLoaded()
{
    
h_HunterLimit FindConVar("l4d_infectedbots_hunter_limit");
    
HunterLimit GetConVarInt(h_HunterLimit);
    
HookConVarChange(h_HunterLimitCVar_HunterLimit);
}

public 
void OnClientSayCommand_Post(int client, const char[] command, const char[] sArgs)
{
    if (
IsValidClient(client) && GetClientTeam(client) == 2)
    {
        
char tmpnumber[32];
        
char htcommand[32];
        
Format(htcommandsizeof(htcommand), "%sht"tmpnumber);
        if (
strcmp(sArgshtcommandfalse) == 0)
        {
            
GetCmdArg(0tmpnumbersizeof(tmpnumber));
            
int number StringToInt(tmpnumber);
            
int MaxSI number 1;
            
ServerCommand("sm_cvar l4d_infectedbots_hunter_limit %i"number)
            
ServerCommand("sm_cvar l4d_infectedbots_max_specials %i"MaxSI)
            
CreateTimer(0.3TimerShowCurrentMode);
        }
    }
}

public 
Action:TimerShowCurrentMode(Handle timerint client)
{
    
PrintToChatAll("\x05Hunter change to %i"HunterLimit);
}

public 
CVar_HunterLimit(Handle:cvarString:oldValue[], String:newValue[])
{
    
HunterLimit GetConVarInt(h_HunterLimit);
}

bool IsValidClient(client)

    if (
client && client MaxClients && IsClientConnected(client) && IsClientInGame(client) && IsPlayerAlive(client)) return true
    return 
false;

Can someone help me? I am a newbie in programming, thank you in advance for your help.

Last edited by fdxx; 12-12-2020 at 22:26.
fdxx is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 12-13-2020 , 03:36   Re: [L4D2] OnClientSayCommand_Post Help
Reply With Quote #2

I noticed three things wrong with your code.

1. You're comparing your formatted string to the args of the command rather than the command itself.
2. The output of Format(htcommand, sizeof(htcommand), "%sht", tmpnumber); is "ht" because the string buffer you're passing is "tmpnumber" which has only been initialized to "\0".
3. You're only storing the command into "tmpnumber" inside your if statement, which will never execute due to what I said in #2.
__________________
Psyk0tik is offline
fdxx
Member
Join Date: Oct 2020
Location: 0xdeadbeef
Old 12-13-2020 , 21:57   Re: [L4D2] OnClientSayCommand_Post Help
Reply With Quote #3

Thank you, but I don’t know how to modify
fdxx is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 12-22-2020 , 22:52   Re: [L4D2] OnClientSayCommand_Post Help
Reply With Quote #4

fdxx, I would write it as:

PHP Code:
#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>

ConVar l4d_infectedbots_hunter_limit;
ConVar l4d_infectedbots_max_specials;

public 
void OnAllPluginsLoaded()
{
    
l4d_infectedbots_hunter_limit FindConVar("l4d_infectedbots_hunter_limit");
    
l4d_infectedbots_max_specials FindConVar("l4d_infectedbots_max_specials");
}

public 
Action OnClientSayCommand(int client, const char[] command, const char[] sArgs)
{
    
int iNum;
    if( 
ParseCmd(sArgs"ht"iNum) )
    {
        
SetCvarSilentInt(l4d_infectedbots_hunter_limitiNum);
        
SetCvarSilentInt(l4d_infectedbots_max_specialsiNum 1);
        
        if( 
client )
            
PrintToChat(client"New limit: %i"iNum);
            
        return 
Plugin_Stop;
    }
    return 
Plugin_Continue;
}

bool ParseCmd(const char[] cmdSourcechar[] cmdSampleint &iNum// examples: 1ht, 16ht
{
    const 
int NUM_MIN 1;
    const 
int NUM_MAX 16;
    const 
int NUMLEN_MAX 2;
    
    
int lenSrc strlen(cmdSource);
    
int CMDLEN strlen(cmdSample);
    
    if( 
CMDLEN <= lenSrc <= CMDLEN NUMLEN_MAX )
    {
        if( 
strcmpcmdSource[lenSrc-CMDLEN], cmdSampletrue ) == )
        {
            
iNum StringToInt(cmdSource);
            if( 
NUM_MIN <= iNum <= NUM_MAX )
            {
                return 
true;
            }
        }
    }
    return 
false;
}

stock bool SetCvarSilentIntConVar cvint value )
{
    if( !
cv )
    {
        return 
false;
    }
    
int flags cv.Flags;
    
cv.Flags &= ~FCVAR_NOTIFY;
    
cv.SetInt(valuetruefalse);
    
cv.Flags flags;
    return 
true;

__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]

Last edited by Dragokas; 12-22-2020 at 22:53.
Dragokas is offline
fdxx
Member
Join Date: Oct 2020
Location: 0xdeadbeef
Old 12-23-2020 , 08:28   Re: [L4D2] OnClientSayCommand_Post Help
Reply With Quote #5

Dragokas, thank you very much, do you have a PayPal account? I want to buy you coffee
fdxx is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 12-23-2020 , 10:32   Re: [L4D2] OnClientSayCommand_Post Help
Reply With Quote #6

I think Patreon can. I added "Donate" link in my signature.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
fdxx
Member
Join Date: Oct 2020
Location: 0xdeadbeef
Old 12-23-2020 , 21:16   Re: [L4D2] OnClientSayCommand_Post Help
Reply With Quote #7

Quote:
Originally Posted by Dragokas View Post
I think Patreon can. I added "Donate" link in my signature.
Already sponsored, It seems that payment will not be made until January 1.
fdxx is offline
plug344
Member
Join Date: Dec 2021
Old 02-09-2022 , 21:40   Re: [L4D2] OnClientSayCommand_Post Help
Reply With Quote #8

sorry, regarding the following part, I don't quite understand

Code:
if( strcmp( cmdSource[lenSrc-CMDLEN], cmdSample, true ) == 0 )
Could this part be rewritten as eg: ?

Code:
if( !strcmp( cmdSource[lenSrc-CMDLEN], cmdSample[0], true ) && !strcmp( cmdSource[lenSrc-CMDLEN+1], cmdSample[1], true ))
Because I think strcmp == 0 and !strcmp are similar, and if it is "1ht", cmdSource[1] seems to be 'h' and cmdSample is "ht", which seems to be compare cmdSource[1] and cmdSample[0] for equality, and compare again cmdSource[2] 't' and cmdSample[1]

Last edited by plug344; 02-09-2022 at 21:44.
plug344 is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 02-10-2022 , 02:01   Re: [L4D2] OnClientSayCommand_Post Help
Reply With Quote #9

You can rewrite it that way, but it has no sense, because:
1) If you mean by that an optimization purposes, strcmp + string[x] behaves differently than you think.
strcmp( a[1], b[0] ) is passing an entire "b" string to comparator. It's a same as strcmp( a[1], b ). So your second part is not required.
2) To compare a single char of string, use ==, e.g. if( a[1] == b[0] ) or strncmp, e.g. if( !strncmp(a[1], b[0], 1).
3) Even if you use comparing by single char, I think strcmp is already enough optimized to cover the case when the first char is not match, to prevent comparing all the remaining characters in passed string.
4) If we mean universal "ParseCmd" stock, than hardcoding +1 offset in cmdSource[lenSrc-CMDLEN+1] is not good, because function will access the memory outside the array bounds in case you would want to check for e.g.: ParseCmd(sArgs, "h", iNum).

The only improvement I would add is to make it recognize larger numbers, like > 16 and < 1, so the final code looks so:
PHP Code:
/*
    Extracts the number iNum from source if it is followed by string passed as sample.
    
    @param cmdSource - string to analyze
    @param cmdSample - string next to the number.
    @param iNum - buffer to store resulting number.
    
    @return True, if analyzed string contains data in correct format and iNum filled with number.
            False - otherwise.
*/
stock bool ParseCmd(const char[] cmdSourcechar[] cmdSampleint &iNum// examples: 1ht, 16ht
{
    const 
int MAX_INT_LEN 11;
    
    
int lenSrc strlen(cmdSource);
    
int cmdLen strlen(cmdSample);
    
    if( 
cmdLen <= lenSrc <= cmdLen MAX_INT_LEN )
    {
        if( 
strncmpcmdSource[lenSrc-cmdLen], cmdSamplecmdLen ) == )
        {
            
iNum StringToInt(cmdSource);
            return 
true;
        }
    }
    return 
false;

__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]

Last edited by Dragokas; 02-10-2022 at 05:28.
Dragokas is offline
plug344
Member
Join Date: Dec 2021
Old 02-10-2022 , 03:59   Re: [L4D2] OnClientSayCommand_Post Help
Reply With Quote #10

thank you Dragokas taking the time out of your busy schedule to reply to me, I have benefited a lot from your code and posts, I am researching how to use patreon in my country for show my thanks to you

Last edited by plug344; 02-10-2022 at 17:39.
plug344 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 03:25.


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