Argument parsing

Hi! I learn how use sponge and how build a command. So I try to create a command whitch enable to send a message to one player, and others if there ae mentioned, no matter the number of optionals players.
I code this:
CommandSpec PrivateMessageCommandSpec = CommandSpec
.builder()
.description(Texts.of(“Envoie un message privé”))
.executor(new PrivateMessageCommand())
.arguments(
GenericArguments.string(Texts.of(“message”)),
GenericArguments.onlyOne(GenericArguments.player(Texts.of(“player”), this.game)),
GenericArguments.optional(GenericArguments.catalogedElement(key, game, catalogType))
.extendedDescription(Texts.of(“Envoie un message privé à un joueur spécifié”))
.build();
game.getCommandDispatcher().register(this, PrivateMessageCommandSpec, “pm”);

What order should I use for the optional argument? I think is catalogedElement (or maybe EnumValue) but I don’t know how use this…

What do you want to do with the cataloged element?

This is a simple example for a /msg command: http://docs.spongepowered.org/en/plugin/basics/commands/arguments.html#example-building-a-command-with-multiple-arguments

I want send the message to all players write after the message
For exemple /pm BobLennon 'Hi! How are you?" TheFantasio notch
send the message to BobLennon, TheFantasio and notch. But I want can adapted to the number of player precised after the message (with a maximum of 10 players afters the message)
For exemple I can do this: /pm BobLennon “What are you doing?” player1 player2 player3 player4 …

public CommandResult execute(CommandSource src, CommandContext args)
		throws CommandException {

	Player player = args.<Player> getOne("player").get();
	Collection<String> othersPlayers = args.<String> getAll("othersPlayers");
	Iterator<String> collectionIterator = othersPlayers.iterator();
	String message = args.<String> getOne("message").get();
	
	player.sendMessage(Texts.of(src.getName() + " vous a envoyé : " + message));
	Tutoriel tuto = Tutoriel.get();
	if (tuto != null){
		while (collectionIterator.hasNext()) {
				tuto.getGame()
						.getServer()
						.getPlayer(collectionIterator.next())
						.get()
						.sendMessage(
								Texts.of(src.getName() + " vous a envoyé : " + message));
		}
	}
	return CommandResult.success();
}

I don’t know whitch GenericArguments command elements I must take (enumValue I think but I don’t know how use them).

Sorry for my english I’m french

May I suggest /pm player1,player2,player3 <message> instead? It may be both simpler to code and use, as players wouldn’t have to mess with quoting the message properly.

Just run .split(",") on the first argument to get a list of recipients.

2 Likes