Compare commits

...

2 commits

Author SHA1 Message Date
slicedlime (Mikael Hedberg)
ed60bb27c8 Use rootian locale for case conversion. 2021-03-18 15:32:47 +01:00
slicedlime (Mikael Hedberg)
b0029617cb Cache lowercase versions of inputs and literals to avoid repeated calls to toLowerCase().
Thank you to Spottedleaf.
2021-03-18 15:11:25 +01:00
4 changed files with 25 additions and 7 deletions

View file

@ -20,6 +20,7 @@ import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -588,12 +589,13 @@ public class CommandDispatcher<S> {
final String fullInput = parse.getReader().getString(); final String fullInput = parse.getReader().getString();
final String truncatedInput = fullInput.substring(0, cursor); 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()]; @SuppressWarnings("unchecked") final CompletableFuture<Suggestions>[] futures = new CompletableFuture[parent.getChildren().size()];
int i = 0; int i = 0;
for (final CommandNode<S> node : parent.getChildren()) { for (final CommandNode<S> node : parent.getChildren()) {
CompletableFuture<Suggestions> future = Suggestions.empty(); CompletableFuture<Suggestions> future = Suggestions.empty();
try { 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) { } catch (final CommandSyntaxException ignored) {
} }
futures[i++] = future; futures[i++] = future;

View file

@ -34,10 +34,10 @@ public class BoolArgumentType implements ArgumentType<Boolean> {
@Override @Override
public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) { public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
if ("true".startsWith(builder.getRemaining().toLowerCase())) { if ("true".startsWith(builder.getRemainingLowerCase())) {
builder.suggest("true"); builder.suggest("true");
} }
if ("false".startsWith(builder.getRemaining().toLowerCase())) { if ("false".startsWith(builder.getRemainingLowerCase())) {
builder.suggest("false"); builder.suggest("false");
} }
return builder.buildFuture(); return builder.buildFuture();

View file

@ -8,18 +8,27 @@ import com.mojang.brigadier.context.StringRange;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public class SuggestionsBuilder { public class SuggestionsBuilder {
private final String input; private final String input;
private final String inputLowerCase;
private final int start; private final int start;
private final String remaining; private final String remaining;
private final String remainingLowerCase;
private final List<Suggestion> result = new ArrayList<>(); 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.input = input;
this.inputLowerCase = inputLowerCase;
this.start = start; this.start = start;
this.remaining = input.substring(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() { public String getInput() {
@ -34,6 +43,10 @@ public class SuggestionsBuilder {
return remaining; return remaining;
} }
public String getRemainingLowerCase() {
return remainingLowerCase;
}
public Suggestions build() { public Suggestions build() {
return Suggestions.create(input, result); return Suggestions.create(input, result);
} }
@ -74,10 +87,10 @@ public class SuggestionsBuilder {
} }
public SuggestionsBuilder createOffset(final int start) { public SuggestionsBuilder createOffset(final int start) {
return new SuggestionsBuilder(input, start); return new SuggestionsBuilder(input, inputLowerCase, start);
} }
public SuggestionsBuilder restart() { public SuggestionsBuilder restart() {
return new SuggestionsBuilder(input, start); return createOffset(start);
} }
} }

View file

@ -16,15 +16,18 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Locale;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate; import java.util.function.Predicate;
public class LiteralCommandNode<S> extends CommandNode<S> { public class LiteralCommandNode<S> extends CommandNode<S> {
private final String literal; 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) { 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); super(command, requirement, redirect, modifier, forks);
this.literal = literal; this.literal = literal;
this.literalLowerCase = literal.toLowerCase(Locale.ROOT);
} }
public String getLiteral() { public String getLiteral() {
@ -66,7 +69,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
@Override @Override
public CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) { public CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
if (literal.toLowerCase().startsWith(builder.getRemaining().toLowerCase())) { if (literalLowerCase.startsWith(builder.getRemainingLowerCase())) {
return builder.suggest(literal).buildFuture(); return builder.suggest(literal).buildFuture();
} else { } else {
return Suggestions.empty(); return Suggestions.empty();