iPLEOMAX

Dynamic Command Permissions - Method #1

Jan 15th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.12 KB | None | 0 0
  1. #include <a_samp.inc>
  2. #include <ZCMD.inc>
  3.  
  4. /******************************************************************************/
  5. /*  If you are going to load the command levels from database, this is the only approach:
  6.     An array with list of cmds "gCMDMinLevel"
  7.     Pros:
  8.         Dynamically load command list from database
  9.     Cons:
  10.         Each executed command needs a loop through all cmds
  11.         Not as bad as it sounds like tho.
  12. */
  13. /******************************************************************************/
  14.  
  15. enum e_PERMISSION {
  16.     e_P_COMMAND[32],
  17.     e_P_LEVEL
  18. };
  19.  
  20. new gCMDMinLevel[][e_PERMISSION] = {
  21.     {"kick", 1},
  22.     {"v", 2},
  23.     {"setlevel", 5}
  24.     //etc...
  25. };
  26.  
  27. //You'll be using this in the ZCMD
  28. #define CMDMinLevel[%0] iCMDMinLevel(#%0)
  29. stock iCMDMinLevel(cmd[]) {
  30.     for(new i = 0; i < sizeof gCMDMinLevel; i++) {
  31.         if(!strcmp(cmd, gCMDMinLevel[i][e_P_COMMAND], true)) {
  32.             return gCMDMinLevel[i][e_P_LEVEL];
  33.         }
  34.     }
  35.     printf("[WARNING] Command '%s' doesn't exist in permissions.", cmd);
  36.     return 0;
  37. }
  38.  
  39. //Use this function to update command's permission level from database or something
  40. stock CMDSetMinLevel(cmd[], level) {
  41.     for(new i = 0; i < sizeof gCMDMinLevel; i++) {
  42.         if(!strcmp(cmd, gCMDMinLevel[i][e_P_COMMAND], true)) {
  43.             gCMDMinLevel[i][e_P_LEVEL] = level;
  44.             printf("[SUCCESS] Command '%s' permission level set to %i.", cmd, level);
  45.             return;
  46.         }
  47.     }
  48.     printf("[WARNING] Command '%s' doesn't exist in permissions.", cmd);
  49. }
  50.  
  51. /******************************************************************************/
  52. /* Your regular scripts here                                                  */
  53. /******************************************************************************/
  54.  
  55. enum e_pInfo {
  56.     aLevel
  57. };
  58.  
  59. new pInfo[MAX_PLAYERS][e_pInfo];
  60.  
  61. main(){}
  62.  
  63. public OnFilterScriptInit() {
  64.     //Changing permissions
  65.     CMDSetMinLevel("v", 3);
  66.     CMDSetMinLevel("banana", 10);
  67.     return true;
  68. }
  69.  
  70. CMD:v(playerid, params[]) {
  71.     if(pInfo[playerid][aLevel] < CMDMinLevel[v])
  72.         return SendClientMessage(playerid, 0xFF0000FF, "ERROR: Insufficient permissions to use this command!");
  73.  
  74.     SendClientMessage(playerid, -1, "Access granted!");
  75.     return true;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment