wyvern.kernel.commands
Class EventImpl

java.lang.Object
  extended bywyvern.kernel.properties.PList
      extended bywyvern.kernel.commands.EventImpl
All Implemented Interfaces:
Broadcaster, CommandEvent, PropertyList
Direct Known Subclasses:
CastEvent, CodeEvent, HelpCommand.HelpEvent, LoginEvent, MouseCommandEvent, MoveEvent, NullEvent, SayEvent, ShoutEvent, TargetedEvent, TurnCommand.TurnEvent

public class EventImpl
extends PList
implements CommandEvent

Wyvern's concrete implementation of the CommandEvent interface.

This class provides some parsing of the command string. In particular, it parses the verb (automatically, from the constructor), the ARGV array on demand when getArgs() is called, and the argument string on demand when getArgString() is called.

A command goes through several parsing phases, in the following order:

  1. Some initial checks are done when the command arrives from the client, if it's player-originated.
  2. wyvern.kernel.players.CommandPreprocessor handles alias and macro expansion, command history, and history substitution.
  3. EventImpl tokenizes the command text, and handles filters and redirects for wizards.
  4. The Command handler for the command does semantic parsing of the arguments.

Author:
weber (1997), stevey

Field Summary
protected  Commandable agent_
           
protected  boolean appendToFile_
           
protected  java.lang.String argString_
           
protected  boolean argStringParsed_
           
protected  java.lang.String[] argv_
           
protected  boolean argvParsed_
           
protected  long delay_
           
protected  java.lang.String failureMessage_
           
protected  CommandFilter[] filterObjects_
          A list of CommandFilters parsed from the filter expressions we found in the command.
protected  java.lang.String[] filters_
          A list of filter expressions that we found within the player's command.
protected  Command handler_
           
protected  GameMap map_
           
protected  boolean modified_
           
protected  java.lang.String originalText_
           
protected  boolean playerOriginated_
           
protected  java.io.File redirectFile_
          If the command ends with " > [filename]", then any calls to message() for the event will be redirected into the specified filename, replacing the existing contents of the file.
protected  boolean redirecting_
           
protected  boolean sendToAgent_
           
protected  java.lang.String successMessage_
           
protected  java.lang.String verb_
           
protected  boolean vetoed_
           
protected  boolean wizParsed_
           
 
Fields inherited from class wyvern.kernel.properties.PList
readOnly_
 
Fields inherited from interface wyvern.lib.PropertyList
PROPERTY_PACKAGE
 
Constructor Summary
EventImpl()
           
EventImpl(CommandEvent c)
          Constructs a new CommandEvent from the information in the passed CommandEvent.
EventImpl(java.lang.String text, Commandable agent)
          Constructs a new EventImpl with the specified text and agent.
 
Method Summary
 java.util.List broadcast(java.lang.String msg)
          Broadcasts a message to every player in sight of this object, not including the player who initiated it (if this is a player).
 java.util.List broadcast(java.lang.String msg, byte style)
          Broadcasts a message to every player in earshot of this object, not including the player who initiated it (if this is a player).
 java.util.List broadcast(java.lang.String msg, Predicate p)
          Sends the specified message to players in earshot of this object, not including the player who initiated it (if this is a player).
 java.util.List broadcast(java.lang.String msg, Predicate p, byte style)
          Sends the specified message to player in sight of this object, not including the player who initiated it (if this is a player).
 java.util.List broadcast(java.lang.String msg, Predicate p, byte style, int radius)
          Sends the specified message to everyone in sight.
 boolean checkVeto()
          If the event is vetoed, sends the event's failureMessage() to the event's agent.
 boolean fail()
          Issues the event's failure-message to the event's agent.
