[TUTORIAL] Registering events

Discussion in 'Resources' started by sgavster, Nov 30, 2013.

?

Was this thread helpful?

  1. Indeed!

    3 vote(s)
    50.0%
  2. Nope.avi

    3 vote(s)
    50.0%
Thread Status:
Not open for further replies.
  1. Offline

    sgavster

    Okay, I've noticed a lot of threads about "My event wont work" "My plugin doesn't work"

    A lot of times, it's because

    1. You need to register your events
    2. You need @EventHandler
    So, if you do not know what an event is, this is an example:

    PHP:
        @EventHandler
        
    public void onInteract(PlayerInteractEvent event) {
            if(
    event.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                
    event.getPlayer().sendMessage("Look! This is an event! :D");
            }
        }
    But, in order for this event to work, you need the @EventHandler tag above it.
    Doing something like this:

    PHP:
        public void onInteract(PlayerInteractEvent event) {
            if(
    event.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                
    event.getPlayer().sendMessage("Look! This is an event! :D");
            }
        }
    will not work, because there is no @EventHandler tag.

    Now moving on to Registering your events.

    Lets say that these events are in your MAIN class. To register your events you would simply do
    PHP:
    public void onEnable() {
    Bukkit.getPluginManager().registerEvents(thisthis);
    }
    But say it was in a different class, for example a class called "Listeners" we would have to do this:

    PHP:
    Listeners listeners = new Listeners();
    public 
    void onEnable() {
    Bukkit.getPluginManager().registerEvents(listenersthis);
    }

    I hope this helps!

    For any more info, you can go here: http://wiki.bukkit.org/Event_API_Reference#Registering_Events

    If you have any ideas or suggestions: please leave a comment!
     
  2. Offline

    AoH_Ruthless

  3. Offline

    sgavster

  4. Offline

    xTrollxDudex

    sgavster
    Make sure to mentioned to use the same class instance if you are using ArrayLists and such with commands and listeners in the same class.
     
  5. Offline

    user_90854156

    My signature has a pretty good tutorial. loljk
     
    sgavster likes this.
Thread Status:
Not open for further replies.

Share This Page