Registering events

Good day everyone,
today I tried to create a new class Listeners for my project an wrote the following code there:

package eu.letsplayonline.analytics.sponge;

import org.spongepowered.api.event.player.PlayerJoinEvent;
import org.spongepowered.api.util.event.Subscribe;

public class Listeners {
@Subscribe
public void onJoin(PlayerJoinEvent event){

}

}

Now I have to register my new listener, right? I found the following but it gives me an error:

event.getGame().getEventManager().register(this, Listeners)

Now Netbeans says “cannot find symbol”. What am I doing wrong? How do I register EventListeners properly?

Thanks,
Paktosan

You have to pass it an instance of the class.

event.getGame().getEventManager().register(this, new Listeners())

Everything else seems fine.

1 Like

Ahh derp, sometimes it’s too easy…
Thank you

I know this is old but just a few notes for clarity; It might be best to display what “this” is, here, since it’s not clear where you’re passing in that argument from, is it the main class instance? Also which step in the lifecycle it’s best to register these events in for Sponge.