Merge branch 'master' into patch-1
This commit is contained in:
commit
1d1a222611
5 changed files with 29 additions and 15 deletions
README.md
src/main/java/com/mojang/brigadier
|
@ -38,7 +38,7 @@ And then use this library (change `(the latest version)` to the latest version!)
|
|||
```
|
||||
|
||||
# Contributing
|
||||
Contributions are welcome ! :D
|
||||
Contributions are welcome! :D
|
||||
|
||||
Most contributions will require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to,
|
||||
and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
|
||||
|
@ -128,8 +128,8 @@ the reason why is inside this exception map.
|
|||
## Displaying usage info
|
||||
There are two forms of "usage strings" provided by this library, both require a target node.
|
||||
|
||||
`getAllUsage(node, source, restricted)` will return a list of all possible commands (executable end-points) under the target node and their human readable path. If `restricted`, it will ignore commands that `source` does not have access to. This will look like [`foo`, `foo <bar>`]
|
||||
`getAllUsage(node, source, restricted)` will return a list of all possible commands (executable end-points) under the target node and their human readable path. If `restricted`, it will ignore commands that `source` does not have access to. This will look like [`foo`, `foo <bar>`].
|
||||
|
||||
`getSmartUsage(node, source)` will return a map of the child nodes to their "smart usage" human readable path. This tries to squash future-nodes together and show optional & typed information, and can look like `foo (<bar>)`
|
||||
`getSmartUsage(node, source)` will return a map of the child nodes to their "smart usage" human readable path. This tries to squash future-nodes together and show optional & typed information, and can look like `foo (<bar>)`.
|
||||
|
||||
[](https://github.com/Mojang/brigadier/fork) [](https://github.com/Mojang/brigadier/stargazers)
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.util.Collections;
|
|||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
@ -588,12 +589,13 @@ public class CommandDispatcher<S> {
|
|||
|
||||
final String fullInput = parse.getReader().getString();
|
||||
final String truncatedInput = fullInput.substring(0, cursor);
|
||||
final String truncatedInputLowerCase = truncatedInput.toLowerCase(Locale.ROOT);
|
||||
@SuppressWarnings("unchecked") final CompletableFuture<Suggestions>[] futures = new CompletableFuture[parent.getChildren().size()];
|
||||
int i = 0;
|
||||
for (final CommandNode<S> node : parent.getChildren()) {
|
||||
CompletableFuture<Suggestions> future = Suggestions.empty();
|
||||
try {
|
||||
future = node.listSuggestions(context.build(truncatedInput), new SuggestionsBuilder(truncatedInput, start));
|
||||
future = node.listSuggestions(context.build(truncatedInput), new SuggestionsBuilder(truncatedInput, truncatedInputLowerCase, start));
|
||||
} catch (final CommandSyntaxException ignored) {
|
||||
}
|
||||
futures[i++] = future;
|
||||
|
|
|
@ -8,18 +8,27 @@ import com.mojang.brigadier.context.StringRange;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class SuggestionsBuilder {
|
||||
private final String input;
|
||||
private final String inputLowerCase;
|
||||
private final int start;
|
||||
private final String remaining;
|
||||
private final String remainingLowerCase;
|
||||
private final List<Suggestion> result = new ArrayList<>();
|
||||
|
||||
public SuggestionsBuilder(final String input, final int start) {
|
||||
public SuggestionsBuilder(final String input, final String inputLowerCase, final int start) {
|
||||
this.input = input;
|
||||
this.inputLowerCase = inputLowerCase;
|
||||
this.start = start;
|
||||
this.remaining = input.substring(start);
|
||||
this.remainingLowerCase = inputLowerCase.substring(start);
|
||||
}
|
||||
|
||||
public SuggestionsBuilder(final String input, final int start) {
|
||||
this(input, input.toLowerCase(Locale.ROOT), start);
|
||||
}
|
||||
|
||||
public String getInput() {
|
||||
|
@ -34,6 +43,10 @@ public class SuggestionsBuilder {
|
|||
return remaining;
|
||||
}
|
||||
|
||||
public String getRemainingLowerCase() {
|
||||
return remainingLowerCase;
|
||||
}
|
||||
|
||||
public Suggestions build() {
|
||||
return Suggestions.create(input, result);
|
||||
}
|
||||
|
@ -74,10 +87,10 @@ public class SuggestionsBuilder {
|
|||
}
|
||||
|
||||
public SuggestionsBuilder createOffset(final int start) {
|
||||
return new SuggestionsBuilder(input, start);
|
||||
return new SuggestionsBuilder(input, inputLowerCase, start);
|
||||
}
|
||||
|
||||
public SuggestionsBuilder restart() {
|
||||
return new SuggestionsBuilder(input, start);
|
||||
return createOffset(start);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,12 +22,11 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
private Map<String, CommandNode<S>> children = new LinkedHashMap<>();
|
||||
private Map<String, LiteralCommandNode<S>> literals = new LinkedHashMap<>();
|
||||
private Map<String, ArgumentCommandNode<S, ?>> arguments = new LinkedHashMap<>();
|
||||
private final Map<String, CommandNode<S>> children = new LinkedHashMap<>();
|
||||
private final Map<String, LiteralCommandNode<S>> literals = new LinkedHashMap<>();
|
||||
private final Map<String, ArgumentCommandNode<S, ?>> arguments = new LinkedHashMap<>();
|
||||
private final Predicate<S> requirement;
|
||||
private final CommandNode<S> redirect;
|
||||
private final RedirectModifier<S> modifier;
|
||||
|
@ -88,8 +87,6 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
|||
arguments.put(node.getName(), (ArgumentCommandNode<S, ?>) node);
|
||||
}
|
||||
}
|
||||
|
||||
children = children.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
||||
}
|
||||
|
||||
public void findAmbiguities(final AmbiguityConsumer<S> consumer) {
|
||||
|
|
|
@ -22,10 +22,12 @@ import java.util.function.Predicate;
|
|||
|
||||
public class LiteralCommandNode<S> extends CommandNode<S> {
|
||||
private final String literal;
|
||||
private final String literalLowerCase;
|
||||
|
||||
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
|
||||
super(command, requirement, redirect, modifier, forks);
|
||||
this.literal = literal.toLowerCase(Locale.ROOT);
|
||||
this.literal = literal;
|
||||
this.literalLowerCase = literal.toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
public String getLiteral() {
|
||||
|
@ -67,7 +69,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
|
|||
|
||||
@Override
|
||||
public CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
||||
if (literal.startsWith(builder.getRemaining().toLowerCase(Locale.ROOT))) {
|
||||
if (literalLowerCase.startsWith(builder.getRemainingLowerCase())) {
|
||||
return builder.suggest(literal).buildFuture();
|
||||
} else {
|
||||
return Suggestions.empty();
|
||||
|
|
Loading…
Reference in a new issue