use of org.lanternpowered.server.command.LanternCommandManager in project LanternServer by LanternPowered.
the class HandlerPlayInTabComplete method handle.
@Override
public void handle(NetworkContext context, MessagePlayInTabComplete message) {
final String text = message.getText();
// The content with normalized spaces, the spaces are trimmed
// from the ends and there are never two spaces directly after eachother
final String textNormalized = StringUtils.normalizeSpace(text);
final Player player = context.getSession().getPlayer();
final Location<World> targetBlock = message.getBlockPosition().map(pos -> new Location<>(player.getWorld(), pos)).orElse(null);
final boolean hasPrefix = textNormalized.startsWith("/");
if (hasPrefix || message.getAssumeCommand()) {
String command = textNormalized;
// Don't include the '/'
if (hasPrefix) {
command = command.substring(1);
}
// Keep the last space, it must be there!
if (text.endsWith(" ")) {
command = command + " ";
}
// Get the suggestions
List<String> suggestions = ((LanternCommandManager) Sponge.getCommandManager()).getSuggestions(player, command, targetBlock, message.getAssumeCommand());
// If the suggestions are for the command and there was a prefix, then append the prefix
if (hasPrefix && command.split(" ").length == 1 && !command.endsWith(" ")) {
suggestions = suggestions.stream().map(suggestion -> '/' + suggestion).collect(ImmutableList.toImmutableList());
}
context.getSession().send(new MessagePlayOutTabComplete(suggestions));
} else {
// Vanilla mc will complete user names if
// no command is being completed
final int index = text.lastIndexOf(' ');
final String part;
if (index == -1) {
part = text;
} else {
part = text.substring(index + 1);
}
if (part.isEmpty()) {
return;
}
final String part1 = part.toLowerCase();
final List<String> suggestions = Sponge.getServer().getOnlinePlayers().stream().map(CommandSource::getName).filter(n -> n.toLowerCase().startsWith(part1)).collect(Collectors.toList());
final Cause cause = Cause.of(EventContext.empty(), context.getSession().getPlayer());
final TabCompleteEvent.Chat event = SpongeEventFactory.createTabCompleteEventChat(cause, ImmutableList.copyOf(suggestions), suggestions, text, Optional.ofNullable(targetBlock), false);
if (!Sponge.getEventManager().post(event)) {
context.getSession().send(new MessagePlayOutTabComplete(suggestions));
}
}
}
Aggregations