| 1 | package net.minecraft.commands.arguments.item;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 5 | import com.mojang.brigadier.context.CommandContext;
|
| 6 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 7 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 8 | import com.mojang.datafixers.util.Either;
|
| 9 | import com.mojang.datafixers.util.Pair;
|
| 10 | import java.util.Arrays;
|
| 11 | import java.util.Collection;
|
| 12 | import java.util.Collections;
|
| 13 | import net.minecraft.commands.CommandSourceStack;
|
| 14 | import net.minecraft.commands.functions.CommandFunction;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.resources.Identifier;
|
| 17 |
|
| 18 | public class FunctionArgument implements ArgumentType<FunctionArgument.Result> {
|
| 19 | private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "#foo");
|
| 20 | private static final DynamicCommandExceptionType ERROR_UNKNOWN_TAG = new DynamicCommandExceptionType(
|
| 21 | tag -> Component.translatableEscape("arguments.function.tag.unknown", tag)
|
| 22 | );
|
| 23 | private static final DynamicCommandExceptionType ERROR_UNKNOWN_FUNCTION = new DynamicCommandExceptionType(
|
| 24 | value -> Component.translatableEscape("arguments.function.unknown", value)
|
| 25 | );
|
| 26 |
|
| 27 | public static FunctionArgument functions() {
|
| 28 | return new FunctionArgument();
|
| 29 | }
|
| 30 |
|
| 31 | public FunctionArgument.Result parse(final StringReader reader) throws CommandSyntaxException {
|
| 32 | if (reader.canRead() && reader.peek() == '#') {
|
| 33 | reader.skip();
|
| 34 | final Identifier id = Identifier.read(reader);
|
| 35 | return new FunctionArgument.Result() {
|
| 36 | @Override
|
| 37 | public Collection<CommandFunction<CommandSourceStack>> create(final CommandContext<CommandSourceStack> c) throws CommandSyntaxException {
|
| 38 | return FunctionArgument.getFunctionTag(c, id);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public Pair<Identifier, Either<CommandFunction<CommandSourceStack>, Collection<CommandFunction<CommandSourceStack>>>> unwrap(
|
| 43 | final CommandContext<CommandSourceStack> context
|
| 44 | ) throws CommandSyntaxException {
|
| 45 | return Pair.of(id, Either.right(FunctionArgument.getFunctionTag(context, id)));
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public Pair<Identifier, Collection<CommandFunction<CommandSourceStack>>> unwrapToCollection(final CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
| 50 | return Pair.of(id, FunctionArgument.getFunctionTag(context, id));
|
| 51 | }
|
| 52 | };
|
| 53 | } else {
|
| 54 | final Identifier id = Identifier.read(reader);
|
| 55 | return new FunctionArgument.Result() {
|
| 56 | @Override
|
| 57 | public Collection<CommandFunction<CommandSourceStack>> create(final CommandContext<CommandSourceStack> c) throws CommandSyntaxException {
|
| 58 | return Collections.singleton(FunctionArgument.getFunction(c, id));
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public Pair<Identifier, Either<CommandFunction<CommandSourceStack>, Collection<CommandFunction<CommandSourceStack>>>> unwrap(
|
| 63 | final CommandContext<CommandSourceStack> context
|
| 64 | ) throws CommandSyntaxException {
|
| 65 | return Pair.of(id, Either.left(FunctionArgument.getFunction(context, id)));
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public Pair<Identifier, Collection<CommandFunction<CommandSourceStack>>> unwrapToCollection(final CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
| 70 | return Pair.of(id, Collections.singleton(FunctionArgument.getFunction(context, id)));
|
| 71 | }
|
| 72 | };
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | private static CommandFunction<CommandSourceStack> getFunction(final CommandContext<CommandSourceStack> c, final Identifier id) throws CommandSyntaxException {
|
| 77 | return c.getSource().getServer().getFunctions().get(id).orElseThrow(() -> ERROR_UNKNOWN_FUNCTION.create(id.toString()));
|
| 78 | }
|
| 79 |
|
| 80 | private static Collection<CommandFunction<CommandSourceStack>> getFunctionTag(final CommandContext<CommandSourceStack> c, final Identifier id) throws CommandSyntaxException {
|
| 81 | Collection<CommandFunction<CommandSourceStack>> tag = c.getSource().getServer().getFunctions().getTag(id);
|
| 82 | if (tag == null) {
|
| 83 | throw ERROR_UNKNOWN_TAG.create(id.toString());
|
| 84 | } else {
|
| 85 | return tag;
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | public static Collection<CommandFunction<CommandSourceStack>> getFunctions(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 90 | return context.getArgument(name, FunctionArgument.Result.class).create(context);
|
| 91 | }
|
| 92 |
|
| 93 | public static Pair<Identifier, Either<CommandFunction<CommandSourceStack>, Collection<CommandFunction<CommandSourceStack>>>> getFunctionOrTag(
|
| 94 | final CommandContext<CommandSourceStack> context, final String name
|
| 95 | ) throws CommandSyntaxException {
|
| 96 | return context.getArgument(name, FunctionArgument.Result.class).unwrap(context);
|
| 97 | }
|
| 98 |
|
| 99 | public static Pair<Identifier, Collection<CommandFunction<CommandSourceStack>>> getFunctionCollection(
|
| 100 | final CommandContext<CommandSourceStack> context, final String name
|
| 101 | ) throws CommandSyntaxException {
|
| 102 | return context.getArgument(name, FunctionArgument.Result.class).unwrapToCollection(context);
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public Collection<String> getExamples() {
|
| 107 | return EXAMPLES;
|
| 108 | }
|
| 109 |
|
| 110 | public interface Result {
|
| 111 | Collection<CommandFunction<CommandSourceStack>> create(CommandContext<CommandSourceStack> context) throws CommandSyntaxException;
|
| 112 |
|
| 113 | Pair<Identifier, Either<CommandFunction<CommandSourceStack>, Collection<CommandFunction<CommandSourceStack>>>> unwrap(
|
| 114 | CommandContext<CommandSourceStack> context
|
| 115 | ) throws CommandSyntaxException;
|
| 116 |
|
| 117 | Pair<Identifier, Collection<CommandFunction<CommandSourceStack>>> unwrapToCollection(CommandContext<CommandSourceStack> context) throws CommandSyntaxException;
|
| 118 | }
|
| 119 | }
|
| 120 |
|