[Tutorial] ZCMD: How to make 'forcecmd' command.
#1


Hello there,
I'm making this tutorial because three days ago I was looking for exactly the same command but I found nothing.
Now, finally I made this command and I want so share it to all of you. It is very simple, only 11 lines, we are forcing only commands made with ZCMD. I think you will like it. Lets do it.. And If I have any mistakes, please tell me to fix them. Enjoy

Requirements:
1. ZCMD by ZeeX.
2. SSCANF by Y_Less.

The tutorial...

Line 1: - Creating the command.
Code:
CMD:forcecmd(playerid, params[]) {
The 'CMD' prefix before 'forcecmd' is telling the include 'zcmd' to create new command.
'forcecmd' is the new command.
'playerid' in the brackets is the player who is using this command.
'params[]' is string, this will store the parameters after the command.

Line2: - creating variables...
Code:
new TargetID, Command[34], Params[129], string[50];
Now, 'new' is telling the script to make new variables separating them with comma.
'TargetID' We will use this to store the player's id of the player you want.
'Command[34]' There will be stored the command you want to use onto 'TargetID'. Max lenght 34 characters.
'Params[129]' There will be stored the parameters of the command you want to use onto a player.
'string[50]' There will be stored the formated command that is using from 'zcmd' include.

Line 3: - Checking if the player who is using 'forcecmd' is admin (RCON).
Code:
if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1 , "{E03636}[ERROR]: {FFFFFF}You are not authorized to use this command!");
If the player who uses the command is not an admin will receive a message - [ERROR]: You are not authorized to use this command!

Line 4: - sscanf part
Code:
if(sscanf(params, "us[32]S()[128]", TargetID, Command, Params)) return SendClientMessage(playerid, -1, "{36E0B9}[USAGE]: {FFFFFF}/forcecmd <PlayerID/PartOfName> <Command> <Params>");
As I know 'sscanf' splits parameters by separating them by space. And the player who is using this command 'forcecmd' will recieve another message - [USAGE]: /forcecmd <PlayerID/PartOfName> <Command> <Params> ofcourse the player will recieve it when the params are not defined correctly

Line 5: Checking ids
Code:
if(playerid == TargetID) return SendClientMessage(playerid, -1, "{E0AE36}[WARNING]: {FFFFFF}You cannot use this command on yourself.");
Checking if the player who is using the command 'forcecmd' is trying to force command to him/herself. And will receive message. [WARNING]: You cannot use this command on yourself.

Line 6: - Checking for TargetID if is connected.
Code:
if(!IsPlayerConnected(TargetID)) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This player is not connected.");
If the target is not connected the player who is using the command 'forcecmd' will recieve another message [ERROR]: This player is not connected.

Line 7: - Some details.
Code:
if(Command[0] == '/') strdel(Command, 0, 1);
Checking if the first character of the 'Command' for force is a slash then it will automatically remove it. This is needed for calling the command.

Line 8: - Formating the command.
Code:
format(string, sizeof(string), "cmd_%s", Command);
Formating the command which will be used for calling.

Line 9: - If the 'Command' is unknown for the server.
Code:
if(!CallLocalFunction(string, "ds", TargetID, "\1")) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This command is unknown.");
If we call for example the command 'pm' for one player and if the command is not existing it will send a message to the player who is using the command 'forcecmd' - [ERROR]: This command is unknown.

Line 10: Return value.
Code:
return 1;
Setting the retirn value to true

Line 11: The closing bracket.
Code:
}
Closing bracked - the end of the command.

And now, the final result should be:
Code:
CMD:forcecmd(playerid, params[]) {
	new TargetID, Command[34], Params[129], string[50];
	if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1 , "{E03636}[ERROR]: {FFFFFF}You are not authorized to use this command!");
	if(sscanf(params, "us[32]S()[128]", TargetID, Command, Params)) return SendClientMessage(playerid, -1, "{36E0B9}[USAGE]: {FFFFFF}/forcecmd <PlayerID/PartOfName> <Command> <Params>");
	if(playerid == TargetID) return SendClientMessage(playerid, -1, "{E0AE36}[WARNING]: {FFFFFF}You cannot use this command on yourself.");
	if(!IsPlayerConnected(TargetID)) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This player is not in this server.");
	if(Command[0] == '/') strdel(Command, 0, 1);
	format(string, sizeof(string), "cmd_%s", Command);
	if(!CallLocalFunction(string, "ds", TargetID, isnull(Params) ? ("\1") : Params)) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This command is unknown.");
}
Ask if you have any questions. I thing I don't have mistakes but if I have exuse me.
Reply
#2

