Event Listeners Overview
Inubot provides a large array of events for you to write reactive code.
An example of this would be if you wanted to do something when another player trades you, listening to the chat message instead of reading interfaces to detect it.
Here you can find Events that Inubot supports.
To listen for events, you simply need to use the @Subscribe
annotation.
Here is an example listening to chat messages for when you try to buy an item and don't have enough coins. Based on the chat message, we stop the script to avoid spam buying.
@Subscribe
public void onChatMessage(ChatMessageEvent event) {
String message = event.getContents().toLowerCase();
if (event.getType() == ChatMessageEvent.Type.GAME
&& message.contains("that offer costs")
&& message.contains("you haven't got enough")) {
Log.warn("Low on coins, stopping script");
stopScript = true;
}
}
Note: You can name the method whatever you want, it just needs a single event argument and the annotation.
If you implement an event in your main script class, it will automatically receive events. If
you want to use events on a Task, then you need register = true
in your TaskDescriptor
annotation.
Using it outside of that requires you to subscribe and unsubscribe from the EventDispatcher
yourself. Game.getEventDispatcher()
gives you an instance of the EventDispatcher
. Generally
you want to call dispatcher#subscribe
in your scripts initialize function and
dispatcher#unsubscribe
in your shutdown function.