List support
This commit is contained in:
parent
a6c4baa1b6
commit
804e4f9ca5
3 changed files with 64 additions and 1 deletions
|
@ -0,0 +1,26 @@
|
|||
package com.mojang.brigadier.builder;
|
||||
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.tree.ArgumentCommandNode;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
import com.mojang.brigadier.tree.ListCommandNode;
|
||||
|
||||
public class ListArgumentBuilder<S, T> extends RequiredArgumentBuilder<S, T> {
|
||||
protected ListArgumentBuilder(String name, ArgumentType<T> type) {
|
||||
super(name, type);
|
||||
}
|
||||
|
||||
public static <S, T> ListArgumentBuilder<S, T> list(final String name, final ArgumentType<T> type) {
|
||||
return new ListArgumentBuilder<>(name, type);
|
||||
}
|
||||
|
||||
public ArgumentCommandNode<S, T> build() {
|
||||
final ListCommandNode<S, T> result = new ListCommandNode<>(getName(), getType(), getCommand(), getRequirement(), getRedirect(), getRedirectModifier(), isFork(), getSuggestionsProvider());
|
||||
|
||||
for (final CommandNode<S> argument : getArguments()) {
|
||||
result.addChild(argument);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -13,7 +13,7 @@ public class RequiredArgumentBuilder<S, T> extends ArgumentBuilder<S, RequiredAr
|
|||
private final ArgumentType<T> type;
|
||||
private SuggestionProvider<S> suggestionsProvider = null;
|
||||
|
||||
private RequiredArgumentBuilder(final String name, final ArgumentType<T> type) {
|
||||
protected RequiredArgumentBuilder(final String name, final ArgumentType<T> type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
|
37
src/main/java/com/mojang/brigadier/tree/ListCommandNode.java
Normal file
37
src/main/java/com/mojang/brigadier/tree/ListCommandNode.java
Normal file
|
@ -0,0 +1,37 @@
|
|||
package com.mojang.brigadier.tree;
|
||||
|
||||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.RedirectModifier;
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||
import com.mojang.brigadier.context.ParsedArgument;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.suggestion.SuggestionProvider;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class ListCommandNode<S, T> extends ArgumentCommandNode<S, T> {
|
||||
public ListCommandNode(String name, ArgumentType<T> type, Command<S> command, Predicate<S> requirement, CommandNode<S> redirect, RedirectModifier<S> modifier, boolean forks, SuggestionProvider<S> customSuggestions) {
|
||||
super(name, type, command, requirement, redirect, modifier, forks, customSuggestions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandSyntaxException {
|
||||
final int start = reader.getCursor();
|
||||
final T result = getType().parse(reader);
|
||||
|
||||
ParsedArgument<S, List<T>> parsed = (ParsedArgument<S, List<T>>) contextBuilder.getArguments().get(getName());
|
||||
if (parsed == null) {
|
||||
parsed = new ParsedArgument<>(start, reader.getCursor(), new ArrayList<>());
|
||||
} else {
|
||||
parsed = new ParsedArgument<>(parsed.getRange().getStart(), reader.getCursor(), parsed.getResult());
|
||||
}
|
||||
parsed.getResult().add(result);
|
||||
|
||||
contextBuilder.withArgument(getName(), parsed);
|
||||
contextBuilder.withNode(this, parsed.getRange());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue