@EventHandler annotation

Discussion in 'Plugin Development' started by MaxFireIce, Feb 15, 2017.

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

    MaxFireIce

    This isn't so much of a problem as it is a question. Why is it required to have @EventHandler and what does it even do?
     
  2. Offline

    JanTuck

    It tells bukkit that the next function should be triggered on the event put inside it.

    Bukkit uses reflection to allow you a little more flexability in your code.

    Written on my mobile device.
     
  3. Offline

    Drkmaster83

    Annotations are an obscurity, aren't they?
    I feel like it's just an easy way for the Bukkit API to know which methods to register for the listener, as opposed to all the methods in your listener class. It is an interface with many predefined methods, after all. However, this is definitely just speculation, but it makes sense to me.
     
  4. Offline

    PhantomUnicorns

    They could do it by methods, and they could just make it get the parameter from each methods and do it that way. The thing is, if you have a event listener and you want different things to happen in let's say different worlds and you test the world in the event listener and depending on the world you run a different Method, that requires the same arg as the event listener. I'm a little confusing so here it is in code:
    Code:
    @EventHandler
    public void thatOneListener(PlayerMoveEvent event) {
      if (event.getPlayer().getLocation().getWorld().getName().equals("world")) {
        worldMoveEventMethod(event);
      }
    }
    
    public void worldMoveEventMethod(PlayerMoveEvent event) {
      Player player = event.getPlayer();
      // What you would regularly do in the one above
    }
    
    As @JanTuck it gives more flexibility and is most likely for the better.
     
  5. Offline

    Drkmaster83

    By function, do you mean your implementation of the method? And flexibility how?
     
  6. Offline

    JanTuck

    Well it reads the method after the annotation and triggers it with the correct event.

    It is alot nicer that you can name your function what you want and not have a one function only per class, for example a commandExecutor has 1 function wouldnt it be nicer to have an annotation with the name of the command and something else like usage.



    Written on my mobile device.
     
  7. Offline

    johnny boy

    Also you are all forgetting it can be used to set priorities! Like this:
    Code:Java
    1.  
    2. @EventHandler(priority = EventPriority.HIGHEST)
    3.  
     
  8. Offline

    PhantomUnicorns

    To test this out I made a program that acts like bukkits event stuff, it's a pretty simple program and it works exactly like bukkits. It was easier for me to just use the parameter and/or name to test for a specific method, but it is just more flexible for users (Not for the coder who made the system, believe me) to use.
     
Thread Status:
Not open for further replies.

Share This Page