SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 03/12/2012, 06:13 PM   #1
gtakillerIV
High-roller
 
gtakillerIV's Avatar
 
Join Date: Sep 2011
Location: UAE, Sharjah.
Posts: 1,002
Reputation: 201
Default Making a /renameaccount Command - Using ZCMD + SScanf 2

Hey guys! Today I'm going to tell you how to make a /renameaccount command using ZCMD and SScanf2.

Credits:

SScanf 2 (Y_Less)

ZCMD (Zeex)

Note: You have to have a user system for this to work with you.

How can I make one?

There are quite a few tutorials, I'll give you two that I recommend.

1.Newbienoob's Tutorial.

2.Kush's Tutorial.

What is ZCMD?

Zcmd is a fast and simple command processor made by Zeex.

Example of a command in ZCMD:

pawn Code:
CMD:test(playerid, params[]) // Zcmd command format.
{
    SendClientMessage(playerid, -1, "Hey!"); //Sending a message to ourself saying "Hey!".
}

Now take a look at the SScanf Specifiers (Thanks to Y_Less), they will come in handy when you start making advanced commands.

Code:
Specifier(s)			Name				Example values
	i, d			Integer				1, 42, -10
	c			Character			a, o, *
	l			Logical				true, false
	b			Binary				01001, 0b1100
	h, x			Hex				1A, 0x23
	o			Octal				045 12
	n			Number				42, 0b010, 0xAC, 045
	f			Float				0.7, -99.5
	g			IEEE Float			0.7, -99.5, INFINITY, -INFINITY, NAN, NAN_E
	u			User name/id (bots and players)	Y_Less, 0
	q			Bot name/id			ShopBot, 27
	r			Player name/id			Y_Less, 42
Now let's start!

First of all we have to include our downloaded includes which are Zcmd and SScanf 2. At the top of your script but below your defines, add this:

pawn Code:
#include <zcmd>
#include <sscanf2>

Add those anywhere in your script:

pawn Code:
stock frename(oldname[],newname[]) {
    if (!fexist(oldname)) return false;
    fremove(newname);
    if (!fcopy(oldname,newname)) return false;
    fremove(oldname);
    return true;
}

pawn Code:
stock fcopy(oldname[],newname[]) {
    new File:ohnd,File:nhnd;
    if (!fexist(oldname)) return false;
    ohnd=fopen(oldname,io_read);
    nhnd=fopen(newname,io_write);
    new buf2[1];
    new i;
    for (i=flength(ohnd);i>0;i--) {
        fputchar(nhnd, fgetchar(ohnd, buf2[0],false),false);
    }
    fclose(ohnd);
    fclose(nhnd);
    return true;
}


Hit compile if you get no errors, then continue with me, else post a comment here saying the errors you get.

Now for the command, the command will look like this:

pawn Code:
CMD:renameaccount(playerid, params[])
{
    new oldname[24], newname[24];
    if(sscanf(params,"s[24]s[24]", oldname, newname)) return SendClientMessage(playerid, -1 , "Correct Usage: /renameaccount [OldName][New name]");
    new filestring2[128];
    format(filestring2, sizeof(filestring2), "/Users/%s.ini", oldname);
    if(!fexist(filestring2)) return SendClientMessage(playerid, -1 , "That account name doesn't exist!");
    new filestring[128];
    format(filestring, sizeof(filestring), "/Users/%s.ini", newname);
    if(fexist(filestring)) return SendClientMessage(playerid,  -1, "The new name you've chosen is already taken");
    frename(filestring2, filestring);
    new string[128];
    format(string, sizeof(string), "You have successfully renamed %s's account to %s" ,oldname, newname);
    SendClientMessage(playerid, -1, string);
    return 1;
}

I'll explain each line for you now.

pawn Code:
new oldname[24], newname[24];

