package net.minecraft.commands.arguments.item; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; import com.mojang.datafixers.util.Either; import com.mojang.datafixers.util.Pair; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.functions.CommandFunction; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; public class FunctionArgument implements ArgumentType { private static final Collection EXAMPLES = Arrays.asList("foo", "foo:bar", "#foo"); private static final DynamicCommandExceptionType ERROR_UNKNOWN_TAG = new DynamicCommandExceptionType( object -> Component.translatableEscape("arguments.function.tag.unknown", object) ); private static final DynamicCommandExceptionType ERROR_UNKNOWN_FUNCTION = new DynamicCommandExceptionType( object -> Component.translatableEscape("arguments.function.unknown", object) ); public static FunctionArgument functions() { return new FunctionArgument(); } public FunctionArgument.Result parse(StringReader reader) throws CommandSyntaxException { if (reader.canRead() && reader.peek() == '#') { reader.skip(); final ResourceLocation resourceLocation = ResourceLocation.read(reader); return new FunctionArgument.Result() { @Override public Collection> create(CommandContext context) throws CommandSyntaxException { return FunctionArgument.getFunctionTag(context, resourceLocation); } @Override public Pair, Collection>>> unwrap( CommandContext context ) throws CommandSyntaxException { return Pair.of(resourceLocation, Either.right(FunctionArgument.getFunctionTag(context, resourceLocation))); } @Override public Pair>> unwrapToCollection(CommandContext context) throws CommandSyntaxException { return Pair.of(resourceLocation, FunctionArgument.getFunctionTag(context, resourceLocation)); } }; } else { final ResourceLocation resourceLocation = ResourceLocation.read(reader); return new FunctionArgument.Result() { @Override public Collection> create(CommandContext context) throws CommandSyntaxException { return Collections.singleton(FunctionArgument.getFunction(context, resourceLocation)); } @Override public Pair, Collection>>> unwrap( CommandContext context ) throws CommandSyntaxException { return Pair.of(resourceLocation, Either.left(FunctionArgument.getFunction(context, resourceLocation))); } @Override public Pair>> unwrapToCollection(CommandContext context) throws CommandSyntaxException { return Pair.of(resourceLocation, Collections.singleton(FunctionArgument.getFunction(context, resourceLocation))); } }; } } static CommandFunction getFunction(CommandContext context, ResourceLocation id) throws CommandSyntaxException { return (CommandFunction)context.getSource() .getServer() .getFunctions() .get(id) .orElseThrow(() -> ERROR_UNKNOWN_FUNCTION.create(id.toString())); } static Collection> getFunctionTag(CommandContext context, ResourceLocation id) throws CommandSyntaxException { Collection> collection = context.getSource().getServer().getFunctions().getTag(id); if (collection == null) { throw ERROR_UNKNOWN_TAG.create(id.toString()); } else { return collection; } } public static Collection> getFunctions(CommandContext context, String name) throws CommandSyntaxException { return context.getArgument(name, FunctionArgument.Result.class).create(context); } public static Pair, Collection>>> getFunctionOrTag( CommandContext context, String name ) throws CommandSyntaxException { return context.getArgument(name, FunctionArgument.Result.class).unwrap(context); } public static Pair>> getFunctionCollection( CommandContext context, String name ) throws CommandSyntaxException { return context.getArgument(name, FunctionArgument.Result.class).unwrapToCollection(context); } @Override public Collection getExamples() { return EXAMPLES; } public interface Result { Collection> create(CommandContext context) throws CommandSyntaxException; Pair, Collection>>> unwrap( CommandContext context ) throws CommandSyntaxException; Pair>> unwrapToCollection(CommandContext context) throws CommandSyntaxException; } }