Fix suggestions and add a bukkit test module

This commit is contained in:
Alexander Söderberg 2020-09-13 22:07:34 +02:00
parent 2cb367903f
commit e7c59b2062
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
10 changed files with 248 additions and 26 deletions

View file

@ -141,6 +141,9 @@ public abstract class CommandManager<C extends CommandSender, M extends CommandM
while (stringTokenizer.hasMoreElements()) {
tokens.add(stringTokenizer.nextToken());
}
if (input.endsWith(" ")) {
tokens.add("");
}
return tokens;
}

View file

@ -272,25 +272,18 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
final Node<CommandComponent<C, ?>> child = children.get(0);
if (child.getValue() != null) {
if (commandQueue.isEmpty()) {
if (child.isLeaf()) {
/* Child is leaf, and so no suggestions should be sent */
return Collections.emptyList();
} else {
/* Send all suggestions */
return child.getValue().getParser().suggestions(commandContext, "");
}
return child.getValue().getParser().suggestions(commandContext, "");
} else if (child.isLeaf() && commandQueue.size() < 2) {
return child.getValue().getParser().suggestions(commandContext, commandQueue.peek());
} else if (child.isLeaf()) {
return Collections.emptyList();
}
final ComponentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue);
if (result.getParsedValue().isPresent()) {
if (child.isLeaf()) {
/* Child is leaf, and so no suggestions should be sent */
return Collections.emptyList();
}
commandContext.store(child.getValue().getName(), result.getParsedValue().get());
return this.getSuggestions(commandContext, commandQueue, child);
} else if (result.getFailure().isPresent()) {
/* I need to return ze error */
return Collections.emptyList();
return child.getValue().getParser().suggestions(commandContext, commandQueue.peek());
}
}
}

View file

@ -30,17 +30,23 @@ import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.sender.CommandSender;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.StringJoiner;
import java.util.function.BiFunction;
@SuppressWarnings("unused")
public final class StringComponent<C extends CommandSender> extends CommandComponent<C, String> {
private final StringMode stringMode;
private StringComponent(final boolean required, @Nonnull final String name,
@Nonnull final StringMode stringMode, @Nonnull final String defaultValue) {
super(required, name, new StringParser<>(stringMode), defaultValue);
private StringComponent(final boolean required,
@Nonnull final String name,
@Nonnull final StringMode stringMode,
@Nonnull final String defaultValue,
@Nonnull final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider) {
super(required, name, new StringParser<>(stringMode, suggestionsProvider), defaultValue);
this.stringMode = stringMode;
}
@ -115,6 +121,7 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, String> {
private StringMode stringMode = StringMode.SINGLE;
private BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider = (v1, v2) -> Collections.emptyList();
protected Builder(@Nonnull final String name) {
super(name);
@ -153,6 +160,19 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
return this;
}
/**
* Set the suggestions provider
*
* @param suggestionsProvider Suggestions provider
* @return Builder instance
*/
@Nonnull
public Builder<C> withSuggestionsProvider(@Nonnull final BiFunction<CommandContext<C>,
String, List<String>> suggestionsProvider) {
this.suggestionsProvider = suggestionsProvider;
return this;
}
/**
* Builder a new string component
*
@ -161,7 +181,8 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
@Nonnull
@Override
public StringComponent<C> build() {
return new StringComponent<>(this.isRequired(), this.getName(), this.stringMode, this.getDefaultValue());
return new StringComponent<>(this.isRequired(), this.getName(), this.stringMode,
this.getDefaultValue(), this.suggestionsProvider);
}
}
@ -170,9 +191,12 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
private static final class StringParser<C extends CommandSender> implements ComponentParser<C, String> {
private final StringMode stringMode;
private final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider;
private StringParser(@Nonnull final StringMode stringMode) {
private StringParser(@Nonnull final StringMode stringMode,
@Nonnull final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider) {
this.stringMode = stringMode;
this.suggestionsProvider = suggestionsProvider;
}
@Nonnull
@ -228,6 +252,12 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
return ComponentParseResult.success(sj.toString());
}
@Nonnull
@Override
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext, @Nonnull final String input) {
return this.suggestionsProvider.apply(commandContext, input);
}
}