getCommandDispatcher()

How to manage with CommandService cmdService = game.getCommandDispatcher();

Try being more descriptive about what you want to achieve.

Ok sorry.
I don’t know how to get into this interface.
On Docs page I can see only:

CommandService cmdService = game.getCommandDispatcher();
cmdService.register(plugin, new MyBroadcastCommand(server), "message", "broadcast");

What is this ‘game’ property?

I’m going to spoon feed to you the code:

@Inject
private Game game;

private CommandService cmdService = game.getCommandDispatcher();
//Do what you want with cmdSerice
    @Plugin(id = "MyPluginID", name = "My Plugin")
public class main {
	
	@Subscribe
	public void onServerStart(ServerStartedEvent event){
		
		@Inject
		private Game game;

		private CommandService cmdService = game.getCommandDispatcher();
		
		
	}
}

  > The annotation @Inject is disallowed for this location
@Plugin(/*Whatever*/)
public class Stuff {
    @Inject
    private Game game;
    private CommandService cmdService = game.getCommandDispatcher();
    @Subscribe
    public void onServerStart(ServerStartedEvent event) {
        //Do what you want
    }
}

You should take a look at a Java course if you really just tried to define two private variables in a method.

5 Likes

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

good answer ^.^
Thank you

2 Likes

@Samuel_Boczek udemy.com

Many people said that I need more Java lessons, Yes that is true, I am assembly programmer with java I’m starting but reason why @Inject didn’t worked is that I used different annotation.

Well, the issue with the snippet you posted was that the annotation was out of scope. Anyhow, good to see that this was resolved :smiley:

1 Like