Guest User

Untitled

a guest
Apr 20th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.26 KB | None | 0 0
  1. /*
  2.     * License System: By TheHoodRat *
  3.     * Based on the East Coast Roleplay licensing system. *
  4.     * This filterscript is automatically built in with Dini, to save the licenses. *
  5.     * Credits to DracoBlue for Dini *
  6.  
  7.     * WARNING: You must create a folder called Licenses in your scriptfiles folder! *
  8.     * It is required to create the folder or else the server will crash! *
  9. */
  10.  
  11. #include <a_samp>
  12.  
  13. #define DINI_MAX_STRING 255
  14.  
  15. new Menu:licensemenu;
  16. new playerfile[256];
  17. new pname[MAX_PLAYER_NAME];
  18.  
  19. enum cInfo{Car}; new LicenseInfo[MAX_PLAYERS][cInfo];
  20.  
  21. public OnFilterScriptInit()
  22. {
  23.     licensemenu = CreateMenu("Select an option", 1, 50.0, 180.0, 200.0, 200.0);
  24.  
  25.     AddMenuItem(licensemenu, 0, "Car License - $50");
  26.  
  27.     AddStaticPickup(1239, 23, 361.8299,173.6111,1008.3828, 0);
  28.     Create3DTextLabel("Licenses\nType /buylicense to buy a license.",0x33CCFFAA,361.8299,173.6111,1008.3828,40.0,0);
  29.     return 1;
  30. }
  31.  
  32. public OnFilterScriptExit()
  33. {
  34.     return 1;
  35. }
  36.  
  37. public OnPlayerCommandText(playerid, cmdtext[])
  38. {
  39.     if (strcmp("/buylicense", cmdtext, true, 10) == 0) {
  40.     if(!IsPlayerInRangeOfPoint(playerid, 3.0, 361.8299,173.6111,1008.3828)){SendClientMessage(playerid, 0xAA3333AA, "ERROR: You must be at City Hall!"); return 1;}
  41.     ShowMenuForPlayer(licensemenu, playerid);
  42.     return 1;} return 0;
  43. }
  44.  
  45. public OnPlayerDisconnect(playerid, reason)
  46. {
  47.     GetPlayerName(playerid, pname, sizeof(pname));
  48.     format(playerfile, sizeof(playerfile), "Licenses/%s.ini",pname);
  49.     dini_IntSet(playerfile, "Car License", LicenseInfo[playerid][Car]);
  50.     return 1;
  51. }
  52.  
  53. public OnPlayerConnect(playerid)
  54. {
  55.     GetPlayerName(playerid, pname, sizeof(pname));
  56.     format(playerfile, sizeof(playerfile), "Licenses/%s.ini",pname);
  57.     return 1;
  58. }
  59.  
  60. public OnPlayerRequestSpawn(playerid)
  61. {
  62.     GetPlayerName(playerid, pname, sizeof(pname));
  63.     format(playerfile, sizeof(playerfile), "Licenses/%s.ini",pname);
  64.     LicenseInfo[playerid][Car] = dini_Int(playerfile, "Car License");
  65.     return 1;
  66. }
  67.  
  68. public OnPlayerStateChange(playerid, newstate, oldstate)
  69. {
  70.     if(newstate == PLAYER_STATE_DRIVER)
  71.     {
  72.      if(LicenseInfo[playerid][Car] < 1)
  73.      {
  74.      SendClientMessage(playerid, 0xFF6347AA, "You can't drive this car, you don't have a license!");
  75.      RemovePlayerFromVehicle(playerid);
  76.      return 1; }
  77.  }
  78.     return 1;
  79. }
  80.  
  81. public OnPlayerSelectedMenuRow(playerid, row)
  82. {
  83.     HideMenuForPlayer(licensemenu, playerid);
  84.     new Menu:current;
  85.     current = GetPlayerMenu(playerid);
  86.     if(current == licensemenu)
  87.     {
  88.         switch(row)
  89.         {
  90.             case 0:
  91.             {
  92.                 if(GetPlayerMoney(playerid) < 50)
  93.                 {
  94.                     SendClientMessage(playerid, 0xFF6347AA, "You can't afford the license, sorry!");
  95.                     return 1;
  96.                 }
  97.                 GetPlayerName(playerid, pname, sizeof(pname));
  98.                 format(playerfile, sizeof(playerfile), "Licenses/%s.ini",pname);
  99.                 if(!dini_Exists(playerfile))
  100.                 {
  101.                     dini_Create(playerfile);
  102.                     LicenseInfo[playerid][Car] = 1;
  103.                     GivePlayerMoney(playerid, - 50);
  104.                     SendClientMessage(playerid, 0x33CCFFAA, "You have bought a Car License for $50!");
  105.                     LicenseInfo[playerid][Car] = 1;
  106.                     dini_Set(playerfile, "Car License", LicenseInfo[playerid][Car]);
  107.                 }
  108.                 else
  109.                 {
  110.                     SendClientMessage(playerid, 0x33CCFFAA, "You already have a license!");
  111.                 }
  112.             }
  113.         }
  114.         return 1;
  115.     }
  116.     return 1;
  117. }
  118.  
  119. public OnPlayerExitedMenu(playerid)
  120. {
  121.     TogglePlayerControllable(playerid, 1);
  122.     return 1;
  123. }
  124.  
  125. /*
  126.  
  127. * DINI BY DRACOBLUE *
  128.  
  129. */
  130.  
  131. stock dini_Exists(filename[]) {
  132.     return fexist(filename);
  133. }
  134.  
  135. stock dini_Remove(filename[]) {
  136.     return fremove(filename);
  137. }
  138.  
  139. stock dini_Create(filename[]) {
  140.     if (fexist(filename)) return false;
  141.     new File:fhnd;
  142.     fhnd=fopen(filename,io_write);
  143.     if (fhnd) {
  144.         fclose(fhnd);
  145.         return true;
  146.     }
  147.     return false;
  148. }
  149.  
  150. stock dini_Set(filename[],key[],value[]) {
  151.     new key_length = strlen(key);
  152.     new value_length = strlen(value);
  153.     if (key_length==0 || key_length+value_length+2>DINI_MAX_STRING) return false;
  154.  
  155.     new File:fohnd, File:fwhnd;
  156.     new tmpres[DINI_MAX_STRING];
  157.     new bool:wasset=false;
  158.  
  159.     format(tmpres,sizeof(tmpres),"%s.part",filename);
  160.     fremove(tmpres);
  161.     fohnd=fopen(filename,io_read);
  162.     if (!fohnd) return false;
  163.  
  164.     fwhnd=fopen(tmpres,io_write);
  165.     if (!fwhnd) {
  166.         fclose(fohnd);
  167.         return false;
  168.     }
  169.  
  170.     while (fread(fohnd,tmpres)) {
  171.         if (
  172.             !wasset
  173.             && tmpres[key_length]=='='
  174.             && !strcmp(tmpres, key, true, key_length)
  175.         ) {
  176.                 format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  177.                 wasset=true;
  178.         } else {
  179.             DINI_StripNewLine(tmpres);
  180.         }
  181.         fwrite(fwhnd,tmpres);
  182.         fwrite(fwhnd,"\r\n");
  183.     }
  184.  
  185.     if (!wasset) {
  186.         format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  187.         fwrite(fwhnd,tmpres);
  188.         fwrite(fwhnd,"\r\n");
  189.     }
  190.  
  191.     fclose(fohnd);
  192.     fclose(fwhnd);
  193.  
  194.     format(tmpres,sizeof(tmpres),"%s.part",filename);
  195.     if (DINI_fcopytextfile(tmpres,filename)) {
  196.         return fremove(tmpres);
  197.     }
  198.     return false;
  199. }
  200.  
  201.  
  202. stock dini_IntSet(filename[],key[],value) {
  203.    new valuestring[DINI_MAX_STRING];
  204.    format(valuestring,DINI_MAX_STRING,"%d",value);
  205.    return dini_Set(filename,key,valuestring);
  206. }
  207.  
  208. stock dini_Int(filename[],key[]) {
  209.    return strval(dini_Get(filename,key));
  210. }
  211.  
  212.  
  213. stock dini_FloatSet(filename[],key[],Float:value) {
  214.    new valuestring[DINI_MAX_STRING];
  215.    format(valuestring,DINI_MAX_STRING,"%f",value);
  216.    return dini_Set(filename,key,valuestring);
  217. }
  218.  
  219. stock Float:dini_Float(filename[],key[]) {
  220.    return floatstr(dini_Get(filename,key));
  221. }
  222.  
  223. stock dini_Bool(filename[],key[]) {
  224.    return strval(dini_Get(filename,key));
  225. }
  226.  
  227. stock dini_BoolSet(filename[],key[],value) {
  228.     if (value) {
  229.         return dini_Set(filename,key,"1");
  230.     }
  231.     return dini_Set(filename,key,"0");
  232. }
  233.  
  234. stock dini_Unset(filename[],key[]) {
  235.     new key_length = strlen(key);
  236.     if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
  237.  
  238.     new File:fohnd, File:fwhnd;
  239.     new tmpres[DINI_MAX_STRING];
  240.  
  241.     format(tmpres,DINI_MAX_STRING,"%s.part",filename);
  242.     fremove(tmpres);
  243.  
  244.     fohnd=fopen(filename,io_read);
  245.     if (!fohnd) return false;
  246.  
  247.     fwhnd=fopen(tmpres,io_write);
  248.     if (!fwhnd) {
  249.         fclose(fohnd);
  250.         return false;
  251.     }
  252.  
  253.     while (fread(fohnd,tmpres)) {
  254.         if (
  255.             tmpres[key_length]=='='
  256.             && !strcmp(tmpres, key, true, key_length)
  257.         ) {
  258.         } else {
  259.             DINI_StripNewLine(tmpres);
  260.             fwrite(fwhnd,tmpres);
  261.             fwrite(fwhnd,"\r\n");
  262.         }
  263.     }
  264.  
  265.     fclose(fohnd);
  266.     fclose(fwhnd);
  267.  
  268.     format(tmpres,DINI_MAX_STRING,"%s.part",filename);
  269.     if (DINI_fcopytextfile(tmpres,filename)) {
  270.         return fremove(tmpres);
  271.     }
  272.     return false;
  273. }
  274.  
  275. stock dini_Get(filename[],key[]) {
  276.     new tmpres[DINI_MAX_STRING];
  277.  
  278.     new key_length = strlen(key);
  279.     if (key_length==0 || key_length+2>DINI_MAX_STRING) return tmpres;
  280.  
  281.     new File:fohnd;
  282.     fohnd=fopen(filename,io_read);
  283.     if (!fohnd) return tmpres;
  284.  
  285.     while (fread(fohnd,tmpres)) {
  286.         if (
  287.             tmpres[key_length]=='='
  288.             && !strcmp(tmpres, key, true, key_length)
  289.         ) {
  290.             DINI_StripNewLine(tmpres);
  291.             strmid(tmpres, tmpres, key_length + 1, strlen(tmpres), DINI_MAX_STRING);
  292.             fclose(fohnd);
  293.             return tmpres;
  294.         }
  295.     }
  296.     fclose(fohnd);
  297.     return tmpres;
  298. }
  299.  
  300.  
  301. stock dini_Isset(filename[],key[]) {
  302.     new key_length = strlen(key);
  303.     if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
  304.  
  305.     new File:fohnd;
  306.     fohnd=fopen(filename,io_read);
  307.     if (!fohnd) return false;
  308.  
  309.     new tmpres[DINI_MAX_STRING];
  310.     while (fread(fohnd,tmpres)) {
  311.         if (
  312.                 tmpres[key_length]=='='
  313.             &&  !strcmp(tmpres, key, true, key_length)
  314.         ) {
  315.             fclose(fohnd);
  316.             return true;
  317.         }
  318.     }
  319.     fclose(fohnd);
  320.     return false;
  321. }
  322.  
  323.  
  324.  
  325. stock DINI_StripNewLine(string[]) {
  326.     new len = strlen(string);
  327.     if (string[0]==0) return ;
  328.     if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  329.         string[len - 1] = 0;
  330.         if (string[0]==0) return ;
  331.         if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  332.     }
  333. }
  334.  
  335. stock DINI_fcopytextfile(oldname[],newname[]) {
  336.     new File:ohnd,File:nhnd;
  337.     if (!fexist(oldname)) return false;
  338.     ohnd=fopen(oldname,io_read);
  339.     if (!ohnd) return false;
  340.     nhnd=fopen(newname,io_write);
  341.     if (!nhnd) {
  342.         fclose(ohnd);
  343.         return false;
  344.     }
  345.     new tmpres[DINI_MAX_STRING];
  346.     while (fread(ohnd,tmpres)) {
  347.         DINI_StripNewLine(tmpres);
  348.         format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
  349.         fwrite(nhnd,tmpres);
  350.     }
  351.     fclose(ohnd);
  352.     fclose(nhnd);
  353.     return true;
  354. }
Advertisement
Add Comment
Please, Sign In to add comment