SA-MP Forums Archive
command for /changename? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: command for /changename? (/showthread.php?tid=380409)



command for /changename? - .v - 25.09.2012

How do I make my own changename command? because in my admin system it doesn't have /changename command and more info it should remain its stats only name must change.

Before I forgot also check if name is already taken so their stats wouldn't conflict.


Re: command for /changename? - West X - 26.09.2012

Use the function SetPlayerName to change a user's name when they are currently in game.
As for detecting if the username is already taken, format the path of the new name and use fexist to determine if it's taken or not.
(If your using MySQL, fexist will not work obviously). If the user's file currently exists and they are registered, you can use this custom stock function:


pawn Code:
stock frename(oldname[], newname[])
{
    if(!fexist(oldname)) return false;
    new string[128], File:old, File:neww;
    old = fopen(oldname, io_read);
    neww = fopen(newname, io_write);
    while(fread(old, string))
    {
        StripNewLine(string);
        format(string, sizeof(string), "%s\r\n", string);
        fwrite(neww, string);
    }
    fclose(old);
    fclose(neww);
    fremove(oldname);
    return true;
}
To modify and re-name their userfile to the new username.



Re: command for /changename? - .v - 26.09.2012

Quote:
Originally Posted by West X
View Post
Use the function SetPlayerName to change a user's name when they are currently in game.
As for detecting if the username is already taken, format the path of the new name and use fexist to determine if it's taken or not.
(If your using MySQL, fexist will not work obviously). If the user's file currently exists and they are registered, you can use this custom stock function:


pawn Code:
stock frename(oldname[], newname[])
{
    if(!fexist(oldname)) return false;
    new string[128], File:old, File:neww;
    old = fopen(oldname, io_read);
    neww = fopen(newname, io_write);
    while(fread(old, string))
    {
        StripNewLine(string);
        format(string, sizeof(string), "%s\r\n", string);
        fwrite(neww, string);
    }
    fclose(old);
    fclose(neww);
    fremove(oldname);
    return true;
}
To modify and re-name their userfile to the new username.
Thanks for this man.. But how could I start the command for change name?

Like this?

pawn Code:
CMD:changename(playerid,params[])
{
        SendClientMessage(playerid, white, "{FFFF00}Usage: /changename [NewName]");
        if(length < 3 || length > MAX_PLAYER_NAME) return SendClientMessage(playerid,white,"{FF0000}>> Incorrect Name Length");
        new string[64];
        format(string,sizeof(string),"Your have been changed your current Name to %s, ", tmp);
        SendClientMessage(playerid, LIGHTBLUE, string);
        SetPlayerName(playerid, );
        return OnPlayerConnect(playerid);
}



Re: command for /changename? - West X - 27.09.2012

<The content in this post has been removed.>


Re: command for /changename? - .v - 27.09.2012

Got 2 warnings

Code:
warning 219: local variable "pName" shadows a variable at a preceding level
error 017: undefined symbol "StripNewLine"
I've got the 2nd pName. What should I remove or change?
Code:
stock pName(playerid)
{
	new paname[MAX_PLAYER_NAME];
	GetPlayerName(playerid, paname, sizeof(paname));
	return paname;
}



Re: command for /changename? - West X - 27.09.2012

<The content in this post has been removed.>


Re: command for /changename? - .v - 27.09.2012

Quote:
Originally Posted by West X
View Post
Here is the fix:

pawn Code:
#define COLOR_GREY 0xAFAFAFAA
#define COLOR_WHITE 0xFFFFFFAA

CMD:changename(playerid, params[])
{
    new newname[MAX_PLAYER_NAME], file1[128], file2[128];
    if(sscanf(params, "s[24]", newname)) return SendClientMessage(playerid, COLOR_GREY, "Usage: /changename [New Username]");
    if(strlen(newname) < 3 || strlen(newname) > MAX_PLAYER_NAME) return SendClientMessage(playerid, COLOR_GREY, "Invalid username length.");
    format(file2, sizeof(file2), "/Users/%s.ini", newname);
    if(fexist(file2)) return SendClientMessage(playerid, COLOR_GREY, "The specified username is already taken.");
    format(file1, sizeof(file1), "/Users/%s.ini", pName(playerid));
    format(file2, sizeof(file2), "/Users/%s.ini", newname);
    frename(file1, file2);
    SetPlayerName(playerid, newname);
    format(file1, sizeof(file1), "You have changed your name to %s.", newname);
    SendClientMessage(playerid, COLOR_WHITE, file1);
    return 1;
}
You will also need this stock function:

pawn Code:
stock StripNewLine(string[])
{
    new len = strlen(string);
    if(string[0] == 0) return;
    if((string[len - 1] == '\n') || (string[len - 1] == '\r'))
    {
        string[len - 1] = 0;
        if(string[0]==0) return ;
        if((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
    }
}
Thanks +rep it's all working now. Also it deletes the old account and replaced it with the new one and that is great! And lastly could you help me with Unban command? just like changename command.


Re: command for /changename? - West X - 27.09.2012

Exactly what file saving system does your game-mode use?


Re: command for /changename? - .v - 27.09.2012

I'm using a filterscript MellAdmin and it use Dini for saving.

This is the stock..

pawn Code:
stock DiniBanPlayer(playerid, admin[], reason[])
{
    new file[128], date[16], year, month, day;
    getdate(year, month, day);
    format(date, sizeof(date), "%d/%d/%d", day, month, year);
    format(file, sizeof(file), "/%s/%s.ini", DINI_PATH, GetName(playerid));
    dini_IntSet(file, "Status", 2);
    dini_Set(file, "BannedBy", admin);
    dini_Set(file, "BanReason", reason);
    dini_Set(file, "BanDate", date);
    Kick(player);
    return 1;
}
In the thread is says
pawn Code:
Status (0 = offline, 1 = online, 2 = banned)
If I do /unban [Exact Name]

Status=2 must change to Status=0


Re: command for /changename? - West X - 27.09.2012

<The content in this post has been removed.>