06.07.2014, 12:13
(
Последний раз редактировалось Smileys; 22.02.2019 в 22:31.
)
Hi,
Introduction
alright, so looking at those old(er) tutorials of how to make a /suspend, /ban or /unban command, they use pretty complicated methods, which is pretty obvious as there wasn't a SA-MP 0.3z yet. therefore I've decided to create a tutorial which shows how easy you can actually suspend, ban and unban a player with the new 0.3z functions.
Requirements
sscanf - for the command parameters. you can get it right here
zcmd - I'll be using zcmd for this tutorial. available here
Lets Go
lets include sscanf and zcmd so we can use it in our commands.
at the top of your script, under "#include <a_samp>", add this:
also let's add these natives to make sure you won't get any "undefined" errors.
for more info on these functions, please visit this page
under your defines and includes, add:
then lets create 2 functions that'll help us when building the commands.
explanation is commented in the code, please make sure you read it!
Then let's create our first command!
at the bottom of your script(or wherever you want your commands to be, if you don't know where to put it, put it at the bottom of your script) add this.
once again, explanation is commented in the code, please make sure you read it!
then we have our /suspend command, once again there's explanation in the code itself.
and last but not least, the unban command, you can use this command to unban a player, but also to unsuspend a player, it'll work either way.
Well that's all, basically you've just created your own ban/unban system!
You could also create a file/database table which stores the banned player's IP Address and name, and then change the /unban command parameter to player-name, then check whether there's a matching IP-Address in the file/Database table, if so, it unblocks the IP Address, if not it gives a message like "This player isn't banned".
if there are any questions, feel free to leave a reply below.
Special Thanks to ****** and Zeex for creating sscanf and zcmd!
Regards,
Smileys
Introduction
alright, so looking at those old(er) tutorials of how to make a /suspend, /ban or /unban command, they use pretty complicated methods, which is pretty obvious as there wasn't a SA-MP 0.3z yet. therefore I've decided to create a tutorial which shows how easy you can actually suspend, ban and unban a player with the new 0.3z functions.
Requirements
sscanf - for the command parameters. you can get it right here
zcmd - I'll be using zcmd for this tutorial. available here
Lets Go
lets include sscanf and zcmd so we can use it in our commands.
at the top of your script, under "#include <a_samp>", add this:
pawn Код:
#include <sscanf2>
#include <zcmd>
for more info on these functions, please visit this page
under your defines and includes, add:
pawn Код:
native BlockIpAddress(ip_address[], timems); // blocks an IP address from further communication (wildcards allowed)
native UnBlockIpAddress(ip_address[]); // IP unblock
explanation is commented in the code, please make sure you read it!
pawn Код:
// at the top of your script, under includes and defines, add this.
stock GetPlayersName( playerid )
{
// creating a variable to store the player's name in.
new name[ MAX_PLAYER_NAME ];
// getting the player's name and storing it into the variable
GetPlayerName( playerid, name, sizeof( name ) );
return name; // returning the player's name.
}
stock ReturnPlayerIp( playerid )
{
// creating a variable to store the player's IP address in.
new Ip[ 16 ];
// getting the IP address of the player and storing it into the variable.
GetPlayerIp( playerid, Ip, sizeof( Ip ) );
return Ip; // returning the IP address.
}
at the bottom of your script(or wherever you want your commands to be, if you don't know where to put it, put it at the bottom of your script) add this.
once again, explanation is commented in the code, please make sure you read it!
pawn Код:
CMD:ban( playerid, params[ ] )
{
// checking whether the player is an RCON admin or not, if you have your own admin variables/arrays, you can simply replace it.
// if the player isn't logged into the RCON system, it'll send a message saying he can't use this command.
if( !IsPlayerAdmin( playerid ) ) return SendClientMessage( playerid, 0xFFFF0000, "Error: You aren't authorized to use this command!" );
// Defining variables for our parameters.
new targetid, reason[ 64 ];
// grabbing the parameters with sscanf, let's go through it step-by-step.
// params --> Checks the parameters of the command that's submitted, if the parameters are empty, it'll send a message on how to use this command.
// targetid and reason are stored into this function to assign the parameters to them, so we can then use those variables later.
// for more documentation on how the sscanf function works, please read: https://github.com/Y-Less/sscanf/wiki
if( sscanf( params, "us[64]", targetid, reason ) ) return SendClientMessage( playerid, 0xFF7100FF, "Usage: /ban [playerid] [reason]" );
// targetid == INVALID_PLAYER_ID --> checks whether the targeted player is an invalid playerid, which means the player either disconnected, or an invalid playerid has been entered.
// playerid == targetid --> checks whether the targeted player isn't the player sending the command, this makes sure you can't ban yourself, remove it if you want to ban yourself :P
if( targetid == INVALID_PLAYER_ID || playerid == targetid ) return SendClientMessage( playerid, 0xFFFF0000, "Error: Invalid playerid." );
// creating a string to store the message in.
new str[ 128 ];
// formatting the message, and sending it to everyone online. for more information on this function please visit the SA-MP wiki.
format( str, sizeof( str ), "%s has banned %s for %s.", GetPlayersName( playerid ), GetPlayersName( targetid ), reason );
SendClientMessageToAll( -1, str );
// Blocks the targeted player's IP from any furthur communication with the server
// for more documentation on this function, please check https://sampwiki.blast.hk/wiki/BlockIpAddress
BlockIpAddress( ReturnPlayerIp( targetid ), 0 );
return 1;
}
pawn Код:
CMD:suspend( playerid, params[ ] )
{
// checking whether the player is an RCON admin or not, if you have your own admin variables/arrays, you can simply replace it.
// if the player isn't logged into the RCON system, it'll send a message saying he can't use this command.
if( !IsPlayerAdmin( playerid ) ) return SendClientMessage( playerid, 0xFFFF0000, "Error: You aren't authorized to use this command!" );
// Defining variables for our parameters.
new targetid, reason[ 64 ], hours, days;
// grabbing the parameters with sscanf, let's go through it step-by-step.
// params --> Checks the parameters of the command that's submitted, if the parameters are empty, it'll send a message on how to use this command.
// targetid, hours, days and reason are stored into this function to assign the parameters to them, so we can then use those variables later.
// for more documentation on how the sscanf function works, please read: https://github.com/Y-Less/sscanf/wiki
if( sscanf( params, "uiis[64]", targetid, hours, days, reason ) ) return SendClientMessage( playerid, 0xFF7100FF, "Usage: /suspend [playerid] [hours] [days] [reason]" );
// targetid == INVALID_PLAYER_ID --> checks whether the targeted player is an invalid playerid, which means the player either disconnected, or an invalid playerid has been entered.
// playerid == targetid --> checks whether the targeted player isn't the player sending the command, this makes sure you can't ban yourself, remove it if you want to ban yourself :P
if( targetid == INVALID_PLAYER_ID || playerid == targetid ) return SendClientMessage( playerid, 0xFFFF0000, "Error: Invalid playerid." );
// creating a new variable which will convert our hours and days into milliseconds, as the BlockIpAddress uses milliseconds as second parameter.
new total_suspension = ( ( ( ( hours * 60 ) * 60 ) * 1000 ) + ( ( ( ( days * 24 ) * 60 ) * 60 ) * 1000 ) );
// creating a string to store the message in.
new str[ 186 ];
// formatting the message, and sending it to everyone online. for more information on this function please visit the SA-MP wiki.
format( str, sizeof( str ), "%s has suspended %s for %i days and %i hours, reason: %s", ReturnPlayerName( playerid ), ReturnPlayerName( targetid ), days, hours, reason );
SendClientMessageToAll( -1, str );
// Blocks the targeted player's IP from any furthur communication with the server for the given amount of time.
// for more documentation on this function, please check https://sampwiki.blast.hk/wiki/BlockIpAddress
BlockIpAddress( ReturnPlayerIp( targetid ), total_suspension );
return 1;
}
pawn Код:
CMD:unban( playerid, params[ ] )
{
// checking whether the player is an RCON admin or not, if you have your own admin variables/arrays, you can simply replace it.
// if the player isn't logged into the RCON system, it'll send a message saying he can't use this command.
if( !IsPlayerAdmin( playerid ) ) return SendClientMessage( playerid, 0xFFFF0000, "Error: You aren't authorized to use this command!" );
// Defining a variable for our parameter.
new ip_address[ 16 ];
// grabbing the parameters with sscanf, let's go through it step-by-step.
// params --> Checks the parameters of the command that's submitted, if the parameters are empty, it'll send a message on how to use this command.
// playername will simply store the given playername into our variable, so we can use it later on.
// for more documentation on how the sscanf function works, please read: https://github.com/Y-Less/sscanf/wiki
if( sscanf( params, "s[16]", ip_address ) ) return SendClientMessage( playerid, 0xFF7100FF, "Usage: /unban [ip-address]" );
// creating a string to store the message in.
new str[ 94 ];
// formatting the message, and sending it to everyone online. for more information on this function please visit the SA-MP wiki.
format( str, sizeof( str ), "You've unbanned IP: %s", ip_address );
SendClientMessagel(playerid, -1, str );
UnBlockIpAddress( ip_address );
return 1;
}
You could also create a file/database table which stores the banned player's IP Address and name, and then change the /unban command parameter to player-name, then check whether there's a matching IP-Address in the file/Database table, if so, it unblocks the IP Address, if not it gives a message like "This player isn't banned".
if there are any questions, feel free to leave a reply below.
Special Thanks to ****** and Zeex for creating sscanf and zcmd!
Regards,
Smileys