[Include] [INC] zcmd 0.3.1 | Fast & Simple Command Processor (updated 30/10/2009)
#1

Description

This is just a little include that uses OnPlayerCommandText() to process players' commands. Each command has a separate function like in dcmd, but zcmd calls them directly via CallLocalFunction(). Such method is much faster than when you successively compare the text player entered to each command you have in your script (especially if he send a nonexistent cmd, you pass though all then) and its superiority over the old way is proportional to the number of commands. I did a speed test when I've just statrted thinking about this approach, you can find its results here.


Usage

All you need to add a command is just make a public function using special pre-defined macro, like this:

pawn Code:
COMMAND:mycommand(playerid, params[]) // or CMD:mycommand(playerid, params[])
{
  // Do something
  return 1;
}
or (old style):

pawn Code:
command(mycommand, playerid, params[]) // or cmd(mycommand, playerid, params[])
{
  // Do something
  return 1;
}
Here params[] is the parameters string, playerid is an ID of the player who send this command.
That's all! Very easy, isn't it?


Important: Since v0.3 OnPlayerCommandText cannot be used anymore (also ZCMD_NO_CALLBACK option has been removed), but there are two new callbacks instead:

pawn Code:
OnPlayerCommandReceived(playerid, cmdtext[])
This one is called when someone sends a command. If you return 0 here, the command won't be performed.

