List support

This commit is contained in:
SoniEx2 2022-12-03 08:38:48 -03:00
parent a6c4baa1b6
commit 804e4f9ca5
3 changed files with 64 additions and 1 deletions

View file

@ -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;
}
}

View file

@ -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;
}

View 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());
}
}