PDA

View Full Version : OnPlayerCommandReceived Question


maximthepain
27/03/2015, 02:45 PM
I'm trying to make that when play uses command when he is injured he wouldn't be able to actually execute them (return 0), however I want him to be able only to execute the /ems, /me, /do commands.

Can you help me on how build this so I can do it on any command i want to?

Here is what I have made so far (This code wont compile & show error) :

public OnPlayerCommandReceived(playerid, cmdtext[])
{
if(GetPVarInt(playerid, "Injured") != 0 || GetPVarInt(playerid, "Cuffed") != 0)
{
if(cmdtext[] != ems)
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands while: injured/recovering/cuffed.");
return 0;
}
}
return 1;
}

Konstantinos
27/03/2015, 02:47 PM
http://wiki.sa-mp.com/wiki/Strcmp to compare strings.

SickAttack
27/03/2015, 02:48 PM
You should do it in a command itself, example:
new bool:pInjured[MAX_PLAYERS] = false;

CMD:test(playerid, params[])
{
if(pInjured[playerid]) return 0;
return 1;
}

maximthepain
27/03/2015, 02:50 PM
http://wiki.sa-mp.com/wiki/Strcmp to compare strings.

I think i'll go with the compering strings method. Thanks.

You should do it in a command itself, example:
new bool:pInjured[MAX_PLAYERS] = false;

CMD:test(playerid, params[])
{
if(pInjured[playerid]) return 0;
return 1;
}

Lets say i have 150 commands, you suggest instead using this on one public, i should do it on all the 150 commands?

SickAttack
27/03/2015, 02:53 PM
I think i'll go with the compering strings method. Thanks.



Lets say i have 150 commands, you suggest instead using this on one public, i should do it on all the 150 commands?

Well, are you going to compare 150 strings everytime a player types a command, and make "zcmd commands into strcmp commands"? And yes, I suggest adding it to the commands, it would be better and somehow more efficient.

Konstantinos
27/03/2015, 02:55 PM
Well, are you going to compare 150 strings everytime a player types a command, and make "zcmd commands into strcmp commands"? And yes, I suggest adding it to the commands.

No need to, his way was good. His only mistake was the way he compared the string. He'll be still using ZCMD.

If the player is injured and the command was performed is not equal to "/ems", then do something.

SickAttack
27/03/2015, 03:01 PM
No need to, his way was good. His only mistake was the way he compared the string. He'll be still using ZCMD.

If the player is injured and the command was performed is not equal to "/ems", then do something.

Oh, he doesn't want anyone that's injured to use commands, except /ems? I thought he wanted to compare those 150 commands (and that he had more) on that callback and return 0 afterwards if a match was found (making strcmp type of commands).

Anyway, it should look like this:
public OnPlayerCommandReceived(playerid, cmdtext[])
{
if(GetPVarInt(playerid, "Injured") != 0 || GetPVarInt(playerid, "Cuffed") != 0)
{
if(strcmp(cmdtext, "ems", true, 3) == 0)
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands while: injured/recovering/cuffed.");
return 0;
}
}
return 1;
}

"cmdtext" returns the command and params too.

Konstantinos
27/03/2015, 03:07 PM
No, he wants injured/cuffed players only to be able to use /ems /me and /do commands. Like the way you did it, but in this case you need to check if the strcmp returns a value not equal to 0.

maximthepain
27/03/2015, 03:08 PM
Oh, he doesn't want anyone that's injured to use commands, except /ems? I thought he wanted to compare those 150 commands (and that he had more) on that callback and return 0 afterwards if a match was found (making strcmp type of commands).

Anyway, it should look like this:
public OnPlayerCommandReceived(playerid, cmdtext[])
{
if(GetPVarInt(playerid, "Injured") != 0 || GetPVarInt(playerid, "Cuffed") != 0)
{
if(strcmp(cmdtext, "ems", true, 3) == 0)
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands while: injured/recovering/cuffed.");
return 0;
}
}
return 1;
}

"cmdtext" returns the command and params too.

public OnPlayerCommandReceived(playerid, cmdtext[])
{
if(GetPVarInt(playerid, "LoggedIn") == 0)
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands if you didn't login.");
return 0;
}
if(GetPVarInt(playerid, "Injured") != 0 || GetPVarInt(playerid, "Dragged") == 1 || GetPVarInt(playerid, "Cuffed") != 0)
{
if(strcmp(cmdtext, "/ems", true, 3) == 0) || strcmp(cmdtext, "/me", true, 2) == 0) || strcmp(cmdtext, "/do", true, 2) == 0) || strcmp(cmdtext, "/giveup", true, 6) == 0) || strcmp(cmdtext, "/revive", true, 6) == 0))
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands while: injured/dragged/cuffed.");
return 0;
}
}
return 1;
}

