From 804e4f9ca54334b6b5213d29a7582a2239a50a42 Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sat, 3 Dec 2022 08:38:48 -0300 Subject: [PATCH] List support --- .../builder/ListArgumentBuilder.java | 26 +++++++++++++ .../builder/RequiredArgumentBuilder.java | 2 +- .../brigadier/tree/ListCommandNode.java | 37 +++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/mojang/brigadier/builder/ListArgumentBuilder.java create mode 100644 src/main/java/com/mojang/brigadier/tree/ListCommandNode.java diff --git a/src/main/java/com/mojang/brigadier/builder/ListArgumentBuilder.java b/src/main/java/com/mojang/brigadier/builder/ListArgumentBuilder.java new file mode 100644 index 00000000..55fd6026 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/builder/ListArgumentBuilder.java @@ -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 extends RequiredArgumentBuilder { + protected ListArgumentBuilder(String name, ArgumentType type) { + super(name, type); + } + + public static ListArgumentBuilder list(final String name, final ArgumentType type) { + return new ListArgumentBuilder<>(name, type); + } + + public ArgumentCommandNode build() { + final ListCommandNode result = new ListCommandNode<>(getName(), getType(), getCommand(), getRequirement(), getRedirect(), getRedirectModifier(), isFork(), getSuggestionsProvider()); + + for (final CommandNode argument : getArguments()) { + result.addChild(argument); + } + + return result; + } +} diff --git a/src/main/java/com/mojang/brigadier/builder/RequiredArgumentBuilder.java b/src/main/java/com/mojang/brigadier/builder/RequiredArgumentBuilder.java index a09b9e15..20f282c1 100644 --- a/src/main/java/com/mojang/brigadier/builder/RequiredArgumentBuilder.java +++ b/src/main/java/com/mojang/brigadier/builder/RequiredArgumentBuilder.java @@ -13,7 +13,7 @@ public class RequiredArgumentBuilder extends ArgumentBuilder type; private SuggestionProvider suggestionsProvider = null; - private RequiredArgumentBuilder(final String name, final ArgumentType type) { + protected RequiredArgumentBuilder(final String name, final ArgumentType type) { this.name = name; this.type = type; } diff --git a/src/main/java/com/mojang/brigadier/tree/ListCommandNode.java b/src/main/java/com/mojang/brigadier/tree/ListCommandNode.java new file mode 100644 index 00000000..c4de55c4 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/tree/ListCommandNode.java @@ -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 extends ArgumentCommandNode { + public ListCommandNode(String name, ArgumentType type, Command command, Predicate requirement, CommandNode redirect, RedirectModifier modifier, boolean forks, SuggestionProvider customSuggestions) { + super(name, type, command, requirement, redirect, modifier, forks, customSuggestions); + } + + @Override + public void parse(StringReader reader, CommandContextBuilder contextBuilder) throws CommandSyntaxException { + final int start = reader.getCursor(); + final T result = getType().parse(reader); + + ParsedArgument> parsed = (ParsedArgument>) 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()); + } +}