Java Code Examples for com.mojang.brigadier.suggestion.SuggestionsBuilder#buildFuture()

The following examples show how to use com.mojang.brigadier.suggestion.SuggestionsBuilder#buildFuture() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityArgumentType_1_12_2.java    From multiconnect with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    if (!(context.getSource() instanceof CommandSource))
        return builder.buildFuture();

    StringReader reader = new StringReader(builder.getInput());
    reader.setCursor(builder.getStart());

    CompletableFuture<Suggestions> playerCompletions;
    if ((reader.canRead() && reader.peek() == '@') || !suggestPlayerNames) {
        playerCompletions = Suggestions.empty();
    } else {
        playerCompletions = ((CommandSource) context.getSource()).getCompletions((CommandContext<CommandSource>) context, builder.restart());
    }

    EntitySelectorParser parser = new EntitySelectorParser(reader, singleTarget, playersOnly);
    try {
        parser.parse();
    } catch (CommandSyntaxException ignore) {
    }
    CompletableFuture<Suggestions> selectorCompletions = parser.suggestor.apply(builder.restart());

    return CompletableFuture.allOf(playerCompletions, selectorCompletions)
            .thenCompose(v -> UnionArgumentType.mergeSuggestions(playerCompletions.join(), selectorCompletions.join()));
}
 
Example 2
Source File: VRCommandHandler.java    From ViaFabric with MIT License 6 votes vote down vote up
public CompletableFuture<Suggestions> suggestion(CommandContext<? extends CommandSource> ctx, SuggestionsBuilder builder) {
    String[] args;
    try {
        args = StringArgumentType.getString(ctx, "args").split(" ", -1);
    } catch (IllegalArgumentException ignored) {
        args = new String[]{""};
    }
    String[] pref = args.clone();
    pref[pref.length - 1] = "";
    String prefix = String.join(" ", pref);
    onTabComplete(new NMSCommandSender(ctx.getSource()), args)
            .stream()
            .map(it -> {
                SuggestionsBuilder b = new SuggestionsBuilder(builder.getInput(), prefix.length() + builder.getStart());
                b.suggest(it);
                return b;
            })
            .forEach(builder::add);
    return builder.buildFuture();
}
 
Example 3
Source File: ScriptCommand.java    From fabric-carpet with MIT License 6 votes vote down vote up
private static CompletableFuture<Suggestions> suggestCode(
        CommandContext<ServerCommandSource> context,
        SuggestionsBuilder suggestionsBuilder
)
{
    CarpetScriptHost currentHost = getHost(context);
    String previous = suggestionsBuilder.getRemaining().toLowerCase(Locale.ROOT);
    int strlen = previous.length();
    StringBuilder lastToken = new StringBuilder();
    for (int idx = strlen-1; idx >=0; idx--)
    {
        char ch = previous.charAt(idx);
        if (Character.isLetterOrDigit(ch) || ch == '_') lastToken.append(ch); else break;
    }
    if (lastToken.length() == 0)
        return suggestionsBuilder.buildFuture();
    String prefix = lastToken.reverse().toString();
    String previousString =  previous.substring(0,previous.length()-prefix.length()) ;
    suggestFunctions(currentHost, previousString, prefix).forEach(text -> suggestionsBuilder.suggest(previousString+text));
    return suggestionsBuilder.buildFuture();
}
 
Example 4
Source File: AbstractSuggestionProvider.java    From BlueMap with MIT License 5 votes vote down vote up
@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<S> context, SuggestionsBuilder builder) throws CommandSyntaxException {
	Collection<String> possibleValues = getPossibleValues();
	if(possibleValues.isEmpty()) return Suggestions.empty();

	String remaining = builder.getRemaining().toLowerCase();
	for (String str : possibleValues) {
		if (str.toLowerCase().startsWith(remaining)) {
			builder.suggest(str = StringArgumentType.escapeIfRequired(str));
		}
	}
	
	return builder.buildFuture();
}
 
