Java Reference
In-Depth Information
1.
Import the plugin listener and the event hook(s) classes.
2.
Declare that your plugin
implementsPluginListener
.
3.
Register to listen for events with
registerListener
.
4.
Add the magical tag
@HookHandler
, marking your function as an event
handler. Give the event you want as an argument.
You'll see all four parts in this short example skeleton of a listener:
import
net.canarymod.hook.HookHandler;
import
net.canarymod.plugin.PluginListener;
❶
// import the particular hook class here
// Add "implements PluginListener" here:
public class
HelloWorld
extends
EZPlugin
implements
PluginListener {
❷
@Override
public boolean
enable() {
❸
Canary.hooks().registerListener(this, this);
return
super.enable();
// Call parent class's version too.
}
// Here's one event listener:
@HookHandler
❹
public void
anyname(SomeHook hookevent) {
// Some code goes here
}
}
First off, you have to import the class for the event you're interested in (you'd
add this somewhere around
❶
).
There are a
ton
of events available, all listed in the Canary documentation
under
net.canarymod.hook
and its children. Suppose you're interested in doing
something whenever someone in the game teleports. You'd want the
TeleportHook
class in the package
net.canarymod.hook.player
, so first thing here you'd
import
net.canarymod.hook.player.TeleportHook
.
Then the declaration for the plugin needs to add the magic words
implements
PluginListener
, as shown at
❷
.
Next you need to add your own
enable()
function, which then calls the parent
class's
enable
as shown starting at
❸
. This is a standard piece of boilerplate
code that just says “make this plugin listen for events.” You need this only
once in this file; it will work for all events you'll use. Add it in, and off we go.
Finally we come to the event listener itself, starting at
❹
. That
@HookHandler
thing is an annotation that tells Java that the next function is special, just