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

PrintToChat with AddCommandListener on say.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Aderic
Senior Member
Join Date: Oct 2011
Old 11-13-2013 , 12:42   PrintToChat with AddCommandListener on say.
Reply With Quote #1

I'm trying to get this plugin to send a message to the player after he says the chat trigger. Now the command seems to be firing just fine, but the issue is this... It's firing before the client's chat message is broadcasted to all players. It looks kind of clunky this way.

I'm sure I'm going about this all the wrong way, but what would be the most efficient method of printing text to a client after they say a message?

This is the code I've got:

PHP Code:
#include <sourcemod>

public Plugin:myinfo 
{
    
name "Chat Test",
    
author "Nobody",
    
version "1.0",
}

public 
OnPluginStart()
{
    
AddCommandListener(Command_Say,    "say");
    
AddCommandListener(Command_Say,    "say2");
    
AddCommandListener(Command_Say"say_team");
}

public 
Action:Command_Say(client, const String:command[], argc){
    new 
String:text[16];
    new 
String:cmd[16];
    
    
GetCmdArg(0cmdsizeof(cmd));
    
    if (
GetCmdArg(1textsizeof(text)) < 1)
        return 
Plugin_Continue;
    
    
StripQuotes(text);
    
    new 
bool:suppressMsg;
    
    if (
text[0] == '!')
        
suppressMsg false;
    else if (
text[0] == '/')
        
suppressMsg true;
    else
        return 
Plugin_Continue;
        
// This is firing before the client's message is sent!
    
if (StrEqual(text[1], "ping"false))
    {
        
PrintToChat(client"pong");
    }
    
    if (
suppressMsg == true)
        return 
Plugin_Handled;
    else
        return 
Plugin_Continue;

Thanks,

Mark.
__________________

Last edited by Aderic; 11-13-2013 at 12:45. Reason: Changed [Code] tags to [PHP] tags. Derp.
Aderic is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 11-13-2013 , 12:57   Re: PrintToChat with AddCommandListener on say.
Reply With Quote #2

OK, two things going on here:

1. If you're looking for / and ! command, use RegConsoleCmd as it will automatically create those for you with the proper handling.
2. If you want to delay something happening, you'll need timers.

So, I'm thinking something like:

PHP Code:
#include <sourcemod> 

public Plugin:myinfo =  

    
name "Chat Test"
    
author "Nobody"
    
version "1.0"


public 
OnPluginStart() 
{
    
RegConsoleCmd("ping"Command_Ping);


public 
Action:Command_Ping(clientargs){ 
{
    
CreateTimer(0.1PongGetClientUserId(client));
    return 
Plugin_Handled;
}

public 
Action:Pong(Handle:timerany:userid)
{
    new 
client GetClientOfUserId(userid);
    if (
client 0)
    {
        
PrintToChat(client"pong");
    }


The reason for the GetClientUserId and GetClientOfUserId is to prevent errors when a client disconnects before the timer fires.

For other kinds of chat commands, SourceMod 1.5 introduced OnClientSayCommand, but I'm not really sure how it works.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 11-13-2013 at 13:01.
Powerlord is offline
Aderic
Senior Member
Join Date: Oct 2011
Old 11-13-2013 , 13:24   Re: PrintToChat with AddCommandListener on say.
Reply With Quote #3

Quote:
Originally Posted by Powerlord View Post
OK, two things going on here:

1. If you're looking for / and ! command, use RegConsoleCmd as it will automatically create those for you with the proper handling.
2. If you want to delay something happening, you'll need timers.

So, I'm thinking something like:

PHP Code:
#include <sourcemod> 

public Plugin:myinfo =  

    
name "Chat Test"
    
author "Nobody"
    
version "1.0"


public 
OnPluginStart() 
{
    
RegConsoleCmd("ping"Command_Ping);


public 
Action:Command_Ping(clientargs){ 
{
    
CreateTimer(0.1PongGetClientUserId(client));
    return 
Plugin_Handled;
}

public 
Action:Pong(Handle:timerany:userid)
{
    new 
client GetClientOfUserId(userid);
    if (
client 0)
    {
        
PrintToChat(client"pong");
    }


The reason for the GetClientUserId and GetClientOfUserId is to prevent errors when a client disconnects before the timer fires.

For other kinds of chat commands, SourceMod 1.5 introduced OnClientSayCommand, but I'm not really sure how it works.
Hmm yeah, I thought about timers but figured there'd be a different method. We can hook events Pre/Post. I'm surprised we can't do something like that for listener callbacks. Just tried OnClientSayCommand, it seems to be handled the same as manually adding the listeners. Anything in the listener gets fired before the message is broadcasted. Thanks

Edit: Hmm. I see a OnClientSayCommand_Post in the sourcemod api docs. Gonna look at that

Edit 2: This seems to be doing exactly what I need! I'm so glad you mentioned OnClientSayCommand! If I could give you rep I would!


Edit #9259:

I think there's a bug in the way Sourcemod handles OnClientSayCommand with OnClientSayCommand_Post.

If you return Plugin_Handled on OnClientSayCommand then OnClientSayCommand_Post will receive the full sArgs string.
If you return Plugin_Continue on OnClientSayCommand then OnClientSayCommand_Post will receive the sArgs string minus the end quote.

PHP Code:
public Action:OnClientSayCommand(client, const String:command[], const String:sArgs[]) {
    
PrintToServer(String:sArgs);
    return 
Plugin_Continue;
}

public 
OnClientSayCommand_Post(client, const String:command[], const String:sArgs[]) {
    
PrintToServer(sArgs);


I made a workaround for it.

PHP Code:
public Action:OnClientSayCommand(client, const String:command[], const String:sArgs[]) {
    return 
Plugin_Continue;
}

public 
OnClientSayCommand_Post(client, const String:command[], const String:sArgs[]) {
    
decl String:msg[16];
    new 
sArgsSize strlen(sArgs) - 1;
    
    if (
sArgsSize == 0)
        return;
    
    if (
sArgs[0] == '"') {
        if (
sArgs[sArgsSize] == '"') {
            
strcopy(msgsizeof(msg), sArgs);    // Copy the whole thing.
            
StripQuotes(msg);                     // Strip the quotes.
        
}
        else {                                     
// Copy the portion after the quote.
            
strcopy(msgsizeof(msg), sArgs[1]); // This is unquoted.
        
}
    }
    
    
PrintToServer(msg);

__________________

Last edited by Aderic; 11-13-2013 at 14:52. Reason: Workaround.
Aderic is offline
xf117
Senior Member
Join Date: Mar 2010
Location: Russia
Old 11-14-2013 , 05:44   Re: PrintToChat with AddCommandListener on say.
Reply With Quote #4

You can assign \0 to the position where ending quote is. Using strcopy for this kinda redundant.
xf117 is offline
Send a message via ICQ to xf117
Aderic
Senior Member
Join Date: Oct 2011
Old 11-14-2013 , 16:16   Re: PrintToChat with AddCommandListener on say.
Reply With Quote #5

Quote:
Originally Posted by xf117 View Post
You can assign \0 to the position where ending quote is. Using strcopy for this kinda redundant.
What will \0 do to it? I still have to strcopy anyways.
__________________
Aderic is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 11-14-2013 , 17:21   Re: PrintToChat with AddCommandListener on say.
Reply With Quote #6

Quote:
Originally Posted by Aderic View Post
What will \0 do to it? I still have to strcopy anyways.
\0 is the byte that signals the end of a string.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Aderic
Senior Member
Join Date: Oct 2011
Old 11-14-2013 , 17:37   Re: PrintToChat with AddCommandListener on say.
Reply With Quote #7

Quote:
Originally Posted by Powerlord View Post
\0 is the byte that signals the end of a string.
Hmm. That's what I thought it did.

Quote:
Originally Posted by xf117 View Post
You can assign \0 to the position where ending quote is. Using strcopy for this kinda redundant.
The issue is not that the string is missing a null line terminator, it was about either grabbing the string from the position after the opening quote (if the end quote is missing) to get the full unquoted string..

or grabbing the string with both quotes and stripping them.
__________________

Last edited by Aderic; 11-14-2013 at 17:44.
Aderic is offline
xf117
Senior Member
Join Date: Mar 2010
Location: Russia
Old 11-19-2013 , 11:24   Re: PrintToChat with AddCommandListener on say.
Reply With Quote #8

You don't need strcopy
PHP Code:
new pos 0// or any starting position you want
if (str[strlen(str)-1] == '"') {
  
str[strlen(str)-1] = '\0';
  
pos++;
}

PrintToServer(str[pos]); 
xf117 is offline
Send a message via ICQ to xf117
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 07:38.


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