Search in sources :

Example 1 with SuggestionsBuilder

use of com.mojang.brigadier.suggestion.SuggestionsBuilder in project SpongeCommon by SpongePowered.

the class SpongeCommandDispatcher method getCompletionSuggestions.

@Override
public CompletableFuture<Suggestions> getCompletionSuggestions(final ParseResults<CommandSourceStack> parse, final int cursor) {
    final CommandContextBuilder<CommandSourceStack> context = parse.getContext();
    // Sponge Start - redirect if this actually represents a non-Brig command
    final CommandContextBuilder<CommandSourceStack> child = context.getLastChild();
    if (child instanceof SpongeCommandContextBuilder) {
        final SpongeCommandContextBuilder spongeChild = (SpongeCommandContextBuilder) child;
        if (((SpongeCommandContextBuilder) child).representsNonBrigCommand()) {
            // special handling!
            final String rawCommand = parse.getReader().getString();
            final String[] command = spongeChild.nonBrigCommand();
            // we know this will exist.
            final CommandMapping mapping = this.commandManager.commandMapping(command[0]).get();
            return CommandUtil.createSuggestionsForRawCommand(rawCommand, spongeChild.nonBrigCommand(), spongeChild.cause(), mapping).buildFuture();
        }
    }
    // Sponge End
    final SuggestionContext<CommandSourceStack> nodeBeforeCursor = context.findSuggestionContext(cursor);
    final CommandNode<CommandSourceStack> parent = nodeBeforeCursor.parent;
    final int start = Math.min(nodeBeforeCursor.startPos, cursor);
    final String fullInput = parse.getReader().getString();
    final String truncatedInput = fullInput.substring(0, cursor);
    // Sponge Start: the collection might be different.
    final Collection<CommandNode<CommandSourceStack>> children;
    if (parent instanceof SpongeNode) {
        children = ((SpongeNode) parent).getChildrenForSuggestions();
    } else {
        children = parent.getChildren();
    }
    // @SuppressWarnings("unchecked") final CompletableFuture<Suggestions>[] futures = new CompletableFuture[parent.getChildren().size()];
    @SuppressWarnings("unchecked") final CompletableFuture<Suggestions>[] futures = new CompletableFuture[children.size()];
    // Sponge End
    int i = 0;
    for (final CommandNode<CommandSourceStack> node : children) {
        // Sponge: parent.getChildren() -> children
        CompletableFuture<Suggestions> future = Suggestions.empty();
        try {
            future = node.listSuggestions(context.build(truncatedInput), new SuggestionsBuilder(truncatedInput, start));
        } catch (final CommandSyntaxException ignored) {
        }
        futures[i++] = future;
    }
    // See https://github.com/Mojang/brigadier/pull/81
    return CompletableFuture.allOf(futures).handle((voidResult, exception) -> {
        final List<Suggestions> suggestions = new ArrayList<>();
        for (final CompletableFuture<Suggestions> future : futures) {
            if (!future.isCompletedExceptionally()) {
                suggestions.add(future.join());
            }
        }
        return Suggestions.merge(fullInput, suggestions);
    });
// Sponge End
}
Also used : LiteralCommandNode(com.mojang.brigadier.tree.LiteralCommandNode) DummyCommandNode(org.spongepowered.common.command.brigadier.tree.DummyCommandNode) SpongeRootCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeRootCommandNode) RootCommandNode(com.mojang.brigadier.tree.RootCommandNode) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) CommandNode(com.mojang.brigadier.tree.CommandNode) ArrayList(java.util.ArrayList) CommandSourceStack(net.minecraft.commands.CommandSourceStack) SpongeCommandContextBuilder(org.spongepowered.common.command.brigadier.context.SpongeCommandContextBuilder) Suggestions(com.mojang.brigadier.suggestion.Suggestions) CompletableFuture(java.util.concurrent.CompletableFuture) CommandMapping(org.spongepowered.api.command.manager.CommandMapping) SpongeNode(org.spongepowered.common.command.brigadier.tree.SpongeNode) SuggestionsBuilder(com.mojang.brigadier.suggestion.SuggestionsBuilder) SpongeCommandSyntaxException(org.spongepowered.common.command.exception.SpongeCommandSyntaxException) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 2 with SuggestionsBuilder

use of com.mojang.brigadier.suggestion.SuggestionsBuilder in project SpongeCommon by SpongePowered.

the class SpongeArgumentCommandNode method suggestUsingModifier.

private CompletableFuture<Suggestions> suggestUsingModifier(final CommandContext<?> context, final SuggestionsBuilder suggestionsBuilder, final CompletableFuture<Suggestions> suggestions) {
    if (this.modifier != null) {
        return suggestions.thenApply(x -> {
            final List<CommandCompletion> originalSuggestions = x.getList().stream().map(SpongeCommandCompletion::from).collect(Collectors.toList());
            final List<CommandCompletion> modifiedSuggestions = this.modifier.modifyCompletion((org.spongepowered.api.command.parameter.CommandContext) context, suggestionsBuilder.getRemaining(), new ArrayList<>(originalSuggestions));
            if (originalSuggestions.equals(modifiedSuggestions)) {
                return x;
            }
            final SuggestionsBuilder newBuilder = suggestionsBuilder.restart();
            for (final CommandCompletion suggestion : modifiedSuggestions) {
                newBuilder.suggest(suggestion.completion(), suggestion.tooltip().map(SpongeAdventure::asVanilla).orElse(null));
            }
            return newBuilder.build();
        });
    }
    return suggestionsBuilder.buildFuture();
}
Also used : SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) SpongeCommandCompletion(org.spongepowered.common.command.SpongeCommandCompletion) CommandCompletion(org.spongepowered.api.command.CommandCompletion) SuggestionsBuilder(com.mojang.brigadier.suggestion.SuggestionsBuilder)

