Last active
December 21, 2020 19:12
-
-
Save TPGamesNL/ee79fc4c348721925e2b4cfafb1a9ea0 to your computer and use it in GitHub Desktop.
Why not to use base events implementing getHandlerList() for the Bukkit event API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A lot of plugins have some sort of BaseEvent, which looks like this: | |
public class BaseEvent extends Event { | |
private static final HandlerList handlers = new HandlerList(); | |
@Override | |
public HandlerList getHandlers() { | |
return handlers; | |
} | |
public static HandlerList getHandlerList() { | |
return handlers; | |
} | |
} | |
Don't do this. | |
Doing this causes all listeners to be stored in the BaseEvent class when registered, which causes all listeners to be called | |
when a subclass of BaseEvent is called, even if that listener isn't registered to listen to that event. | |
Each event needs its own HanderList, you cannot have another (abstract) class do this. | |
It makes sense that you're not supposed to have an abstract BaseEvent class: if this was fine, it'd have been done in | |
Bukkit's own Event class. | |
The reason this issue is so common in plugins, is that it doesn't cause problems when using the (more common) @EventHandler | |
annotation in listeners. If you listen to events using your own EventExecutor, it will cause your listeners to be fired when | |
they shouldn't be. | |
Related issue: https://github.com/EssentialsX/Essentials/issues/3852 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment