03.01.2016, 15:37
I got this straight from my RP gamemode which I made quite a few years back ago:
It should work properly
EDIT: Just to let you know, ZCMD is faster than strcmp.
Код:
stock IsNumeric(const string[]) { for (new i = 0, j = strlen(string); i < j; i++) if (string[i] > '9' || string[i] < '0') return 0; return 1; } ReturnUser(text[], playerid = INVALID_PLAYER_ID) { new pos = 0; while (text[pos] < 0x21) { if (text[pos] == 0) return INVALID_PLAYER_ID; pos++; } new userid = INVALID_PLAYER_ID; if (IsNumeric(text[pos])) { userid = strval(text[pos]); if (userid >=0 && userid < MAX_PLAYERS) { if(!IsPlayerConnected(userid)) userid = INVALID_PLAYER_ID; else return userid; } } new len = strlen(text[pos]); new count = 0; new pname[MAX_PLAYER_NAME]; for (new i = 0; i < MAX_PLAYERS; i++) { if (IsPlayerConnected(i)) { GetPlayerName(i, pname, sizeof (pname)); if (strcmp(pname, text[pos], true, len) == 0) { if (len == strlen(pname)) return i; else { count++; userid = i; } } } } if (count != 1) { if (playerid != INVALID_PLAYER_ID) { if (count) SendClientMessage(playerid, 0xFF0000AA, "Please type the player's full name."); else SendClientMessage(playerid, 0xFF0000AA, "The Player's name has not been found."); } userid = INVALID_PLAYER_ID; } return userid; } stock strvalEx( const string[] ) { //Register Protection if( strlen( string ) >= 50 ) return 0; return strval(string); } stock 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; } public OnPlayerCommandText(playerid, cmdtext[]) { new cmd[256],string[248]; new tmp[256]; new idx; cmd = strtok(cmdtext, idx); if(strcmp(cmd, "/pay", true) == 0) { tmp = strtok(cmdtext,idx); if(strlen(tmp)) { new giveplayerid = ReturnUser(tmp); if(IsPlayerConnected(giveplayerid) && !IsPlayerNPC(giveplayerid)) { if(giveplayerid == playerid) { SendClientMessage(playerid,0xFF0000AA," You can't pay yourself."); return 1; } tmp = strtok(cmdtext,idx); new cash = strvalEx(tmp); if(cash < 1 || cash > 999999) { SendClientMessage(playerid,0xFFFFFFAA," Given cash can't be lower than 1 or above 10.000$."); return 1; } if(GetPlayerMoney(playerid) >= cash) { new pName[MAX_PLAYER_NAME], giveplayername[MAX_PLAYER_NAME]; GetPlayerName(playerid, pName,sizeof(pName)); GetPlayerName(giveplayerid, giveplayername, sizeof(giveplayername)); format(string,sizeof(string)," You have received {58A624}%d$ {FFFFFF}from {58A624}%s{FFFFFF}.",cash,pName); SendClientMessage(giveplayerid,0xFFFFFFAA,string); format(string,sizeof(string)," You have given {58A624}%d$ {FFFFFF}to {58A624}%s{FFFFFF}.",cash,giveplayername); SendClientMessage(playerid,0xFFFFFFAA,string); GivePlayerMoney(playerid,-cash); GivePlayerMoney(giveplayerid,cash); } else { SendClientMessage(playerid,0xFF0000AA," You don't have that money."); } } else { SendClientMessage(playerid,0xFF0000AA," The Player is not connected."); } } else { SendClientMessage(playerid,0xFF0000AA," Usage: /pay [playerid/playername] [money]"); } return 1; } return 0; }
EDIT: Just to let you know, ZCMD is faster than strcmp.