getCommandDispatcher()

Spoon-feeding the code is one way to help, however that doesn’t let them understand how and why it works and thus doesn’t allow them to use it in all the right ways. In this instance it might be implied that @Inject can be used to grab all kinds of instances from thin air, yet its only limited to a few services provided to the plugin.


As for the topic, the Game instance is the core accessor of the API, as defined by documentation, used to retrieve services and various information about the running server. The typical way to grab an instance is to allow it to be injected into your main class, which is the class annotated with the @Plugin annotation, using the previously mentioned @Inject annotation. This cannot be done for all instances, a temporarily compiled list can be seen here, and if you can understand the code, the class that lists these is here. Because this method cannot be used in all classes since Sponge will only inject these into your main class, it is advised to create a method holding a reference to the Game instance to access it from other classes.

pseudo-code example

@Plugin(id = "id", name = "name", version = "version")
public class Plugin {
    @Inject private Game game;
    
    public Game getGame() {
        return game;
    }
}
4 Likes