com.mojang.brigadier.suggestion.Suggestions Java Examples
The following examples show how to use
com.mojang.brigadier.suggestion.Suggestions.
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: BukkitCommands.java From BlueMap with MIT License | 6 votes |
@EventHandler public void onTabComplete(TabCompleteEvent evt) { try { Suggestions suggestions = dispatcher.getCompletionSuggestions(dispatcher.parse(evt.getBuffer().substring(1), evt.getSender())).get(100, TimeUnit.MILLISECONDS); List<String> completions = new ArrayList<>(); for (Suggestion suggestion : suggestions.getList()) { String text = suggestion.getText(); if (text.indexOf(' ') == -1) { completions.add(text); } } if (!completions.isEmpty()) { completions.sort((s1, s2) -> s1.compareToIgnoreCase(s2)); evt.setCompletions(completions); } } catch (InterruptedException | ExecutionException | TimeoutException ignore) {} }
Example #2
Source File: SpongeCommands.java From BlueMap with MIT License | 6 votes |
@Override public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition) throws CommandException { String command = label; if (!arguments.isEmpty()) { command += " " + arguments; } List<String> completions = new ArrayList<>(); try { Suggestions suggestions = dispatcher.getCompletionSuggestions(dispatcher.parse(command, source)).get(100, TimeUnit.MILLISECONDS); for (Suggestion suggestion : suggestions.getList()) { String text = suggestion.getText(); if (text.indexOf(' ') == -1) { completions.add(text); } } } catch (InterruptedException | ExecutionException | TimeoutException ignore) {} completions.sort((s1, s2) -> s1.compareToIgnoreCase(s2)); return completions; }
Example #3
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 6 votes |
@Test public void getCompletionSuggestions_redirect_lots() throws Exception { final LiteralCommandNode<Object> loop = subject.register(literal("redirect")); subject.register( literal("redirect") .then( literal("loop") .then( argument("loop", integer()) .redirect(loop) ) ) ); final Suggestions result = subject.getCompletionSuggestions(subject.parse("redirect loop 1 loop 02 loop 003 ", source)).join(); assertThat(result.getRange(), equalTo(StringRange.at(33))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(33), "loop")))); }
Example #4
Source File: FabricClientSparkPlugin.java From spark with GNU General Public License v3.0 | 6 votes |
@Override public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException { String[] split = context.getInput().split(" "); if (split.length == 0 || (!split[0].equals("/sparkc") && !split[0].equals("/sparkclient"))) { return Suggestions.empty(); } String[] args = Arrays.copyOfRange(split, 1, split.length); return CompletableFuture.supplyAsync(() -> { for (String suggestion : this.platform.tabCompleteCommand(new FabricCommandSender(this.minecraft.player, this), args)) { builder.suggest(suggestion); } return builder.build(); }); }
Example #5
Source File: EntityArgumentType_1_12_2.java From multiconnect with MIT License | 6 votes |
@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 #6
Source File: ForgeClientSparkPlugin.java From spark with GNU General Public License v3.0 | 6 votes |
@Override public CompletableFuture<Suggestions> getSuggestions(CommandContext<ISuggestionProvider> context, SuggestionsBuilder builder) throws CommandSyntaxException { String chat = context.getInput(); String[] split = chat.split(" "); if (split.length == 0 || (!split[0].equals("/sparkc") && !split[0].equals("/sparkclient"))) { return Suggestions.empty(); } String[] args = Arrays.copyOfRange(split, 1, split.length); return CompletableFuture.supplyAsync(() -> { for (String suggestion : this.platform.tabCompleteCommand(new ForgeCommandSender(this.minecraft.player, this), args)) { builder.suggest(suggestion); } return builder.build(); }); }
Example #7
Source File: VRCommandHandler.java From ViaFabric with MIT License | 6 votes |
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 #8
Source File: ScriptCommand.java From fabric-carpet with MIT License | 6 votes |
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 #9
Source File: LiteralCommandNode.java From brigadier with MIT License | 5 votes |
@Override public CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) { if (literal.toLowerCase().startsWith(builder.getRemaining().toLowerCase())) { return builder.suggest(literal).buildFuture(); } else { return Suggestions.empty(); } }
Example #10
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_execute_simulation_partial() throws Exception { final LiteralCommandNode<Object> execute = subject.register(literal("execute")); subject.register( literal("execute") .then( literal("as") .then(literal("bar").redirect(execute)) .then(literal("baz").redirect(execute)) ) .then( literal("store") .then( argument("name", word()) .redirect(execute) ) ) .then( literal("run") .executes(c -> 0) ) ); final ParseResults<Object> parse = subject.parse("execute as bar as ", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.at(18))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(18), "bar"), new Suggestion(StringRange.at(18), "baz")))); }
Example #11
Source File: ArgumentCommandNode.java From brigadier with MIT License | 5 votes |
@Override public CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) throws CommandSyntaxException { if (customSuggestions == null) { return type.listSuggestions(context, builder); } else { return customSuggestions.getSuggestions(context, builder); } }
Example #12
Source File: BoolArgumentType.java From brigadier with MIT License | 5 votes |
@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 #13
Source File: LiteralCommandNodeTest.java From brigadier with MIT License | 5 votes |
@Test public void testSuggestions() throws Exception { final Suggestions empty = node.listSuggestions(contextBuilder.build(""), new SuggestionsBuilder("", 0)).join(); assertThat(empty.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(0), "foo")))); final Suggestions foo = node.listSuggestions(contextBuilder.build("foo"), new SuggestionsBuilder("foo", 0)).join(); assertThat(foo.isEmpty(), is(true)); final Suggestions food = node.listSuggestions(contextBuilder.build("food"), new SuggestionsBuilder("food", 0)).join(); assertThat(food.isEmpty(), is(true)); final Suggestions b = node.listSuggestions(contextBuilder.build("b"), new SuggestionsBuilder("b", 0)).join(); assertThat(food.isEmpty(), is(true)); }
Example #14
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
private void testSuggestions(final String contents, final int cursor, final StringRange range, final String... suggestions) { final Suggestions result = subject.getCompletionSuggestions(subject.parse(contents, source), cursor).join(); assertThat(result.getRange(), equalTo(range)); final List<Suggestion> expected = Lists.newArrayList(); for (final String suggestion : suggestions) { expected.add(new Suggestion(range, suggestion)); } assertThat(result.getList(), equalTo(expected)); }
Example #15
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_rootCommands() throws Exception { subject.register(literal("foo")); subject.register(literal("bar")); subject.register(literal("baz")); final Suggestions result = subject.getCompletionSuggestions(subject.parse("", source)).join(); assertThat(result.getRange(), equalTo(StringRange.at(0))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(0), "bar"), new Suggestion(StringRange.at(0), "baz"), new Suggestion(StringRange.at(0), "foo")))); }
Example #16
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_rootCommands_withInputOffset() throws Exception { subject.register(literal("foo")); subject.register(literal("bar")); subject.register(literal("baz")); final Suggestions result = subject.getCompletionSuggestions(subject.parse(inputWithOffset("OOO", 3), source)).join(); assertThat(result.getRange(), equalTo(StringRange.at(3))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(3), "bar"), new Suggestion(StringRange.at(3), "baz"), new Suggestion(StringRange.at(3), "foo")))); }
Example #17
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_rootCommands_partial() throws Exception { subject.register(literal("foo")); subject.register(literal("bar")); subject.register(literal("baz")); final Suggestions result = subject.getCompletionSuggestions(subject.parse("b", source)).join(); assertThat(result.getRange(), equalTo(StringRange.between(0, 1))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(0, 1), "bar"), new Suggestion(StringRange.between(0, 1), "baz")))); }
Example #18
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_rootCommands_partial_withInputOffset() throws Exception { subject.register(literal("foo")); subject.register(literal("bar")); subject.register(literal("baz")); final Suggestions result = subject.getCompletionSuggestions(subject.parse(inputWithOffset("Zb", 1), source)).join(); assertThat(result.getRange(), equalTo(StringRange.between(1, 2))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(1, 2), "bar"), new Suggestion(StringRange.between(1, 2), "baz")))); }
Example #19
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_subCommands() throws Exception { subject.register( literal("parent") .then(literal("foo")) .then(literal("bar")) .then(literal("baz")) ); final Suggestions result = subject.getCompletionSuggestions(subject.parse("parent ", source)).join(); assertThat(result.getRange(), equalTo(StringRange.at(7))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(7), "bar"), new Suggestion(StringRange.at(7), "baz"), new Suggestion(StringRange.at(7), "foo")))); }
Example #20
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_subCommands_partial() throws Exception { subject.register( literal("parent") .then(literal("foo")) .then(literal("bar")) .then(literal("baz")) ); final ParseResults<Object> parse = subject.parse("parent b", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.between(7, 8))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(7, 8), "bar"), new Suggestion(StringRange.between(7, 8), "baz")))); }
Example #21
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_subCommands_partial_withInputOffset() throws Exception { subject.register( literal("parent") .then(literal("foo")) .then(literal("bar")) .then(literal("baz")) ); final ParseResults<Object> parse = subject.parse(inputWithOffset("junk parent b", 5), source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.between(12, 13))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(12, 13), "bar"), new Suggestion(StringRange.between(12, 13), "baz")))); }
Example #22
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_redirect() throws Exception { final LiteralCommandNode<Object> actual = subject.register(literal("actual").then(literal("sub"))); subject.register(literal("redirect").redirect(actual)); final ParseResults<Object> parse = subject.parse("redirect ", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.at(9))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(9), "sub")))); }
Example #23
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_redirectPartial() throws Exception { final LiteralCommandNode<Object> actual = subject.register(literal("actual").then(literal("sub"))); subject.register(literal("redirect").redirect(actual)); final ParseResults<Object> parse = subject.parse("redirect s", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.between(9, 10))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(9, 10), "sub")))); }
Example #24
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_redirectPartial_withInputOffset() throws Exception { final LiteralCommandNode<Object> actual = subject.register(literal("actual").then(literal("sub"))); subject.register(literal("redirect").redirect(actual)); final ParseResults<Object> parse = subject.parse(inputWithOffset("/redirect s", 1), source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.between(10, 11))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(10, 11), "sub")))); }
Example #25
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_execute_simulation() throws Exception { final LiteralCommandNode<Object> execute = subject.register(literal("execute")); subject.register( literal("execute") .then( literal("as") .then( argument("name", word()) .redirect(execute) ) ) .then( literal("store") .then( argument("name", word()) .redirect(execute) ) ) .then( literal("run") .executes(c -> 0) ) ); final ParseResults<Object> parse = subject.parse("execute as Dinnerbone as", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.isEmpty(), is(true)); }
Example #26
Source File: NMS_1_16_R1.java From 1.13-Command-API with Apache License 2.0 | 5 votes |
@Override public SuggestionProvider getSuggestionProvider(SuggestionProviders provider) { switch (provider) { case FUNCTION: return (context, builder) -> { CustomFunctionData functionData = getCLW(context).getServer().getFunctionData(); ICompletionProvider.a(functionData.g(), builder, "#"); return ICompletionProvider.a(functionData.f(), builder); }; case RECIPES: return CompletionProviders.b; case SOUNDS: return CompletionProviders.c; case ADVANCEMENTS: return (cmdCtx, builder) -> { Collection<Advancement> advancements = ((CommandListenerWrapper) cmdCtx.getSource()).getServer() .getAdvancementData().getAdvancements(); return ICompletionProvider.a(advancements.stream().map(Advancement::getName), builder); }; case LOOT_TABLES: return (context, builder) -> { LootTableRegistry lootTables = getCLW(context).getServer().getLootTableRegistry(); return ICompletionProvider.a(lootTables.a(), builder); }; case BIOMES: return CompletionProviders.d; case ENTITIES: return CompletionProviders.e; default: return (context, builder) -> Suggestions.empty(); } }
Example #27
Source File: AbstractSuggestionProvider.java From BlueMap with MIT License | 5 votes |
@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 #28
Source File: UnionArgumentType.java From multiconnect with MIT License | 5 votes |
@Override public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { CompletableFuture<Suggestions> leftFuture = left.listSuggestions(context, builder.restart()); CompletableFuture<Suggestions> rightFuture = right.listSuggestions(context, builder.restart()); return CompletableFuture.allOf(leftFuture, rightFuture) .thenCompose(v -> mergeSuggestions(leftFuture.join(), rightFuture.join())); }
Example #29
Source File: UnionArgumentType.java From multiconnect with MIT License | 5 votes |
static CompletableFuture<Suggestions> mergeSuggestions(Suggestions a, Suggestions b) { if (a.isEmpty() && b.isEmpty()) return Suggestions.empty(); if (a.isEmpty()) return CompletableFuture.completedFuture(b); if (b.isEmpty()) return CompletableFuture.completedFuture(a); Set<Suggestion> suggestions = new LinkedHashSet<>(); suggestions.addAll(a.getList()); suggestions.addAll(b.getList()); return CompletableFuture.completedFuture(new Suggestions(StringRange.encompassing(a.getRange(), b.getRange()), new ArrayList<>(suggestions))); }
Example #30
Source File: EntityArgumentType_1_12_2.java From multiconnect with MIT License | 5 votes |
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())); }; }