minecraft-src/net/minecraft/client/gui/screens/inventory/CraftingScreen.java
2025-07-04 01:41:11 +03:00

115 lines
4.7 KiB
Java

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.screens.recipebook.RecipeBookComponent;
import net.minecraft.client.gui.screens.recipebook.RecipeUpdateListener;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.inventory.CraftingMenu;
import net.minecraft.world.inventory.Slot;
@Environment(EnvType.CLIENT)
public class CraftingScreen extends AbstractContainerScreen<CraftingMenu> implements RecipeUpdateListener {
private static final ResourceLocation CRAFTING_TABLE_LOCATION = ResourceLocation.withDefaultNamespace("textures/gui/container/crafting_table.png");
private final RecipeBookComponent recipeBookComponent = new RecipeBookComponent();
private boolean widthTooNarrow;
public CraftingScreen(CraftingMenu menu, Inventory playerInventory, Component title) {
super(menu, playerInventory, title);
}
@Override
protected void init() {
super.init();
this.widthTooNarrow = this.width < 379;
this.recipeBookComponent.init(this.width, this.height, this.minecraft, this.widthTooNarrow, this.menu);
this.leftPos = this.recipeBookComponent.updateScreenPosition(this.width, this.imageWidth);
this.addRenderableWidget(new ImageButton(this.leftPos + 5, this.height / 2 - 49, 20, 18, RecipeBookComponent.RECIPE_BUTTON_SPRITES, button -> {
this.recipeBookComponent.toggleVisibility();
this.leftPos = this.recipeBookComponent.updateScreenPosition(this.width, this.imageWidth);
button.setPosition(this.leftPos + 5, this.height / 2 - 49);
}));
this.addWidget(this.recipeBookComponent);
this.titleLabelX = 29;
}
@Override
public void containerTick() {
super.containerTick();
this.recipeBookComponent.tick();
}
@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
if (this.recipeBookComponent.isVisible() && this.widthTooNarrow) {
this.renderBackground(guiGraphics, mouseX, mouseY, partialTick);
this.recipeBookComponent.render(guiGraphics, mouseX, mouseY, partialTick);
} else {
super.render(guiGraphics, mouseX, mouseY, partialTick);
this.recipeBookComponent.render(guiGraphics, mouseX, mouseY, partialTick);
this.recipeBookComponent.renderGhostRecipe(guiGraphics, this.leftPos, this.topPos, true, partialTick);
}
this.renderTooltip(guiGraphics, mouseX, mouseY);
this.recipeBookComponent.renderTooltip(guiGraphics, this.leftPos, this.topPos, mouseX, mouseY);
}
@Override
protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX, int mouseY) {
int i = this.leftPos;
int j = (this.height - this.imageHeight) / 2;
guiGraphics.blit(CRAFTING_TABLE_LOCATION, i, j, 0, 0, this.imageWidth, this.imageHeight);
}
@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 charTyped(char codePoint, int modifiers) {
return this.recipeBookComponent.charTyped(codePoint, modifiers) ? true : super.charTyped(codePoint, modifiers);
}
@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
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 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 recipesUpdated() {
this.recipeBookComponent.recipesUpdated();
}
@Override
public RecipeBookComponent getRecipeBookComponent() {
return this.recipeBookComponent;
}
}