After deciding which event we will listen for, it is time to start programming. Create a new project, as described in Chapter 3, Creating Your First Bukkit Plugin, and call it NoRain
. Don't forget to create a plugin.yml
file as well.
In order to listen for an event, your plugin must have a class that is registered as a Listener class
. We will only have one class, named NoRain.java
, for this project. Therefore, we will make this a Listener
class as well. The class declaration will look like the following line of code:
public class NoRain extends JavaPlugin implements Listener
Alternatively, if this is a large project, you can make a class for the Listener class
, which is similar to how the Enchanter
project had CommandExecutor
as a separate class. Also, like CommandExecutor
, a Listener class
will implement an interface method
. The interface method
that we wish to implement is org.bukkit.event.Listener
.
The class is declared as a Listener
class but it is still not registered with Bukkit. To register all the events within the listener, insert the following line of code in the onEnable
method:
getServer().getPluginManager().registerEvents(this, this);
This line retrieves the PluginManager class
and uses it to register the events. The PluginManager class
is used for several things, including handling events, enabling/disabling plugins, and handling player permissions. Most of the time, you will use it to register event listeners. It has a registerEvents
method that takes a Listener
object and a JavaPlugin
object as parameters. The only class that exists is both the Listener
and JavaPlugin
. So, we will pass the this
object to both the parameters. If the Listener
class is separated from the main
class, then the line will look like the following line of code:
getServer().getPluginManager().registerEvents(new WeatherListener(), this);
This is all that is needed within the onEnable
method.
18.220.1.239