DyedItemColor .java

net.minecraft.world.item.component.DyedItemColor (record): members: rgb, LEATHER_COLOR, applyDyes, getOrDefault, addToTooltip, STREAM_CODEC, CODEC.

net.minecraft.world.item.componentrecord90 linescommon3.5 KB

Implements

API Surface (4)

int getOrDefault(final ItemStack itemStack, final int defaultColor)
ItemStack applyDyes(final ItemStack itemStack, final List<DyeColor> dyes)
DyedItemColor applyDyes(final @Nullable DyedItemColor currentDye, final List<DyeColor> dyes)
void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components)

Fields & Constants (3)

CODEC
STREAM_CODEC
LEATHER_COLOR

String literals (2)

"item.color"
"item.dyed"

Full Source

DyedItemColor.java90 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.component;
2
3
import com.mojang.serialization.Codec;
4
import io.netty.buffer.ByteBuf;
5
import java.util.List;
6
import java.util.Locale;
7
import java.util.function.Consumer;
8
import net.minecraft.ChatFormatting;
9
import net.minecraft.core.component.DataComponentGetter;
10
import net.minecraft.core.component.DataComponents;
11
import net.minecraft.network.chat.Component;
12
import net.minecraft.network.codec.ByteBufCodecs;
13
import net.minecraft.network.codec.StreamCodec;
14
import net.minecraft.util.ARGB;
15
import net.minecraft.util.ExtraCodecs;
16
import net.minecraft.world.item.DyeColor;
17
import net.minecraft.world.item.Item;
18
import net.minecraft.world.item.ItemStack;
19
import net.minecraft.world.item.TooltipFlag;
20
import org.jspecify.annotations.Nullable;
21
22
public record DyedItemColor(int rgb) implements TooltipProvider {
23
	public static final Codec<DyedItemColor> CODEC = ExtraCodecs.RGB_COLOR_CODEC.xmap(DyedItemColor::new, DyedItemColor::rgb);
24
	public static final StreamCodec<ByteBuf, DyedItemColor> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.INT, DyedItemColor::rgb, DyedItemColor::new);
25
	public static final int LEATHER_COLOR = -6265536;
26
27
	public static int getOrDefault(final ItemStack itemStack, final int defaultColor) {
28
		DyedItemColor color = itemStack.get(DataComponents.DYED_COLOR);
29
		return color != null ? ARGB.opaque(color.rgb()) : defaultColor;
30
	}
31
32
	public static ItemStack applyDyes(final ItemStack itemStack, final List<DyeColor> dyes) {
33
		DyedItemColor currentDye = itemStack.get(DataComponents.DYED_COLOR);
34
		DyedItemColor newDyedColor = applyDyes(currentDye, dyes);
35
		ItemStack result = itemStack.copyWithCount(1);
36
		result.set(DataComponents.DYED_COLOR, newDyedColor);
37
		return result;
38
	}
39
40
	public static DyedItemColor applyDyes(final @Nullable DyedItemColor currentDye, final List<DyeColor> dyes) {
41
		int redTotal = 0;
42
		int greenTotal = 0;
43
		int blueTotal = 0;
44
		int intensityTotal = 0;
45
		int colorCount = 0;
46
		if (currentDye != null) {
47
			int red = ARGB.red(currentDye.rgb());
48
			int green = ARGB.green(currentDye.rgb());
49
			int blue = ARGB.blue(currentDye.rgb());
50
			intensityTotal += Math.max(red, Math.max(green, blue));
51
			redTotal += red;
52
			greenTotal += green;
53
			blueTotal += blue;
54
			colorCount++;
55
		}
56
57
		for (DyeColor dye : dyes) {
58
			int color = dye.getTextureDiffuseColor();
59
			int red = ARGB.red(color);
60
			int green = ARGB.green(color);
61
			int blue = ARGB.blue(color);
62
			intensityTotal += Math.max(red, Math.max(green, blue));
63
			redTotal += red;
64
			greenTotal += green;
65
			blueTotal += blue;
66
			colorCount++;
67
		}
68
69
		int red = redTotal / colorCount;
70
		int green = greenTotal / colorCount;
71
		int blue = blueTotal / colorCount;
72
		float averageIntensity = (float)intensityTotal / colorCount;
73
		float resultIntensity = Math.max(red, Math.max(green, blue));
74
		red = (int)(red * averageIntensity / resultIntensity);
75
		green = (int)(green * averageIntensity / resultIntensity);
76
		blue = (int)(blue * averageIntensity / resultIntensity);
77
		int rgb = ARGB.color(0, red, green, blue);
78
		return new DyedItemColor(rgb);
79
	}
80
81
	@Override
82
	public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
83
		if (flag.isAdvanced()) {
84
			consumer.accept(Component.translatable("item.color", String.format(Locale.ROOT, "#%06X", this.rgb)).withStyle(ChatFormatting.GRAY));
85
		} else {
86
			consumer.accept(Component.translatable("item.dyed").withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC));
87
		}
88
	}
89
}
90

Decompiled Minecraft 26.3 source of DyedItemColor (net/minecraft/world/item/component/DyedItemColor.java) with Mojang official mappings, stored verbatim on the site.