How to getGame();

Im new when it comes to Java / Spongepowered. I wrote a method:

public Optional<CommandResult> process(CommandSource source, String arg_string) throws CommandException {
	//Check if source has permission
	if(!source.hasPermission("account.use")) {
		source.sendMessage(Texts.builder("Keine Berechtigungen!").color(TextColors.RED).build());
		return Optional.of(CommandResult.builder().successCount(0).build());
	String[] args = arg_string.split(" ");
		source.sendMessage(Texts.builder("Verwendung: /account new <mail> <password>").color(TextColors.GRAY).build());
		return Optional.of(CommandResult.builder().successCount(0).build());
		String user_mail = args[1];
		String user_pass = args[2];
		String name = source.getName();
		String uuid = source.getIdentifier();

			source.sendMessage(Texts.builder("Dieser Account ist bereits registriert!").style(TextStyles.BOLD).color(TextColors.RED).build());
			source.sendMessage(Texts.builder(" Fragen? Kontaktiere den Support auf").color(TextColors.GRAY).build());
			//here i want to run a cmd command!

			if(mailValidator.isEmail(user_mail)) {
				if (dataEditor.addCaveAccount(user_mail, user_pass, name, uuid)) {
					source.sendMessage(Texts.builder("Dein Account wurde registriert!").style(TextStyles.BOLD).color(TextColors.GREEN).build());

					source.sendMessage(Texts.builder(" Du kannst dich ab sofort mit deiner Mail").color(TextColors.GRAY).build());
					source.sendMessage(Texts.builder(" oder deinem Minecraft-Namen & deinem Passwort").color(TextColors.GRAY).build());
					source.sendMessage(Texts.builder(" auf anmelden!").color(TextColors.GRAY).build());
					return Optional.of(CommandResult.success());
				source.sendMessage(Texts.builder("Account konnte nicht erstellt werden").style(TextStyles.BOLD).color(TextColors.RED).build());

				source.sendMessage(Texts.builder(" Bitte gib eine valide E-Mail an!").color(TextColors.GRAY).build());
		return Optional.of(CommandResult.builder().successCount(0).build());
		//Invalid first argument
		source.sendMessage(Texts.builder("Verwendung: /account new <mail> <password>").color(TextColors.RED).build());
		return Optional.of(CommandResult.builder().successCount(0).build());

I have marked, where i want to run a cmd command -> “//here i want to run a cmd command!”. My problem is, that i have no clue, how i can use getGame(); there, because i havent an event to use it. I have read the API, but there is no detailed information about that.

My class: public class Commands implements CommandCallable

Thanks for helping,

If you pass your plugin class into this class via a contractor, you can call plugin.getGame(). This is, of course, if you set up the game object in your plugin class.

Hey ! thank for your answer. As I said in my first post, I’m new in Java/Sponge. Pleae can you change my code / show me, how it should look ? :slight_smile:

If you’re new to Java in general, I’d recommend reading a few tutorials to understand the basics before relying on any API. The Oracle Documentation is a great starting point.

Now, the idea is to pass the required instance to your class.


@Plugin(id = "MyPlugin", name = "MyPlugin", version = "0.1")
public class MyPlugin {
    @Inject private Game game;
    private CommandHandler commandHandler;

    @Subscribe public void onInit(InitializationEvent event) {
            this.commandHandler = new CommandHandler(;

        public Game getGame() {

Command Class

public class CommandHandler implements CommandExecutor {
    private final Game game;

    public CommandHandler(Game game) {
   = game;

        // The code that you already used can now access the 'Game' object via ''
1 Like

Thanks :smile:
Yeah, I’m new in Java, but i used many other lanuages before (like C#, which is similar to Java)

Here i get: "CommandHandler is abstract; …

This gives me “Invalid method declaration; return type required”

Replace CommandHandler with the class that contains your commands.