External Listeners

Discussion in 'Resources' started by 1Achmed1, Sep 21, 2013.

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

    1Achmed1

    This is a quick guide on how to send events to separate classes, rather than your main.

    Step 1:
    Create a class to listen on. Make sure it isn't named Listener, or it will throw errors.

    Step 2:
    In your main class add the following code to your onEnable method:
    Code:java
    1. getServer().getPluginManager().registerEvents(new MyListener(), this);

    Be sure to replace "MyListener" with your listener class's name.

    Step 3:
    In your listening class add the following code:
    Code:java
    1.  
    2. public static Main plugin;
    3.  
    4. @EventHandler
    5. public void onEvent(EventHere event) {
    6. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    7. }
    8.  

    What this code does is, it tells the Main class that it really is handling the events, and handles them. There are a few things to change:
    1. Replace "Main" with your main class
    2. Replace "onEvent(EventHere event)" with your event, and its variable.

    Hope this helped!
     
  2. 1Achmed1 You're doing it completly wrong. What your code now does: whenever event gets fired > register new listener class. Just remove the getServer... line in the listener class and it should be fine.

    We also have a beautiful tutorial page where this gets explained a little bit better so I'm not entirely sure why we would need this tutorial?
     
  3. Offline

    1Achmed1

    CaptainBern if you remove the getServer... Line un the listener class, it doesn't register correctly. Also, the wiki page about it is a little confusing, and this explains it in steps to make it easier.
     
  4. Offline

    user_43347

    "How many times can we register the listener before the server crashes, it's the hit game all the kids are playing!"
     
    Cirno, ferrybig, _DSH105_ and 3 others like this.
  5. 1Achmed1 Excuse me sir, everyone here on bukkit will agree with me. You don't have to register listeners inside your listener class. Your code will register itself each time something happens. Like I said before, remove tje getServer line in the listener class. In case you don't know how the event listening system works, let me explain you: when registering a listener bukkit will register that class, then get all the methods that have the @EventHandler annotation and when an event gets fired invoke the methods that have the right event param. Please change this or I report this thread for providing false information.

    Edit: Also our beloved member md_5 already created a tutorial providing true information: http://forums.bukkit.org/threads/new-event-system.55249/
     
    KingFaris11 likes this.
  6. Offline

    Necrodoom

    Locked since subject is already covered by the sticky and as other people said here, this is not the way to register listeners.
     
    xTrollxDudex and KingFaris11 like this.
Thread Status:
Not open for further replies.

Share This Page