A question which is asked from time to time is how to listen to all events, how to…
- User
A question which is asked from time to time is how to listen to all events, how to register a listener for all events in Bukkit/Spigot. Sadly, I was unable to find a real, up to date answer.
Some sources suggest using HandlerList. As a user later pointed out, this is an unreliable way of doing it: an Event is only registered in the HandlerList once it has been initialized, it being loaded is not enough.
What are loading and initialization? Simply put, loading is when the bytecode (read: compiled) representation of a class is located and loaded. Initialization is a step later down the line which only happens once. It is done when a new instance of the class is created or when one of its static methods is called (there are some other triggers as well). During the initialization (among other things) the static initialization is done.
All events have a static HanderList variable, it is assigned during initialization. The constructor of the HandlerList registers the event. As mentioned earlier, we can’t rely on this behavior except if it’s enough to know that an event exists before it is fired. Aikar mentions using this registration process in his Timings, but I could find it in neither Paper nor Spigot. I think he meant something else because HandlerList instances do not reference the class they store the handlers of, therefore the call stack would have to be examined to extract this information.
It suggest tinkering with the server’s PluginManager. Events are fired by calling PluginManager#callEvent(Event). If we were to inject our omni listener before/after the implementation of that method, we would be able to listen to all events before/after the other handlers are executed. Unfortunately, this is not possible: CraftServer stores an instance of SimplePluginManager instead of PluginManager. This means that we would have to wrap/proxy a SimplePluginManager, but the class is final, therefore there is no way to do this with just reflection: bytecode manipulation is necessary. That is not ideal.
It would seem that there is only one possible solution left: getting all loaded classes which extend Event, filter them and then register a listener for them. If we want to list all events, the filtering is easy: the class mustn’t be abstract, that’s all. We can also use this filtering if we want to listen to all events, but in this case, we would need to use the HandlerList class when registering our listener. This is because we don’t want our listener to be called multiple times for the same event instance.
To achieve this task of registering a listener for all events in Bukkit/Spigot, you’ll need to make use of reflection to discover all classes that extend Event and are non-abstract. You can then register your listener for each of these events. Here’s a basic outline of how you could implement such a solution:
import org.bukkit.Bukkit; import org.bukkit.event.Event; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; import org.reflections.Reflections;
- Assistant