protected  java.lang.String filterMessage(java.lang.String message)
          Runs the message through any filters the player specified.
 java.util.List findAllMatches(Commandable agent, Predicate p)
          Finds all objects in the map, adjacent to or beneath the agent, that match the passed predicate.
 GameObject findByHashcode(java.lang.String hashcode)
          Looks for an object by its hashcode.
 GameObject findInMap(Commandable agent, Predicate p)
          Searches for an object matching the specified predicate.
 GameObject findNeighbor(Commandable agent, Predicate p)
          Searches for an object on the ground under and adjacent to the agent.
 GameObject findObject(Commandable agent, Predicate p)
          Searches for an object matching the specified predicate.
 GameObject findTarget(Commandable agent, Predicate p)
          Searches for an object matching the specified predicate.
 Commandable getAgent()
          Get the agent associated with this command event - usually a player or monster, but it can be anything with an event queue (such as a spell).
 java.lang.String[] getArgs()
          Gets the arguments to this command, split into an array.
 java.lang.String getArgString()
          Gets the arguments to the command as a whitespace-delimited string.
 long getDelay()
          Returns the delay associated with this event.
 java.lang.String getFailureMessage()
          Returns the "failure" message for this event.
 CommandFilter[] getFilterObjects()
          Returns the parsed CommandFilter objects that will be applied to the message for this event.
 java.lang.String[] getFilters()
          Returns the filter strings being applied to this event.
 Command getHandler()
          Returns the Command that will handle this event, if the Command is different from the one that created the event.
 GameMap getMap()
          Returns the map where this event is taking place.
 java.lang.String getOriginalText()
          Returns the original text for this command event, before removing aliases, prepositions, etc.
 java.io.File getRedirectFile()
          Returns the file that we're redirecting output to, if any.
 java.lang.String getSuccessMessage()
          Returns the "success" message for this event.
 java.lang.String getVerb()
          Returns the command verb for this event.
 boolean hasArgs()
          Returns true if there were any arguments to the event.
 boolean isAppending()
          Returns true if the redirect-to-file operator was "+>>" or ">>".
 boolean isModified()
          Returns true if this event has been modified by a hook callback.
 boolean isMonster()
          Returns true if the event's agent is a Monster (not a player).
 boolean isPlayer()
          Returns true if the event's agent is a Player (or wizard).
 boolean isPlayerOriginated()
          Returns true if a player actually typed in this command.
 boolean isRedirecting()
          True if there was a redirect operator detected at the end of the command: ">", ">>", "+>", or "+>>".
 boolean isSendingToAgent()
          Returns true if the redirect operator included a "+" to send the output to the agent as well.
 boolean isVetoed()
          Returns true if this event has been vetoed by someone.
 boolean isWizard()
          Returns true if the event's agent is a Wizard.
static void main(java.lang.String[] args)
          Function for testing the command parser.
 void message(java.lang.String msg)
          Sends a message to the event's agent.
 void message(java.lang.String msg, byte style)
          Sends a message to the event's agent, in the specified TextStyle.
 boolean noArgs()
          Returns true if there were no arguments to the event.
protected  void parseArgString()
          Parses the original text and sets argString_ variable to the substring of the text starting at the 2nd token, or null if there are no args.
protected  void parseARGV()
          Parses the original text and produces a String[] of tokens, following the verb.
protected  void parseVerb()
          Parses the verb from the head of the command string.
static java.lang.String profileParsing()
          Returns information about how long we've spent just doing event parsing in this class.
protected  boolean redirectMessage(java.lang.String msg)
          Sends the specified message to the output file.
 void runPostHooks(boolean success)
          Runs one of the event's post hooks. 99.9% of the time this is called by the game kernel after execute() runs.
 void runPreHooks()
          Runs the event's pre-hooks.
 GameObject searchInv(Predicate p)
          Searches the agent's inventory for the first object matching the passed Predicate.
 GameObject searchInvAndMapNearby(Predicate p)
          Searches for an object matching the specified predicate.
 GameObject searchInvAndMapUnder(Predicate p)
          Searches for an object matching the specified predicate.
 GameObject searchInvByHashcode(java.lang.String hashcode)
          Searches the agent's inventory for an object with the specified hashcode.
 GameObject searchMapNearby(Predicate p)
          Searches for an object on the ground under and adjacent to the agent.
 GameObject searchMapUnder(Predicate p)
          Searches for an object matching the specified predicate.
 GameObject searchMapUnderByHashcode(java.lang.String hashcode)
          Searches agent's inventory and the ground under the agent for an object with the specified hashcode.
 void setAgent(Commandable agent)
          Sets the agent for this event.
 void setArgs(java.lang.String[] args)
          Sets the arguments for this command.
 void setDelay(long delay)
          Sets the delay time to wait after this event is executed.
 void setFailureMessage(java.lang.String m)
          Sets the "failure" message for this event.
 void setHandler(Command handler)
          Sets the Command that will handle this event.
 void setMap(GameMap map)
          Sets the map where this event is taking place.
 void setModified(boolean mod)
          Sets the "modified" flag for this event.
 void setOriginalText(java.lang.String t)
          This method is here for the sake of completeness, but it's not recommended that objects change the original text of a command, as other objects may rely on it.
 void setPlayerOriginated(boolean po)
          Sets the event flag indicating that this command was originated by a player.
 void setSuccessMessage(java.lang.String m)
          Sets the "success" message for this event.
 void setVerb(java.lang.String verb)
          Convenience method; when constructing an event, a Command should set the verb; otherwise it'll have to be deduced from the command text, and the guess could be incorrect.
 void setVeto(boolean veto)
          Sets the veto-flag for this event.
 java.lang.String toString()
          Returns a string representation of this PropertyList.
 CommandEvent veto(java.lang.String msg)
          Sets the veto flag and the failure message for the event.
 
