RecipeButton .java

net.minecraft.client.gui.screens.recipebook.RecipeButton (class): members: allRecipesHaveSameResultDisplay, SLOT_MANY_UNCRAFTABLE_SPRITE, getCurrentRecipe.

net.minecraft.client.gui.screens.recipebookclass181 linesclientextends AbstractWidget6.9 KB

API Surface (11)

void init(final RecipeCollection collection, final boolean isFiltering, final RecipeBookPage page, final ContextMap resolutionContext)
RecipeCollection getCollection()
void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
boolean isOnlyOption()
RecipeDisplayId getCurrentRecipe()
ItemStack getDisplayStack()
List<Component> getTooltipText(final ItemStack displayStack)
void updateWidgetNarration(final NarrationElementOutput output)
int getWidth()
boolean isValidClickButton(final MouseButtonInfo buttonInfo)
ItemStack selectItem(final int index)

Fields & Constants (12)

SLOT_MANY_CRAFTABLE_SPRITE
SLOT_CRAFTABLE_SPRITE
SLOT_MANY_UNCRAFTABLE_SPRITE
SLOT_UNCRAFTABLE_SPRITE
ANIMATION_TIME
BACKGROUND_SIZE
MORE_RECIPES_TOOLTIP
collection
selectedEntries
allRecipesHaveSameResultDisplay
slotSelectTime
animationTime

String literals (8)

"recipe_book/slot_many_craftable"
"recipe_book/slot_craftable"
"recipe_book/slot_many_uncraftable"
"recipe_book/slot_uncraftable"
"gui.recipebook.moreRecipes"
"narration.recipe"
"narration.button.usage.hovered"
"narration.recipe.usage.more"

Full Source

RecipeButton.java181 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.recipebook;
2
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6
import net.fabricmc.api.EnvType;
7
import net.fabricmc.api.Environment;
8
import net.minecraft.client.Minecraft;
9
import net.minecraft.client.gui.GuiGraphicsExtractor;
10
import net.minecraft.client.gui.components.AbstractWidget;
11
import net.minecraft.client.gui.narration.NarratedElementType;
12
import net.minecraft.client.gui.narration.NarrationElementOutput;
13
import net.minecraft.client.gui.screens.Screen;
14
import net.minecraft.client.input.MouseButtonInfo;
15
import net.minecraft.client.renderer.RenderPipelines;
16
import net.minecraft.network.chat.CommonComponents;
17
import net.minecraft.network.chat.Component;
18
import net.minecraft.resources.Identifier;
19
import net.minecraft.util.context.ContextMap;
20
import net.minecraft.world.item.ItemStack;
21
import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
22
import net.minecraft.world.item.crafting.display.RecipeDisplayId;
23
24
@Environment(EnvType.CLIENT)
25
public class RecipeButton extends AbstractWidget {
26
	private static final Identifier SLOT_MANY_CRAFTABLE_SPRITE = Identifier.withDefaultNamespace("recipe_book/slot_many_craftable");
27
	private static final Identifier SLOT_CRAFTABLE_SPRITE = Identifier.withDefaultNamespace("recipe_book/slot_craftable");
28
	private static final Identifier SLOT_MANY_UNCRAFTABLE_SPRITE = Identifier.withDefaultNamespace("recipe_book/slot_many_uncraftable");
29
	private static final Identifier SLOT_UNCRAFTABLE_SPRITE = Identifier.withDefaultNamespace("recipe_book/slot_uncraftable");
30
	private static final float ANIMATION_TIME = 15.0F;
31
	private static final int BACKGROUND_SIZE = 25;
32
	private static final Component MORE_RECIPES_TOOLTIP = Component.translatable("gui.recipebook.moreRecipes");
33
	private RecipeCollection collection = RecipeCollection.EMPTY;
34
	private List<RecipeButton.ResolvedEntry> selectedEntries = List.of();
35
	private boolean allRecipesHaveSameResultDisplay;
36
	private final SlotSelectTime slotSelectTime;
37
	private float animationTime;
38
39
	public RecipeButton(final SlotSelectTime slotSelectTime) {
40
		super(0, 0, 25, 25, CommonComponents.EMPTY);
41
		this.slotSelectTime = slotSelectTime;
42
	}
43
44
	public void init(final RecipeCollection collection, final boolean isFiltering, final RecipeBookPage page, final ContextMap resolutionContext) {
45
		this.collection = collection;
46
		List<RecipeDisplayEntry> fittingRecipes = collection.getSelectedRecipes(
47
			isFiltering ? RecipeCollection.CraftableStatus.CRAFTABLE : RecipeCollection.CraftableStatus.ANY
48
		);
49
		this.selectedEntries = fittingRecipes.stream().map(entry -> new RecipeButton.ResolvedEntry(entry.id(), entry.resultItems(resolutionContext))).toList();
50
		this.allRecipesHaveSameResultDisplay = allRecipesHaveSameResultDisplay(this.selectedEntries);
51
		List<RecipeDisplayId> newlyShownRecipes = fittingRecipes.stream().map(RecipeDisplayEntry::id).filter(page.getRecipeBook()::willHighlight).toList();
52
		if (!newlyShownRecipes.isEmpty()) {
53
			newlyShownRecipes.forEach(page::recipeShown);
54
			this.animationTime = 15.0F;
55
		}
56
	}
57
58
	private static boolean allRecipesHaveSameResultDisplay(final List<RecipeButton.ResolvedEntry> entries) {
59
		Iterator<ItemStack> itemsIterator = entries.stream().flatMap(e -> e.displayItems().stream()).iterator();
60
		if (!itemsIterator.hasNext()) {
61
			return true;
62
		}
63
64
		ItemStack firstItem = itemsIterator.next();
65
66
		while (itemsIterator.hasNext()) {
67
			ItemStack nextItem = itemsIterator.next();
68
			if (!ItemStack.isSameItemSameComponents(firstItem, nextItem)) {
69
				return false;
70
			}
71
		}
72
73
		return true;
74
	}
75
76
	public RecipeCollection getCollection() {
77
		return this.collection;
78
	}
79
80
	@Override
81
	public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
82
		Identifier sprite;
83
		if (this.collection.hasCraftable()) {
84
			if (this.hasMultipleRecipes()) {
85
				sprite = SLOT_MANY_CRAFTABLE_SPRITE;
86
			} else {
87
				sprite = SLOT_CRAFTABLE_SPRITE;
88
			}
89
		} else if (this.hasMultipleRecipes()) {
90
			sprite = SLOT_MANY_UNCRAFTABLE_SPRITE;
91
		} else {
92
			sprite = SLOT_UNCRAFTABLE_SPRITE;
93
		}
94
95
		boolean shouldAnimate = this.animationTime > 0.0F;
96
		if (shouldAnimate) {
97
			float squeeze = 1.0F + 0.1F * (float)Math.sin(this.animationTime / 15.0F * (float) Math.PI);
98
			graphics.pose().pushMatrix();
99
			graphics.pose().translate(this.getX() + 8, this.getY() + 12);
100
			graphics.pose().scale(squeeze, squeeze);
101
			graphics.pose().translate(-(this.getX() + 8), -(this.getY() + 12));
102
			this.animationTime -= a;
103
		}
104
105
		graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, this.getX(), this.getY(), this.width, this.height);