pawn Code:
OnPlayerCommandPerformed(playerid, cmdtext[], success)
And this one gets called after command execution, here if you do "return 0" the player will see standard "Unknown command" message. The "success" parameter is equal to value returned by command function returns (if it doesn't exist success will be 0).

Note that it's not necessary to add these callbacks to your script if you don't use them.


How to make two different commands doing the same thing

For example, you have /something cmd:

pawn Code:
COMMAND:something(playerid, params[])
{
  // some stuff here
  return 1;
}
and you want to create another one such as /another that does what /something does. The simpliest way of doing that is:

pawn Code:
COMMAND:another(playerid, params[])
{
  return cmd_something(playerid, params);
}

Note #1: If you want to use zcmd in a filterscript, put this define before including:

pawn Code:
#define FILTERSCRIPT
Note #2: If you want to check whether parameters string is empty you should not do it like:

pawn Code:
if (!strlen(params))
{
  // no parameters
}
or :

pawn Code:
if (!params[0])
because its length is never null (read more here), simply use isnull() included into zcmd:

pawn Code:
if (isnull(params))
Actually, if you use sscanf you don't need to do this as it has built-in isnull checking.

Here is an example of how you can make /givemoney command using zcmd with sscanf:

pawn Code:
COMMAND:givemoney(playerid, params[])
{
    if (IsPlayerAdmin(playerid))
    {
        new
          toplayerid, // the player we want to give money to
          amount;
        // extracting player's ID and amount from params
        if (!sscanf(params, "ii", toplayerid, amount))
        {
          if (toplayerid != INVALID_PLAYER_ID)
          {
            new
              message[40];
            GivePlayerMoney(toplayerid, amount);
            format(message, sizeof(message), "You got $%d from admin!", amount);
            SendClientMessage(toplayerid, 0x00FF00FF, message);
          }
          else SendClientMessage(playerid, 0xFF0000FF, "That player is not connected");
        }
        else SendClientMessage(playerid, 0xFFFFFFFF, "Usage: /givemoney <playerid> <amount>");
    }
    else SendClientMessage(playerid, 0xFF0000FF, "Only admins can use this command!");
    return 1;
}
Download

Special thanks to:
- ******
Reply
#2

nice work, this good for newbie pawners
Reply
#3

Great job
Reply
#4

Thanks
Reply
#5

Good for new scripters
Reply
#6

This is some kind,similar to dcmd,isn't it?!
Anyway,nice work.

Ecko
Reply
#7

Updated! Current version is 0.2

Quote:
Originally Posted by .::: [E
Ecko :::. ]
This is some kind,similar to dcmd,isn't it?!
Maybe they are similar, but zcmd works much faster.
Reply
#8

nvm I worked it out.
Reply
#9

Sorry for double post, I have a new problem. This is my code:

pawn Code:
zcmd(setrank, playerid, params[])
{
  new string[128];
    new idx;
    new tmp[256];
    new sendername[MAX_PLAYER_NAME];
    new giveplayername[MAX_PLAYER_NAME];
   
  if (AccountInfo[playerid][AdminLevel] >= 3 || IsPlayerAdmin(playerid))
    {
        tmp = strtok(params, idx);
        if(isnull(tmp))
        {
            SendClientMessage(playerid, COLOR_WHITE, "USAGE: /giverank [playername/id] [rank 1-8]");
            SendClientMessage(playerid, COLOR_WHITE, "FUNCTION: Will give selected player selective rank.");
            return 1;
        }
        new giveplayerid = ReturnUser(tmp, playerid);
        new rank = strval(tmp);
        if(giveplayerid != INVALID_PLAYER_ID)
        {
          if ((rank >= 1) && (rank <= 8))
          {
                GetPlayerName(giveplayerid, giveplayername, sizeof(giveplayername));
                GetPlayerName(playerid, sendername, sizeof(sendername));
                printf("ADMIN: %s gave %s rank %s.", sendername, giveplayername, RankNames[rank-1]);
                format(string, sizeof(string), "Admin %s has set your rank to %s.", sendername, RankNames[rank-1]);
                SendClientMessage(giveplayerid, COLOR_LIGHTBLUE, string);
                format(string, sizeof(string), "ADMIN: Administrator %s has given %s rank %s.", sendername,giveplayername,RankNames[rank-1]);
                SendClientMessageToAll(COLOR_LIGHTRED, string);
                AccountInfo[giveplayerid][Rank] = rank-1;
                format(string, sizeof(string), "You have given %s rank %s.", giveplayername,RankNames[AccountInfo[giveplayerid][Rank]]);
                SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
            }
            else return SendClientMessage(playerid, COLOR_RED, "Invalid Rank!");
        }
    }
    else SendClientMessage(playerid, COLOR_GREY, "You are not authorized to use that command!");
    return 1;
}
It compiles fine and all, but when I use the command it always returns rank as 0. If I type /setrank 0 5 it would say "Invalid Rank".
Reply
#10

Quote:
Originally Posted by ZeeX
I think you placed first else statement where "Invalid rank" message is sent not to where it should be and also strtok should be called for rank too.
Try this:
pawn Code:
zcmd(setrank, playerid, params[])
{
  new string[128];
    new idx;
    new tmp[256];
    new sendername[MAX_PLAYER_NAME];
    new giveplayername[MAX_PLAYER_NAME];

  if (AccountInfo[playerid][AdminLevel] >= 3 || IsPlayerAdmin(playerid))
    {
        tmp = strtok(params, idx);
        if(isnull(tmp))
        {
            SendClientMessage(playerid, COLOR_WHITE, "USAGE: /giverank [playername/id] [rank 1-8]");
            SendClientMessage(playerid, COLOR_WHITE, "FUNCTION: Will give selected player selective rank.");
            return 1;
        }
        new giveplayerid = ReturnUser(tmp, playerid);
        new rank = strval(tmp);
        if(giveplayerid != INVALID_PLAYER_ID)
        {
          if ((rank >= 1) && (rank <= 8))
          {
                GetPlayerName(giveplayerid, giveplayername, sizeof(giveplayername));
                GetPlayerName(playerid, sendername, sizeof(sendername));
                printf("ADMIN: %s gave %s rank %s.", sendername, giveplayername, RankNames[rank-1]);
                format(string, sizeof(string), "Admin %s has set your rank to %s.", sendername, RankNames[rank-1]);
                SendClientMessage(giveplayerid, COLOR_LIGHTBLUE, string);
                format(string, sizeof(string), "ADMIN: Administrator %s has given %s rank %s.", sendername,giveplayername,RankNames[rank-1]);
                SendClientMessageToAll(COLOR_LIGHTRED, string);
                AccountInfo[giveplayerid][Rank] = rank-1;
                format(string, sizeof(string), "You have given %s rank %s.", giveplayername,RankNames[AccountInfo[giveplayerid][Rank]]);
                SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
            }
        }
        else return SendClientMessage(playerid, COLOR_RED, "Invalid Rank!");
    }
    else SendClientMessage(playerid, COLOR_GREY, "You are not authorized to use that command!");
    return 1;
}
#else
That would say "Invalid Rank" if I tried to set the rank of a person that isn't connected to the server.
Reply
#11

pawn Code:
zcmd(setrank, playerid, params[])
{
  if (AccountInfo[playerid][AdminLevel] >= 3 || IsPlayerAdmin(playerid))
  {
     new tmp[256], idx;
     
     if (isnull(params) || isnull((tmp = strtok(params, idx))))
     {
        SendClientMessage(playerid, COLOR_WHITE, "USAGE: /giverank [playername/id] [rank 1-8]");
        SendClientMessage(playerid, COLOR_WHITE, "FUNCTION: Will give selected player selective rank.");
        return 1;
     }
       
     new giveplayerid = ReturnUser(tmp, playerid);
     new rank = strval(strtok(params, idx));
       
     if (giveplayerid != INVALID_PLAYER_ID)
     {
       if ((rank >= 1) && (rank <= 8))
       {
         new sendername[MAX_PLAYER_NAME];
         new giveplayername[MAX_PLAYER_NAME];
         
         GetPlayerName(giveplayerid, giveplayername, sizeof(giveplayername));
         GetPlayerName(playerid, sendername, sizeof(sendername));
         printf("ADMIN: %s gave %s rank %s.", sendername, giveplayername, RankNames[rank-1]);
         format(tmp, sizeof(tmp), "Admin %s has set your rank to %s.", sendername, RankNames[rank-1]);
         SendClientMessage(giveplayerid, COLOR_LIGHTBLUE, tmp);
         format(tmp, sizeof(tmp), "ADMIN: Administrator %s has given %s rank %s.", sendername,giveplayername,RankNames[rank-1]);
         SendClientMessageToAll(COLOR_LIGHTRED, tmp);
         AccountInfo[giveplayerid][Rank] = rank-1;
         format(tmp, sizeof(tmp), "You have given %s rank %s.", giveplayername,RankNames[AccountInfo[giveplayerid][Rank]]);
         SendClientMessage(playerid, COLOR_LIGHTBLUE, tmp);
      }
      else return SendClientMessage(playerid, COLOR_RED, "Invalid Rank!");
     }
     else return SendClientMessage(playerid, COLOR_RED, "Invalid Player ID!");
  }
  else SendClientMessage(playerid, COLOR_GREY, "You are not authorized to use that command!");
  return 1;
}
Reply
#12

The command doesn't work at all now, it just get "Unknown Command"
Reply
#13

hm... replace
pawn Code:
if (isnull(params) || isnull((tmp = strtok(params, idx))))
with
pawn Code:
tmp = strtok(params, idx);
if (isnull(tmp))
It should work.
Reply
#14

Thanks it works now.
Reply
#15

i do recommend u use dcmd instead its all out easier and more efficient for this type of thing although zcmd is good too
Reply
#16

Except for the parameter thingy, this is really nice. Good job.
Reply
#17

I WILL use this definitely.

Ecko
Reply
#18

Nice release, ZeeX. Going to use this, bro.
Reply
#19

I really recommend you to use "zcmd" combined with "sscanf" function to check params; because strtok is really pieace of shit
Reply
#20

Quote:
Originally Posted by [MeTalgEaR
]
i do recommend u use dcmd instead its all out easier and more efficient for this type of thing although zcmd is good too
Err... no? The main reason to switch from dcmd to zcmd is that zcmd is MORE efficient than dcmd in a large percentage of cases.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)