I need a reason
#1

pawn Код:
if(strcmp("/kick", cmdtext, true, 5) == 0)
    {
            if(IsPlayerAdmin(playerid) return SendClientMessage(playerid, 0xFF0000AA, "RCON needed.");
            if(!strlen(cmdtext[6])) return SendClientMessage(playerid, 0xFF0000AA, "Use: /kick [playerid]");
            new gp = strval(cmdtext[6]);
            if(!IsPlayerConnected(gp)) return SendClientMessage(playerid, 0xFF0000AA, "Invalid Playerid");
            new pn[24], an[24], str[70];
            GetPlayerName(playerid, an, 24); GetPlayerName(gp, pn, 24);
            format(str, sizeof(str), "%s kicked by admin %s", pn, an);
            SendClientMessageToAll(0xA9A9A9AA, str);
            Kick(gp);
            return 1;
    }
can someone take that & make it /kick id reason? and make it so admin cannot submit cmd without reason?
thx, i need two learn off this example
Reply
#2

pawn Код:
if(strcmp("/kick", cmdtext, true, 5) == 0)
    {
            if(IsPlayerAdmin(playerid) return SendClientMessage(playerid, 0xFF0000AA, "RCON needed.");
            if(!strlen(cmdtext[6])) return SendClientMessage(playerid, 0xFF0000AA, "Use: /kick [playerid][reason]");
            new gp = strval(cmdtext[6]);
            if(!IsPlayerConnected(gp)) return SendClientMessage(playerid, 0xFF0000AA, "Invalid Playerid");
            new pn[24], an[24], str[70], reason;
            GetPlayerName(playerid, an, 24); GetPlayerName(gp, pn, 24);
            format(str, sizeof(str), "%s kicked by admin %s", pn, an, reason);
            SendClientMessageToAll(0xA9A9A9AA, str);
            Kick(gp);
            return 1;
    }
Try that
Reply
#3

you added

pawn Код:
if(!strlen(cmdtext[6])) return SendClientMessage(playerid, 0xFF0000AA, "Use: /kick [playerid][reason]");
i need the reason to actually work and make it so admin cannot submit the cmd without including a reason
Reply
#4

Quote:
Originally Posted by CSMajor
Посмотреть сообщение
you added

pawn Код:
if(!strlen(cmdtext[6])) return SendClientMessage(playerid, 0xFF0000AA, "Use: /kick [playerid][reason]");
i need the reason to actually work and make it so admin cannot submit the cmd without including a reason
he forgot to add:
"%s" at the string
this will help
Код:
format(str, sizeof(str), "%s kicked by admin %s[Reason: %s]", pn, an, reason);
and reason is a string itself ( reason[156]; )
oh and:
Код:
reason = bigstrtok(cmdtext, idx);
Reply
#5

Oh my bad, I did the correction in a hurray :/
Reply
#6

Introducing the String Tokenise method!

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;
}
Put that badboy in your script and you will be able to use it in your /kick command as such:

pawn Код:
new cmd[128]; new idx;
cmd = strtok(cmdtext, idx);
    if(strcmp("/kick", cmd, true) == 0)
    {
        if(IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFF0000AA, "RCON needed.");
        idx++; cmd = strtok(cmdtext, idx);
        if(!strlen(cmd)) return SendClientMessage(playerid, 0xFF0000AA, "Use: /kick [PLAYERID] [REASON]");
        new gp = strval(cmd);
        if(!IsPlayerConnected(gp)) return SendClientMessage(playerid, 0xFF0000AA, "Invalid Playerid");
        idx++; cmd = strtok(cmdtext, idx);
        if(!strlen(cmd)) return SendClientMessage(playerid, 0xFF0000AA, "Use: /kick [playerid] [REASON]");
        new reason[128];
        while(strlen(cmd)) {
            strcat(reason, cmd, sizeof(reason));
            strcat(reason, " ", sizeof (reason));
            idx++; cmd = strtok(cmdtext, idx);
        }
        strdel(reason, strlen(reason)-1, strlen(reason));
        new pn[24], an[24], str[70];
        GetPlayerName(playerid, an, 24); GetPlayerName(gp, pn, 24);
        format(str, sizeof(str), "%s kicked by admin %s (reason: %s)", pn, an, reason);
        SendClientMessageToAll(0xA9A9A9AA, str);
        Kick(gp);
        return 1;
    }
The strtok method is commonly used along with strcmp. For an explanation on how it method works, visit the wiki page here: https://sampwiki.blast.hk/wiki/Strtok
Reply
#7

Benjo's way makes my compiler not respond, so i went

pawn Код:
if(strcmp("/kick", cmdtext, true, 5) == 0)
    {
            if(IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFF0000AA, "RCON needed.");
            if(!strlen(cmdtext[6])) return SendClientMessage(playerid, 0xFF0000AA, "Use: /kick [playerid][reason]");
            new gp = strval(cmdtext[6]);
            if(!IsPlayerConnected(gp)) return SendClientMessage(playerid, 0xFF0000AA, "Invalid Playerid");
            new pn[24], an[24], str[70], reason[156];
            reason = bigstrtok(cmdtext, idx);
            GetPlayerName(playerid, an, 24); GetPlayerName(gp, pn, 24);
            format(str, sizeof(str), "%s kicked by admin %s[Reason: %s]", pn, an, reason);
            SendClientMessageToAll(0xA9A9A9AA, str);
            Kick(gp);
            return 1;
    }
but got

Код:
C:\Users\Admin\Desktop\Pawn\gamemodes\new_rp.pwn(503) : warning 219: local variable "str" shadows a variable at a preceding level
C:\Users\Admin\Desktop\Pawn\gamemodes\new_rp.pwn(504) : error 017: undefined symbol "bigstrtok"
C:\Users\Admin\Desktop\Pawn\gamemodes\new_rp.pwn(504) : error 033: array must be indexed (variable "reason")
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


2 Errors.
0.0...
Reply
#8

Correction of Benjo's script
pawn Код:
if(!strcmp("/kick", cmdtext, true, 5))
    {
            if(IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFF0000AA, "RCON needed.");
            if(!strlen(cmdtext[6])) return SendClientMessage(playerid, 0xFF0000AA, "Use: /kick [playerid][reason]");
            new gp = strval(cmdtext[6]);
            if(!IsPlayerConnected(gp)) return SendClientMessage(playerid, 0xFF0000AA, "Invalid Playerid");
            new pn[24], an[24], str2[70], reason[128];
            reason = strtok(cmdtext, idx);
            GetPlayerName(playerid, an, 24); GetPlayerName(gp, pn, 24);
            format(str2, sizeof(str2), "%s kicked by admin %s[Reason: %s]", pn, an, reason);
            SendClientMessageToAll(0xA9A9A9AA, str2);
            Kick(gp);
            return 1;
    }
Reply
#9

Why don't you use ZCMD, and Sccanf? Must easier, faster and more efficient coding.
Reply
#10

Agree. Use ZCMD + sscanf.

pawn Код:
CMD:kick(playerid, params[])
{
    new PID, Str[70], PlayerName[MAX_PLAYER_NAME], PIDName[MAX_PLAYER_NAME];

    if(sscanf(params, "us[50]", PID, params)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /Kick < Player ID > < Reason >");

    if(!IsPlayerConnected(PID)) return SendClientMessage(playerid, COLOR_RED, "That player is not connected!");
   
    GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
    GetPlayerName(PID, PIDName, sizeof(PIDName));

    format(Str, sizeof(Str), "Admin: %s(%d) has kicked: %s(%d) for: %s", PlayerName, playerid, PIDName, PID, params);
    SendClientMessageToAll(COLOR_GREY, Str);

    return Kick(PID);
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)