You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
xRa1ny edited this page May 23, 2023
·
2 revisions
Setting up this API and creating your first main class, was only the tip of the iceberg
Now we'll get into creating your first listener...
The way you would normally create a listener has not changed, the way you would register it though has changed a bit
Lets get started!
To create a listener, create a class that implements from the Listener interface like this:
publicclassMyListenerimplementsListener {
...
}
This class now acts as an event listener for all sorts of events that might take place on our server
But currently, there is not code inside our class, which handles said events
For example, to handle an event which fires when a block breaks, you would implement it this way:
publicclassMyListenerimplementsListener {
@EventHandlerpublicvoidonPlayerBlockBreak(@NotNullBlockBreakEvente) { // <- THE NAME OF THIS METHOD CAN BE WHATEVER YOU WANTe.getPlayer().sendMessage("You broke a block of type: " + e.getBlock().getType());
}
...
}
Now that we have a listener that handles a BlockBreakEvent, we can go ahead and register it when our plugin enables...
To do this, we head on over back to our main class and register the listener we just created within our onPluginEnable() Method:
publicclassMyPluginextendsRPlugin {
@OverridepublicvoidonPluginEnable() {
getListenerManager().register(newMyListener()); // <- Singular listener registrationgetListenerManager().registerAll("PACKAGE NAME OF MULTIPLE LISTENERS"); // <- Multiple listener registrationBukkit.getPluginManager().registerEvents(this, newMyAwesomeListener()) // <- WORKS, BUT NOT RECOMMENDED!
}
@OverridepublicvoidonPluginDisable() {
...
}
}
DONE!
Thats all we need to create and register a fully functioning listener!