| 1 | package net.minecraft.nbt;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import com.mojang.serialization.DynamicOps;
|
| 9 | import com.mojang.serialization.Lifecycle;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.util.parsing.packrat.commands.Grammar;
|
| 12 |
|
| 13 | public class TagParser<T> {
|
| 14 | public static final SimpleCommandExceptionType ERROR_TRAILING_DATA = new SimpleCommandExceptionType(Component.translatable("argument.nbt.trailing"));
|
| 15 | public static final SimpleCommandExceptionType ERROR_EXPECTED_COMPOUND = new SimpleCommandExceptionType(
|
| 16 | Component.translatable("argument.nbt.expected.compound")
|
| 17 | );
|
| 18 | public static final char ELEMENT_SEPARATOR = ',';
|
| 19 | public static final char NAME_VALUE_SEPARATOR = ':';
|
| 20 | private static final TagParser<Tag> NBT_OPS_PARSER = create(NbtOps.INSTANCE);
|
| 21 | public static final Codec<CompoundTag> FLATTENED_CODEC = Codec.STRING
|
| 22 | .comapFlatMap(
|
| 23 | s -> {
|
| 24 | try {
|
| 25 | Tag result = NBT_OPS_PARSER.parseFully(s);
|
| 26 | return result instanceof CompoundTag compoundTag
|
| 27 | ? DataResult.success(compoundTag, Lifecycle.stable())
|
| 28 | : DataResult.error(() -> "Expected compound tag, got " + result);
|
| 29 | } catch (CommandSyntaxException e) {
|
| 30 | return DataResult.error(e::getMessage);
|
| 31 | }
|
| 32 | },
|
| 33 | CompoundTag::toString
|
| 34 | );
|
| 35 | public static final Codec<CompoundTag> LENIENT_CODEC = Codec.withAlternative(FLATTENED_CODEC, CompoundTag.CODEC);
|
| 36 | private final DynamicOps<T> ops;
|
| 37 | private final Grammar<T> grammar;
|
| 38 |
|
| 39 | private TagParser(final DynamicOps<T> ops, final Grammar<T> grammar) {
|
| 40 | this.ops = ops;
|
| 41 | this.grammar = grammar;
|
| 42 | }
|
| 43 |
|
| 44 | public DynamicOps<T> getOps() {
|
| 45 | return this.ops;
|
| 46 | }
|
| 47 |
|
| 48 | public static <T> TagParser<T> create(final DynamicOps<T> ops) {
|
| 49 | return new TagParser<>(ops, SnbtGrammar.createParser(ops));
|
| 50 | }
|
| 51 |
|
| 52 | private static CompoundTag castToCompoundOrThrow(final StringReader reader, final Tag result) throws CommandSyntaxException {
|
| 53 | if (result instanceof CompoundTag compoundTag) {
|
| 54 | return compoundTag;
|
| 55 | } else {
|
| 56 | throw ERROR_EXPECTED_COMPOUND.createWithContext(reader);
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | public static CompoundTag parseCompoundFully(final String input) throws CommandSyntaxException {
|
| 61 | StringReader reader = new StringReader(input);
|
| 62 | return castToCompoundOrThrow(reader, NBT_OPS_PARSER.parseFully(reader));
|
| 63 | }
|
| 64 |
|
| 65 | public T parseFully(final String input) throws CommandSyntaxException {
|
| 66 | return this.parseFully(new StringReader(input));
|
| 67 | }
|
| 68 |
|
| 69 | public T parseFully(final StringReader reader) throws CommandSyntaxException {
|
| 70 | T result = this.grammar.parseForCommands(reader);
|
| 71 | reader.skipWhitespace();
|
| 72 | if (reader.canRead()) {
|
| 73 | throw ERROR_TRAILING_DATA.createWithContext(reader);
|
| 74 | } else {
|
| 75 | return result;
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | public T parseAsArgument(final StringReader reader) throws CommandSyntaxException {
|
| 80 | return this.grammar.parseForCommands(reader);
|
| 81 | }
|
| 82 |
|
| 83 | public static CompoundTag parseCompoundAsArgument(final StringReader reader) throws CommandSyntaxException {
|
| 84 | Tag result = NBT_OPS_PARSER.parseAsArgument(reader);
|
| 85 | return castToCompoundOrThrow(reader, result);
|
| 86 | }
|
| 87 | }
|
| 88 |
|