NbtContents .java

net.minecraft.network.chat.contents.NbtContents (record): members: nbtPath, interpreting, plain, separator, dataSource, NBT_PATH_CODEC, resolve, MAP_CODEC.

net.minecraft.network.chat.contentsrecord106 linescommon4.6 KB

Implements

API Surface (4)

NbtPathArgument.NbtPath parse(final StringReader reader)
String errorMessage(final String original, final CommandSyntaxException exception)
MutableComponent resolve(final ResolutionContext context, final int recursionDepth)
MapCodec<NbtContents> codec()

Fields & Constants (3)

LOGGER
NBT_PATH_CODEC
MAP_CODEC

String literals (5)

"Invalid NBT path: "
"interpret"
"plain"
"separator"
"'interpret' and 'plain' flags can't be both on"

Full Source

NbtContents.java106 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network.chat.contents;
2
3
import com.mojang.brigadier.StringReader;
4
import com.mojang.brigadier.exceptions.CommandSyntaxException;
5
import com.mojang.datafixers.DataFixUtils;
6
import com.mojang.logging.LogUtils;
7
import com.mojang.serialization.Codec;
8
import com.mojang.serialization.DataResult;
9
import com.mojang.serialization.MapCodec;
10
import com.mojang.serialization.codecs.RecordCodecBuilder;
11
import java.util.Optional;
12
import java.util.stream.Stream;
13
import net.minecraft.commands.CommandSourceStack;
14
import net.minecraft.commands.arguments.NbtPathArgument;
15
import net.minecraft.nbt.NbtOps;
16
import net.minecraft.nbt.Tag;
17
import net.minecraft.nbt.TextComponentTagVisitor;
18
import net.minecraft.network.chat.Component;
19
import net.minecraft.network.chat.ComponentContents;
20
import net.minecraft.network.chat.ComponentSerialization;
21
import net.minecraft.network.chat.ComponentUtils;
22
import net.minecraft.network.chat.MutableComponent;
23
import net.minecraft.network.chat.ResolutionContext;
24
import net.minecraft.network.chat.contents.data.DataSource;
25
import net.minecraft.network.chat.contents.data.DataSources;
26
import net.minecraft.resources.RegistryOps;
27
import net.minecraft.util.CompilableString;
28
import org.slf4j.Logger;
29
30
public record NbtContents(
31
	CompilableString<NbtPathArgument.NbtPath> nbtPath, boolean interpreting, boolean plain, Optional<Component> separator, DataSource dataSource
32
) implements ComponentContents {
33
	private static final Logger LOGGER = LogUtils.getLogger();
34
	public static final Codec<CompilableString<NbtPathArgument.NbtPath>> NBT_PATH_CODEC = CompilableString.codec(
35
		new CompilableString.CommandParserHelper<NbtPathArgument.NbtPath>() {
36
			protected NbtPathArgument.NbtPath parse(final StringReader reader) throws CommandSyntaxException {
37
				return new NbtPathArgument().parse(reader);
38
			}
39
40
			@Override
41
			protected String errorMessage(final String original, final CommandSyntaxException exception) {
42
				return "Invalid NBT path: " + original + ": " + exception.getMessage();
43
			}
44
		}
45
	);
46
	public static final MapCodec<NbtContents> MAP_CODEC = RecordCodecBuilder.<NbtContents>mapCodec(
47
			i -> i.group(
48
					NBT_PATH_CODEC.fieldOf("nbt").forGetter(NbtContents::nbtPath),
49
					Codec.BOOL.lenientOptionalFieldOf("interpret", false).forGetter(NbtContents::interpreting),
50
					Codec.BOOL.lenientOptionalFieldOf("plain", false).forGetter(NbtContents::plain),
51
					ComponentSerialization.CODEC.lenientOptionalFieldOf("separator").forGetter(NbtContents::separator),
52
					DataSources.CODEC.forGetter(NbtContents::dataSource)
53
				)
54
				.apply(i, NbtContents::new)
55
		)
56
		.validate(v -> v.interpreting && v.plain ? DataResult.error(() -> "'interpret' and 'plain' flags can't be both on") : DataResult.success(v));
57
58
	@Override
59
	public MutableComponent resolve(final ResolutionContext context, final int recursionDepth) throws CommandSyntaxException {
60
		CommandSourceStack source = context.source();
61
		if (source == null) {
62
			return Component.empty();
63
		} else if (context.resolvedComponentCount().intValue() > context.resolvedComponentLimit()) {
64
			return Component.empty();
65
		} else {
66
			context.resolvedComponentCount().increment();
67
			Stream<Tag> elements = this.dataSource.getData(source).flatMap(t -> {
68
				try {
69
					return this.nbtPath.compiled().get(t).stream();
70
				} catch (CommandSyntaxException ignored) {
71
					return Stream.empty();
72
				}
73
			});
74
			Component resolvedSeparator = DataFixUtils.orElse(ComponentUtils.resolve(context, this.separator, recursionDepth), ComponentUtils.DEFAULT_NO_STYLE_SEPARATOR);
75
			if (this.interpreting) {
76
				RegistryOps<Tag> registryOps = source.registryAccess().createSerializationContext(NbtOps.INSTANCE);
77
				return elements.flatMap(tag -> {
78
					try {
79
						Component component = ComponentSerialization.CODEC.parse(registryOps, tag).getOrThrow();
80
						return Stream.of(ComponentUtils.resolve(context, component, recursionDepth));
81
					} catch (Exception e) {
82
						LOGGER.warn("Failed to parse component: {}", tag, e);
83
						return Stream.of();
84
					}
85
				}).reduce((left, right) -> left.append(resolvedSeparator).append(right)).orElseGet(Component::empty);
86
			} else {
87
				return elements.map(
88
						tag -> {
89
							TextComponentTagVisitor visitor = new TextComponentTagVisitor(
90
								"", this.plain ? TextComponentTagVisitor.PlainStyling.INSTANCE : TextComponentTagVisitor.RichStyling.INSTANCE
91
							);
92
							return (MutableComponent)visitor.visit(tag);
93
						}
94
					)
95
					.reduce((left, right) -> left.append(resolvedSeparator).append(right))
96
					.orElseGet(Component::empty);
97
			}
98
		}
99
	}
100
101
	@Override
102
	public MapCodec<NbtContents> codec() {
103
		return MAP_CODEC;
104
	}
105
}
106

Decompiled Minecraft 26.3 source of NbtContents (net/minecraft/network/chat/contents/NbtContents.java) with Mojang official mappings, stored verbatim on the site.