Example 5
Source File: EntityArgumentType_1_12_2.java    From multiconnect with MIT License 5 votes vote down vote up
private void suggestOption() {
    int start = reader.getCursor();
    List<String> seenOptionsCopy = new ArrayList<>(seenOptions);

    suggestor = builder -> {
        SuggestionsBuilder normalOptionBuilder = builder.createOffset(start);
        CommandSource.suggestMatching(SELECTOR_OPTIONS.keySet().stream()
                .filter(opt -> SELECTOR_OPTIONS.get(opt).isAllowed(this))
                .filter(opt -> !seenOptionsCopy.contains(opt))
                .map(opt -> opt + "=")
                .collect(Collectors.toSet()), normalOptionBuilder);
        CompletableFuture<Suggestions> normalOptions = normalOptionBuilder.buildFuture();

        SuggestionsBuilder scoreOptionBuilder = builder.createOffset(start);
        CompletableFuture<Suggestions> scoreOptions = getScoreObjectives().thenCompose(objectives -> {
            CommandSource.suggestMatching(objectives.stream()
                    .map(str -> "score_" + str)
                    .filter(str -> !seenOptionsCopy.contains(str))
                    .map(str -> str + "="),
                    scoreOptionBuilder);
            CommandSource.suggestMatching(objectives.stream()
                    .map(str -> "score_" + str + "_min")
                    .filter(str -> !seenOptionsCopy.contains(str))
                    .map(str -> str + "="),
                    scoreOptionBuilder);
            return scoreOptionBuilder.buildFuture();
        });

        return CompletableFuture.allOf(normalOptions, scoreOptions)
                .thenCompose(v -> UnionArgumentType.mergeSuggestions(normalOptions.join(), scoreOptions.join()));
    };
}
 
Example 6
Source File: BlockStateArgumentType_1_12_2.java    From multiconnect with MIT License 5 votes vote down vote up
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    int spaceIndex = builder.getRemaining().indexOf(' ');
    if (spaceIndex == -1) {
        CommandSource.suggestIdentifiers(Registry.BLOCK.getIds().stream().filter(id -> isValidBlock(Registry.BLOCK.get(id))), builder);
        return builder.buildFuture();
    }

    Identifier blockId = Identifier.tryParse(builder.getInput().substring(builder.getStart(), builder.getStart() + spaceIndex));

    String propertiesStr = builder.getInput().substring(builder.getStart() + spaceIndex + 1);

    int commaIndex = propertiesStr.lastIndexOf(',');
    int equalsIndex = propertiesStr.lastIndexOf('=');
    builder = builder.createOffset(builder.getStart() + spaceIndex + commaIndex + 2);

    if (commaIndex == -1 && equalsIndex == -1) {
        CommandSource.suggestMatching(new String[] {"default"}, builder);
        if (test)
            CommandSource.suggestMatching(new String[] {"*"}, builder);
    }

    if (blockId == null || !BlockStateReverseFlattening.OLD_PROPERTIES.containsKey(blockId))
        return builder.buildFuture();

    if (equalsIndex <= commaIndex) {
        CommandSource.suggestMatching(BlockStateReverseFlattening.OLD_PROPERTIES.get(blockId).stream().map(str -> str + "="), builder);
    } else {
        String property = builder.getInput().substring(builder.getStart(), builder.getStart() + equalsIndex - commaIndex - 1);
        List<String> values = BlockStateReverseFlattening.OLD_PROPERTY_VALUES.get(Pair.of(blockId, property));
        builder = builder.createOffset(builder.getStart() + equalsIndex - commaIndex);
        CommandSource.suggestMatching(values, builder);
    }

    return builder.buildFuture();
}
 
Example 7
Source File: CommandAPIHandler.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Suggestions> getSuggestionsBuilder(SuggestionsBuilder builder, String[] array) {
	String remaining = builder.getRemaining().toLowerCase(Locale.ROOT);
	for (int i = 0; i < array.length; i++) {
		String str = array[i];
		if (str.toLowerCase(Locale.ROOT).startsWith(remaining)) {
			builder.suggest(str);
		}
	}
	return builder.buildFuture();
}
 
