DyeRecipe .java

net.minecraft.world.item.crafting.DyeRecipe (class): members: dye, createPlacementInfo, bookInfo, commonInfo, assemble, SERIALIZER, getSerializer, result.

net.minecraft.world.item.craftingclass150 linescommonextends NormalCraftingRecipe4.5 KB

API Surface (5)

boolean matches(final CraftingInput input, final Level level)
ItemStack assemble(final CraftingInput input)
RecipeSerializer<DyeRecipe> getSerializer()
PlacementInfo createPlacementInfo()
List<RecipeDisplay> display()

Fields & Constants (8)

MAP_CODEC
STREAM_CODEC
SERIALIZER
target
dye
result
commonInfo
bookInfo

String literals (2)

"target"
"result"

Full Source

DyeRecipe.java150 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.crafting;
2
3
import com.mojang.serialization.MapCodec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import java.util.ArrayList;
6
import java.util.List;
7
import net.minecraft.core.component.DataComponents;
8
import net.minecraft.network.RegistryFriendlyByteBuf;
9
import net.minecraft.network.codec.StreamCodec;
10
import net.minecraft.world.item.DyeColor;
11
import net.minecraft.world.item.ItemStack;
12
import net.minecraft.world.item.ItemStackTemplate;
13
import net.minecraft.world.item.Items;
14
import net.minecraft.world.item.component.DyedItemColor;
15
import net.minecraft.world.item.crafting.display.RecipeDisplay;
16
import net.minecraft.world.item.crafting.display.ShapelessCraftingRecipeDisplay;
17
import net.minecraft.world.item.crafting.display.SlotDisplay;
18
import net.minecraft.world.level.Level;
19
20
public class DyeRecipe extends NormalCraftingRecipe {
21
	public static final MapCodec<DyeRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
22
		i -> i.group(
23
				Recipe.CommonInfo.MAP_CODEC.forGetter(o -> o.commonInfo),
24
				CraftingRecipe.CraftingBookInfo.MAP_CODEC.forGetter(o -> o.bookInfo),
25
				Ingredient.CODEC.fieldOf("target").forGetter(o -> o.target),
26
				Ingredient.CODEC.fieldOf("dye").forGetter(o -> o.dye),
27
				ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result)
28
			)
29
			.apply(i, DyeRecipe::new)
30
	);
31
	public static final StreamCodec<RegistryFriendlyByteBuf, DyeRecipe> STREAM_CODEC = StreamCodec.composite(
32
		Recipe.CommonInfo.STREAM_CODEC,
33
		o -> o.commonInfo,
34
		CraftingRecipe.CraftingBookInfo.STREAM_CODEC,
35
		o -> o.bookInfo,
36
		Ingredient.CONTENTS_STREAM_CODEC,
37
		o -> o.target,
38
		Ingredient.CONTENTS_STREAM_CODEC,
39
		o -> o.dye,
40
		ItemStackTemplate.STREAM_CODEC,
41
		o -> o.result,
42
		DyeRecipe::new
43
	);
44
	public static final RecipeSerializer<DyeRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
45
	private final Ingredient target;
46
	private final Ingredient dye;
47
	private final ItemStackTemplate result;
48
49
	public DyeRecipe(
50
		final Recipe.CommonInfo commonInfo,
51
		final CraftingRecipe.CraftingBookInfo bookInfo,
52
		final Ingredient target,
53
		final Ingredient dye,
54
		final ItemStackTemplate result
55
	) {
56
		super(commonInfo, bookInfo);
57
		this.target = target;
58
		this.dye = dye;
59
		this.result = result;
60
	}
61
62
	public boolean matches(final CraftingInput input, final Level level) {
63
		if (input.ingredientCount() < 2) {
64
			return false;
65
		}
66
67
		boolean hasTarget = false;
68
		boolean hasDyes = false;
69
70
		for (int slot = 0; slot < input.size(); slot++) {
71
			ItemStack itemStack = input.getItem(slot);
72
			if (!itemStack.isEmpty()) {
73
				if (this.target.test(itemStack)) {
74
					if (hasTarget) {
75
						return false;
76
					}
77
78
					hasTarget = true;
79
				} else {
80
					if (!this.dye.test(itemStack) || !itemStack.has(DataComponents.DYE)) {
81
						return false;
82
					}
83
84
					hasDyes = true;
85
				}
86
			}
87
		}
88
89
		return hasDyes && hasTarget;
90
	}
91
92
	public ItemStack assemble(final CraftingInput input) {
93
		List<DyeColor> dyes = new ArrayList<>();
94
		ItemStack targetStack = ItemStack.EMPTY;
95
96
		for (int slot = 0; slot < input.size(); slot++) {
97
			ItemStack itemStack = input.getItem(slot);
98
			if (!itemStack.isEmpty()) {
99
				if (this.target.test(itemStack)) {
100
					if (!targetStack.isEmpty()) {
101
						return ItemStack.EMPTY;
102
					}
103
104
					targetStack = itemStack;
105
				} else {
106
					if (!this.dye.test(itemStack)) {
107
						return ItemStack.EMPTY;
108
					}
109
110
					DyeColor dye = itemStack.getOrDefault(DataComponents.DYE, DyeColor.WHITE);
111
					dyes.add(dye);
112
				}
113
			}
114
		}
115
116
		if (!targetStack.isEmpty() && !dyes.isEmpty()) {
117
			DyedItemColor currentDye = targetStack.get(DataComponents.DYED_COLOR);
118
			DyedItemColor newDyedColor = DyedItemColor.applyDyes(currentDye, dyes);
119
			ItemStack result = TransmuteRecipe.createWithOriginalComponents(this.result, targetStack);
120
			result.set(DataComponents.DYED_COLOR, newDyedColor);
121
			return result;
122
		} else {
123
			return ItemStack.EMPTY;
124
		}
125
	}
126
127
	@Override
128
	public RecipeSerializer<DyeRecipe> getSerializer() {
129
		return SERIALIZER;
130
	}
131
132
	@Override
133
	protected PlacementInfo createPlacementInfo() {
134
		return PlacementInfo.create(List.of(this.target, this.dye));
135
	}
136
137
	@Override
138
	public List<RecipeDisplay> display() {
139
		SlotDisplay.OnlyWithComponent dyesWithDyeComponent = new SlotDisplay.OnlyWithComponent(this.dye.display(), DataComponents.DYE);
140
		SlotDisplay targetDisplay = this.target.display();
141
		return List.of(
142
			new ShapelessCraftingRecipeDisplay(
143
				List.of(targetDisplay, dyesWithDyeComponent),
144
				new SlotDisplay.DyedSlotDemo(dyesWithDyeComponent, targetDisplay),
145
				new SlotDisplay.ItemSlotDisplay(Items.CRAFTING_TABLE)
146
			)
147
		);
148
	}
149
}
150

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