View Single Post
devicenull
Veteran Member
Join Date: Mar 2004
Location: CT
Old 08-01-2004 , 15:40  
#2

Let's start off with how to get some arguments from the commandline. We will create a basic amx_kick plugin.
Here's the code for it, I will explain it after.
Code:
#include <amxmodx> public plugin_init() {     register_plugin("Amx-Kick","0.1","devicenull")     register_concmd("amx_kick2","do_kick",ADMIN_KICK," user Kicks the specified user") } public do_kick(id) {     if (!(get_user_flags(id)&ADMIN_KICK)) {         console_print(id,"[AMXX] No access")         return PLUGIN_HANDLED     }     if (read_argc() == 0) {         console_print(id,"[AMXX] You must specify a user")         return PLUGIN_HANDLED     }     new user[32], uid     read_argv(1,user,32)     uid = find_player("bh",user)     if (uid == 0) {         console_print(id,"[AMXX] Invalid User Id")         return PLUGIN_HANDLED     }     client_cmd(uid,"echo Kicked from server!")     client_cmd(uid,"disconnect")     console_print(id,"Kicked player!")     return PLUGIN_HANDLED }

Code:
#include <amxmodx>
The first thing I do is include a file called "amxmodx.inc". This file contains basic commands for working with players, and is included in every plugin. Open this file up, its located in amxx\scriping\include. As you can see, every command in it is documented. There are different includes you can use in addition to this one, if you are working with other modules, like engine or cstrike.

Code:
public plugin_init() {     register_plugin("Amx-Kick","0.1","devicenull")     register_concmd("amx_kick2","do_kick",ADMIN_KICK," user Kicks the specified user") }
In register_plugin, I tell AmxX the name of my plugin, the version, and the author name. This line is in almost every plugin, except for some very very small ones. Its a good idea to put this line in your plugins.
Next, in register_concmd, I tell AmxX that I am creating a new command called "amx_kick2", that it should call "do_kick" when a client uses this command. It also specifys the access level needed (ADMIN_KICK) and the help for the command. You almost always have several of these, and there are different types:
  • register_srvcmd() - This is used for commands that are only done in the server console (And over RCON)
  • register_clcmd() - This is used for commands that are only done on clients
  • register_concmd() - This is used for commands that can be done anywhere

Code:
public do_kick(id) {
This is the code that will be executed for amx_kick2. The variable id is the id of the player that called this command. It is a number from 1-32, representing a slot on the server. This is the first number you see in a "status" display.

Code:
if (!(get_user_flags(id)&ADMIN_KICK)) {     console_print(id,"[AMXX] No access")     return PLUGIN_HANDLED }
This bit of code is used to make sure the player has the correct access. There are a few different ways of doing this, and this is the one that seems simplest to me. I could explain how it works, but its really not that important to know. If the player does not have the correct access, "[AMXX] No access" is printed to their console, and the "return PLUGIN_HANDLED" stops code execution

Code:
if (read_argc() == 0) {     console_print(id,"[AMXX] You must specify a user")     return PLUGIN_HANDLED }
Here we check if they executed the command correctly, it needs to have a user after it (amx_kick2 Username). If it doesn't, we can't kick anyone, because we don't know who to kick. read_argc() returns the number of arguments after the command. If there are no arguments, then it prints "[AMXX] You must specify a user", and we stop code execution.

Code:
new user[32], uid
Here we declare two variables, an array named user, and an integer named uid. Arrays are used to hold strings in small.

Code:
read_argv(1,user,32)
With this command, we get the first argument of the command, and put it into the user variable. The maximum length of the argument that we would get is 32 characters.

Code:
uid = find_player("bh",user)
With this, we take the argument, and try and match it to a username. The "bh" means Ignore bots and find users with the given part of the name.

Code:
    if (uid == 0) {     console_print(id,"[AMXX] Invalid User Id")     return PLUGIN_HANDLED }
This makes sure that the user we found was an actual user. It works just like the other checks we did above.

Code:
client_cmd(uid,"echo Kicked from server!") client_cmd(uid,"disconnect")
Here, we execute two commands on the targets computer. One echos "Kicked from server!" into their console, the other disconnects them from the game. There are many ways to kick clients, this is one of the simpler ones.

Code:
console_print(id,"Kicked player!")
This prints a message to the client that kicked the player, telling them that the player was kicked.

Code:
    return PLUGIN_HANDLED }
This halts code execution.

A note, the "return PLUGIN_HANDLED" prevents Half life from displaying "Command not found".
devicenull is offline