BannerPatternLayers .java

net.minecraft.world.level.block.entity.BannerPatternLayers (record): members: layers, removeLast, Layer, addToTooltip, EMPTY, Builder, STREAM_CODEC, CODEC.

net.minecraft.world.level.block.entityrecord98 linescommon4.0 KB

Implements

API Surface (9)

BannerPatternLayers removeLast()
void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components)
BannerPatternLayers.Builder addIfRegistered(final HolderGetter<BannerPattern> patternGetter, final ResourceKey<BannerPattern> patternKey, final DyeColor color)
BannerPatternLayers.Builder add(final Holder<BannerPattern> pattern, final DyeColor color)
BannerPatternLayers.Builder add(final BannerPatternLayers.Layer layer)
BannerPatternLayers.Builder addAll(final BannerPatternLayers layers)
BannerPatternLayers build()
record Layer(Holder<BannerPattern> pattern, DyeColor color)
MutableComponent description()

Fields & Constants (4)

LOGGER
EMPTY
CODEC
STREAM_CODEC

String literals (2)

"pattern"
"color"

Full Source

BannerPatternLayers.java98 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.entity;
2
3
import com.google.common.collect.ImmutableList;
4
import com.mojang.logging.LogUtils;
5
import com.mojang.serialization.Codec;
6
import com.mojang.serialization.codecs.RecordCodecBuilder;
7
import java.util.List;
8
import java.util.Optional;
9
import java.util.function.Consumer;
10
import net.minecraft.ChatFormatting;
11
import net.minecraft.core.Holder;
12
import net.minecraft.core.HolderGetter;
13
import net.minecraft.core.component.DataComponentGetter;
14
import net.minecraft.network.RegistryFriendlyByteBuf;
15
import net.minecraft.network.chat.Component;
16
import net.minecraft.network.chat.MutableComponent;
17
import net.minecraft.network.codec.ByteBufCodecs;
18
import net.minecraft.network.codec.StreamCodec;
19
import net.minecraft.resources.ResourceKey;
20
import net.minecraft.world.item.DyeColor;
21
import net.minecraft.world.item.Item;
22
import net.minecraft.world.item.TooltipFlag;
23
import net.minecraft.world.item.component.TooltipProvider;
24
import org.slf4j.Logger;
25
26
public record BannerPatternLayers(List<BannerPatternLayers.Layer> layers) implements TooltipProvider {
27
	private static final Logger LOGGER = LogUtils.getLogger();
28
	public static final BannerPatternLayers EMPTY = new BannerPatternLayers(List.of());
29
	public static final Codec<BannerPatternLayers> CODEC = BannerPatternLayers.Layer.CODEC.listOf().xmap(BannerPatternLayers::new, BannerPatternLayers::layers);
30
	public static final StreamCodec<RegistryFriendlyByteBuf, BannerPatternLayers> STREAM_CODEC = BannerPatternLayers.Layer.STREAM_CODEC
31
		.apply(ByteBufCodecs.list())
32
		.map(BannerPatternLayers::new, BannerPatternLayers::layers);
33
34
	public BannerPatternLayers removeLast() {
35
		return new BannerPatternLayers(List.copyOf(this.layers.subList(0, this.layers.size() - 1)));
36
	}
37
38
	@Override
39
	public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
40
		for (int i = 0; i < Math.min(this.layers().size(), 6); i++) {
41
			consumer.accept(this.layers().get(i).description().withStyle(ChatFormatting.GRAY));
42
		}
43
	}
44
45
	public static class Builder {
46
		private final ImmutableList.Builder<BannerPatternLayers.Layer> layers = ImmutableList.builder();
47
48
		@Deprecated
49
		public BannerPatternLayers.Builder addIfRegistered(
50
			final HolderGetter<BannerPattern> patternGetter, final ResourceKey<BannerPattern> patternKey, final DyeColor color
51
		) {
52
			Optional<Holder.Reference<BannerPattern>> pattern = patternGetter.get(patternKey);
53
			if (pattern.isEmpty()) {
54
				BannerPatternLayers.LOGGER.warn("Unable to find banner pattern with id: '{}'", patternKey.identifier());
55
				return this;
56
			} else {
57
				return this.add(pattern.get(), color);
58
			}
59
		}
60
61
		public BannerPatternLayers.Builder add(final Holder<BannerPattern> pattern, final DyeColor color) {
62
			return this.add(new BannerPatternLayers.Layer(pattern, color));
63
		}
64
65
		public BannerPatternLayers.Builder add(final BannerPatternLayers.Layer layer) {
66
			this.layers.add(layer);
67
			return this;
68
		}
69
70
		public BannerPatternLayers.Builder addAll(final BannerPatternLayers layers) {
71
			this.layers.addAll(layers.layers);
72
			return this;
73
		}
74
75
		public BannerPatternLayers build() {
76
			return new BannerPatternLayers(this.layers.build());
77
		}
78
	}
79
80
	public record Layer(Holder<BannerPattern> pattern, DyeColor color) {
81
		public static final Codec<BannerPatternLayers.Layer> CODEC = RecordCodecBuilder.create(
82
			i -> i.group(
83
					BannerPattern.CODEC.fieldOf("pattern").forGetter(BannerPatternLayers.Layer::pattern),
84
					DyeColor.CODEC.fieldOf("color").forGetter(BannerPatternLayers.Layer::color)
85
				)
86
				.apply(i, BannerPatternLayers.Layer::new)
87
		);
88
		public static final StreamCodec<RegistryFriendlyByteBuf, BannerPatternLayers.Layer> STREAM_CODEC = StreamCodec.composite(
89
			BannerPattern.STREAM_CODEC, BannerPatternLayers.Layer::pattern, DyeColor.STREAM_CODEC, BannerPatternLayers.Layer::color, BannerPatternLayers.Layer::new
90
		);
91
92
		public MutableComponent description() {
93
			String prefix = this.pattern.value().translationKey();
94
			return Component.translatable(prefix + "." + this.color.getName());
95
		}
96
	}
97
}
98

Decompiled Minecraft 26.3 source of BannerPatternLayers (net/minecraft/world/level/block/entity/BannerPatternLayers.java) with Mojang official mappings, stored verbatim on the site.