Scripting car colors
#1

I've been trying to make an admin level 5 command to do /carcolor, or /cc [color1] [color2] but, it hasn't seemed to have been working properly, it has messed up other commands. I do not use sscanf or the others, and I don't really intend to.

Here is what I've done so far:

Код:
if (IsPlayerAdmin(playerid) || adlvl[playerid] == 5)
 {
  if (strcmp(string, "/acolor1", true) == 0)
   return SendClientMessage2(playerid, COLOR_RED, "Server: Unknown command.");
  if (pvehicle[playerid][0] == 0)
   return SendClientMessage2(playerid, COLOR_RED, "Error: You must own a car to change its color!");
  tmp = strtok(cmdtext, idx);
  if (!strlen(tmp)) return SendClientMessage2(playerid, COLOR_ORANGE, "Usage: /acolor1 [color]");
  giveplayerid = strval(tmp);
  if (giveplayerid < 0 || giveplayerid > 255 || !isNumeric(tmp))
   SendClientMessage2(playerid, COLOR_RED, "Invalid color id! (0 - 255)");
  vehcol[pvehicle[playerid][0]][0] = giveplayerid;
  ChangeVehicleColor(pvehicle[playerid][0], giveplayerid, vehcol[pvehicle[playerid][0]][1]);
  format(string, 128, "You have changed your vehicles first color to %d.", giveplayerid);
  SendClientMessage2(playerid, COLOR_WHITE, string);
  return 1;
 }

if (IsPlayerAdmin(playerid) || adlvl[playerid] == 5)
 {
  if (strcmp(string, "/acolor2", true) == 0)
   return SendClientMessage2(playerid, COLOR_RED, "Server: Unknown command.");
  if (pvehicle[playerid][0] == 0)
   return SendClientMessage2(playerid, COLOR_RED, "Error: You must own a car to change its color!");
  tmp = strtok(cmdtext, idx);
  if (!strlen(tmp)) return SendClientMessage2(playerid, COLOR_ORANGE, "Usage: /acolor2 [color]");
  giveplayerid = strval(tmp);
  if (giveplayerid < 0 || giveplayerid > 255 || !isNumeric(tmp))
    SendClientMessage2(playerid, COLOR_RED, "Invalid color id! (0 - 255)");
  vehcol[pvehicle[playerid][0]][1] = giveplayerid;
  ChangeVehicleColor(pvehicle[playerid][0], vehcol[pvehicle[playerid][0]][0], giveplayerid);
  format(string, 128, "You have changed your vehicles second color to %d.", giveplayerid);
  SendClientMessage2(playerid, COLOR_WHITE, string);
  return 1;
 }
Reply
#2

Quote:
Originally Posted by JeffDiamond
Посмотреть сообщение
I do not use sscanf or the others, and I don't really intend to.
Why would you not want to upgrade? Using strcmp as a command processor with strtok is incredibly inefficient and (as you already mentioned) causes more issues than it solves. zcmd and sscanf are way easier to use and are better in at least a hundred ways.

Also, it'll take you longer to adjust to zcmd if you start with strcmp now so it's better to switch now instead of waiting until we've got something more advanced. Besides, it's pointless trying to program something and putting time in to something when you don't aim for the best results because all the time you put in to it will just be wasted.

I redid your code but in zcmd and sscanf, I highly recommend you to use it instead of waiting for a doofus to actually fix your strcmp code (and basically giving you bad advice).

Here it is; it compiles but has not been tested:

pawn Код:
CMD:carcolor(playerid, params[])
{
    new val[2];
   
    if(adminlevel[playerid] < 5) return false;
   
    if(sscanf(params, "ii", val[0], val[1])) return SendClientMessage(playerid, 0xff0000aa, "/carcolor [color 1] [color 2]");
    if(val[0] < 0 || val[0] > 255 || val[1] < 0 || val[1] > 255) return SendClientMessage(playerid, 0xff0000aa, "Invalid color id! (0 - 255)");

    if(pvehicle[playerid][0] == 0)  return SendClientMessage(playerid, 0xff0000aa, "You don't own a car!");

    new str[56];
   
    format(str, sizeof(str), "You have changed your car color(s) to %d and %d.", val[0], val[1]);
    SendClientMessage(playerid, 0xffff00aa, str);

    for(new i; i < 2; i ++)
    {
        vehcol[pvehicle[playerid][0]][i] = val[i];
    }

    ChangeVehicleColor(pvehicle[playerid][0], val[0], val[1]);
       
    return true;
}
Probably not the most efficient way as I'm very tired at the moment but you get the point. If anyone notices any flaws, feel free to correct me.
Reply
#3

It's just that the script I'm using uses only strcmp and I'm not sure in adding this will wreak things. Plus, I'm not all to sure how to install that stuff, I've tried it before and it messed everything up.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)