Search in sources :

Example 1 with MessagePlayInTabComplete

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInTabComplete in project LanternServer by LanternPowered.

the class CodecPlayInTabComplete method decode.

@Override
public MessagePlayInTabComplete decode(CodecContext context, ByteBuffer buf) throws CodecException {
    String text = buf.readString();
    boolean assumeCommand = buf.readBoolean();
    Vector3i blockPosition = null;
    if (buf.readBoolean()) {
        blockPosition = buf.read(Types.VECTOR_3_I);
    }
    return new MessagePlayInTabComplete(text, assumeCommand, blockPosition);
}
Also used : MessagePlayInTabComplete(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInTabComplete) Vector3i(com.flowpowered.math.vector.Vector3i)

Example 2 with MessagePlayInTabComplete

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInTabComplete 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));
        }
    }
}
Also used : Location(org.spongepowered.api.world.Location) NetworkContext(org.lanternpowered.server.network.NetworkContext) CommandSource(org.spongepowered.api.command.CommandSource) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) MessagePlayInTabComplete(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInTabComplete) Handler(org.lanternpowered.server.network.message.handler.Handler) Sponge(org.spongepowered.api.Sponge) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) World(org.spongepowered.api.world.World) TabCompleteEvent(org.spongepowered.api.event.command.TabCompleteEvent) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) LanternCommandManager(org.lanternpowered.server.command.LanternCommandManager) MessagePlayOutTabComplete(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutTabComplete) EventContext(org.spongepowered.api.event.cause.EventContext) Player(org.spongepowered.api.entity.living.player.Player) World(org.spongepowered.api.world.World) CommandSource(org.spongepowered.api.command.CommandSource) TabCompleteEvent(org.spongepowered.api.event.command.TabCompleteEvent) LanternCommandManager(org.lanternpowered.server.command.LanternCommandManager) Cause(org.spongepowered.api.event.cause.Cause) MessagePlayOutTabComplete(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutTabComplete) Location(org.spongepowered.api.world.Location)

Aggregations

MessagePlayInTabComplete (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInTabComplete)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 LanternCommandManager (org.lanternpowered.server.command.LanternCommandManager)1 NetworkContext (org.lanternpowered.server.network.NetworkContext)1 Handler (org.lanternpowered.server.network.message.handler.Handler)1 MessagePlayOutTabComplete (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutTabComplete)1 Sponge (org.spongepowered.api.Sponge)1 CommandSource (org.spongepowered.api.command.CommandSource)1 Player (org.spongepowered.api.entity.living.player.Player)1 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)1 Cause (org.spongepowered.api.event.cause.Cause)1 EventContext (org.spongepowered.api.event.cause.EventContext)1 TabCompleteEvent (org.spongepowered.api.event.command.TabCompleteEvent)1 Location (org.spongepowered.api.world.Location)1 World (org.spongepowered.api.world.World)1