Methods inherited from class wyvern.kernel.properties.PList
addProperty, addTransientProperty, adjustDoubleProperty, adjustIntProperty, adjustLongProperty, adjustTransientDoubleProperty, adjustTransientIntProperty, adjustTransientLongProperty, countLocalProperties, debugProperties, dismantlePropertyList, getDoubleProperty, getInheritedProperty, getInteger, getIntProperty, getLocalProperties, getLocalProperty, getLongProperty, getParent, getPersistentDoubleProperty, getPersistentIntProperty, getPersistentLocalProperties, getPersistentLongProperty, getPersistentProperty, getProfilingInfo, getProperties, getProperties, getPropertiesIncludingTransients, getPropertiesIncludingTransients, getProperty, getSerializableProperties, getSerializableProperty, getStringProperty, getTransientDoubleProperty, getTransientIntProperty, getTransientLongProperty, getTransientProperties, getTransientProperty, hasLocalProperty, hasPersistentProperty, hasProperty, hasTransientProperty, inheritProperty, isReadOnly, isRemoved, isTransientlyRemoved, printLocalProperties, printProperties, printProperties, printTransientProperties, removeProperty, removeTransientProperty, setDoubleProperty, setIntProperty, setLongProperty, setParent, setProperty, setReadOnly, setTransientDoubleProperty, setTransientIntProperty, setTransientLongProperty, setTransientProperty, toString, transientlyRemoveProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface wyvern.lib.PropertyList
addProperty, addTransientProperty, adjustDoubleProperty, adjustIntProperty, adjustLongProperty, adjustTransientDoubleProperty, adjustTransientIntProperty, adjustTransientLongProperty, countLocalProperties, getDoubleProperty, getInheritedProperty, getIntProperty, getLocalProperties, getLocalProperty, getLongProperty, getParent, getPersistentDoubleProperty, getPersistentIntProperty, getPersistentLocalProperties, getPersistentLongProperty, getPersistentProperty, getProperties, getProperties, getPropertiesIncludingTransients, getPropertiesIncludingTransients, getProperty, getSerializableProperties, getSerializableProperty, getStringProperty, getTransientDoubleProperty, getTransientIntProperty, getTransientLongProperty, getTransientProperties, getTransientProperty, hasLocalProperty, hasPersistentProperty, hasProperty, hasTransientProperty, inheritProperty, isReadOnly, isRemoved, isTransientlyRemoved, printLocalProperties, printProperties, printProperties, printTransientProperties, removeProperty, removeTransientProperty, setDoubleProperty, setIntProperty, setLongProperty, setParent, setProperty, setReadOnly, setTransientDoubleProperty, setTransientIntProperty, setTransientLongProperty, setTransientProperty, toString, transientlyRemoveProperty
 

Field Detail

agent_

protected Commandable agent_

map_

protected GameMap map_

originalText_

protected java.lang.String originalText_

verb_

protected java.lang.String verb_

argString_

protected java.lang.String argString_

argStringParsed_

protected boolean argStringParsed_

argv_

protected java.lang.String[] argv_

argvParsed_

protected boolean argvParsed_

wizParsed_

protected boolean wizParsed_

modified_

protected boolean modified_

vetoed_

protected boolean vetoed_

successMessage_

protected java.lang.String successMessage_

failureMessage_

protected java.lang.String failureMessage_

delay_

protected long delay_

handler_

protected Command handler_

playerOriginated_

protected boolean playerOriginated_

redirecting_

protected boolean redirecting_

appendToFile_

protected boolean appendToFile_

sendToAgent_

protected boolean sendToAgent_

redirectFile_

protected java.io.File redirectFile_
If the command ends with " > [filename]", then any calls to message() for the event will be redirected into the specified filename, replacing the existing contents of the file. If the operator is ">>", it will append to the file. If the operator is "+>", the output will go to both the agent and the file, and if the operator is "+>>", it will go to the agent and append to the file. The filename is either an absolute path or a path relative to the wizard's current directory.


filters_

protected java.lang.String[] filters_
A list of filter expressions that we found within the player's command. Null if none were specified.


filterObjects_

protected CommandFilter[] filterObjects_
A list of CommandFilters parsed from the filter expressions we found in the command. Null if none were specified.

Constructor Detail

EventImpl

public EventImpl()

EventImpl

public EventImpl(java.lang.String text,
                 Commandable agent)
Constructs a new EventImpl with the specified text and agent. This constructor is used to create a "blank" event when someone issues a command to an agent. The Command handling the command will create a new EventImpl subclass containing more parameters for the event.

Parameters:
text - the original text of the command
agent - the Commandable (e.g. player) performing the command

EventImpl

public EventImpl(CommandEvent c)
Constructs a new CommandEvent from the information in the passed CommandEvent. This method is provided because an Command's createEvent method may wish to copy its parameter into a subclass of CommandEvent.

Parameters:
c - the initial event
Method Detail

isPlayer

public boolean isPlayer()
Description copied from interface: CommandEvent
Returns true if the event's agent is a Player (or wizard).

