Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support list/repeating arguments #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/mojang/brigadier/tree/ListCommandNode.java
Original file line number Diff line number Diff line change
@@ -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());
}
}