How to register an event handler from a separate class?

I have two classes: JavaPlugin (Main Plugin Class), and PlayerJoin, which contains the PlayerJoinEvent event. I want to register that event and let Sponge know that is an event handler. Events in the @Plugin annotated class are automatically registered, but how would I do this manually? Oh yeah, congrats @FerusGrim, you are a moderator now.

IMO, I think there is enough information in the Events documentation to be able to learn how to do what you’re asking:
https://docs.spongepowered.org/en/plugin/basics/events.html#event-handlers

It mentions having to register the class with the EventManager. The page on working with Services uses the EventManager specifically as one of its examples:
https://docs.spongepowered.org/en/plugin/advanced/services.html#working-with-services

If you’re still stuck, please let us know what’s tripping you up.

2 Likes

game.getEventManager().register(JavaPlugin.getInstance(), new PlayerJoin())

1 Like

What is JavaPlugin.getInstance()? Is it

@Plugin(/*Blah blah*/)
public class JavaPlugin {
    public static JavaPlugin getInstance() {
        return new JavaPlugin();
    }
}

Yes but return this;

Just do this:

game.getEventManager().register(this, new PlayerJoin());

Also I wouldn’t recommend that you name your main class JavaPlugin, it really should be the name of the plugin.

@simon816 's example was using a singleton model pattern. all that means is that you make it so there is only one existing instance of your plugin at any given time. which means returning a new instance of your plugin (new JavaPlugin();) would be against the pupose of that pattern, and it would also be inefficient since you would never get the same data you set if you always used JavaPlugin.getInstance() to get your main class.

public class Singleton {
    private static Singleton instance;
    public Singleton() {
        if(Singleton.instance != null) {
            throw new IllegalStateException("Singleton instances cannot be instantiated more than once; please use Singleton.getInstance()");
        }
    
        Singleton.instance = this;
    }
    
    public static Singleton getInstance() {
        return instance;
    }
}

pseudo-code. do not expect to compile.
the above code is an example of one of the few ways you can set up a singleton class. please note that doing everything this way isn’t always the best way; some people even see it as just a lazy way of doing things which you can easily say is almost never the best way. use it appropriately.

that’s where getInstance() comes from. i don’t recommend you use it until you’ve garnered more experience with the language.


on the topic, @mmonkey gave a solution while i was writing this entire thing. damnit.
one thing i want to correct about it though; “this” is supposed to be the instance of your plugin, which isn’t always denoted by the this keyword.

1 Like

@Xemiru is completely right! I agree that “this” is not always denoted that way, however, unless otherwise set to something else, “this” should always refer to your main class, when called inside the main class.

i.e. Sponge only knows the main class when the @Plugin annotation is used prior to specifying the class name. Inside that class, and ONLY inside that class, does “this” reference the instance of the plugin.

We could get even more technical about classes being instantiated inside of the main class, but, I think in regards to the topic at hand, “this” should work.

of course it applies in the scenario of the topic, but he did ask for how to do it manually and that he knows Sponge automagically registers event handling in a main class. that implies he might do it a different way where it wouldn’t exist in the main class, e.g. a separate event-handling class; in which case ‘this’ wouldn’t apply as the plugin object.

1 Like

@Xemiru good thought! This is why I shouldn’t answer questions when I’m getting tired. :smile:

1 Like