Specified by:
isPlayer in interface CommandEvent

isWizard

public boolean isWizard()
Description copied from interface: CommandEvent
Returns true if the event's agent is a Wizard.

Specified by:
isWizard in interface CommandEvent

isMonster

public boolean isMonster()
Description copied from interface: CommandEvent
Returns true if the event's agent is a Monster (not a player).

Specified by:
isMonster in interface CommandEvent

setAgent

public void setAgent(Commandable agent)
Description copied from interface: CommandEvent
Sets the agent for this event. Normally set only once, by the object that creates the event, but a hook object that knows what it's doing can change it if necessary. Changing it may have weird effects; the event will try to act as if the new agent executed it (e.g. if you change it to a different player, that player will execute the command.) There's no guarantee it will work in any given situation; the hook object has to combine a bit of knowledge and a bit of luck to make it work.

Specified by:
setAgent in interface CommandEvent
Parameters:
agent - the new agent to perform the command

getAgent

public Commandable getAgent()
Description copied from interface: CommandEvent
Get the agent associated with this command event - usually a player or monster, but it can be anything with an event queue (such as a spell).

Specified by:
getAgent in interface CommandEvent
Returns:
the agent for this event

setHandler

public void setHandler(Command handler)
Description copied from interface: CommandEvent
Sets the Command that will handle this event. Normally the Command handling the event is the same as the one that created the event, but not always. The game engine will check to see if this has changed before calling the handler to execute the event.

Specified by:
setHandler in interface CommandEvent
Parameters:
handler - the new Command to execute the command

getHandler

public Command getHandler()
Description copied from interface: CommandEvent
Returns the Command that will handle this event, if the Command is different from the one that created the event.

This is a somewhat rare occurrence; an example is the "move" command, which can result in a move, a push, an attack, or other events depending on the context. One Command will create the initial MoveEvent, and it may be transmogrified into an AttackEvent or other event, which requires a new handler to execute it.

This is the method the game engine calls to see if someone called setHandler() on the event during the hook callbacks.

Specified by:
getHandler in interface CommandEvent
Returns:
the Command to handle the event, or null to default to the Command that created the event.

getOriginalText

public java.lang.String getOriginalText()
Description copied from interface: CommandEvent
Returns the original text for this command event, before removing aliases, prepositions, etc.

Specified by:
getOriginalText in interface CommandEvent
Returns:
the original text of the command

setOriginalText

public void setOriginalText(java.lang.String t)
Description copied from interface: CommandEvent
This method is here for the sake of completeness, but it's not recommended that objects change the original text of a command, as other objects may rely on it.

Specified by:
setOriginalText in interface CommandEvent

getVerb

public java.lang.String getVerb()
Description copied from interface: CommandEvent
Returns the command verb for this event.

Specified by:
getVerb in interface CommandEvent
Returns:
the first (and sometimes only) word of the command text

setVerb

public void setVerb(java.lang.String verb)
Description copied from interface: CommandEvent
Convenience method; when constructing an event, a Command should set the verb; otherwise it'll have to be deduced from the command text, and the guess could be incorrect. It's set in the EventImpl constructor to the first word of the original text - the Command only needs to change it if this isn't the correct verb to use. (It almost always is).

Specified by:
setVerb in interface CommandEvent
Parameters:
verb - the command verb (e.g. "move")

setArgs

public void setArgs(java.lang.String[] args)
Description copied from interface: CommandEvent
Sets the arguments for this command. An Command usually does all its argument-parsing in the createEvent() method, before the event is passed to the hooks, so setting the args in a hook callback may have no effect. Some event classes (if their documentation says so) may re-check the arg array during execute() to see if it changed, but this is rare. Usually a hook callback will modify the target of the event directly, rather than the arguments naming the target.

Specified by:
setArgs in interface CommandEvent
Parameters:
args - the arguments to use to interpret the command

hasArgs

public boolean hasArgs()
Description copied from interface: CommandEvent
Returns true if there were any arguments to the event.

Specified by:
hasArgs in interface CommandEvent
Returns:
opposite of noArgs()

noArgs

public boolean noArgs()
Description copied from interface: CommandEvent
Returns true if there were no arguments to the event.

Specified by:
noArgs in interface CommandEvent
Returns:
true if the command no arguments, false if there were any non-whitespace characters following the command verb.

getArgs

public java.lang.String[] getArgs()
Description copied from interface: CommandEvent
Gets the arguments to this command, split into an array. This array is produced during the call to event.parse().

Specified by:
getArgs in interface CommandEvent
Returns:
the arg array, null if the command had no arguments.

getArgString

public java.lang.String getArgString()
Description copied from interface: CommandEvent
Gets the arguments to the command as a whitespace-delimited string.

Specified by:
getArgString in interface CommandEvent
Returns:
the argument string, basically everything after the command verb. Returns null if there were no args.

