Listeners
Listeners
Lectures
Implementation | Create a countdown | How listeners work
What do they do?
You can implement them to listen to events fired by countdowns that belong to your plugin.
What kinds of events are there?
- CountdownCountEvent is fired when the countdown has proceeded a second.
- CoutndownFinishEvent is fired when the countdown has counted to 0.
- CountdownPauseEvent is fired when the countdown has been paused.
- CountdownResumeEvent is fired when the countdown has been resumed.
- CountdownStartEvent is fired when the countdown has been started.
- CountdownStopEvent is fired when the countdown has been stopped.
How to use them
They work like Bukkit Events.
But always remember to be sure that the countdown is added to the CountdownManager. Because the CountdownManager fires the events.
Create a listener class
public class YourListenerClass implements CountdownListener
onEnable()
YourListenerClass yourListenerObject = new YourListenerClass(); CountdownManager.registerListener(Class<? extends CountdownPlugin>, yourListenerObject);
Listen to an specific event
@CountdownEventHandler public void methodName(EventType event) { //Do what you want }
onDisable()
Please always unregister every event listening class by using in the onDisable()
CountdownManager.unregisterListener(this, yourListenerObject);
Example
I know that it's hard to read but just copy it into your favorite editor (e.g. Eclipse) and then you can read it properly.
Main class:
public class TestPlugin extends CountdownPlugin { private CountdownEventListener countdownEventListener; @Override public void onDisable() { CountdownManager.unregisterListener(this, countdownEventListener); } @Override public void onEnable() { CountdownEventListener countdownEventListener = new CountdownEventListener(this); CountdownManager.registerListener(this, countdownEventListener); } }
Listener class
public class CountdownEventListener implements CountdownListener { @CountdownEventHandler public void onCountdownFinish(CountdownFinishEvent event) { //Do want you want } }
Comments