| 1 | package net.minecraft.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.ParseResults;
|
| 4 | import com.mojang.brigadier.context.CommandContextBuilder;
|
| 5 | import com.mojang.brigadier.context.ParsedArgument;
|
| 6 | import com.mojang.brigadier.context.ParsedCommandNode;
|
| 7 | import com.mojang.brigadier.tree.ArgumentCommandNode;
|
| 8 | import java.util.Map;
|
| 9 | import org.jspecify.annotations.Nullable;
|
| 10 |
|
| 11 | public class ArgumentVisitor {
|
| 12 | public static <S> void visitArguments(final ParseResults<S> command, final ArgumentVisitor.Output<S> output, final boolean rejectRootRedirects) {
|
| 13 | CommandContextBuilder<S> rootContext = command.getContext();
|
| 14 | CommandContextBuilder<S> context = rootContext;
|
| 15 | visitNodeArguments(context, output);
|
| 16 |
|
| 17 | CommandContextBuilder<S> child;
|
| 18 | while ((child = context.getChild()) != null && (!rejectRootRedirects || child.getRootNode() != rootContext.getRootNode())) {
|
| 19 | visitNodeArguments(child, output);
|
| 20 | context = child;
|
| 21 | }
|
| 22 | }
|
| 23 |
|
| 24 | private static <S> void visitNodeArguments(final CommandContextBuilder<S> context, final ArgumentVisitor.Output<S> output) {
|
| 25 | Map<String, ParsedArgument<S, ?>> values = context.getArguments();
|
| 26 |
|
| 27 | for (ParsedCommandNode<S> node : context.getNodes()) {
|
| 28 | if (node.getNode() instanceof ArgumentCommandNode<S, ?> argument) {
|
| 29 | ParsedArgument<S, ?> value = values.get(argument.getName());
|
| 30 | callVisitor(context, output, argument, value);
|
| 31 | }
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | private static <S, T> void callVisitor(
|
| 36 | final CommandContextBuilder<S> context,
|
| 37 | final ArgumentVisitor.Output<S> output,
|
| 38 | final ArgumentCommandNode<S, T> argument,
|
| 39 | final @Nullable ParsedArgument<S, ?> value
|
| 40 | ) {
|
| 41 | output.accept(context, argument, (ParsedArgument<S, T>)value);
|
| 42 | }
|
| 43 |
|
| 44 | @FunctionalInterface
|
| 45 | public interface Output<S> {
|
| 46 | <T> void accept(CommandContextBuilder<S> context, ArgumentCommandNode<S, T> argument, final @Nullable ParsedArgument<S, T> value);
|
| 47 | }
|
| 48 | }
|
| 49 |
|