Hey,
I started to write a plugin to send private messages.
As you should know, it’s very annoying to write long usernames to send a message.
So the plugin should auto-complete usernames (pf online users).
This is, what I have:
@Override
public List getSuggestions(CommandSource source, String args) throws CommandException {
List players = new ArrayList();
for (Player player : Sponge.getGame().getServer().getOnlinePlayers()) {
players.add(player.getName());
}
return players;
}
So when I press TAB, my plugin does complete the command with player names, but it only takes the usernames from the array and overwrites my input.
This is NOT what I want … I want to complete usernames, so when I typed in “Hi”, it should replace it with a username ( or offer suggestions for usernames) starting with “Hi”.
In terms of your actual code there, the problem is that you’re ignoring the String args parameter. args is the current thing you’ve typed so if you type Hi<TAB> then args will contain Hi
You would need to test whether each player in getOnlinePlayers() begins with Hi then return a list of player names that only begin with Hi.
However, what you’re doing here has already been provided for you from Sponge itself, it’s called CommandSpec.
and the key here is GenericArguments.player()
You use it like this:
CommandSpec.builder()
.arguments(GenericArguments.player(Texts.of("player"))) // Define command arguments here
.executor((src, args) -> {
Optional<Player> player = args.getOne("player"); // Get the "player" argument, it may not exist so Optional.empty() may be returned
// Do stuff here
return CommandResult.empty();
})
Thanks for your help, but how can I use this, if my code is in another class? So I want to register it like I do it with .child() methods (by using external classes)
Thanks
Edit: I wanted to use this feature of Sponge, but I don’t know how to register “parent” commands / commands without sub commands via “CommandSpec”…
I don’t have much experience with CommandSpec myself.
I know there is an executor called ChildCommandElementExecutor which may be what you’re looking for
But I think a simple child command can be done like so: