Tracking Player Names

This section shows the creation of a simple plugin to report on the number of times each player has changed their name. This demonstrates some of the principal techniques in creating Admin Mod plugins.

We start with the standard template:

/*********************************************************
 * Player tracking plugin - Version 1.0                  *
 *********************************************************
 *                                                       *
 * Name: plugin_track                                    *
 * Author: ravenousbugblatterbeast@hotmail.com           *
 * Released: 1st September 2002                          *
 *                                                       *
 * Version 1.0:                                          *
 *                                                       *
 *  -  Initial version                                   *
 *                                                       *
 *********************************************************
 */

/* Includes */
#include <core>
#include <console>
#include <string>
#include <plugin>
#include <admin>
#include <adminlib>

/* Constants */

/* The access level for the command admin_sample */

/* Global Variables */
new g_Version[]="1.0"	/* Plugin version number */


/* Function Declarations */

/* Event Handlers */
public plugin_init() {
  plugin_registerinfo("Player tracking Plugin","Counts player name changes",g_Version);
  return PLUGIN_CONTINUE;
}

/* Command Handlers */

/* Support Functions */

Storing the name change counts

In order to track player name changes we need an array to hold the counters of how many times each player has changed name.

/* Global Variables */
new g_Version[]="1.0";	/* Plugin version number */
new g_NameChanges[MAX_PLAYERS];	/* Number of times each player has changed their name */

Intercepting name changes

Next we need to know about when players join and leave the server, as well as when they change their name. Therefore we need to handle the plugin_info event (called on joining and on name changes) and the plugin_disconnect event.

The plugin_info function demonstrates an important point about receiving strings from Admin Mod. Small plugins use an array of 32-Bit values to process strings. However, the half-life engine and Admin Mod itself use one-byte-per-character. By convention all strings that are in the half-life format are prefixed with HL. You can convert them into a usable format using the convert_string native function.

plugin_info is called both when a user connects, and when they change their name. You can differentiate between the two situations because when the user is connecting, their OldName is empty. It is generally recommended that if you can, you handle player connection event inside plugin_info by performing this test than be handling plugin_connect. This is because the player will have got further through the connection process when plugin_info is called. There are some Admin Mod native functions that will not work correctly if called from plugin_connect.

/* Event Handlers */
public plugin_info(HLOldName, HLNewName, UserIndex) {
  new OldName[MAX_NAME_LENGTH];
  new NewName[MAX_NAME_LENGTH];
  convert_string(HLOldName,OldName,MAX_NAME_LENGTH);
  convert_string(HLNewName,NewName,MAX_NAME_LENGTH);

  if (strlen(OldName) == 0) {
    /* Player is connecting */
    g_NameChanges[UserIndex] = 0;
  }
  else {
    if (streq(OldName,NewName) == 0) {
      /* Player is changing their name */
      g_NameChanges[UserIndex] = g_NameChanges[UserIndex] + 1;
    }
  }

  return PLUGIN_CONTINUE;
}

public plugin_disconnect(HLUserName, UserIndex) {
  /* Player is connecting */
  g_NameChanges[UserIndex] = 0;
  return PLUGIN_CONTINUE;
}

Producing output

We now have a plugin that counts the number of times each player has changed their names, but we don't have any way to see the output. Let's send it to the server console:

public plugin_info(HLOldName, HLNewName, UserIndex) {
  new OldName[MAX_NAME_LENGTH];
  new NewName[MAX_NAME_LENGTH];
  convert_string(HLOldName,OldName,MAX_NAME_LENGTH);
  convert_string(HLNewName,NewName,MAX_NAME_LENGTH);

  if (strlen(OldName) == 0) {
    /* Player is connecting */
    g_NameChanges[UserIndex] = 0;
  }
  else {
    if (streq(OldName,NewName) == 0) {
      /* Player is changing their name */
      g_NameChanges[UserIndex] = g_NameChanges[UserIndex] + 1;

      new msg[MAX_TEXT_LENGTH];
      snprintf(msg,MAX_TEXT_LENGTH,"Player %s has changed their name %i times",NewName,g_NameChanges[UserIndex]);
      log(msg);
    }
  }

  return PLUGIN_CONTINUE;
}

Complete source code

Paste this into a file, save it as plugin_track.sma and compile. Then add it into your plugin.ini file and you should see a message logged in your server console / log files each time a player changes their name stating how many times it has been changed.

/*********************************************************
 * Player tracking plugin - Version 1.0                  *
 *********************************************************
 *                                                       *
 * Name: plugin_track                                    *
 * Author: ravenousbugblatterbeast@hotmail.com           *
 * Released: 1st September 2002                          *
 *                                                       *
 * Version 1.0:                                          *
 *                                                       *
 *  -  Initial version                                   *
 *                                                       *
 *********************************************************
 */

/* Includes */
#include <core>
#include <console>
#include <string>
#include <plugin>
#include <admin>
#include <adminlib>

/* Global Variables */
new g_Version[]="1.0";	/* Plugin version number */
new g_NameChanges[MAX_PLAYERS];	/* Number of times each player has changed their name */


/* Function Declarations */

/* Event Handlers */
public plugin_init() {
  plugin_registerinfo("Player tracking Plugin","Counts player name changes",g_Version);
  return PLUGIN_CONTINUE;
}

public plugin_info(HLOldName, HLNewName, UserIndex) {
  new OldName[MAX_NAME_LENGTH];
  new NewName[MAX_NAME_LENGTH];
  convert_string(HLOldName,OldName,MAX_NAME_LENGTH);
  convert_string(HLNewName,NewName,MAX_NAME_LENGTH);

  if (strlen(OldName) == 0) {
    /* Player is connecting */
    g_NameChanges[UserIndex] = 0;
  }
  else {
    if (streq(OldName,NewName) == 0) {
      /* Player is changing their name */
      g_NameChanges[UserIndex] = g_NameChanges[UserIndex] + 1;

      new msg[MAX_TEXT_LENGTH];
      snprintf(msg,MAX_TEXT_LENGTH,"Player %s has changed their name %i times",NewName,g_NameChanges[UserIndex]);
      log(msg);
    }
  }

  return PLUGIN_CONTINUE;
}

public plugin_disconnect(HLUserName, UserIndex) {
  g_NameChanges[UserIndex] = 0;
  return PLUGIN_CONTINUE;
}