setVeto

public void setVeto(boolean veto)
Description copied from interface: CommandEvent
Sets the veto-flag for this event. Normally, you don't call this method directly, but instead call veto (msg). This is because the first thing a Command typically does when executing an event is check the veto flag, and if it's set, issues the event's failure message to the agent. So if you set the veto, you normally set an appropriate failure-msg in the event as well. veto(msg) does just this.

If you want to use the default failure message, you can call this method to set the flag. This method can also be used to clear the veto flag, meaning you want to un-veto the event.

Specified by:
setVeto in interface CommandEvent
Parameters:
veto - true to veto the event.

isVetoed

public boolean isVetoed()
Description copied from interface: CommandEvent
Returns true if this event has been vetoed by someone.

Specified by:
isVetoed in interface CommandEvent
Returns:
true if the event was vetoed; false if not.

checkVeto

public boolean checkVeto()
Description copied from interface: CommandEvent
If the event is vetoed, sends the event's failureMessage() to the event's agent. This is a useful call to make at the beginning of every CommandEvent's execute() method.

Specified by:
checkVeto in interface CommandEvent
Returns:
true if event was vetoed

veto

public CommandEvent veto(java.lang.String msg)
Description copied from interface: CommandEvent
Sets the veto flag and the failure message for the event.

Specified by:
veto in interface CommandEvent
Parameters:
msg - the failure message to send to the agent (i.e. the reason for vetoing the event).
Returns:
this event object, primarily for syntactic sugaring. It allows you to say "return event.veto(msg)", which you'll do often, as vetoing the event is usually cause for returning from the createEvent() handler.

fail

public boolean fail()
Description copied from interface: CommandEvent
Issues the event's failure-message to the event's agent. Equivalent to event.message(event.getFailureMessage())

Specified by:
fail in interface CommandEvent
Returns:
false - makes it more convenient in event handlers, which can simply say "return event.fail()" if the event was vetoed.

setSuccessMessage

public void setSuccessMessage(java.lang.String m)
Description copied from interface: CommandEvent
Sets the "success" message for this event. Typically set only once, by the Command creating the event. However, hook callbacks may change it where appropriate.

Specified by:
setSuccessMessage in interface CommandEvent
Parameters:
m - the message to display if the event succeeds.

getSuccessMessage

public java.lang.String getSuccessMessage()
Description copied from interface: CommandEvent
Returns the "success" message for this event. Used by the Command when it completes the event successfully and is ready to display feedback to the client. EventImpl subclasses may have more than one success message, and should provide getter and setter methods for the other messages.

Specified by:
getSuccessMessage in interface CommandEvent
Returns:
the message to display if the event succeeds.

setFailureMessage

public void setFailureMessage(java.lang.String m)
Description copied from interface: CommandEvent
Sets the "failure" message for this event. Typically set only once, by the Command creating the event. However, hook callbacks may change it where appropriate.

Specified by:
setFailureMessage in interface CommandEvent
Parameters:
m - the message to display if the event fails.

getFailureMessage

public java.lang.String getFailureMessage()
Description copied from interface: CommandEvent
Returns the "failure" message for this event. Used by the Command to display feedback to the agent when the command fails.

Specified by:
getFailureMessage in interface CommandEvent

setModified

public void setModified(boolean mod)
Description copied from interface: CommandEvent
Sets the "modified" flag for this event. HookCallbacks should be careful to set this flag if they change any parameters in the event that might invalidate the conditions for which the event is legal. For example, a callback changing the direction a player moves should set this flag, or the player could wind up walking into a wall.
Specified by:
setModified in interface CommandEvent
Parameters:
mod - true to mark the event as modified

isModified

public boolean isModified()
Description copied from interface: CommandEvent
Returns true if this event has been modified by a hook callback.

Specified by:
isModified in interface CommandEvent
Returns:
true if the event parameters were modified.

setDelay

public void setDelay(long delay)
Description copied from interface: CommandEvent
Sets the delay time to wait after this event is executed. The delay corresponds to how long it took the agent to perform the action.

Specified by:
setDelay in interface CommandEvent
Parameters:
delay - the command delay in milliseconds

getDelay

public long getDelay()
Description copied from interface: CommandEvent
Returns the delay associated with this event.

Specified by:
getDelay in interface CommandEvent
Returns:
the total delay for this command, in millis

isPlayerOriginated

public boolean isPlayerOriginated()
Description copied from interface: CommandEvent
Returns true if a player actually typed in this command.

Specified by:
isPlayerOriginated in interface CommandEvent
Returns:
true if command was originated by a player. If false, the command was placed in the queue by the game engine.

setPlayerOriginated

public void setPlayerOriginated(boolean po)
Description copied from interface: CommandEvent
Sets the event flag indicating that this command was originated by a player. Gives us a way to prevent the player from typing in the name of a game command that they shouldn't be able to originate (e.g. "teleport").

