package net.minecraft.client.gui.screens.inventory; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.ImageButton; import net.minecraft.client.gui.navigation.ScreenPosition; import net.minecraft.client.gui.screens.recipebook.RecipeBookComponent; import net.minecraft.client.gui.screens.recipebook.RecipeUpdateListener; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.inventory.ClickType; import net.minecraft.world.inventory.RecipeBookMenu; import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.crafting.display.RecipeDisplay; @Environment(EnvType.CLIENT) public abstract class AbstractRecipeBookScreen extends AbstractContainerScreen implements RecipeUpdateListener { private final RecipeBookComponent recipeBookComponent; private boolean widthTooNarrow; public AbstractRecipeBookScreen(T menu, RecipeBookComponent recipeBookComponent, Inventory playerInventory, Component title) { super(menu, playerInventory, title); this.recipeBookComponent = recipeBookComponent; } @Override protected void init() { super.init(); this.widthTooNarrow = this.width < 379; this.recipeBookComponent.init(this.width, this.height, this.minecraft, this.widthTooNarrow); this.leftPos = this.recipeBookComponent.updateScreenPosition(this.width, this.imageWidth); this.initButton(); } protected abstract ScreenPosition getRecipeBookButtonPosition(); private void initButton() { ScreenPosition screenPosition = this.getRecipeBookButtonPosition(); this.addRenderableWidget(new ImageButton(screenPosition.x(), screenPosition.y(), 20, 18, RecipeBookComponent.RECIPE_BUTTON_SPRITES, button -> { this.recipeBookComponent.toggleVisibility(); this.leftPos = this.recipeBookComponent.updateScreenPosition(this.width, this.imageWidth); ScreenPosition screenPositionx = this.getRecipeBookButtonPosition(); button.setPosition(screenPositionx.x(), screenPositionx.y()); this.onRecipeBookButtonClick(); })); this.addWidget(this.recipeBookComponent); } protected void onRecipeBookButtonClick() { } @Override public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) { if (this.recipeBookComponent.isVisible() && this.widthTooNarrow) { this.renderBackground(guiGraphics, mouseX, mouseY, partialTick); } else { super.render(guiGraphics, mouseX, mouseY, partialTick); } this.recipeBookComponent.render(guiGraphics, mouseX, mouseY, partialTick); this.renderTooltip(guiGraphics, mouseX, mouseY); this.recipeBookComponent.renderTooltip(guiGraphics, mouseX, mouseY, this.hoveredSlot); } @Override protected void renderSlots(GuiGraphics guiGraphics) { super.renderSlots(guiGraphics); this.recipeBookComponent.renderGhostRecipe(guiGraphics, this.isBiggerResultSlot()); } protected boolean isBiggerResultSlot() { return true; } @Override public boolean charTyped(char codePoint, int modifiers) { return this.recipeBookComponent.charTyped(codePoint, modifiers) ? true : super.charTyped(codePoint, modifiers); } @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { return this.recipeBookComponent.keyPressed(keyCode, scanCode, modifiers) ? true : super.keyPressed(keyCode, scanCode, modifiers); } @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { if (this.recipeBookComponent.mouseClicked(mouseX, mouseY, button)) { this.setFocused(this.recipeBookComponent); return true; } else { return this.widthTooNarrow && this.recipeBookComponent.isVisible() ? true : super.mouseClicked(mouseX, mouseY, button); } } @Override protected boolean isHovering(int x, int y, int width, int height, double mouseX, double mouseY) { return (!this.widthTooNarrow || !this.recipeBookComponent.isVisible()) && super.isHovering(x, y, width, height, mouseX, mouseY); } @Override protected boolean hasClickedOutside(double mouseX, double mouseY, int guiLeft, int guiTop, int mouseButton) { boolean bl = mouseX < guiLeft || mouseY < guiTop || mouseX >= guiLeft + this.imageWidth || mouseY >= guiTop + this.imageHeight; return this.recipeBookComponent.hasClickedOutside(mouseX, mouseY, this.leftPos, this.topPos, this.imageWidth, this.imageHeight, mouseButton) && bl; } @Override protected void slotClicked(Slot slot, int slotId, int mouseButton, ClickType type) { super.slotClicked(slot, slotId, mouseButton, type); this.recipeBookComponent.slotClicked(slot); } @Override public void containerTick() { super.containerTick(); this.recipeBookComponent.tick(); } @Override public void recipesUpdated() { this.recipeBookComponent.recipesUpdated(); } @Override public void fillGhostRecipe(RecipeDisplay recipeDisplay) { this.recipeBookComponent.fillGhostRecipe(recipeDisplay); } }