29.07.2011, 13:43
(
Последний раз редактировалось Odyssey; 30.07.2011 в 11:18.
)
How to make simple admin commands!
**This is my first tutorial, please dont hate.Well, just a couple of days ago, I had no idea on how to make a admin command. But, I just learned how to use "Sscanf" by ******, and it all came really simple to me! So, I thought I would show you some basics on how to use it.
This is surely %100 beter than using "Strtok".
So, we will use SA-MP's defualt admin system, called "RCON". Which uses the function IsPlayerAdmin(playerid);.
Lets start of with the basic command, lets do a "Kick" command.
So, we make the start of the command. I dont really need to explain this, most people know how to do it.
pawn Код:
CMD:kick(playerid, params[])
{
pawn Код:
CMD:kick(playerid, params[])
{
new id, reason[128];
if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid, COLOR_WHITE, "You need to be a admin to use that command!");
The "!" in the code is checking if they are NOT a admin, so therefore sending the error message, and returning.
It can be used in all sorts. Here is a quick example.
pawn Код:
CMD:lol(playerid, params)
{
if(!IsPlayerInAnyVehicle(playerid))return SendClientMessage(playerid, COLOR_WHITE, "u need to be in teh car 2 use teh lol command!");
else {
SendClientMessage(playerid, COLOR_WHITE, "LOL! U IZ IN CAR!");
}
return 1;
}
pawn Код:
CMD:kick(playerid, params[])
{
new id, reason[128];
if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid, COLOR_WHITE, "You need to be a admin to use that command!");
else if(sscanf(params, "us", id, reason))SendClientMessage(playerid, COLOR_WHITE, "Usage: /kick [id/name][reason]");
The next most basic thing is sending the message. If the paramters in the command the user entered are false. It will send the "Usage" message, telling them how to use it.
e.g. If I do /kick techkid100, it will send the usage message.
Now, the tricky bit for me to explain.
Now, the "US" is the the type of paramter we are typing.
We can change theese for many things.
U = Playerid/Name
S = String (e.g. Ban reason)
I = Interger (e.g. Set Money, a number)
D = Interger (e.g. Set Money, a number)
F = Float (e.g. Co-ordinate)
Those are the basic paramters which you will MOSTLY use.
pawn Код:
(sscanf(params, "us", id, reason))
Yeh, I did my best at explaining that. Sorry if it sucked. :P
Now, we want to prevent errors, e.g. Kicking yourself, or Kicking a player what is not connected, or kicking another admin, so we will overcome theese.
First we will stop the abilty to kick our self.
pawn Код:
else if(id==playerid)SendClientMessage(playerid,COLOR_WHITE,"Error: You can not kick yourself!");
Now, you will probably know how to make the others.
Not kicking another admin.
pawn Код:
else if(IsPlayerAdmin(id))SendClientMessage(playerid,COLOR_WHITE,"Error: You can not kick another admin!");
And finaly checking if the player is not connected.
pawn Код:
else if (id==INVALID_PLAYER_ID)SendClientMessage(playerid,COLOR_WHITE,"Error: Player is not connected!");
pawn Код:
CMD:kick(playerid, params[])
{
new id, reason[128];
if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid, COLOR_WHITE, "You need to be a admin to use that command!");
else if(sscanf(params, "us", id, reason))SendClientMessage(playerid, COLOR_WHITE, "Usage: /kick [id/name][reason]");
else if(id==playerid)SendClientMessage(playerid,COLOR_WHITE,"Error: You can not kick yourself!");
else if(id==IsPlayerAdmin(id))SendClientMessage(playerid,COLOR_WHITE,"Error: You can not kick another admin!");
else if (id==INVALID_PLAYER_ID)SendClientMessage(playerid,COLOR_WHITE,"Error: Player is not connected!");
pawn Код:
else {
new Name[MAX_PLAYER_NAME], KickMessage[128];
new Name2[MAX_PLAYER_NAME];
We create new variables to store the names in, and the KickMessage.
Next we need to get the names. The kicking admins name, and the person who was kicked name. We do this with GetPlayerName.
pawn Код:
else {
new Name[MAX_PLAYER_NAME], KickMessage[128];
new Name2[MAX_PLAYER_NAME];
GetPlayerName(playerid, Name, sizeof(Name));
GetPlayerName(id, Name2, sizeof(Name2));
We now make the kick message.
pawn Код:
format(KickMessage, sizeof(KickMessage), "%s(%d) has kicked player %s(%d). Reason: %s", Name, playerid, Name2, id);
Next thing to do is Send the message to all players, and kick the player.
pawn Код:
SendClientMessageToAll(COLOR_WHITE, KickMessage);
Kick(id);
pawn Код:
CMD:kick(playerid, params[])
{
new id, reason[128];
if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid, COLOR_WHITE, "You need to be a admin to use that command!");
else if(sscanf(params, "us", id, reason))SendClientMessage(playerid, COLOR_WHITE, "Usage: /kick [id/name][reason]");
else if(id==playerid)SendClientMessage(playerid,COLOR_WHITE,"Error: You can not kick yourself!");
else if(IsPlayerAdmin(id))SendClientMessage(playerid,COLOR_WHITE,"Error: You can not kick another admin!");
else if (id==INVALID_PLAYER_ID)SendClientMessage(playerid,COLOR_WHITE,"Error: Player is not connected!");
else {
new Name[MAX_PLAYER_NAME], KickMessage[128];
new Name2[MAX_PLAYER_NAME];
GetPlayerName(playerid, Name, sizeof(Name));
GetPlayerName(id, Name2, sizeof(Name2));
format(KickMessage, sizeof(KickMessage), "%s(%d) has kicked player %s(%d). Reason: %s", Name, playerid, Name2, id);
SendClientMessageToAll(COLOR_WHITE, KickMessage);
Kick(id);
}
return 1;
}
Ban - with Ban();
Freeze - With TogglePlayerControlable
Get and Goto - with GetPlayerPos and SetPlayerPos
And many more!
I hope I did my best as it is my first tutorial.
-TechKid100