2025-11-24 22:52:51 +03:00

196 lines
8.8 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.google.common.collect.ImmutableList
* com.google.common.collect.Lists
* org.jspecify.annotations.Nullable
*/
package net.minecraft.client.gui.screens.recipebook;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.function.Consumer;
import net.minecraft.client.ClientRecipeBook;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.StateSwitchingButton;
import net.minecraft.client.gui.components.WidgetSprites;
import net.minecraft.client.gui.screens.recipebook.OverlayRecipeComponent;
import net.minecraft.client.gui.screens.recipebook.RecipeBookComponent;
import net.minecraft.client.gui.screens.recipebook.RecipeButton;
import net.minecraft.client.gui.screens.recipebook.RecipeCollection;
import net.minecraft.client.gui.screens.recipebook.SlotSelectTime;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.Identifier;
import net.minecraft.util.context.ContextMap;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.display.RecipeDisplayId;
import net.minecraft.world.item.crafting.display.SlotDisplayContext;
import org.jspecify.annotations.Nullable;
public class RecipeBookPage {
public static final int ITEMS_PER_PAGE = 20;
private static final WidgetSprites PAGE_FORWARD_SPRITES = new WidgetSprites(Identifier.withDefaultNamespace("recipe_book/page_forward"), Identifier.withDefaultNamespace("recipe_book/page_forward_highlighted"));
private static final WidgetSprites PAGE_BACKWARD_SPRITES = new WidgetSprites(Identifier.withDefaultNamespace("recipe_book/page_backward"), Identifier.withDefaultNamespace("recipe_book/page_backward_highlighted"));
private final List<RecipeButton> buttons = Lists.newArrayListWithCapacity((int)20);
private @Nullable RecipeButton hoveredButton;
private final OverlayRecipeComponent overlay;
private Minecraft minecraft;
private final RecipeBookComponent<?> parent;
private List<RecipeCollection> recipeCollections = ImmutableList.of();
private StateSwitchingButton forwardButton;
private StateSwitchingButton backButton;
private int totalPages;
private int currentPage;
private ClientRecipeBook recipeBook;
private @Nullable RecipeDisplayId lastClickedRecipe;
private @Nullable RecipeCollection lastClickedRecipeCollection;
private boolean isFiltering;
public RecipeBookPage(RecipeBookComponent<?> parent, SlotSelectTime slotSelectTime, boolean isFurnaceMenu) {
this.parent = parent;
this.overlay = new OverlayRecipeComponent(slotSelectTime, isFurnaceMenu);
for (int i = 0; i < 20; ++i) {
this.buttons.add(new RecipeButton(slotSelectTime));
}
}
public void init(Minecraft minecraft, int xo, int yo) {
this.minecraft = minecraft;
this.recipeBook = minecraft.player.getRecipeBook();
for (int i = 0; i < this.buttons.size(); ++i) {
this.buttons.get(i).setPosition(xo + 11 + 25 * (i % 5), yo + 31 + 25 * (i / 5));
}
this.forwardButton = new StateSwitchingButton(xo + 93, yo + 137, 12, 17, false);
this.forwardButton.initTextureValues(PAGE_FORWARD_SPRITES);
this.backButton = new StateSwitchingButton(xo + 38, yo + 137, 12, 17, true);
this.backButton.initTextureValues(PAGE_BACKWARD_SPRITES);
}
public void updateCollections(List<RecipeCollection> recipeCollections, boolean resetPage, boolean isFiltering) {
this.recipeCollections = recipeCollections;
this.isFiltering = isFiltering;
this.totalPages = (int)Math.ceil((double)recipeCollections.size() / 20.0);
if (this.totalPages <= this.currentPage || resetPage) {
this.currentPage = 0;
}
this.updateButtonsForPage();
}
private void updateButtonsForPage() {
int startOffset = 20 * this.currentPage;
ContextMap context = SlotDisplayContext.fromLevel(this.minecraft.level);
for (int i = 0; i < this.buttons.size(); ++i) {
RecipeButton button = this.buttons.get(i);
if (startOffset + i < this.recipeCollections.size()) {
RecipeCollection recipeCollection = this.recipeCollections.get(startOffset + i);
button.init(recipeCollection, this.isFiltering, this, context);
button.visible = true;
continue;
}
button.visible = false;
}
this.updateArrowButtons();
}
private void updateArrowButtons() {
this.forwardButton.visible = this.totalPages > 1 && this.currentPage < this.totalPages - 1;
this.backButton.visible = this.totalPages > 1 && this.currentPage > 0;
}
public void render(GuiGraphics graphics, int xo, int yo, int mouseX, int mouseY, float a) {
if (this.totalPages > 1) {
MutableComponent pageMsg = Component.translatable("gui.recipebook.page", this.currentPage + 1, this.totalPages);
int pWidth = this.minecraft.font.width(pageMsg);
graphics.drawString(this.minecraft.font, pageMsg, xo - pWidth / 2 + 73, yo + 141, -1);
}
this.hoveredButton = null;
for (RecipeButton recipeBookButton : this.buttons) {
recipeBookButton.render(graphics, mouseX, mouseY, a);
if (!recipeBookButton.visible || !recipeBookButton.isHoveredOrFocused()) continue;
this.hoveredButton = recipeBookButton;
}
this.backButton.render(graphics, mouseX, mouseY, a);
this.forwardButton.render(graphics, mouseX, mouseY, a);
graphics.nextStratum();
this.overlay.render(graphics, mouseX, mouseY, a);
}
public void renderTooltip(GuiGraphics graphics, int mouseX, int mouseY) {
if (this.minecraft.screen != null && this.hoveredButton != null && !this.overlay.isVisible()) {
ItemStack displayStack = this.hoveredButton.getDisplayStack();
Identifier tooltipStyle = displayStack.get(DataComponents.TOOLTIP_STYLE);
graphics.setComponentTooltipForNextFrame(this.minecraft.font, this.hoveredButton.getTooltipText(displayStack), mouseX, mouseY, tooltipStyle);
}
}
public @Nullable RecipeDisplayId getLastClickedRecipe() {
return this.lastClickedRecipe;
}
public @Nullable RecipeCollection getLastClickedRecipeCollection() {
return this.lastClickedRecipeCollection;
}
public void setInvisible() {
this.overlay.setVisible(false);
}
public boolean mouseClicked(MouseButtonEvent event, int xo, int yo, int imageWidth, int imageHeight, boolean doubleClick) {
this.lastClickedRecipe = null;
this.lastClickedRecipeCollection = null;
if (this.overlay.isVisible()) {
if (this.overlay.mouseClicked(event, doubleClick)) {
this.lastClickedRecipe = this.overlay.getLastRecipeClicked();
this.lastClickedRecipeCollection = this.overlay.getRecipeCollection();
} else {
this.overlay.setVisible(false);
}
return true;
}
if (this.forwardButton.mouseClicked(event, doubleClick)) {
++this.currentPage;
this.updateButtonsForPage();
return true;
}
if (this.backButton.mouseClicked(event, doubleClick)) {
--this.currentPage;
this.updateButtonsForPage();
return true;
}
ContextMap context = SlotDisplayContext.fromLevel(this.minecraft.level);
for (RecipeButton button : this.buttons) {
if (!button.mouseClicked(event, doubleClick)) continue;
if (event.button() == 0) {
this.lastClickedRecipe = button.getCurrentRecipe();
this.lastClickedRecipeCollection = button.getCollection();
} else if (event.button() == 1 && !this.overlay.isVisible() && !button.isOnlyOption()) {
this.overlay.init(button.getCollection(), context, this.isFiltering, button.getX(), button.getY(), xo + imageWidth / 2, yo + 13 + imageHeight / 2, button.getWidth());
}
return true;
}
return false;
}
public void recipeShown(RecipeDisplayId recipe) {
this.parent.recipeShown(recipe);
}
public ClientRecipeBook getRecipeBook() {
return this.recipeBook;
}
protected void listButtons(Consumer<AbstractWidget> buttonConsumer) {
buttonConsumer.accept(this.forwardButton);
buttonConsumer.accept(this.backButton);
this.buttons.forEach(buttonConsumer);
}
}