Specified by:
setPlayerOriginated in interface CommandEvent
Parameters:
po - true if it was originated by a user, via the client.

findNeighbor

public GameObject findNeighbor(Commandable agent,
                               Predicate p)
Description copied from interface: CommandEvent
Searches for an object on the ground under and adjacent to the agent. The squares are not guaranteed to be searched in any particular order. The search may intermix squares beneath the agent with squares adjacent to the agent.

NOTE: If you're coding a command that looks for a target at the agent's feet first, use findObject() instead. Otherwise you'll get weird bugs if there's an item with the same name next to the agent, since this may find that one first.

Specified by:
findNeighbor in interface CommandEvent
Parameters:
agent - the GameObject to look around
p - a Predicate to use for searching the map
Returns:
the first matching object, or null if not found

findObject

public GameObject findObject(Commandable agent,
                             Predicate p)
Description copied from interface: CommandEvent
Searches for an object matching the specified predicate. Uses the following search order:
  1. The agent's inventory.
  2. The ground beneath the agent.
  3. All squares immediately adjacent to the agent.

Specified by:
findObject in interface CommandEvent
Parameters:
agent - the agent to check (must be in a map)
p - the predicate used to tell which object to grab.
Returns:
the first matching object, or null if not found

findTarget

public GameObject findTarget(Commandable agent,
                             Predicate p)
Description copied from interface: CommandEvent
Searches for an object matching the specified predicate. Uses the following search order:
  1. The agent's inventory.
  2. The ground beneath the agent.
Unlike findObject() and findNeighbor(), this method doesn't look in squares adjacent the agent.

Specified by:
findTarget in interface CommandEvent
Parameters:
agent - the agent to look near
p - the predicate to use to locate the object
Returns:
the first GameObject found that passes the predicate, or null if none was found.

findInMap

public GameObject findInMap(Commandable agent,
                            Predicate p)
Description copied from interface: CommandEvent
Searches for an object matching the specified predicate. Only looks in the map immediately beneath the agent.

Specified by:
findInMap in interface CommandEvent
Parameters:
agent - the agent to check (must be in a map)
p - the predicate used to tell which object to grab.
Returns:
the first matching object, or null if not found

findAllMatches

public java.util.List findAllMatches(Commandable agent,
                                     Predicate p)
Description copied from interface: CommandEvent
Finds all objects in the map, adjacent to or beneath the agent, that match the passed predicate.

Specified by:
findAllMatches in interface CommandEvent
Parameters:
agent - the agent to check (must be in a map)
p - the predicate used to tell which object to grab.
Returns:
all matching objects, or an empty list if none are found

findByHashcode

public GameObject findByHashcode(java.lang.String hashcode)
Description copied from interface: CommandEvent
Looks for an object by its hashcode. Checks inventory first, then the ground beneath the agent. Used when manipulating objects in ground or inventory with the mouse.

Specified by:
findByHashcode in interface CommandEvent
Parameters:
hashcode - the hash code of the object to retrieve
Returns:
the object, if found, else null

searchInv

public GameObject searchInv(Predicate p)
Description copied from interface: CommandEvent
Searches the agent's inventory for the first object matching the passed Predicate.

Specified by:
searchInv in interface CommandEvent
Parameters:
p - a predicate to test the objects in inventory
Returns:
the first object in the agent's inventory for which the passed predicate returns true, or null if none matched

searchMapUnder

public GameObject searchMapUnder(Predicate p)
Description copied from interface: CommandEvent
Searches for an object matching the specified predicate. Only looks in the map immediately beneath the agent.

Specified by:
searchMapUnder in interface CommandEvent
Parameters:
p - the predicate used to tell which object to grab.
Returns:
the first matching object, or null if not found

searchMapNearby

public GameObject searchMapNearby(Predicate p)
Description copied from interface: CommandEvent
Searches for an object on the ground under and adjacent to the agent. The squares are not guaranteed to be searched in any particular order. The search may intermix squares beneath the agent with squares adjacent to the agent.

NOTE: If you're coding a command that looks for a target at the agent's feet first, use findObject() instead. Otherwise you'll get weird bugs if there's an item with the same name next to the agent, since this may find that one first.

Specified by:
searchMapNearby in interface CommandEvent
Parameters:
p - a Predicate to use for searching the map
Returns:
the first matching object, or null if not found

searchInvAndMapUnder

public GameObject searchInvAndMapUnder(Predicate p)
Description copied from interface: CommandEvent
Searches for an object matching the specified predicate. Uses the following search order:
  1. The agent's inventory.
  2. The ground beneath the agent.
Only looks beneath the agent, not in adjacent squares.

Specified by:
searchInvAndMapUnder in interface CommandEvent
Parameters:
p - the predicate to use to locate the object
Returns:
the first GameObject found that passes the predicate, or null if none was found.

