Event Handler to listen to all events in one method?

Discussion in 'Plugin Development' started by Chiller, Sep 3, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Chiller

    I am wondering if there is an event handler to listen to every single event that gets called instead of creating a separate method for each event...
     
  2. Offline

    makskay

    What would be the point of such a thing? The value in having event handlers is that your plugin can react to specific things that happen within the game world.
     
  3. Offline

    Chiller

    I want to be able to listen to every single event so I am able to link it to stats. I don't want to have to create a new method for every single event...
     
  4. Offline

    Chiller

  5. Offline

    ZeusAllMighty11

    Every single event? From PlayerMoveEvent, to BowShootEvent, to EntityDamageByEntityEvent?

    Have fun with a computer crash.

    If you just want to make stats for stuff such as 'Bow skill' or 'Mob Slaying Skill' or 'Woodcutting' and 'building', listen for those specific events, record the player and their action and how much they do, and then save it to a file. Then display information later on when called

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  6. Offline

    Chiller

    Then I have to create a method for every single stat i want to implement... I want to eliminate that by having one method being called and excluding certain events...
     
  7. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Each event object needs a separate method with few exceptions (PlayerDeathEvent doesn't need to be caught, you can just catch the EntityDeathEvent for example)
     
  8. Offline

    Firefly

    Do you understand the point of OOP?
     
    Taco likes this.
  9. Offline

    Chiller

    Whats OOP?
     
  10. Offline

    skore87

    Just use a parent event, like Event. =P
     
  11. Offline

    Chiller

    Tried it, doesn't work...
     
  12. You could try AspectJ and make it trigger whenever callEvent() triggers... it will also trigger for custom events as well.
     
  13. Offline

    Courier

    Yeah, that won't work. Bukkit looks through all you @EventHandler methods, and adds them to the list of handlers for that event. Event doesn't have a handler list. You might be able to get something to work if you really mess with the SimplePluginManager, but it would surely be no simpler than this sort of thing:
    Code:java
    1. @EventHandler
    2. public void onEntityDeath(EntityDeathEvent event) {handleEvent(event);}
    3. @EventHandler
    4. public void onInteract(PlayerInteractEvent event) {handleEvent(event);}
    5. /*etc...*/
    6.  
    7. private void handleEvent(Event event)
    8. {
    9. /*do something*/
    10. }
     
  14. Offline

    Chiller

    Ya ik its just that i dont want to have a billion of event handlers...
     
  15. Offline

    p000ison

    If i understand this right: You want to make stats for every Event? Lets take the BlockFromToEvent. It can fire about 20 times per second. Lets take the movement event. It fires again for multiple times per second for every player. Also im sure you want to handle all the events in another way. Like the move event with distance walked. Or the blockbreak even with logging stats. I see no point in doing all in one method.

    Btw: OOP = Object Oriented Programming
     
  16. Offline

    Chiller

    Im not planning on making stats for EVERY single event just the majority of them.

    Why the smart ass comment?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
    ndgriffeth likes this.
  17. Offline

    Sagacious_Zed Bukkit Docs

    Because it would have been obvious as to why it is a bad idea. Unless you are literally doing the same thing for every event.
     
  18. Offline

    Chiller

    I am doing the same thing for every event.
     
  19. Offline

    Sagacious_Zed Bukkit Docs

    In which case you are still stuck with multiple event handler, or if you really know what you are doing, modify the plugin manager in some way. In fact, Wolverness does that in his devBukkit fork.
     
  20. Offline

    md_5

    This can however be done by using reflection to replace the servers plugin manager with your own wrapper plugin manager that performs preprocessing before passing it on to the real plugin manager.
     
    iKeirNez likes this.
  21. Offline

    p000ison

    k, you do not want to do it for every single event, only for the major ones(those which you need) and you want to listen to all events? xD

    You can make a method for all block events, all damage event etc. and call this method then in multiple eventhandlers. But if you would make one event which listens to every action, and you check in this method then which action it was, then its useless. Why not have multiple if you want to handle each other? because you have to handle each in an other way...
    Or minimum choose which you want...
     
  22. Offline

    Chiller

    Im going to have the user specify what event they want so i want it to be dynamic and not hardcoded...
     
  23. Offline

    nisovin

    So are you not going to need any event data from the different events? You wouldn't have access to that if you listened to them all from the same method, all you'd know is that an event fired.
     
  24. Offline

    evilmidget38

    You could use a separate listener for each event you're using, and then only register the ones specified.
     
    makskay likes this.
  25. Offline

    nisovin

    You can also use PluginManager.registerEvent to register specific events instead of PluginManager.registerEvents. You could easily use this to pass all events to a single handling method.
     
Thread Status:
Not open for further replies.

Share This Page