Doesn't the command get executed twice?

pawn Code:
if(!CallLocalFunction(string, "ds", TargetID, "\1")) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This command is unknown.");
if(!isnull(Params)) return CallRemoteFunction(string, "ds", TargetID, Params);
else return CallRemoteFunction(string, "ds", TargetID, "\1");
You could do it like this:

pawn Code:
if(!isnull(Params))
{
    if (!CallRemoteFunction(string, "ds", TargetID, Params)) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This command is unknown.");
}
else if (!CallRemoteFunction(string, "ds", TargetID, "\1")) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This command is unknown.");
Otherwise nice work.

Edit:

pawn Code:
if (!CallRemoteFunction(string, "ds", TargetID, isnull(Params) ? ("\1") : Params)) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This command is unknown.");
Reply
#3

Seems a legit tutorial although this guy have found some errors maybe but still good job and it might be useful in the future I guess.

Quote:
Originally Posted by kvann
View Post
Doesn't the command get executed twice?

pawn Code:
if(!CallLocalFunction(string, "ds", TargetID, "\1")) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This command is unknown.");
if(!isnull(Params)) return CallRemoteFunction(string, "ds", TargetID, Params);
else return CallRemoteFunction(string, "ds", TargetID, "\1");
You could do it like this:

pawn Code:
if(!isnull(Params))
{
    if (!CallRemoteFunction(string, "ds", TargetID, Params)) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This command is unknown.");
}
else if (!CallRemoteFunction(string, "ds", TargetID, "\1")) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}This command is unknown.");
Otherwise nice work.
Reply
#4

Quote:
Originally Posted by kvann
View Post
Doesn't the command get executed twice?
I don't think so.
Quote:
Originally Posted by kvann
View Post
Otherwise nice work.
Thank you!
Reply
#5

CallLocal/RemoteFunction gets executed twice. I added a really short way to do it to my previous post.
Reply
#6

@kvann Thank you. Fixed
Reply
#7

oi man you made me happy i thought [code] got its colors again
Reply
#8

This is more optimized :
PHP Code:
CMD:forcecmd(playeridparams[]) 
{
    new 
        
TargetID
        
Command[32], 
        
Params[129];
        
    if(!
IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -"{E03636}[ERROR]: {FFFFFF}You are not authorized to use this command!");
    if(
sscanf(params"us[32]S()[128]"TargetIDCommandParams)) return SendClientMessage(playerid, -1"{36E0B9}[USAGE]: {FFFFFF}/forcecmd <PlayerID/PartOfName> <Command> <Params>");
    if(
playerid == TargetID) return SendClientMessage(playerid, -1"{E0AE36}[WARNING]: {FFFFFF}You cannot use this command on yourself.");
    if(!
IsPlayerConnected(TargetID)) return SendClientMessage(playerid, -1"{E03636}[ERROR]: {FFFFFF}This player is not in this server.");
    
    new
        
string[36] = "cmd_";
    
    
strcat(stringCommand[1]);
        
    if(!
CallLocalFunction(string"ds"TargetIDisnull(Params) ? ("\1") : Params)) return SendClientMessage(playerid, -1"{E03636}[ERROR]: {FFFFFF}This command is unknown.");

Reply
#9

Nice and useful tutorial.
I never didn't think for something like that
Reply
#10

aint this?
pawn Code:
CMD:forceradio (playerid, params[])
{
    new str[128], id, input;
    if(sscanf(params, "ui", id,input)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /forceradio [ID][1,2,3]");
    if(!IsPlayerConnected(id))  return SendClientMessage(playerid, COLOR_RED, "Player is not connected!");
    if(playerid == id)return SendClientMessage(playerid, COLOR_NORMALBLUE, "You forced yourself to to play a radio");
    SendClientMessage(playerid,-1,"You forced a player to play a radio!");
    SendClientMessage(id,-1,"You were forced by an admin to play a radio!");
    cmd_audiomsg(id,input);
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)