Creating Events - Zoweb/LoginMSG GitHub Wiki
This class is one of the main classes for use by LoginMSG. It contains all the logic for displaying messages and playing sounds to the user when an event happens.
You cannot simply initialise a new MessageDisplayer
, as it is an abstract class. Instead, create a new class for an event type, e.g. PlayerJoinEvent
(in this case the class is called PlayerJoinListener
), extend MessageDisplayer<event type>
, e.g. MessageDisplayer<PlayerJoinEvent>
, and implement org.bukkit.event.Listener
. It should have a public constructor which super
s for String name, Consumer<event type> valueResetter
.
Now @Override
an onEvent
method, taking event type
as its one and only argument. This should call run(argument)
. Make sure that this method also has @EventHandler
so Spigot recognises it as a listener.
The above code will work for any player event, but for player entity events it will not work, as these extend EntityEvent
instead of PlayerEvent
. For these, extend CastedPlayerMessageDisplayer<event type>
instead of MessageDisplayer
and take PlayerEntityEvent<event type>
as the event handler argument instead of event type
.
Finally you are ready to create your listener. Create a new instance of your class and put it in a variable. Run me.zoweb.loginmsg.LoginMSG.writeListenerConfig(listener)
, passing in your instance as listener
, then me.zoweb.loginmsg.LoginMSG.writeConfigDefaults()
. The first method will set the defaults for configuration and create files for the listener, and the second will write the default configuration to config.yml
. Note, this will all save in the LoginMSG plugin folder, not yours. Now run plugin.getServer().getPluginManager().registerEvents(listener, plugin)
(where plugin is an instance of your plugin) or LoginMSG.registerEvents(listener)
(not recommended, adds to LoginMSG's listeners, not your plugins) to register the events with Spigot.
Not running writeConfigDefaults()
will result in always recieving a "no permissions" error when attempting to set settings for this listener from the /loginmsg
command, and failing to run writeListenerConfig(listener)
will mean no messages/sounds get sent/played.