|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object wyvern.kernel.commands.HookList
This class provides a standard implementation for objects that wish to provide hookable events.
If an object Obj wants to provide custom hooks for its methods, it can implement the Hookable interface and delegate the method calls to a private instance of a HookList object, which will handle the storage and notification of the callback objects.
Constructor Summary | |
HookList()
|
Method Summary | |
void |
addHook(HookCallback callback,
java.lang.String hookName)
Registers a HookCallback object for the specified hook. |
void |
addMethodHook(MethodHookCallback c,
java.lang.String m)
Registers for the specified method-hook. |
void |
clear()
Removes all the hooks from the list. |
static java.lang.String |
getFailedPostHookName(java.lang.String command)
Returns command + "FailedPostHook", but gets it from a cache rather than constructing it with a StringBuffer. |
HookList |
getHookList()
Returns the HookList containing the registered hooks for this object. |
java.lang.String[] |
getHooks()
Returns a list of custom hooks implemented by this object. |
static java.lang.String |
getPostHookName(java.lang.String command)
Returns command + "PostHook", but gets it from a cache rather than constructing it with a StringBuffer. |
static java.lang.String |
getPreHookName(java.lang.String command)
Returns command + "PreHook", but gets it from a cache rather than constructing it with a StringBuffer. |
void |
removeHook(HookCallback callback,
java.lang.String hookName)
Removes a callback from a specified hook list. |
void |
removeMethodHook(MethodHookCallback c,
java.lang.String m)
Unregisters for the specified method-hook. |
void |
runFailedPostHook(CommandEvent event)
Notifies all the objects registered on this post-hook that the command failed (for whatever reason). |
void |
runMethodHook(java.lang.String hookName,
Hookable target,
java.lang.Object data)
Runs a hook provided by the caller. |
void |
runMethodHook(java.lang.String hookName,
java.lang.Object data)
Runs the specific method hook. |
void |
runPostHook(CommandEvent event)
Notifies all the objects registered on this post-hook that the event completed successfully. |
void |
runPreHook(CommandEvent event)
Calls all objects registered for a particular event and gives them a chance to deal with the event before it happens. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public HookList()
Method Detail |
public static java.lang.String getPreHookName(java.lang.String command)
command
- the command verb, such as "move"
public static java.lang.String getPostHookName(java.lang.String command)
command
- the command verb, such as "move"
public static java.lang.String getFailedPostHookName(java.lang.String command)
command
- the command verb, such as "move"
public java.lang.String[] getHooks()
Hookable
The list doesn't include names of default hooks for built-in commands.
getHooks
in interface Hookable
public void addHook(HookCallback callback, java.lang.String hookName)
Hookable
The reserved pseudo-command "any" is reserved for callbacks that need to get every single command that comes through the queue. This should very rarely be necessary, and can hurt overall game performance considerably if abused.
addHook
in interface Hookable
callback
- the HookCallback to notify when the hook is run.hookName
- the name of the hook list to register on.public void removeHook(HookCallback callback, java.lang.String hookName)
Hookable
removeHook
in interface Hookable
callback
- the HookCallback to remove from the listhookName
- the name of the hook list to unregister from.public void runPreHook(CommandEvent event)
Hookable
Callbacks may veto the event, but it's still passed to any other callbacks on the list, and eventually to the Command. For callbacks, it typically doesn't really matter if the flag has been set by a previous callback; callbacks should do the same thing they would have done even if it wasn't vetoed, because the Command (or any other callback downstream) may un-veto it.
The callbacks may modify any of the event parameters. Callbacks aren't required to do rigorous checking of the validity of their changes. For example, a simple callback registered on a player's preMoveHook might change the direction in the MoveEvent randomly.
To prevent callbacks from generating illegal event parameters, the Command handling the event is implicitly the last hook on the list. Before executing the command it re-checks the preconditions to make sure the move is still legal, and if it isn't, it vetoes it and issues an error message. The message, of course, is a modifiable parameter of the event.
If a HookCallback wants to force an "illegal" event, it has to change the game state (in the agent, the map, the Command, or whatever) to make the move legal. For example, if the hook wants to make sure the player moves over a wall, the hook can set a property on the player (or the wall) that allows this move, before returning control to this method.
runPreHook
in interface Hookable
event
- a CommandEvent that has already run. Calling
getVerb() should return the canonical verb for this command,
such as "move" or "get". The verb is used to construct the
hook name, by appending "PreHook" to the verb.public void runPostHook(CommandEvent event)
Hookable
runPostHook
in interface Hookable
event
- a CommandEvent that has already run. Calling
getVerb() should return the canonical verb for this command,
such as "move" or "get". The verb is used to construct the
hook name, by appending "PostHook" to the verb.public void runFailedPostHook(CommandEvent event)
Hookable
Changing the event parameters will have no effect at this point.
runFailedPostHook
in interface Hookable
event
- a CommandEvent that has failed for some reason.
getVerb() should return the canonical verb for this command,
such as "move" or "get". The verb is used to construct the
hook name, by appending "FailedPostHook" to the verb.public void runMethodHook(java.lang.String hookName, Hookable target, java.lang.Object data)
hookName
- the hooktarget
- the callbackdata
- opaque datapublic void addMethodHook(MethodHookCallback c, java.lang.String m)
MethodHookable
addMethodHook
in interface MethodHookable
m
- the name of the method to hook intoc
- the MethodHookCallback object to notify on the hookpublic void removeMethodHook(MethodHookCallback c, java.lang.String m)
MethodHookable
removeMethodHook
in interface MethodHookable
m
- the name of the methodc
- the MethodHookCallback object to notify on the hookpublic void runMethodHook(java.lang.String hookName, java.lang.Object data)
MethodHookable
runMethodHook
in interface MethodHookable
hookName
- the name of the method, e.g. "add"data
- any data to pass to the callbacks; can be nullpublic java.lang.String toString()
public HookList getHookList()
getHookList
in interface Hookable
public void clear()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |