Commands are being skipped.
#1

I've been trying to write some commands and see how it goes as its been a while so I opened a new PAWNO file and started working on it, I wrote four commands and went in game to test them

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    new giveplayerid;
	new cmd[128], idx;
	if(strcmp(cmd, "/kick", true) == 0)
	{
		new kicker[128];
		new tmp[128];
		tmp = strtok(cmdtext, idx);
		{
  			if(!strlen(tmp))
			{
				SendClientMessage(playerid, 0xFFFFFFFF, "Use /kick [playerid]");
				return 1;
			}
			giveplayerid = ReturnUser(tmp);
			if(IsPlayerConnected(giveplayerid))
			{
			format(kicker, sizeof(kicker), "You have kicked Player ID : %d from the server.", giveplayerid);
			SendClientMessage(playerid, 0xFFFFFFFF, kicker);
			Kick(strval(tmp));
			return 1;
			}
		}
	}
 	if(strcmp(cmd, "/ban", true) == 0)
	{
	    new tmp[128];
	    tmp = strtok(cmdtext, idx);
		{
		    if(!strlen(tmp))
			{
				SendClientMessage(playerid, 0xFFFFFFFF, "Use /ban [playerid]");
				return 1;
			}
		    Ban(strval(tmp));
		    return 1;
	  }
	}
	if(strcmp(cmd, "/afk", true) == 0)
	{
		GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
		strins(pName, "[AFK]", 0);
		SetPlayerName(playerid, pName);
		return 1;
	}
	if(strcmp(cmd, "/giveplayergun", true) == 0)
	{
	    new tmp[128];
	    tmp = strtok(cmdtext, idx);
    	if(!strlen(tmp))
			{
				SendClientMessage(playerid, 0xFFFFFFFF, "Use /giveplayergun [playerid] [gun-id]");
				return 1;
			}
	    new gunidstring[128];
		new gunget;
		new gun;
		gunget = ReturnUser(tmp);
		gun = strvalEx(tmp);
		tmp = strtok(cmdtext, idx);
		if(strlen(tmp) == 0) return SendClientMessage(playerid, 0xFFFFFFFF, "You know the guns.");
		if(IsPlayerConnected(gunget))
		{
		GivePlayerWeapon(gunget, gun, 99999);
		format(gunidstring, sizeof(gunidstring), "Admin %s has give you the weapon ID : %d", playerid, gun);
		SendClientMessage(gunget, 0xFFFFFFFF, gunidstring);
		}
		else    { SendClientMessage(playerid, 0xFFFFFFFF, "That player is offline!"); }
		return 1;
	}
	return 0;
}
However, every time I try to type any command, it would just return this message "/ban playerid", doesnt matter what I write, that message would appear unless I type /ban "id" then I would lose connection.

Its been so long and I cant really figure out the problem, help would be greatly appreciated, thanks!
Reply
#2

i dont code in pawno anymore, but something is very wrong here.

Код:
new cmd[128], idx;
if(strcmp(cmd, "/kick", true) == 0)
you're comparing nothing...

Код:
if (strcmp(cmdtext, "/kick", true) == 0) // u shoulda been comparing to cmdtext
{
   
}
Reply
#3

Quote:
Originally Posted by Trucido
Посмотреть сообщение
i dont code in pawno anymore, but something is very wrong here.

Код:
new cmd[128], idx;
if(strcmp(cmd, "/kick", true) == 0)
you're comparing nothing...

Код:
if (strcmp(cmdtext, "/kick", true) == 0) // u shoulda been comparing to cmdtext
{
   
}
I gave it a shot but it didnt really help.

Now /kick returns as an unknown command, /giveplayergun either tell me the "You know the guns" return message when I leave the params blank or else it says unknown command aswell, and then ban closes the server connection without putting any ID.

Edit: Fixed it! You were actually right [somehow, your concept was correct haha], I had to define CMD with strtok first for it to compare, thanks!
Reply
#4

