Now, here's a note for all of you. When you're doing ZCMD commands, do not, I mean never place the commands in any call backs, like in OnPlayerCommandText especially.
Here are the links to downloading the includes.
- sscanf2
- zcmd
You will also need to define some colors.
Here is defining the color 'COLOR_LIGHTBLUE'.
pawn Code:
#define COLOR_LIGHTBLUE 0x33CCFFAA
Now, let's start with the commands.
The structure in starting an command is this, there are several ways into doing this. But I will just be only explaining the latest structure that is formed.
pawn Code:
CMD:commandhere(playerid, params[])
{
return 1;
}
So here is this code, the command starter. The structure to start a command.
The params[] is the parameters string, playerid is an ID of the player who sends the command.
pawn Code:
CMD:commandhere(playerid, params[]) //Structure to begin an command.
We also will need a stock, called GetName.
This defines the name, it will get the playerid's name. And will replace the '_' underscore into nothing so FirstName LastName. Which is mostly useful for RolePlay servers.
pawn Code:
stock GetName(playerid)
{
new
name[24];
GetPlayerName(playerid, name, sizeof(name));
strreplace(name, '_', ' ');
return name;
}
Creating the command
Now, the command we're doing today is the /givemoney command which was said in the Introduction section.
Here is the code, and I will explain the whole code after this.
pawn Code:
CMD:givemoney(playerid, params[])
{
if(IsPlayerAdmin(playerid))
{
new string[128], targetid, money;
if(sscanf(params, "ud", targetid, money)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /givemoney [playerid] [money]");
if(IsPlayerConnected(targetid))
{
GivePlayerMoney(targetid, money);
format(string, sizeof(string), "* You have set %s's cash to an amount of $%d.",GetName(targetid),money);
SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
}
}
else
{
SendClientMessage(playerid, -1, "You are not authorized to use that command!");
}
return 1;
}
Now, this was already explained above the code. But the command is 'givemoney'.
pawn Code:
CMD:givemoney(playerid, params[])
Next is this code, this code is checking if the player who sent the command is logged in RCON admin, '/rcon login [your pass]', that type of thing.
pawn Code:
if(IsPlayerAdmin(playerid))
Now, this code is defining a string, the target who sent the command and the amount of money.
pawn Code:
new string[128], targetid, money;
This is the part where we use sscanf, now. The 'if(sscanf(params' is the part which is also like 'isnull'. The "ud", these are the specifiers. 'u' is the playerid that the person who sent the command places the playerid who he/she wants to give the money to, the 'd' is the integer, which is for the money where the person who sends the command to give a certain amount to the player that he/she wants to. 'return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /givemoney [playerid] [money]");' Is showing if they typed in a wrong parameter, it will guide them to showing them how to use the command correctly, just like 'isnull'. It will send the person a message, in white and saying the usage on how to use the command.
pawn Code:
if(sscanf(params, "ud", targetid, money)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /givemoney [playerid] [money]");'
Here is a list of the specifiers.
Specifiers from Y_Less.
Code:
Specifier(s) Name Example values
i, d Integer 1, 42, -10
c Character a, o, *
l Logical true, false
b Binary 01001, 0b1100
h, x Hex 1A, 0x23
o Octal 045 12
n Number 42, 0b010, 0xAC, 045
f Float 0.7, -99.5
g IEEE Float 0.7, -99.5, INFINITY, -INFINITY, NAN, NAN_E
u User name/id (bots and players) Y_Less, 0
q Bot name/id ShopBot, 27
r Player name/id Y_Less, 42
Here is the code where it checks if the ID who the player sent the command to give the money to that if he/she is connected to the server.
pawn Code:
if(IsPlayerConnected(targetid))
This code is giving the target who the player wants to the amount of money that the player executed in the command.
pawn Code:
GivePlayerMoney(targetid, money);
Now, this part is where we're using our string defined at the top of the command.
We're formatting the string, saying '* You have set %s's cash to an amount of $%d.' Now, the %s is the player's name, that person who executed the command that he wants to send the cash to. Now the %d is the integer, where the amount of money that the person who sended the certain amount of cash. After that, it will show 'GetName', this get's the name of the person who is receiving the cash. The money is the amount of money where the person who sends the amount of cash to, to the person he/she wants.
pawn Code:
format(string, sizeof(string), "* You have set %s's cash to an amount of $%d.",GetName(targetid),money);
This is sending the message to the player who executed the commands in the COLOR_LIGHTBLUE color, and sending the string that I explained above.
pawn Code:
SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
This part it where it's connected to the 'IsPlayerAdmin(playerid))'. So if the player's not an RCON admin/logged in.
It will send the player a message in white which is '-1' by the way, saying 'You are not authorized to use that command!'
pawn Code:
else
{
SendClientMessage(playerid, -1, "You are not authorized to use that command!");
}
Conclusion
And that's it for today in my tutorial on creating the /givemoney command ONLY for logged in RCON admins.
Credits to...
- Y_Less for creating such a wonderful sscanf include.
- Zeex for creating the wonderful ZCMD include.
- And me for creating this tutorial.