Example 8
Source File: BoolArgumentType.java    From brigadier with MIT License 5 votes vote down vote up
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
    if ("true".startsWith(builder.getRemaining().toLowerCase())) {
        builder.suggest("true");
    }
    if ("false".startsWith(builder.getRemaining().toLowerCase())) {
        builder.suggest("false");
    }
    return builder.buildFuture();
}
 
Example 9
Source File: AchievementArgumentType.java    From multiconnect with MIT License 4 votes vote down vote up
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    CommandSource.suggestMatching(Achievements_1_11_2.ACHIEVEMENTS.keySet().stream().map(it -> "achievement." + it), builder);
    return builder.buildFuture();
}
 
Example 10
Source File: TPArgumentType.java    From multiconnect with MIT License 4 votes vote down vote up
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    List<CompletableFuture<Suggestions>> suggestions = new ArrayList<>();

    String[] args = builder.getRemaining().split(" ", -1);
    if (args.length == 1) {
        suggestions.add(blockPos.listSuggestions(context, builder.restart()));
        suggestions.add(victim.listSuggestions(context, builder.restart()));
        suggestions.add(target.listSuggestions(context, builder.restart()));
    } else {
        StringReader reader = new StringReader(builder.getInput());
        reader.setCursor(builder.getStart());

        boolean victimPresent = !isCoordinateArg(reader);
        if (victimPresent) {
            reader.setCursor(builder.getStart());
            try {
                victim.parse(reader);
            } catch (CommandSyntaxException e) {
                return builder.buildFuture();
            }
            if (reader.peek() != ' ')
                return builder.buildFuture();
        }
        reader.skip();
        if (args.length == 2) {
            if (victimPresent) {
                suggestions.add(blockPos.listSuggestions(context, builder.createOffset(reader.getCursor())));
                suggestions.add(target.listSuggestions(context, builder.createOffset(reader.getCursor())));
            } else {
                suggestions.add(blockPos.listSuggestions(context, builder.restart()));
            }
        } else {
            int argStart = victimPresent ? reader.getCursor() : builder.getStart();
            if (!isCoordinateArg(reader))
                return builder.buildFuture();
            if (args.length <= (victimPresent ? 4 : 3)) {
                suggestions.add(blockPos.listSuggestions(context, builder.createOffset(argStart)));
            } else {
                if (victimPresent) {
                    reader.skip();
                    isCoordinateArg(reader);
                }
                reader.skip();
                isCoordinateArg(reader);
                reader.skip();
                suggestions.add(CommandSource.suggestMatching(new String[] {"~", "~ ~"}, builder.createOffset(reader.getCursor())));
            }
        }
    }

    return CompletableFuture.allOf(suggestions.toArray(new CompletableFuture[0]))
            .thenApply(v -> Suggestions.merge(builder.getInput(), suggestions.stream().map(CompletableFuture::join).collect(Collectors.toList())));
}
 
Example 11
Source File: ParticleArgumentType_1_12_2.java    From multiconnect with MIT License 4 votes vote down vote up
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    CommandSource.suggestIdentifiers(Registry.PARTICLE_TYPE.getIds(), builder);
    return builder.buildFuture();
}
 
Example 12
Source File: EnumArgumentType.java    From multiconnect with MIT License 4 votes vote down vote up
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    CommandSource.suggestMatching(values, builder);
    return builder.buildFuture();
}
 
Example 13
Source File: ItemArgumentType_1_12_2.java    From multiconnect with MIT License 4 votes vote down vote up
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    CommandSource.suggestIdentifiers(Registry.ITEM.getIds().stream().filter(id -> isValidItem(Registry.ITEM.get(id))), builder);
    return builder.buildFuture();
}
 
Example 14
Source File: AvailableCommands.java    From Velocity with MIT License 4 votes vote down vote up
@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<Object> context,
    SuggestionsBuilder builder) throws CommandSyntaxException {
  return builder.buildFuture();
}
 
Example 15
Source File: HexColorArgument.java    From BoundingBoxOutlineReloaded with MIT License 4 votes vote down vote up
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    if (builder.getRemaining().length() == 0) builder.suggest("#");
    return builder.buildFuture();
}