How to getGame();

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

@Override
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(" ");
	
	if(args.length<3){
		source.sendMessage(Texts.builder("Verwendung: /account new <mail> <password>").color(TextColors.GRAY).build());
		return Optional.of(CommandResult.builder().successCount(0).build());
	}
	
	if(args[0].equalsIgnoreCase("new")){
		
		String user_mail = args[1];
		String user_pass = args[2];
		String name = source.getName();
		String uuid = source.getIdentifier();

		if(dataEditor.accountExists(uuid)){
			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 http://the-cave.net").color(TextColors.GRAY).build());
			//here i want to run a cmd command!


		}else{
			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 http://the-cave.net anmelden!").color(TextColors.GRAY).build());
					return Optional.of(CommandResult.success());
				}
			}else{
				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());
	}else{
		//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,
Greets

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:
Thanks

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.

Main

@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(this.game);
        }

        public Game getGame() {
            return this.game;
        }
}

Command Class

public class CommandHandler implements CommandExecutor {
    private final Game game;

    public CommandHandler(Game game) {
            this.game = game;
        }

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

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

Here i get: "CommandHandler is abstract; …

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

Replace CommandHandler with the class that contains your commands.

2 Likes