com.mojang.brigadier.suggestion.Suggestion Java Examples

The following examples show how to use com.mojang.brigadier.suggestion.Suggestion. 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 vote down vote up
@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 vote down vote up
@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 vote down vote up
@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: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #5
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #6
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #7
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #8
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #9
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #10
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #11
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #12
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #13
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #14
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #15
Source File: CommandSuggestionsTest.java    From brigadier with MIT License 5 votes vote down vote up
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 #16
Source File: LiteralCommandNodeTest.java    From brigadier with MIT License 5 votes vote down vote up
@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 #17
Source File: UnionArgumentType.java    From multiconnect with MIT License 5 votes vote down vote up
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)));
}