ObjectContents .java

net.minecraft.network.chat.contents.ObjectContents (record, 51 lines): members: contents, fallback, PLACEHOLDER, visit, resolve, MAP_CODEC, codec.

net.minecraft.network.chat.contentsrecord51 linescommon2.2 KB

Implements

API Surface (4)

MapCodec<ObjectContents> codec()
MutableComponent resolve(final ResolutionContext context, final int recursionDepth)
<T> Optional<T> visit(final FormattedText.ContentConsumer<T> output)
<T> Optional<T> visit(final FormattedText.StyledContentConsumer<T> output, final Style currentStyle)

Fields & Constants (2)

PLACEHOLDER
MAP_CODEC

String literals (1)

"fallback"

Full Source

ObjectContents.java51 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network.chat.contents;
2
3
import com.mojang.brigadier.exceptions.CommandSyntaxException;
4
import com.mojang.serialization.MapCodec;
5
import com.mojang.serialization.codecs.RecordCodecBuilder;
6
import java.util.Optional;
7
import net.minecraft.network.chat.Component;
8
import net.minecraft.network.chat.ComponentContents;
9
import net.minecraft.network.chat.ComponentSerialization;
10
import net.minecraft.network.chat.ComponentUtils;
11
import net.minecraft.network.chat.FormattedText;
12
import net.minecraft.network.chat.MutableComponent;
13
import net.minecraft.network.chat.ResolutionContext;
14
import net.minecraft.network.chat.Style;
15
import net.minecraft.network.chat.contents.objects.ObjectInfo;
16
import net.minecraft.network.chat.contents.objects.ObjectInfos;
17
18
public record ObjectContents(ObjectInfo contents, Optional<Component> fallback) implements ComponentContents {
19
	private static final String PLACEHOLDER = Character.toString('');
20
	public static final MapCodec<ObjectContents> MAP_CODEC = RecordCodecBuilder.mapCodec(
21
		i -> i.group(
22
				ObjectInfos.CODEC.forGetter(ObjectContents::contents), ComponentSerialization.CODEC.optionalFieldOf("fallback").forGetter(ObjectContents::fallback)
23
			)
24
			.apply(i, ObjectContents::new)
25
	);
26
27
	@Override
28
	public MapCodec<ObjectContents> codec() {
29
		return MAP_CODEC;
30
	}
31
32
	@Override
33
	public MutableComponent resolve(final ResolutionContext context, final int recursionDepth) throws CommandSyntaxException {
34
		Optional<MutableComponent> fallback = ComponentUtils.resolve(context, this.fallback, recursionDepth);
35
		ObjectInfo validatedContents = context.validate(this.contents);
36
		return validatedContents == null
37
			? fallback.orElseGet(() -> Component.literal(this.contents.defaultFallback()))
38
			: MutableComponent.create(new ObjectContents(validatedContents, fallback.map(o -> (Component)o)));
39
	}
40
41
	@Override
42
	public <T> Optional<T> visit(final FormattedText.ContentConsumer<T> output) {
43
		return this.fallback.isPresent() ? this.fallback.get().visit(output) : output.accept(this.contents.defaultFallback());
44
	}
45
46
	@Override
47
	public <T> Optional<T> visit(final FormattedText.StyledContentConsumer<T> output, final Style currentStyle) {
48
		return output.accept(currentStyle.withFont(this.contents.fontDescription()), PLACEHOLDER);
49
	}
50
}
51

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