Okay, i tried do it with strcmp method, however this is the errors:

(9214) : error 029: invalid expression, assumed zero
(9214) : warning 215: expression has no effect
(9214) : error 001: expected token: ";", but found ")"
(9214) : error 029: invalid expression, assumed zero
(9214) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase

4 Errors.


Line 9214:

if(strcmp(cmdtext, "/ems", true, 3) == 0) || strcmp(cmdtext, "/me", true, 2) == 0) || strcmp(cmdtext, "/do", true, 2) == 0) || strcmp(cmdtext, "/giveup", true, 6) == 0) || strcmp(cmdtext, "/revive", true, 6) == 0))

SickAttack
27/03/2015, 03:10 PM
You added too much parentheses and cmdtext doesn't have "/" before the command.

public OnPlayerCommandReceived(playerid, cmdtext[])
{
if(GetPVarInt(playerid, "LoggedIn") == 0)
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands if you didn't login.");
return 0;
}
if(GetPVarInt(playerid, "Injured") != 0 || GetPVarInt(playerid, "Dragged") == 1 || GetPVarInt(playerid, "Cuffed") != 0)
{
if(strcmp(cmdtext, "ems", true, 3) == 0 || strcmp(cmdtext, "me", true, 2) == 0 || strcmp(cmdtext, "do", true, 2) == 0 || strcmp(cmdtext, "giveup", true, 6) == 0 || strcmp(cmdtext, "revive", true, 6) == 0)
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands while: injured/dragged/cuffed.");
return 0;
}
}
return 1;
}

Konstantinos
27/03/2015, 03:13 PM
Change to:
if(strcmp(cmdtext, "/ems", true, 4) || strcmp(cmdtext, "/me", true, 3) || strcmp(cmdtext, "/do", true, 3) || strcmp(cmdtext, "/giveup", true, 7) || strcmp(cmdtext, "/revive", true, 7))

CalvinC
27/03/2015, 03:14 PM
You have too many parenthesises, as marked here with the first part of your code:
if(strcmp(cmdtext, "/ems", true, 3) == 0) || strcmp(cmdtext, "/me", true, 2) == 0) // the rest
As you can see you already ended the if, so you have to remove the parenthesises after the 0's.
Although you can shorten your code by just making it like so:
if(!strcmp(cmdtext, "/ems", true) || !strcmp(cmdtext, "/me", true) /* the rest */ )

maximthepain
27/03/2015, 03:16 PM
You added too much parentheses.

public OnPlayerCommandReceived(playerid, cmdtext[])
{
if(GetPVarInt(playerid, "LoggedIn") == 0)
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands if you didn't login.");
return 0;
}
if(GetPVarInt(playerid, "Injured") != 0 || GetPVarInt(playerid, "Dragged") == 1 || GetPVarInt(playerid, "Cuffed") != 0)
{
if(strcmp(cmdtext, "ems", true, 3) == 0 || strcmp(cmdtext, "me", true, 2) == 0 || strcmp(cmdtext, "do", true, 2) == 0 || strcmp(cmdtext, "giveup", true, 6) == 0 || strcmp(cmdtext, "revive", true, 6) == 0)
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands while: injured/dragged/cuffed.");
return 0;
}
}
return 1;
}

okay this is working, however, quick fix for anyone who will need this in the future.
this is the right way:

public OnPlayerCommandReceived(playerid, cmdtext[])
{
if(GetPVarInt(playerid, "LoggedIn") == 0)
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands if you didn't login.");
return 0;
}
if(GetPVarInt(playerid, "Injured") != 0 || GetPVarInt(playerid, "Dragged") == 1 || GetPVarInt(playerid, "Cuffed") != 0)
{
if(strcmp(cmdtext, "/ems", true, 3) == 0 || strcmp(cmdtext, "/me", true, 2) == 0 || strcmp(cmdtext, "/do", true, 2) == 0 || strcmp(cmdtext, "/giveup", true, 6) == 0 || strcmp(cmdtext, "/revive", true, 6) == 0)
{
return 1;
}
else
{
SendClientMessage(playerid,COLOR_RED, "[SERVER]: You can't use commands while: injured/dragged/cuffed.");
return 0;
}
}
return 1;
}


Thanks for the guys who replied in the thread! honored :) rep++;