106
		ItemStack currentItemStack = this.getDisplayStack();
107
		int offset = 4;
108
		if (this.hasMultipleRecipes() && this.allRecipesHaveSameResultDisplay) {
109
			graphics.item(currentItemStack, this.getX() + offset + 1, this.getY() + offset + 1, 0);
110
			offset--;
111
		}
112
113
		graphics.fakeItem(currentItemStack, this.getX() + offset, this.getY() + offset);
114
		if (shouldAnimate) {
115
			graphics.pose().popMatrix();
116
		}
117
	}
118
119
	private boolean hasMultipleRecipes() {
120
		return this.selectedEntries.size() > 1;
121
	}
122
123
	public boolean isOnlyOption() {
124
		return this.selectedEntries.size() == 1;
125
	}
126
127
	public RecipeDisplayId getCurrentRecipe() {
128
		int index = this.slotSelectTime.currentIndex() % this.selectedEntries.size();
129
		return this.selectedEntries.get(index).id;
130
	}
131
132
	public ItemStack getDisplayStack() {
133
		int currentIndex = this.slotSelectTime.currentIndex();
134
		int entryCount = this.selectedEntries.size();
135
		int offsetIndex = currentIndex / entryCount;
136
		int entryIndex = currentIndex - entryCount * offsetIndex;
137
		return this.selectedEntries.get(entryIndex).selectItem(offsetIndex);
138
	}
139
140
	public List<Component> getTooltipText(final ItemStack displayStack) {
141
		List<Component> texts = new ArrayList<>(Screen.getTooltipFromItem(Minecraft.getInstance(), displayStack));
142
		if (this.hasMultipleRecipes()) {
143
			texts.add(MORE_RECIPES_TOOLTIP);
144
		}
145
146
		return texts;
147
	}
148
149
	@Override
150
	public void updateWidgetNarration(final NarrationElementOutput output) {
151
		output.add(NarratedElementType.TITLE, Component.translatable("narration.recipe", this.getDisplayStack().getHoverName()));
152
		if (this.hasMultipleRecipes()) {
153
			output.add(NarratedElementType.USAGE, Component.translatable("narration.button.usage.hovered"), Component.translatable("narration.recipe.usage.more"));
154
		} else {
155
			output.add(NarratedElementType.USAGE, Component.translatable("narration.button.usage.hovered"));
156
		}
157
	}
158
159
	@Override
160
	public int getWidth() {
161
		return 25;
162
	}
163
164
	@Override
165
	protected boolean isValidClickButton(final MouseButtonInfo buttonInfo) {
166
		return buttonInfo.button() == 1 || buttonInfo.button() == 3;
167
	}
168
169
	@Environment(EnvType.CLIENT)
170
	private record ResolvedEntry(RecipeDisplayId id, List<ItemStack> displayItems) {
171
		public ItemStack selectItem(final int index) {
172
			if (this.displayItems.isEmpty()) {
173
				return ItemStack.EMPTY;
174
			}
175
176
			int offset = index % this.displayItems.size();
177
			return this.displayItems.get(offset);
178
		}
179
	}
180
}
181

Decompiled Minecraft 26.3 source of RecipeButton (net/minecraft/client/gui/screens/recipebook/RecipeButton.java) with Mojang official mappings, stored verbatim on the site.