[HELP] Command
#1

Why this dont work? O.o

It gives me wrong ID

1. I'll type: "/test 1 Test"
2. Then i'll get this message "Larsey123(0) (You) have tested the code at Larsey123(0) because Test"
That means it dont give me the correct ID, because i typed ID "1" and in the text it give me the id "0"

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
new ID[256], idx, tmp[256];
    ID = strtok(cmdtext, idx);

    new targetid = strval(tmp);
    tmp = strtok(cmdtext, idx);

    new reason[256];
    reason = bigstrtok(cmdtext, idx);

    new pName [MAX_PLAYER_NAME], tName [MAX_PLAYER_NAME], string [128];
    GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
    GetPlayerName(targetid, tName, MAX_PLAYER_NAME);
   
    if (strcmp("/test", ID, true, 10) == 0)
    {
        if(!IsPlayerConnected(targetid)) return SendClientMessage(playerid,COLOR_ERROR,"Player is not connected");
        if(!IsNumeric(tmp)) return SendClientMessage(playerid,COLOR_ERROR,"The ID must be a number");
        if(!strlen(reason)) return SendClientMessage(playerid,COLOR_ERROR,"You must enter the message");
        {
            format (string, sizeof(string), "%s(%d) (You) have tested the code at %s(%d) because %s", pName, playerid, tName, targetid, reason);
            SendClientMessage(playerid, 0xFF000000,string);
            return 1;
        }
    }
    return 0;
}
strtok and bigstrtok:
pawn Код:
strtok(const string[], &index)
{
    new length = strlen(string);
    while ((index < length) && (string[index] <= ' '))
    {
        index++;
    }

    new offset = index;
    new result[20];
    while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
    {
        result[index - offset] = string[index];
        index++;
    }
    result[index - offset] = EOS;
    return result;
}

stock bigstrtok(const string[], &idx)
{
    new length = strlen(string);
    while ((idx < length) && (string[idx] <= ' '))
    {
        idx++;
    }
    new offset = idx;
    new result[128];
    while ((idx < length) && ((idx - offset) < (sizeof(result) - 1)))
    {
        result[idx - offset] = string[idx];
        idx++;
    }
    result[idx - offset] = EOS;
    return result;
}
Reply
#2

Just a little tip:
Use sccanf & dcmd for FAST command processing.
That is much easier to use (your 6 lines with strtok = 1 line with sscanf) and you won't get problems like that.
Reply
#3

Quote:
Originally Posted by *IsBack
Посмотреть сообщение
Just a little tip:
Use sccanf & dcmd for FAST command processing.
That is much easier to use (your 6 lines with strtok = 1 line with sscanf) and you won't get problems like that.
Can you give me an example? (dcmd)

Maybe a Kick command as example?

Thank You
Reply
#4

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    dcmd(kick,4,cmdtext);
    //dcmd([command name],[length of the name],cmdtext);
    return 0;
}

dcmd_kick(playerid, params[])
{
    new id, reason[128];
    if (GetPVarInt(playerid,"AdminLvl") >= 1)
    {
        if (sscanf(params, "us", id, reason))
        {
            UsageMsg(playerid, "USAGE: /Kick <PlayerID/PartOfName> <Reason>");
        }
        else if (id == INVALID_PLAYER_ID) ErrorMsg(playerid, "That player is not found");
        else if(id == playerid) ErrorMsg(playerid,"Use /q");
        else if(GetPVarInt(id,"AdminLvl") > GetPVarInt(playerid,"AdminLvl")) ErrorMsg(playerid,"You cannot use this command on player with higher admin level!");
        else
        {
            ServerMsg(id," ");
            ServerMsg(id," ");
            ServerMsg(id," ");
            SendFMessage(id,cInfo,"You have been kicked by Admin %s. Reason: %s",pName[playerid],reason);
            SendFMessageToAll(cInfo,"%s was kicked by Admin. Reason: %s",pName[id],reason);
            Kick(id);
        }
    }
    else return ErrorMsg(playerid,"You are not authorized to use this command.");
    return 1;
}
Just change "ErrorMsg", "SystemMsg" and "SendFMessage" to your own SendClientMessage functions.. :P
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)