| 1 | package net.minecraft.commands.arguments;
|
| 2 |
|
| 3 | import com.mojang.brigadier.context.CommandContext;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 6 | import com.mojang.serialization.DynamicOps;
|
| 7 | import java.util.Arrays;
|
| 8 | import java.util.Collection;
|
| 9 | import net.minecraft.commands.CommandBuildContext;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.core.HolderLookup;
|
| 12 | import net.minecraft.nbt.NbtOps;
|
| 13 | import net.minecraft.nbt.SnbtGrammar;
|
| 14 | import net.minecraft.nbt.Tag;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.chat.ComponentSerialization;
|
| 17 | import net.minecraft.network.chat.ComponentUtils;
|
| 18 | import net.minecraft.network.chat.ResolutionContext;
|
| 19 | import net.minecraft.util.parsing.packrat.commands.CommandArgumentParser;
|
| 20 | import net.minecraft.util.parsing.packrat.commands.ParserBasedArgument;
|
| 21 | import net.minecraft.world.entity.Entity;
|
| 22 |
|
| 23 | public class ComponentArgument extends ParserBasedArgument<Component> {
|
| 24 | private static final Collection<String> EXAMPLES = Arrays.asList("\"hello world\"", "'hello world'", "\"\"", "{text:\"hello world\"}", "[\"\"]");
|
| 25 | public static final DynamicCommandExceptionType ERROR_INVALID_COMPONENT = new DynamicCommandExceptionType(
|
| 26 | message -> Component.translatableEscape("argument.component.invalid", message)
|
| 27 | );
|
| 28 | private static final DynamicOps<Tag> OPS = NbtOps.INSTANCE;
|
| 29 | private static final CommandArgumentParser<Tag> TAG_PARSER = SnbtGrammar.createParser(OPS);
|
| 30 |
|
| 31 | private ComponentArgument(final HolderLookup.Provider registries) {
|
| 32 | super(TAG_PARSER.withCodec(registries.createSerializationContext(OPS), TAG_PARSER, ComponentSerialization.CODEC, ERROR_INVALID_COMPONENT));
|
| 33 | }
|
| 34 |
|
| 35 | public static Component getRawComponent(final CommandContext<CommandSourceStack> context, final String name) {
|
| 36 | return context.getArgument(name, Component.class);
|
| 37 | }
|
| 38 |
|
| 39 | public static Component getResolvedComponent(final CommandContext<CommandSourceStack> context, final String name, final Entity contentEntity) throws CommandSyntaxException {
|
| 40 | return ComponentUtils.resolve(
|
| 41 | ResolutionContext.builder().withSource(context.getSource()).withEntityOverride(contentEntity).build(), getRawComponent(context, name)
|
| 42 | );
|
| 43 | }
|
| 44 |
|
| 45 | public static Component getResolvedComponent(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 46 | return ComponentUtils.resolve(ResolutionContext.create(context.getSource()), getRawComponent(context, name));
|
| 47 | }
|
| 48 |
|
| 49 | public static ComponentArgument textComponent(final CommandBuildContext context) {
|
| 50 | return new ComponentArgument(context);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public Collection<String> getExamples() {
|
| 55 | return EXAMPLES;
|
| 56 | }
|
| 57 | }
|
| 58 |
|