searchInvAndMapNearby

public GameObject searchInvAndMapNearby(Predicate p)
Description copied from interface: CommandEvent
Searches for an object matching the specified predicate. Uses the following search order:
  1. The agent's inventory.
  2. The ground beneath the agent.
  3. All squares immediately adjacent to the agent.

Specified by:
searchInvAndMapNearby in interface CommandEvent
Parameters:
p - the predicate used to tell which object to grab.
Returns:
the first matching object, or null if not found

searchInvByHashcode

public GameObject searchInvByHashcode(java.lang.String hashcode)
Description copied from interface: CommandEvent
Searches the agent's inventory for an object with the specified hashcode.

Specified by:
searchInvByHashcode in interface CommandEvent
Parameters:
hashcode - the object's hashcode, as a string
Returns:
the object, or null if not found

searchMapUnderByHashcode

public GameObject searchMapUnderByHashcode(java.lang.String hashcode)
Description copied from interface: CommandEvent
Searches agent's inventory and the ground under the agent for an object with the specified hashcode.

Specified by:
searchMapUnderByHashcode in interface CommandEvent
Parameters:
hashcode - the object's hashcode, as a string
Returns:
the object, or null if not found

broadcast

public java.util.List broadcast(java.lang.String msg)
Description copied from interface: Broadcaster
Broadcasts a message to every player in sight of this object, not including the player who initiated it (if this is a player).

Specified by:
broadcast in interface Broadcaster
Parameters:
msg - the message to broadcast.
Returns:
a list of Players who heard the message. Monsters can heard the message if they have the "hears-broadcasts" property, but they won't be added to the return list.

broadcast

public java.util.List broadcast(java.lang.String msg,
                                byte style)
Description copied from interface: Broadcaster
Broadcasts a message to every player in earshot of this object, not including the player who initiated it (if this is a player).

Specified by:
broadcast in interface Broadcaster
Parameters:
msg - the message to broadcast.
style - the RPCConstants.TextStyle to use
Returns:
a list of Players who heard the message. Monsters can heard the message if they have the "hears-broadcasts" property, but they won't be added to the return list.

broadcast

public java.util.List broadcast(java.lang.String msg,
                                Predicate p)
Description copied from interface: Broadcaster
Sends the specified message to players in earshot of this object, not including the player who initiated it (if this is a player).

Specified by:
broadcast in interface Broadcaster
Parameters:
msg - the message to send
p - an optional predicate used to filter the recipients. If p is false for a potential recipient, the message is not delivered to that recipient.
Returns:
a list of Players who heard the message. Monsters can heard the message if they have the "hears-broadcasts" property, but they won't be added to the return list.

broadcast

public java.util.List broadcast(java.lang.String msg,
                                Predicate p,
                                byte style)
Description copied from interface: Broadcaster
Sends the specified message to player in sight of this object, not including the player who initiated it (if this is a player).

Specified by:
broadcast in interface Broadcaster
Parameters:
msg - the message to send
p - an optional predicate used to filter the recipients. If p is false for a potential recipient, the message is not delivered to that recipient.
style - the RPCConstants.TextStyle to use
Returns:
a list of Players who heard the message. Monsters can heard the message if they have the "hears-broadcasts" property, but they won't be added to the return list.

broadcast

public java.util.List broadcast(java.lang.String msg,
                                Predicate p,
                                byte style,
                                int radius)
Description copied from interface: Broadcaster
Sends the specified message to everyone in sight.

Specified by:
broadcast in interface Broadcaster
Parameters:
msg - the message to send
p - an optional predicate used to filter the recipients. If p is false for a potential recipient, the message is not delivered to that recipient.
style - the RPCConstants.TextStyle to use
radius - the radius around the player to broadcast to. Includes the player's location, so a radius of 7 would cover their whole view.
Returns:
a list of Players who heard the message. Monsters can heard the message if they have the "hears-broadcasts" property, but they won't be added to the return list.

runPostHooks

public void runPostHooks(boolean success)
Description copied from interface: CommandEvent
Runs one of the event's post hooks. 99.9% of the time this is called by the game kernel after execute() runs. The return value from execute() is passed into this method along with the agent. However, in certain very rare cases, the event has to run its own post-hooks. For example, the DeathEvent kills a monster, which destroys its hook-list, so the game kernel can't run the post-hooks - so DeathEvent does it manually in the execute() method.

Specified by:
runPostHooks in interface CommandEvent
Parameters:
success - true to run the post hook, false for failed hook

runPreHooks

public void runPreHooks()
Description copied from interface: CommandEvent
Runs the event's pre-hooks.

Specified by:
runPreHooks in interface CommandEvent

setMap

