AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Read certain part of chat message (https://forums.alliedmods.net/showthread.php?t=237797)

Buckshot 03-29-2014 16:13

Read certain part of chat message
 
So, i am making this plugin where a player types "/command playername description", and i want to get the playername and string separately and place each of them in a separate variable which i am going to use, which does not work with read_args since read_args reads everything after the command at once. How do i do this? I guess you gotta read the information before the space, and then put the rest in description variable.. but i don't know how.

Black Rose 03-29-2014 16:15

Re: Read certain part of chat message
 
read_argv(0, command, charsmax(command))
read_argv(1, arg1, charsmax(arg1))
read_argv(2, arg2, charsmax(arg2))

or separate them using parse() or strbreak().

Buckshot 03-29-2014 16:20

Re: Read certain part of chat message
 
Thanks alot! :) I can't believe i didn't know this, it will be VERY useful in alot of plugins.

Buckshot 03-29-2014 16:42

Re: Read certain part of chat message
 
I think i got a problem though. After the second argv, the player is supposed to type a description, which most likely will have spaces. I think will only get the first word of the description with read_argv 2?

An example usage:

/report Player He was cheating!

And the "Player" should be put in my szName variable, and the "He was cheating" in String variable

If i would use read_argv, i would just get the player name, and "He" as the string?.. Or does it take ALL of the text if i don't have a read_argv(3) ?

Black Rose 03-29-2014 16:46

Re: Read certain part of chat message
 
It's normally solved by forcing the user to put the argument inside quotes. Like for example namechange
You can't write
name this is my name
because then it will end up being just "this".
But if you write:
name "this is my name"
you will get the full name.

But again, you could use strbreak() to part args at the first space, which will be between arg1 and arg2.

Buckshot 03-29-2014 16:48

Re: Read certain part of chat message
 
Quote:

Originally Posted by Black Rose (Post 2117611)
It's normally solved by forcing the user to put the argument inside quotes. Like for example namechange
You can't write
name this is my name
because then it will end up being just "this".
But if you write:
name "this is my name"
you will get the full name.

But again, you could use strbreak() to part args at the first space, which will be between arg1 and arg2.

Yeah, thanks. :)

Black Rose 03-29-2014 16:49

Re: Read certain part of chat message
 
You can. Just use read_args() and strbreak(), parse() or strtok() to single out the first argument from the rest.

Buckshot 03-29-2014 17:32

Re: Read certain part of chat message
 
Quote:

Originally Posted by Black Rose (Post 2117614)
You can. Just use read_args() and strbreak(), parse() or strtok() to single out the first argument from the rest.

Hm.. I just tried all this out, but i can't get it to work.. Nothing at all happens, it doesn't even seem to go to the Playerreport function when i type /report bla blablal.. I know why.. It is because "/report blablabla" isn't a command.. But i gotta make it one somehow, so it is a command whatever you type after the /report

Code:

PHP Code:

#include <amxmodx>

new const File[ ] = "addons/amxmodx/reportlog.txt"

public plugin_init( ) {
    
register_plugin"Report Player""1.0""BuckShot" )
    
register_clcmd"say /report""PlayerReport" )
}

public 
PlayerReportid )
{
    new 
FullMsg192 ], Reason161 ], Name32 ], WriteMsg192 ]
    
    
read_argsFullMsg31 )
    
read_argv1Name32 )
    
    new 
len sizeofName )
    
    
strbreakFullMsgNamelenReason160 )
    
formatexWriteMsg192"Player: %s\n Reason: %s"NameReason)
    
write_fileFileWriteMsg )
    



Edit: I gotta register_clcmd("say ", "PlayerReport" ) only, and check if contains /report? that could work

Black Rose 03-29-2014 18:10

Re: Read certain part of chat message
 
Say commands are different.
"say" is the command, everything else is the first argument.
So you have to manually parse that argument into the bits you want.

Code:
#include <amxmodx> public plugin_init( ) {     register_plugin( "Report Player", "1.0", "BuckShot" )     register_clcmd( "say", "PlayerReport" ) } public PlayerReport( id ) {     new FullMsg[ 192 ], Reason[ 161 ], Name[ 32 ], Command[ 32 ]         read_args( FullMsg, charsmax(FullMsg) )     strbreak(FullMsg, Command, charsmax(Command), FullMsg, charsmax(FullMsg))         if ( ! equali(Command, "/report") )         return;         strbreak(FullMsg, Name, charsmax(Name), Reason, charsmax(Reason))         server_print("%s, %s, %s", Command, Name, Reason); }

Buckshot 03-29-2014 18:36

Re: Read certain part of chat message
 
Quote:

Originally Posted by Black Rose (Post 2117653)
Say commands are different.
"say" is the command, everything else is the first argument.
So you have to manually parse that argument into the bits you want.

Code:
#include <amxmodx> public plugin_init( ) {     register_plugin( "Report Player", "1.0", "BuckShot" )     register_clcmd( "say", "PlayerReport" ) } public PlayerReport( id ) {     new FullMsg[ 192 ], Reason[ 161 ], Name[ 32 ], Command[ 32 ]         read_args( FullMsg, charsmax(FullMsg) )     strbreak(FullMsg, Command, charsmax(Command), FullMsg, charsmax(FullMsg))         if ( ! equali(Command, "/report") )         return;         strbreak(FullMsg, Name, charsmax(Name), Reason, charsmax(Reason))         server_print("%s, %s, %s", Command, Name, Reason); }

Thank you :) By the way.. I think the "new len = sizeof( Name )" has to be there, and also len instead of charsmax(Name) i noticed you removed it. Otherwise it is going to strbreak after 32 characters, which is not what we want. All player names are not 32 characters long, so it might put some of the Message in there as well. Also the Command isn't 32 chars long.. Correct me if im wrong


All times are GMT -4. The time now is 05:56.

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