Guest User

Strcmp+strtok -> zcmd+sscanf example 1

a guest
Aug 31st, 2011
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. /* Command from Fort Carson Roleplay (http://forum.sa-mp.com/showthread.php?t=272263) */
  2.  
  3. // Original command with string comparison and strtok-sourced parsing
  4. if(strcmp(cmd,"/i",true)==0)
  5. {
  6.     if(IsPlayerConnected(playerid))
  7.     {
  8.         if(PlayerInfo[playerid][pPlayersChannel] == 999)
  9.             {
  10.             SendClientMessage(playerid, COLOR_GREY, "   You are not in an IRC Channel !");
  11.             return 1;
  12.             }
  13.             if(PlayerInfo2[Mute][playerid] == 1)
  14.         {
  15.             SendClientMessage(playerid, TEAM_CYAN_COLOR, "You cannot speak, you have been silenced");
  16.             return 1;
  17.         }
  18.         GetPlayerName(playerid, sendername, sizeof(sendername));
  19.         new length = strlen(cmdtext);
  20.         while ((idx < length) && (cmdtext[idx] <= ' '))
  21.         {
  22.             idx++;
  23.         }
  24.         new offset = idx;
  25.         new result[128];
  26.         while ((idx < length) && ((idx - offset) < (sizeof(result) - 1)))
  27.         {
  28.             result[idx - offset] = cmdtext[idx];
  29.             idx++;
  30.         }
  31.         result[idx - offset] = EOS;
  32.         if(!strlen(result))
  33.         {
  34.             SendClientMessage(playerid, COLOR_GRAD2, "USAGE: /i [irc chat]");
  35.             return 1;
  36.         }
  37.         format(string, sizeof(string), "** IRC %s: %s. **", sendername, result);
  38.         SendIRCMessage(PlayerInfo[playerid][pPlayersChannel], COLOR_YELLOW2, string);
  39.     }
  40.     return 1;
  41. }
  42.  
  43. // Improved command using zcmd and sscanf (also, removed IsPlayerConnected check - how would the player type the command if they weren't connected???)
  44. CMD:i(playerid, params[])
  45. {
  46.     if(PlayerInfo[playerid][pPlayersChannel] == 999)
  47.         return SendClientMessage(playerid, COLOR_GREY, "   You are not in an IRC channel!"), true;
  48.     if(PlayerInfo2[Mute][playerid])
  49.         return SendClientMessage(playerid, COLOR_GREY, "You cannot speak, you have been silenced!"), true;
  50.     if(isnull(params))
  51.         return SendClientMessage(playerid, COLOR_GRAD2, "USAGE: /i [irc chat]"), true;
  52.     GetPlayerName(playerid, sendername, sizeof(sendername));
  53.     format(string, sizeof(string), "** IRC %s: %s. **", sendername, result);
  54.     SendIRCMessage(PlayerInfo[playerid][pPlayersChannel], COLOR_YELLOW2, string);
  55.     return true;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment