Dynamic Events

Discussion in 'Spigot Plugin Development' started by WAS, Apr 29, 2017.

  1. WAS

    WAS

    I believe this is possible, but I'm not exactly positive how one might do it. How would I be able to loop all of Bukkits events, and dynamically register a Monitor event on it? I see all the fancy magic of the Handlers and want to create a monitor plugin that monitors other plugins accessing events. Easy enough done in any event, but I don't want to manually create classes for every Bukkit event.

    For example take this code, this is something I'd want to apply to every Bukkit event, dynamically, without a new class file.

    Code (Java):
         Logger log = Bukkit.getLogger();
         String name = e.getEventName();
         HandlerList handlers = e.getHandlers();
         RegisteredListener[] listeners = handlers.getRegisteredListeners();

         log.info(ConsoleColor.GREEN + ConsoleColor.BOLD + name + " has triggered. " + ConsoleColor.RESET + ConsoleColor.ITALIC + "The following plugins have access to this event." + ConsoleColor.RESET);

         for (RegisteredListener listener : listeners) {
           Plugin plugin = listener.getPlugin();
           PluginDescriptionFile desc = plugin.getDescription();
           log.info(ConsoleColor.YELLOW + ConsoleColor.BOLD + desc.getName() + ConsoleColor.RESET + " " + ConsoleColor.PURPLE + ConsoleColor.BOLD + desc.getVersion() + ConsoleColor.RESET + " Description: " + desc.getDescription());
         }
     
  2. Altough i don't really see the reason for this and i don't quite understand what you're tryint to do by the given code, i might help you with this:
    "Loop through all events"? You mean a event called in case of for all others?
    Have you tried to register a org.bukkit.event.Event? It's the parent of all bukkit events, so it should be triggered in case of all events.
     
    • Agree Agree x 1
  3. WAS

    WAS

    I'm not sure how it's not obvious. It's to listen on what plugins access events on your server. In case of erroneous behavior and wondering what plugin uses that event. Ultimately can be altered and used to monitor your servers and plugin behavior.
    I have not. I'll look at it. Though I'm not sure that would be specific to each event. Can you get the executing subclass with a parent class?
     
  4. This is definitely possible as @Aikar has it inside the /timings plugin of Spigot. I'm positive the source for timings is inside the Spigot .jar so you can find exactly how he/she did it.

    Hopefully @Aikar would be able to provide some info
     
    • Like Like x 1
  5. That won't work. In order to register a listener, the event class must implement getHandlers() and it must have a static getHandlerList() method. As an abstract class, org.bukkit.event.Event doesn't do either of those things (nor should it).
     
    • Like Like x 1
  6. The only way i could see doing this is to use reflection and to iterate every class loaded in each class loader and test if it extends Event...

    not going to be pretty...

    Timings hooks every event because the code that actually REGISTERS events has been modified to wrap the event registration.
     
    • Like Like x 1
  7. WAS

    WAS

    That's rough. Thanks for commenting.