Here we are creating a new string with the name of "oldname" with the size of 24(Maximum SA-MP Name size, if you use MAX_PLAYER_NAME it's the same as typing in 24) and creating another string called "newname" with the size of 24.



pawn Code:
if(sscanf(params,"s[24]s[24]", oldname, newname)) return SendClientMessage(playerid, -1, "Correct Usage: /renameaccount [OldName][New name]");

Now here we are creating parameters for our command, for example: If I go ingame and type in /renameaccount it will give me an error saying "Correct Usage: /renameaccount [OldName][New name]" and if I type in /renameaccount gtakillerIV it will give me a message with the correct usage
The s[24] means string with the size of 24 now if you look abit more to the right I typed in "oldname" what that means is that when I enter something in the first parameter it will get assigned to my string "oldname" which is the Account's old name, same thing for the string "newname".

If I for example type in /renameaccount gtakillerIV, gtakillerIV will get assigned to our string "oldname", and if I type in /renameaccount gtakillerIV Gta, gtakillerIV will get assigned to our string "oldname" and Gta will get assigned to our string "newname" which is the new name for the account we want to rename.




pawn Code:
new filestring2[128];

Here we are creating a new string called "filestring" with the size of 128, you will know how are going to use it in this Tutorial.


pawn Code:
format(filestring2, sizeof(filestring2), "/Users/%s.ini", oldname);

Here we are formating our string "filestring2" which we have just created, what I mean by formating is like we are assigning/putting in our string "filestring2" what we want, for example if I typed in "/Users/%s.ini" this is the directory which our user accounts are located, %s means a string. Now here is a small example: If I type in /renameaccount gtakillerIV Gta, our string "filestring2" will get formated like this: /Users/gtakillerIV.ini. Why gtakillerIV? Well if you look at our format you will see that I added at the end oldname what that means is that our string(%s) will get replaced with our string oldname which we have just typed in (/renameaccount gtakillerIV Gta).



pawn Code:
if(!fexist(filestring2)) return SendClientMessage(playerid, -1, "That account name doesn't exist!");

Here we are checking to see if our formated string "filestring2" doesn't exist (!), why do we want to do that? Well you can't rename a file that is not there. Example: /renameaccount gtakillerIV Gta, gtakillerIV will get assigned to our string "oldname" from there we format our filestring2 string so it looks like this: /Users/gtakillerIV.ini, so we are checking if there is an account with the name of gtakillerIV, if that is true then we will return an error. This was just an example.


pawn Code:
new filestring[128];

Here we are creating a new string called "filestring" with the size of 128.


pawn Code:
format(filestring, sizeof(filestring), "/Users/%s.ini", newname);

Here we are formating our string "filestring" which we have just created, again why I mean by formating is putting what we want in our string "filestring". For example: If I type in /renameaccount gtakillerIV Gta, our filestring2 will get formated like this: /Users/gtakillerIV.ini , but our filestring will get formated like this: /Users/Gta.ini. If you noticed at the end I added newname instead of oldname what that does is that our %s(string) that we added "/Users/%s.ini" will get replaced with our newname parameter that we have created with SScanf2.



pawn Code:
if(fexist(filestring)) return SendClientMessage(playerid, -1, "The new name you've chosen is already taken");

Here we are checking to see if our filestring that we just formated does exist, why do I want to check if it exists? Because we don't want two user accounts with the same name. For example: If I type in /renameaccount gtakillerIV Gta, gtakillerIV will get assigned to our string "oldname", and Gta will get inserted to our string "newname". Then our filestring2 will get formated like this: /Users/gtakillerIV.ini and our filestring will get formated like this: /Users/Gta.ini. Then we check to see if our filestring exists (Users/Gta.ini), if it does return an error which is: The new name you've chosen is already taken.


pawn Code:
frename(filestring2, filestring);

Here we are using the function frename which just renames our file. We have formated our filestring2 and our filestring, so we are renaming our filestring2 with our filestring. Example: If I type in /renameaccount gtakillerIV Gta, our filestring2 will get formated like this: /Users/gtakillerIV.ini and our filestring will get formated like this: /Users/Gta.ini. Now gtakillerIV will get renamed to Gta.


pawn Code:
new string[128];

Here we are creating a new string with the name of "string" and with the size of 128.


pawn Code:
format(string, sizeof(string), "You have successfully renamed %s's account to %s" ,oldname, newname);

Here we are formating our string "string". Example: If I type in /renameaccount gtakillerIV Gta, our string will get formated like this: You have sucessfully renamed gtakillerIV's account to Gta.


pawn Code:
SendClientMessage(playerid, -1, string);

Here we are sending our formated string to the playerid(us) using the function SendClientMessage.

How does this function work?


Thanks to Y_Less for telling me how:


Quote:
What level do you want? The "playerid" parameter is used to look up the IP and port (i.e. the socket) of a player, then the colour and text information is packed in to a packet and that is sent via TCP* to the specified socket (i.e. the player). Once there, the client checks the incoming message type and calls the relevant decoding code to extract the the colour and message and uses DirectX text operations to draw to the screen. There is also code to store the current message buffer and manipulate that to add things on to the end.



* Actually, it's sent via a custom UDP protocol, but it is similar to TCP in that is ensures delivery.



pawn Code:
return 1;

Here we are returning 1 for our command to work, if we return 0 we will get an error saying: "SERVER: Unkown Command".



This is pretty much it, I hope you have learned something new. Please learn and don't copy-paste.

Found any typo/mistake? Please tell me here.

Thanks.

Last edited by gtakillerIV; 04/12/2012 at 08:48 AM.
gtakillerIV is offline   Reply With Quote
Old 03/12/2012, 06:19 PM   #2
DaRk_RaiN
High-roller
 
DaRk_RaiN's Avatar
 
Join Date: Sep 2012
Posts: 988
Reputation: 207
Default Re: Making a /renameaccount Command - Using ZCMD + SScanf 2

Nice job.

Last edited by DaRk_RaiN; 27/01/2013 at 10:48 AM.
DaRk_RaiN is offline   Reply With Quote
Old 03/12/2012, 06:28 PM   #3
Socan
Huge Clucker
 
Socan's Avatar
 
Join Date: May 2012
Location: Australia
Posts: 232
Reputation: 23
Default Re: Making a /renameaccount Command - Using ZCMD + SScanf 2

nice tutorial.
__________________
SHUT UP AND ENJOY THE MUSIC
╭∩╮(︶︿︶)╭∩╮


Socan is offline   Reply With Quote
Old 03/12/2012, 06:34 PM   #4
gtakillerIV
High-roller
 
gtakillerIV's Avatar
 
Join Date: Sep 2011
Location: UAE, Sharjah.
Posts: 1,002
Reputation: 201
Default Re: Making a /renameaccount Command - Using ZCMD + SScanf 2

Quote:
Originally Posted by DaRk_RaiN View Post
Nice job.
Thanks bro.

Quote:
Originally Posted by Socan View Post
nice tutorial.
Thank you very much.

Last edited by gtakillerIV; 31/08/2013 at 10:14 AM.
gtakillerIV is offline   Reply With Quote
Old 04/12/2012, 05:57 AM   #5
XtremeR
Gangsta
 
XtremeR's Avatar
 
Join Date: Aug 2012
Location: Australia
Posts: 894
Reputation: 565
Default Re: Making a /renameaccount Command - Using ZCMD + SScanf 2

Very nice! +5 reps!
__________________
XtremeR is offline   Reply With Quote
Old 04/12/2012, 07:12 AM   #6
RajatPawar
High-roller
 
RajatPawar's Avatar
 
Join Date: Aug 2011
Location: ヽ༼ຈل͜ຈ༽ノ
Posts: 2,276
Reputation: 311
Default Re: Making a /renameaccount Command - Using ZCMD + SScanf 2

Nice one, looks like you really put effort into it! +rep.
RajatPawar is offline   Reply With Quote
Old 04/12/2012, 08:41 AM   #7
JaKe Elite
High-roller
 
Join Date: Apr 2012
Location: Philippines
Posts: 3,698
Reputation: 484
Default Re: Making a /renameaccount Command - Using ZCMD + SScanf 2

Not bad. But i'm not impress..
__________________


making a comeback soon
JaKe Elite is offline   Reply With Quote
Old 04/12/2012, 09:41 AM   #8
gtakillerIV
High-roller
 
gtakillerIV's Avatar
 
Join Date: Sep 2011
Location: UAE, Sharjah.
Posts: 1,002
Reputation: 201
Default Re: Making a /renameaccount Command - Using ZCMD + SScanf 2

Quote:
Originally Posted by XtremeR View Post
Very nice! +5 reps!
Thanks bro.

Quote:
Originally Posted by Rajat_Pawar View Post
Nice one, looks like you really put effort into it! +rep.
Thanks.

Quote:
Originally Posted by Romel View Post
Not bad. But i'm not impress..
Well I don't know what to tell ya, but thanks
gtakillerIV is offline   Reply With Quote
Old 04/12/2012, 01:41 PM   #9
Lordzy
High-roller
 
Lordzy's Avatar
 
Join Date: Mar 2012
Location: NetherRealm
Posts: 2,759
Reputation: 1175
Default Re: Making a /renameaccount Command - Using ZCMD + SScanf 2

You could explain how does the stocks do perform so it'll be more better and can be considered as a good tutorial. Not bad anyway.
__________________
Currently inactive - I don't play at any SA-MP servers nor work on anything in PAWN for now. The projects that I've done so far in PAWN, which requires updates will be taking some time.
Lordzy is offline   Reply With Quote
Old 04/12/2012, 06:16 PM   #10
xMCx
Huge Clucker
 
xMCx's Avatar
 
Join Date: May 2012
Location: Mars.
Posts: 252
Reputation: 23
Default Re: Making a /renameaccount Command - Using ZCMD + SScanf 2

Quote:
Originally Posted by Lordz View Post
You could explain how does the stocks do perform so it'll be more better and can be considered as a good tutorial. Not bad anyway.
But good job ,thanks
xMCx is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] Making a simple PM system with ZCMD & sscanf Toni Tutorials 36 30/04/2018 01:24 PM
[Tutorial] Making a easy /setadmin command with some admin commands (zcmd & sscanf) Tanush123 Tutorials 41 30/07/2013 05:54 AM
[Tutorial] Making a /announce command in ZCMD and sscanf eеe Tutorials 17 18/08/2012 07:53 AM
[Tutorial] Making a very simple VIP system with /setvip command using dini+sscanf+ZCMD electrux Tutorials 2 25/07/2012 09:00 AM
[Tutorial] Making /vw (virtualworld) command using ZCMD and sscanf electrux Tutorials 19 24/07/2012 10:51 AM


All times are GMT. The time now is 01:27 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.