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
}
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();
}
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();
}
}
}
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());
}
Aggregations