Command Arguments [Solved]

Edit:
Found the solution to my problem. Thank to @Redrield.
Also, thanks to https://docs.spongepowered.org/master/en-GB/plugin/commands/childcommands.html
Spongedocs are very well put together.


Hey everyone. I’m doing command arguments right now, and I have a question about how to do something.

I want to create a centralized command (eg. /cmd ) the “cmd” would be the centralized command with arguments such as “home” or “sethome”. How would I go about doing that?

Main Class:

@Plugin(id="mcs", name="MCS", version="0.0.1")
public class MCS 
{
	@Listener
	public void onServerStarting(GameStartingServerEvent event)
	{
		// Primary Command Registration
		CommandSpec mcsCommandSpec = CommandSpec.builder()
				.description(Text.of("MCS Command"))
				.permission("mcs.homes")
				.executor(new CommandManager())
				.build();

		Sponge.getCommandManager().register(this, mcsCommandSpec, "mcs");
	}
}

CommandManager Class:

public class CommandManager implements CommandExecutor
{
	public CommandResult execute(CommandSource src, CommandContext args) throws CommandException 
	{
		if (src instanceof ConsoleSource)
		{
			src.sendMessage(Text.of("This command can only be used in-game!"));
			return CommandResult.empty();
		}
		return CommandResult.success();
	}
}

This is a very different api from craftbukkit or spigot, this is for learning.

Thank you,
- ST

Of of the things you can do is simply set optional arguments, and check if they’re present in the commandexecutor. But if you have lots of arguments, it can get messy quickly (Think 5 different types of arguments of different lengths)

For that reason, sponge has an implemented child command system. To use this, you would have 2 different command executors, and 2 different CommandSpecs for sethome, and for home. Then you can designate those as child commands in the main command’s CommandSpec with CommandSpec#child(CommandSpec, String...). So when one of the aliases is passed as an argument to the main command, the relevant child CommandExecutor handles it

2 Likes

From your post I found the sponge document for child and parent commands.
Thank you for your help, you’ve fixed my problem.

1 Like