Example 3 with SuggestionsBuilder

use of com.mojang.brigadier.suggestion.SuggestionsBuilder in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin method impl$getSuggestionsFromNonBrigCommand.

@Inject(method = "handleCustomCommandSuggestions", at = @At(value = "NEW", target = "com/mojang/brigadier/StringReader", remap = false), cancellable = true)
private void impl$getSuggestionsFromNonBrigCommand(final ServerboundCommandSuggestionPacket packet, final CallbackInfo ci) {
    final String rawCommand = packet.getCommand();
    final String[] command = CommandUtil.extractCommandString(rawCommand);
    final CommandCause cause = CommandCause.create();
    final SpongeCommandManager manager = SpongeCommandManager.get(this.server);
    if (!rawCommand.contains(" ")) {
        final SuggestionsBuilder builder = new SuggestionsBuilder(command[0], 0);
        if (command[0].isEmpty()) {
            manager.getAliasesForCause(cause).forEach(builder::suggest);
        } else {
            manager.getAliasesThatStartWithForCause(cause, command[0]).forEach(builder::suggest);
        }
        this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), builder.build()));
        ci.cancel();
    } else {
        final Optional<CommandMapping> mappingOptional = manager.commandMapping(command[0].toLowerCase(Locale.ROOT)).filter(x -> !(x.registrar() instanceof BrigadierBasedRegistrar));
        if (mappingOptional.isPresent()) {
            final CommandMapping mapping = mappingOptional.get();
            if (mapping.registrar().canExecute(cause, mapping)) {
                final SuggestionsBuilder builder = CommandUtil.createSuggestionsForRawCommand(rawCommand, command, cause, mapping);
                this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), builder.build()));
            } else {
                this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), Suggestions.empty().join()));
            }
            ci.cancel();
        }
    }
}
Also used : CommandMapping(org.spongepowered.api.command.manager.CommandMapping) SpongeCommandManager(org.spongepowered.common.command.manager.SpongeCommandManager) CommandCause(org.spongepowered.api.command.CommandCause) SuggestionsBuilder(com.mojang.brigadier.suggestion.SuggestionsBuilder) ClientboundCommandSuggestionsPacket(net.minecraft.network.protocol.game.ClientboundCommandSuggestionsPacket) BrigadierBasedRegistrar(org.spongepowered.common.command.registrar.BrigadierBasedRegistrar) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 4 with SuggestionsBuilder

use of com.mojang.brigadier.suggestion.SuggestionsBuilder in project SpongeCommon by SpongePowered.

the class StandardArgumentParser method complete.

@Override
public List<CommandCompletion> complete(@NonNull final CommandCause context, @NonNull final String currentInput) {
    final SuggestionsBuilder suggestionsBuilder = new SuggestionsBuilder(currentInput, 0);
    this.listSuggestions(new SpongeCommandContextBuilder(null, (CommandSourceStack) context, new RootCommandNode<>(), 0).build(currentInput), suggestionsBuilder);
    return suggestionsBuilder.build().getList().stream().map(SpongeCommandCompletion::from).collect(Collectors.toList());
}
Also used : SuggestionsBuilder(com.mojang.brigadier.suggestion.SuggestionsBuilder) SpongeCommandContextBuilder(org.spongepowered.common.command.brigadier.context.SpongeCommandContextBuilder)

Aggregations

SuggestionsBuilder (com.mojang.brigadier.suggestion.SuggestionsBuilder)4 CommandMapping (org.spongepowered.api.command.manager.CommandMapping)2 SpongeCommandContextBuilder (org.spongepowered.common.command.brigadier.context.SpongeCommandContextBuilder)2 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 Suggestions (com.mojang.brigadier.suggestion.Suggestions)1 CommandNode (com.mojang.brigadier.tree.CommandNode)1 LiteralCommandNode (com.mojang.brigadier.tree.LiteralCommandNode)1 RootCommandNode (com.mojang.brigadier.tree.RootCommandNode)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CommandSourceStack (net.minecraft.commands.CommandSourceStack)1 ClientboundCommandSuggestionsPacket (net.minecraft.network.protocol.game.ClientboundCommandSuggestionsPacket)1 CommandCause (org.spongepowered.api.command.CommandCause)1 CommandCompletion (org.spongepowered.api.command.CommandCompletion)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1 SpongeAdventure (org.spongepowered.common.adventure.SpongeAdventure)1 SpongeCommandCompletion (org.spongepowered.common.command.SpongeCommandCompletion)1 DummyCommandNode (org.spongepowered.common.command.brigadier.tree.DummyCommandNode)1 SpongeArgumentCommandNode (org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode)1 SpongeNode (org.spongepowered.common.command.brigadier.tree.SpongeNode)1