RecipeBookPage .java

net.minecraft.client.gui.screens.recipebook.RecipeBookPage (class): members: getLastClickedRecipeCollection, lastClickedRecipeCollection, ITEMS_PER_PAGE.

net.minecraft.client.gui.screens.recipebookclass221 linesclient8.3 KB

API Surface (9)

void init(final Minecraft minecraft, final int xo, final int yo)
void updateCollections(final List<RecipeCollection> recipeCollections, final boolean resetPage, final boolean isFiltering)
void extractRenderState(final GuiGraphicsExtractor graphics, final int xo, final int yo, final int mouseX, final int mouseY, final float a)
void extractTooltip(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY)
void setInvisible()
boolean mouseClicked(final MouseButtonEvent event, final int xo, final int yo, final int imageWidth, final int imageHeight, final boolean doubleClick)
void recipeShown(final RecipeDisplayId recipe)
ClientRecipeBook getRecipeBook()
void listButtons(final Consumer<AbstractWidget> buttonConsumer)

Fields & Constants (21)

ITEMS_PER_PAGE
PAGE_FORWARD_SPRITES
PAGE_BACKWARD_SPRITES
NEXT_PAGE_TEXT
PREVIOUS_PAGE_TEXT
TURN_PAGE_SPRITE_WIDTH
TURN_PAGE_SPRITE_HEIGHT
buttons
hoveredButton
overlay
minecraft
parent
recipeCollections
forwardButton
backButton
totalPages
currentPage
recipeBook
lastClickedRecipe
lastClickedRecipeCollection
isFiltering

String literals (7)

"recipe_book/page_forward"
"recipe_book/page_forward_highlighted"
"recipe_book/page_backward"
"recipe_book/page_backward_highlighted"
"gui.recipebook.next_page"
"gui.recipebook.previous_page"
"gui.recipebook.page"

Full Source

