| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableList.Builder;
|
| 5 | import com.mojang.brigadier.builder.ArgumentBuilder;
|
| 6 | import com.mojang.brigadier.context.CommandContext;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import java.util.List;
|
| 9 | import java.util.function.Function;
|
| 10 | import java.util.function.Supplier;
|
| 11 | import net.minecraft.commands.CommandSourceStack;
|
| 12 | import net.minecraft.commands.Commands;
|
| 13 |
|
| 14 | public interface ArgProvider<T> {
|
| 15 | T access(CommandContext<CommandSourceStack> context) throws CommandSyntaxException;
|
| 16 |
|
| 17 | ArgumentBuilder<CommandSourceStack, ?> wrap(
|
| 18 | ArgumentBuilder<CommandSourceStack, ?> parent, Function<ArgumentBuilder<CommandSourceStack, ?>, ArgumentBuilder<CommandSourceStack, ?>> function
|
| 19 | );
|
| 20 |
|
| 21 | static <T> ArgProvider<T> create(
|
| 22 | final String key, final Supplier<ArgumentBuilder<CommandSourceStack, ?>> child, final InCommandFunction<CommandContext<CommandSourceStack>, T> access
|
| 23 | ) {
|
| 24 | return new ArgProvider<T>() {
|
| 25 | @Override
|
| 26 | public T access(final CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
| 27 | return access.apply(context);
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public ArgumentBuilder<CommandSourceStack, ?> wrap(
|
| 32 | final ArgumentBuilder<CommandSourceStack, ?> parent,
|
| 33 | final Function<ArgumentBuilder<CommandSourceStack, ?>, ArgumentBuilder<CommandSourceStack, ?>> function
|
| 34 | ) {
|
| 35 | return parent.then(Commands.literal(key).then(function.apply(child.get())));
|
| 36 | }
|
| 37 | };
|
| 38 | }
|
| 39 |
|
| 40 | static <T> List<ArgProvider<T>> buildList(final String argName, final List<ArgProvider.Factory<T>> factories) {
|
| 41 | Builder<ArgProvider<T>> result = ImmutableList.builderWithExpectedSize(factories.size());
|
| 42 |
|
| 43 | for (ArgProvider.Factory<T> factory : factories) {
|
| 44 | result.add(factory.create(argName));
|
| 45 | }
|
| 46 |
|
| 47 | return result.build();
|
| 48 | }
|
| 49 |
|
| 50 | @FunctionalInterface
|
| 51 | interface Factory<T> {
|
| 52 | ArgProvider<T> create(String arg);
|
| 53 | }
|
| 54 | }
|
| 55 |
|