I didn't read your post above so I apologize however if you feel that the problem still exists, Try my post below.

I don't even know why you are using an old method to create commands (which is strcmp). I would prefer you using zcmd, You will notice how slow strcmp once you reached 50+ or 100+ commands. Welp i can't force you to move zcmd but yeeah I tried to fix your commands try the code below (I am not good at using strcmp commands anymore)

PHP код:
public OnPlayerCommandText(playeridcmdtext[])
{
    new 
giveplayerid;
    new 
cmd[128], idx;
    if(
strcmp(cmd"/kick"true) == 0)
    {
        new 
kicker[128];
        new 
tmp[128];
        
tmp strtok(cmdtextidx);
        if(!
strlen(tmp))
        {
            
SendClientMessage(playerid0xFFFFFFFF"Use /kick [playerid]");
            return 
1;
        }
        
giveplayerid ReturnUser(tmp);
        if(
IsPlayerConnected(giveplayerid))
        {
            
format(kickersizeof(kicker), "You have kicked Player ID : %d from the server."giveplayerid);
            
SendClientMessage(playerid0xFFFFFFFFkicker);
            
Kick(strval(tmp));
        }
        return 
1;
    }
     if(
strcmp(cmd"/ban"true) == 0)
    {
        new 
tmp[128];
        
tmp strtok(cmdtextidx);
        if(!
strlen(tmp))
        {
            
SendClientMessage(playerid0xFFFFFFFF"Use /ban [playerid]");
            return 
1;
        }
        
Ban(strval(tmp));
        return 
1;
    }
    if(
strcmp(cmdtext"/afk"true) == 0)
    {
        
GetPlayerName(playeridpNameMAX_PLAYER_NAME);
        
strins(pName"[AFK]"0);
        
SetPlayerName(playeridpName);
        return 
1;
    }
    if(
strcmp(cmd"/giveplayergun"true) == 0)
    {
        new 
tmp[128];
        
tmp strtok(cmdtextidx);
        if(!
strlen(tmp))
        {
            
SendClientMessage(playerid0xFFFFFFFF"Use /giveplayergun [playerid] [gun-id]");
            return 
1;
        }
        new 
gunidstring[128];
        new 
gunget;
        new 
gun;
        
gunget ReturnUser(tmp);
        
gun strvalEx(tmp);
        
tmp strtok(cmdtextidx);
        if(
strlen(tmp) == 0) return SendClientMessage(playerid0xFFFFFFFF"You know the guns.");
        if(
IsPlayerConnected(gunget))
        {
            
GivePlayerWeapon(gungetgun99999);
            
format(gunidstringsizeof(gunidstring), "Admin %s has give you the weapon ID : %d"playeridgun);
            
SendClientMessage(gunget0xFFFFFFFFgunidstring);
        }
        else    { 
SendClientMessage(playerid0xFFFFFFFF"That player is offline!"); }
        return 
1;
    }
    return 
0;

Reply
#5

If your command contains parameter, then use cmdtext and place it before any commands that has parameter.
Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
	if(strcmp(cmdtext, "/noparamcommand", true) == 0) // commands without parameter, compares cmdtext
	{
		// do something
		return 1;
	}
	
	new cmd[128], idx;
	cmd = strtok(cmdtext, idx); // you forgot this, this is the string of the command
	
	if(strcmp(cmd, "/kick", true) == 0) // commands with parameter, compares cmd
	{
		new tmp[128]; 
		tmp = strtok(cmdtext, idx); // this is actually string of the parameter
		// do something
		return 1;
	}
	return 0;
}
that strtok (string token) get the substring separated by a space ' ' token, for instance i typed /kick 0 then this is the result:
cmdtext = "/kick 0"
cmd = "/kick"
tmp = "0"
so if you use strtok once more after tmp, it will pick the next parameter (e.g. "reason" from "/kick id reason").

Anyway, consider using command method like zcmd with sscanf, as it's better to handle multiple parameters command.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)