Registering Commands

This section extends the plugin created in the tracking players section to report the number of name changes back to a client using a registered command.

We are going to register the command admin_namechanges to report back the number of times each player has changed their name.

First we add a call to register_cmd in the plugin_init function. This tells Admin Mod we would like our function AdminNameChanges called when any player types admin_namechanges in their client console.

public plugin_init() {
  plugin_registerinfo("Player tracking Plugin","Counts player name changes",g_Version);
  plugin_registercmd("admin_namechanges","AdminNameChanges",ACCESS_ALL,
                     "admin_namechanges: Report how many times each player has changed their name");

  return PLUGIN_CONTINUE;
}

Implementing the command handler

In order for Admin Mod to be able to call our command handler, we have to make it a public function. There is also a fixed number of arguments all command handlers must provide, even though at the moment, we are ignoring them all as our handler doesn't need them.

The loop in this function is extremely common in plugins. We are looping through every possible user index to see if there is currently a player occupying that slot. This is indicated by the playerinfo() function returning 1.

NOTE: the command handler returns PLUGIN_HANDLED, as this is a custom command, and if we allow other plugins to handle it by returning PLUGIN_CONTINUE, it will drop back to Admin Mod which will log it an unhandled command.

/* Command Handlers */

public AdminNameChanges(HLCommand,HLData,HLUserName,UserIndex) {
  new c = maxplayercount();
  new i;
  new name[MAX_NAME_LENGTH];
  new msg[MAX_TEXT_LENGTH];

  for (i=1; i<=c; i++) {
    if (playerinfo(i, name, MAX_NAME_LENGTH)==1) {
      snprintf(msg,MAX_TEXT_LENGTH,"%s has changed their name %i times.", name, g_NameChanges[i]);
      selfmessage(msg);
    }
  }
  return PLUGIN_HANDLED;
}

Command arguments

Admin Mod passes any command arguments in HLData. Lets extend our command so that if a name is supplied, we only report back on the number of times that player has changed their name. If no argument is supplied we report the number of name changes for every player.

As with all strings from Admin Mod, you must call convert_string on HLData before it can be used. We use the local variable display to determine if each player's name changes should be displayed or not.

public AdminNameChanges(HLCommand,HLData,HLUserName,UserIndex) {
  new c = maxplayercount();
  new i;
  new name[MAX_NAME_LENGTH];
  new msg[MAX_TEXT_LENGTH];
  new display;
  new search[MAX_DATA_LENGTH];
  convert_string(HLData,search,MAX_DATA_LENGTH);

  for (i=1; i<=c; i++) {
    if (playerinfo(i, name, MAX_NAME_LENGTH)==1) {
      display = 1;
      if (strlen(search)>0) {
        if (streq(search,name)==0) {
          display = 0;
        }
      }
      if (display) {
        snprintf(msg,MAX_TEXT_LENGTH,"%s has changed their name %i times.", name, g_NameChanges[i]);
        selfmessage(msg);
      }
    }
  }
  return PLUGIN_HANDLED;
}

Complete source code

Note that we have included a forward declaration for our command handler, as there isn't one predefined for us in the include files like there is for plugin_init, plugin_info and plugin_disconnect. This particular plugin would compile fine without it, but its good habit to get in to from the start.

Paste this code over the old version and recompile. You should then be able to issue the command admin_namechanges in your client console to get a report on the number of name changes by each player.

NOTE: To change your name, type "setinfo name <newname>" in the client console.

/*********************************************************
 * Player tracking plugin - Version 1.1                  *
 *********************************************************
 *                                                       *
 * Name: plugin_track                                    *
 * Author: ravenousbugblatterbeast@hotmail.com           *
 * Released: 1st September 2002                          *
 *                                                       *
 * Version 1.1:                                          *
 *                                                       *
 *  -  Command added to report number of changes         *
 *                                                       *
 * Version 1.0:                                          *
 *                                                       *
 *  -  Initial version                                   *
 *                                                       *
 *********************************************************
 */

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

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


/* Function Declarations */
forward AdminNameChanges(HLCommand,HLData,HLUserName,UserIndex);

/* Event Handlers */
public plugin_init() {
  plugin_registerinfo("Player tracking Plugin","Counts player name changes",g_Version);
  plugin_registercmd("admin_namechanges","AdminNameChanges",ACCESS_ALL,
                     "admin_namechanges: Report how many times each player has changed their name");

  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;
}


/* Command Handlers */

public AdminNameChanges(HLCommand,HLData,HLUserName,UserIndex) {
  new c = maxplayercount();
  new i;
  new name[MAX_NAME_LENGTH];
  new msg[MAX_TEXT_LENGTH];
  new display;
  new search[MAX_DATA_LENGTH];
  convert_string(HLData,search,MAX_DATA_LENGTH);

  for (i=1; i<=c; i++) {
    if (playerinfo(i, name, MAX_NAME_LENGTH)==1) {
      display = 1;
      if (strlen(search)>0) {
        if (streq(search,name)==0) {
          display = 0;
        }
      }
      if (display) {
        snprintf(msg,MAX_TEXT_LENGTH,"%s has changed their name %i times.", name, g_NameChanges[i]);
        selfmessage(msg);
      }
    }
  }
  return PLUGIN_HANDLED;
}