public void setMap(GameMap map)
Description copied from interface: CommandEvent
Sets the map where this event is taking place. Normally this isn't necessary, because getMap() will use the agent's map. However, if the agent isn't in a map, or the event is supposed to be taking place in a map other than the agent's location, you can set the map explicitly.

Specified by:
setMap in interface CommandEvent
Parameters:
map - the GameMap where the event is happening. This is primarily important for where the map-hooks for the event will be run.

getMap

public GameMap getMap()
Description copied from interface: CommandEvent
Returns the map where this event is taking place. Usually (but not always) the agent's current map.

Specified by:
getMap in interface CommandEvent
Returns:
the map where the map-hooks are being run. Note that the map pre-hook and post-hook could be run in different maps, if someone called setMap() in a pre-hook or execute(). Also note that the value returned may be null, if the event's agent isn't currently in a map, and nobody has called setMap() for this event.

parseVerb

protected void parseVerb()
Parses the verb from the head of the command string. Verbs are always considered all the non-whitespace chars up to the first whitespace char, or the end of the string, whichever comes first. If we discover that it's a no-arg command, we set the flags saying the args have been parsed as well.


parseARGV

protected void parseARGV()
Parses the original text and produces a String[] of tokens, following the verb. Sets the argvParsed_ flag to avoid doing it again. Stores the arg array in argv_ variable.


parseArgString

protected void parseArgString()
Parses the original text and sets argString_ variable to the substring of the text starting at the 2nd token, or null if there are no args. Sets argStringParsed_, so it won't do the computation again if you call it again.


profileParsing

public static java.lang.String profileParsing()
Returns information about how long we've spent just doing event parsing in this class.


message

public void message(java.lang.String msg)
Description copied from interface: CommandEvent
Sends a message to the event's agent. Equivalent to event.getAgent().message(), except that it can be intercepted by ">" or "|" operators in the command. I.e. if you use event.message() instead of agent.message(), you can redirect the output to a file, or filter it for lines matching a specific string or regular expression.

Specified by:
message in interface CommandEvent
Parameters:
msg - the message to print.

message

public void message(java.lang.String msg,
                    byte style)
Description copied from interface: CommandEvent
Sends a message to the event's agent, in the specified TextStyle. Can be redirected or piped the same way the one-arg message() function can.

Specified by:
message in interface CommandEvent
Parameters:
msg - the message
style - the Kernel.TextStyle constant

getFilters

public java.lang.String[] getFilters()
Description copied from interface: CommandEvent
Returns the filter strings being applied to this event.

Specified by:
getFilters in interface CommandEvent
Returns:
the filters, such as "sort -r" or "wc -l". Returns null if there are no filters.

getFilterObjects

public CommandFilter[] getFilterObjects()
Description copied from interface: CommandEvent
Returns the parsed CommandFilter objects that will be applied to the message for this event.

Specified by:
getFilterObjects in interface CommandEvent
Returns:
the CommandFilter objects, parsed from any filters such as "sort -r" or "wc -l" or "grep -i foo/bar". Returns null if there are no filters on the event output.

isRedirecting

public boolean isRedirecting()
Description copied from interface: CommandEvent
True if there was a redirect operator detected at the end of the command: ">", ">>", "+>", or "+>>". Only Wizards can do this.

Specified by:
isRedirecting in interface CommandEvent
Returns:
true if the agent specified a redirect for the output of this command

isAppending

public boolean isAppending()
Description copied from interface: CommandEvent
Returns true if the redirect-to-file operator was "+>>" or ">>".

Specified by:
isAppending in interface CommandEvent
Returns:
true if the redirect had a "+" in it

isSendingToAgent

public boolean isSendingToAgent()
Description copied from interface: CommandEvent
Returns true if the redirect operator included a "+" to send the output to the agent as well.

Specified by:
isSendingToAgent in interface CommandEvent
Returns:
true if the redirection is going to the agent

getRedirectFile

public java.io.File getRedirectFile()
Description copied from interface: CommandEvent
Returns the file that we're redirecting output to, if any.

Specified by:
getRedirectFile in interface CommandEvent
Returns:
the File object, if the command had a redirect (">") in it.

filterMessage

protected java.lang.String filterMessage(java.lang.String message)
Runs the message through any filters the player specified.

Parameters:
message - the message to filter
Returns:
the filtered message, or null if the filters couldn't be applied. Issues appropriate message to the agent.

redirectMessage

protected boolean redirectMessage(java.lang.String msg)
Sends the specified message to the output file.

Parameters:
msg - the message to append to the file, or to overwrite the file contents with
Returns:
true if successful in writing the file

toString

public java.lang.String toString()
Description copied from class: PList
Returns a string representation of this PropertyList. Doesn't include any inherited properties. Uses transient values of properties - for int properties, adds transient value (if any) to persistent value before printing it.

Overrides:
toString in class PList
Returns:
a debugging string

main

public static void main(java.lang.String[] args)
Function for testing the command parser.