wyvern.kernel.player
Class CommandPreprocessor

java.lang.Object
  extended bywyvern.kernel.player.CommandPreprocessor

public final class CommandPreprocessor
extends java.lang.Object

Takes incoming commands and decides what to do with them. Commands can come from the client, or the server can place the commands in the player's queue.

This class handles alias and multi-command expansion, command history expansion and substitution, command tracking, updating the idle timer, and other activities associated with preprocessing player commands.

Version:
1.0, May 04, 2003
Author:
Steve Yegge

Field Summary
static int COMMAND_HISTORY_LENGTH
          Number of commands we'll store in the command history.
 
Constructor Summary
CommandPreprocessor(Player player)
          Constructs a new CommandPreprocessor
 
Method Summary
protected  void addToHistoryList(java.lang.String cmd)
          Adds a command to the player's history list, so it can be retrieved or examined later.
protected  void doEventTracking(java.lang.String cmd, boolean user)
          Logs a player's command if we're tracking them.
protected  java.lang.String expandAlias(java.lang.String cmd, java.lang.String verb, java.lang.String args)
          Looks at the command verb for the passed command, and does alias expansion.
 java.lang.String findLastMatchingCommand(java.lang.String prefix)
          Looks through the history list to find the most recent command that starts with the passed prefix.
 void handleCommand(java.lang.String cmd, boolean user)
          Deals with a command coming in to the player via the command() method.
protected  boolean handleControlCommand(java.lang.String cmd)
          Handles some special control characters for doing history substitution.
protected  void handleMacroCommand(java.lang.String cmd, boolean user)
          Splits a multiple-command macro into its sub-commands and enqueues them with the player.
protected  void handleNonMacroCommand(java.lang.String cmd, boolean user)
          Handles enqueueing a command string that's not going to be parsed as a multiple-command macro.
protected  void initNoMacroList()
          Initializes the list of command verbs that we don't allow to appear in multi-command macros.
protected  void processCommand(java.lang.String cmd, boolean user)
          Processes a command without doing "!"
protected  boolean tryHistoryExpansion(java.lang.String cmd)
          Checks if the command starts with "!"
protected  boolean tryHistorySubstitution(java.lang.String cmd)
          Attempts to perform a history-substitution on the previous command.
 void updateHistory(java.lang.String cmd)
          Adds the most recent command to the player's history list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMMAND_HISTORY_LENGTH

public static final int COMMAND_HISTORY_LENGTH
Number of commands we'll store in the command history. We skip certain commands, such as "again", and client-control commands like "#refresh". This should eventually be player-configurable, possibly.

See Also:
Constant Field Values
Constructor Detail

CommandPreprocessor

public CommandPreprocessor(Player player)
Constructs a new CommandPreprocessor

Method Detail

handleCommand

public void handleCommand(java.lang.String cmd,
                          boolean user)
Deals with a command coming in to the player via the command() method.

Parameters:
cmd - a command string
user - true if the command was player-initiated (i.e. it came from the client.)

processCommand

protected void processCommand(java.lang.String cmd,
                              boolean user)
Processes a command without doing "!" and "^" expansions. This is invoked by handleControlCommand() if the command started with "!" or "^".

Parameters:
cmd - a command string
user - true if the command was player-initiated (i.e. it came from the client.)

expandAlias

protected java.lang.String expandAlias(java.lang.String cmd,
                                       java.lang.String verb,
                                       java.lang.String args)
Looks at the command verb for the passed command, and does alias expansion.

Parameters:
cmd - the command the player typed
verb - the first whitespace-delimited token of the command
args - the remainder of the command after the verb, or null if there were no args
Returns:
the command string, with substitutions done. Example: "tr hi there" changes to "tell rhialto hi there", if the player had "tr" aliased to "tell rhialto".

handleNonMacroCommand

protected void handleNonMacroCommand(java.lang.String cmd,
                                     boolean user)
Handles enqueueing a command string that's not going to be parsed as a multiple-command macro.

Parameters:
cmd - the command string
user - true if player-initiated

handleMacroCommand

protected void handleMacroCommand(java.lang.String cmd,
                                  boolean user)
Splits a multiple-command macro into its sub-commands and enqueues them with the player.

Parameters:
cmd - the command to split
user - true if user-initiated

doEventTracking

protected void doEventTracking(java.lang.String cmd,
                               boolean user)
Logs a player's command if we're tracking them.

Parameters:
cmd - a command going into the player's queue
user - true if it's user-initiated

updateHistory

public void updateHistory(java.lang.String cmd)
Adds the most recent command to the player's history list.

Parameters:
cmd - the command to add to the history list
Returns:
true if user-initiated. If true, the method just exits.

addToHistoryList

protected void addToHistoryList(java.lang.String cmd)
Adds a command to the player's history list, so it can be retrieved or examined later. Removes oldest command if the list has exceeded the maximum command-history length.

You should store commands in this list in pre-expanded form; for example, if the user types "foo bag" (an alias for something), then "!foo" should retrieve the "foo bag" command, not what it expanded into.

Parameters:
cmd - the command to add.

handleControlCommand

protected boolean handleControlCommand(java.lang.String cmd)
Handles some special control characters for doing history substitution.

Parameters:
cmd - the command the player typed
Returns:
true if it was a control command that we dealt with

tryHistoryExpansion

protected boolean tryHistoryExpansion(java.lang.String cmd)
Checks if the command starts with "!", and if so, looks in the player's command-history list for a command matching whatever follows the "!", and enqueues that previous command. Calls processCommand() to enqueue the command, so it will do normal alias and macro expansion on the replacement command.

Parameters:
cmd - a command the player typed in
Returns:
true if we recognized it as a history-expansion request, regardless of whether we found and enqueued a matching command in the command history. If we didn't find a matching command, then we print a message to the player.

tryHistorySubstitution

protected boolean tryHistorySubstitution(java.lang.String cmd)
Attempts to perform a history-substitution on the previous command.

Parameters:
cmd - the command the user typed
Returns:
true if we recognized it as a history substitution, regardless of whether the substitution was successful. If it was NOT successful, then we issue a message to the player.

findLastMatchingCommand

public java.lang.String findLastMatchingCommand(java.lang.String prefix)
Looks through the history list to find the most recent command that starts with the passed prefix.

Parameters:
prefix - the beginning of a command to match
Returns:
the most recent command that starts with that prefix, or null if none matched.

initNoMacroList

protected void initNoMacroList()
Initializes the list of command verbs that we don't allow to appear in multi-command macros.