RecipeBookPage.java221 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.recipebook;
2
3
import com.google.common.collect.ImmutableList;
4
import com.google.common.collect.Lists;
5
import java.util.List;
6
import java.util.function.Consumer;
7
import net.fabricmc.api.EnvType;
8
import net.fabricmc.api.Environment;
9
import net.minecraft.client.ClientRecipeBook;
10
import net.minecraft.client.Minecraft;
11
import net.minecraft.client.gui.GuiGraphicsExtractor;
12
import net.minecraft.client.gui.components.AbstractWidget;
13
import net.minecraft.client.gui.components.ImageButton;
14
import net.minecraft.client.gui.components.Tooltip;
15
import net.minecraft.client.gui.components.WidgetSprites;
16
import net.minecraft.client.input.MouseButtonEvent;
17
import net.minecraft.core.component.DataComponents;
18
import net.minecraft.network.chat.Component;
19
import net.minecraft.resources.Identifier;
20
import net.minecraft.util.context.ContextMap;
21
import net.minecraft.world.item.ItemStack;
22
import net.minecraft.world.item.crafting.display.RecipeDisplayId;
23
import net.minecraft.world.item.crafting.display.SlotDisplayContext;
24
import org.jspecify.annotations.Nullable;
25
26
@Environment(EnvType.CLIENT)
27
public class RecipeBookPage {
28
	public static final int ITEMS_PER_PAGE = 20;
29
	private static final WidgetSprites PAGE_FORWARD_SPRITES = new WidgetSprites(
30
		Identifier.withDefaultNamespace("recipe_book/page_forward"), Identifier.withDefaultNamespace("recipe_book/page_forward_highlighted")
31
	);
32
	private static final WidgetSprites PAGE_BACKWARD_SPRITES = new WidgetSprites(
33
		Identifier.withDefaultNamespace("recipe_book/page_backward"), Identifier.withDefaultNamespace("recipe_book/page_backward_highlighted")
34
	);
35
	private static final Component NEXT_PAGE_TEXT = Component.translatable("gui.recipebook.next_page");
36
	private static final Component PREVIOUS_PAGE_TEXT = Component.translatable("gui.recipebook.previous_page");
37
	private static final int TURN_PAGE_SPRITE_WIDTH = 12;
38
	private static final int TURN_PAGE_SPRITE_HEIGHT = 17;
39
	private final List<RecipeButton> buttons = Lists.newArrayListWithCapacity(20);
40
	private @Nullable RecipeButton hoveredButton;
41
	private final OverlayRecipeComponent overlay;
42
	private Minecraft minecraft;
43
	private final RecipeBookComponent<?> parent;
44
	private List<RecipeCollection> recipeCollections = ImmutableList.of();
45
	private @Nullable ImageButton forwardButton;
46
	private @Nullable ImageButton backButton;
47
	private int totalPages;
48
	private int currentPage;
49
	private ClientRecipeBook recipeBook;
50
	private @Nullable RecipeDisplayId lastClickedRecipe;
51
	private @Nullable RecipeCollection lastClickedRecipeCollection;
52
	private boolean isFiltering;
53
54
	public RecipeBookPage(final RecipeBookComponent<?> parent, final SlotSelectTime slotSelectTime, final boolean isFurnaceMenu) {
55
		this.parent = parent;
56
		this.overlay = new OverlayRecipeComponent(slotSelectTime, isFurnaceMenu);
57
58
		for (int i = 0; i < 20; i++) {
59
			this.buttons.add(new RecipeButton(slotSelectTime));
60
		}
61
	}
62
63
	public void init(final Minecraft minecraft, final int xo, final int yo) {
64
		this.minecraft = minecraft;
65
		this.recipeBook = minecraft.player.getRecipeBook();
66
67
		for (int i = 0; i < this.buttons.size(); i++) {
68
			this.buttons.get(i).setPosition(xo + 11 + 25 * (i % 5), yo + 31 + 25 * (i / 5));
69
		}
70
71
		this.forwardButton = new ImageButton(xo + 93, yo + 137, 12, 17, PAGE_FORWARD_SPRITES, button -> this.updateArrowButtons(), NEXT_PAGE_TEXT);
72
		this.forwardButton.setTooltip(Tooltip.create(NEXT_PAGE_TEXT));
73
		this.backButton = new ImageButton(xo + 38, yo + 137, 12, 17, PAGE_BACKWARD_SPRITES, button -> this.updateArrowButtons(), PREVIOUS_PAGE_TEXT);
74
		this.backButton.setTooltip(Tooltip.create(PREVIOUS_PAGE_TEXT));
75
	}
76
77
	public void updateCollections(final List<RecipeCollection> recipeCollections, final boolean resetPage, final boolean isFiltering) {
78
		this.recipeCollections = recipeCollections;
79
		this.isFiltering = isFiltering;
80
		this.totalPages = (int)Math.ceil(recipeCollections.size() / 20.0);
81
		if (this.totalPages <= this.currentPage || resetPage) {
82
			this.currentPage = 0;
83
		}
84
85
		this.updateButtonsForPage();
86
	}
87
88
	private void updateButtonsForPage() {
89
		int startOffset = 20 * this.currentPage;
90
		ContextMap context = SlotDisplayContext.fromLevel(this.minecraft.level);
91
92
		for (int i = 0; i < this.buttons.size(); i++) {
93
			RecipeButton button = this.buttons.get(i);
94
			if (startOffset + i < this.recipeCollections.size()) {
95
				RecipeCollection recipeCollection = this.recipeCollections.get(startOffset + i);
96
				button.init(recipeCollection, this.isFiltering, this, context);
97
				button.visible = true;
98
			} else {
99
				button.visible = false;
100
			}
101
		}
102
103
		this.updateArrowButtons();
104
	}
105
106
	private void updateArrowButtons() {
107
		if (this.forwardButton != null) {
108
			this.forwardButton.visible = this.totalPages > 1 && this.currentPage < this.totalPages - 1;
109
		}
110
111
		if (this.backButton != null) {
112
			this.backButton.visible = this.totalPages > 1 && this.currentPage > 0;
113
		}
114
	}
115
116
	public void extractRenderState(final GuiGraphicsExtractor graphics, final int xo, final int yo, final int mouseX, final int mouseY, final float a) {
117
		if (this.totalPages > 1) {
118
			Component pageNumbers = Component.translatable("gui.recipebook.page", this.currentPage + 1, this.totalPages);
119
			int pWidth = this.minecraft.font.width(pageNumbers);
120
			graphics.text(this.minecraft.font, pageNumbers, xo - pWidth / 2 + 73, yo + 141, -1);
121
		}
122
123
		this.hoveredButton = null;
124
125
		for (RecipeButton recipeBookButton : this.buttons) {
126
			recipeBookButton.extractRenderState(graphics, mouseX, mouseY, a);
127
			if (recipeBookButton.visible && recipeBookButton.isHoveredOrFocused()) {
128
				this.hoveredButton = recipeBookButton;
129
			}
130
		}
131
132
		if (this.forwardButton != null) {
133
			this.forwardButton.extractRenderState(graphics, mouseX, mouseY, a);
134
		}
135
136
		if (this.backButton != null) {
137
			this.backButton.extractRenderState(graphics, mouseX, mouseY, a);
138
		}
139
140
		graphics.nextStratum();
141
		this.overlay.extractRenderState(graphics, mouseX, mouseY, a);
142
	}
143
144
	public void extractTooltip(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY) {
145
		if (this.minecraft.gui.screen() != null && this.hoveredButton != null && !this.overlay.isVisible()) {
146
			ItemStack displayStack = this.hoveredButton.getDisplayStack();
147
			Identifier tooltipStyle = displayStack.get(DataComponents.TOOLTIP_STYLE);
148
			graphics.setComponentTooltipForNextFrame(this.minecraft.font, this.hoveredButton.getTooltipText(displayStack), mouseX, mouseY, tooltipStyle);
149
		}
150
	}
151
152
	public @Nullable RecipeDisplayId getLastClickedRecipe() {
153
		return this.lastClickedRecipe;
154
	}
155
156
	public @Nullable RecipeCollection getLastClickedRecipeCollection() {
157
		return this.lastClickedRecipeCollection;
158
	}
159
160
	public void setInvisible() {
161
		this.overlay.setVisible(false);
162
	}
163
164
	public boolean mouseClicked(final MouseButtonEvent event, final int xo, final int yo, final int imageWidth, final int imageHeight, final boolean doubleClick) {
165
		this.lastClickedRecipe = null;
166
		this.lastClickedRecipeCollection = null;
167
		if (this.overlay.isVisible()) {
168
			if (this.overlay.mouseClicked(event, doubleClick)) {
169
				this.lastClickedRecipe = this.overlay.getLastRecipeClicked();
170
				this.lastClickedRecipeCollection = this.overlay.getRecipeCollection();
171
			} else {
172
				this.overlay.setVisible(false);
173
			}
174
175
			return true;
176
		} else {
177
			if (this.forwardButton.mouseClicked(event, doubleClick)) {
178
				this.currentPage++;
179
				this.updateButtonsForPage();
180
				return true;
181
			}
182
183
			if (this.backButton.mouseClicked(event, doubleClick)) {
184
				this.currentPage--;
185
				this.updateButtonsForPage();
186
				return true;
187
			}
188
189
			ContextMap context = SlotDisplayContext.fromLevel(this.minecraft.level);
190
191
			for (RecipeButton button : this.buttons) {
192
				if (button.mouseClicked(event, doubleClick)) {
193
					if (event.button() == 1) {
194
						this.lastClickedRecipe = button.getCurrentRecipe();
195
						this.lastClickedRecipeCollection = button.getCollection();
196
					} else if (event.button() == 3 && !this.overlay.isVisible() && !button.isOnlyOption()) {
197
						this.overlay
198
							.init(button.getCollection(), context, this.isFiltering, button.getX(), button.getY(), xo + imageWidth / 2, yo + 13 + imageHeight / 2, button.getWidth());
199
					}
200
201
					return true;
202
				}
203
			}
204
205
			return false;
206
		}
207
	}
208
209
	public void recipeShown(final RecipeDisplayId recipe) {
210
		this.parent.recipeShown(recipe);
211
	}
212
213
	public ClientRecipeBook getRecipeBook() {
214
		return this.recipeBook;
215
	}
216
217
	protected void listButtons(final Consumer<AbstractWidget> buttonConsumer) {
218
		this.buttons.forEach(buttonConsumer);
219
	}
220
}
221

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