| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 6 | import net.minecraft.client.gui.components.ImageButton;
|
| 7 | import net.minecraft.client.gui.navigation.ScreenPosition;
|
| 8 | import net.minecraft.client.gui.screens.recipebook.RecipeBookComponent;
|
| 9 | import net.minecraft.client.gui.screens.recipebook.RecipeUpdateListener;
|
| 10 | import net.minecraft.client.input.CharacterEvent;
|
| 11 | import net.minecraft.client.input.KeyEvent;
|
| 12 | import net.minecraft.client.input.MouseButtonEvent;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.world.entity.player.Inventory;
|
| 15 | import net.minecraft.world.inventory.ContainerInput;
|
| 16 | import net.minecraft.world.inventory.RecipeBookMenu;
|
| 17 | import net.minecraft.world.inventory.Slot;
|
| 18 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public abstract class AbstractRecipeBookScreen<T extends RecipeBookMenu> extends AbstractContainerScreen<T> implements RecipeUpdateListener {
|
| 22 | private final RecipeBookComponent<?> recipeBookComponent;
|
| 23 | private boolean widthTooNarrow;
|
| 24 |
|
| 25 | public AbstractRecipeBookScreen(final T menu, final RecipeBookComponent<?> recipeBookComponent, final Inventory inventory, final Component title) {
|
| 26 | super(menu, inventory, title);
|
| 27 | this.recipeBookComponent = recipeBookComponent;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | protected void init() {
|
| 32 | super.init();
|
| 33 | this.widthTooNarrow = this.width < 379;
|
| 34 | this.recipeBookComponent.init(this.width, this.height, this.minecraft, this.widthTooNarrow);
|
| 35 | this.leftPos = this.recipeBookComponent.updateScreenPosition(this.width, this.imageWidth);
|
| 36 | this.initButton();
|
| 37 | }
|
| 38 |
|
| 39 | protected abstract ScreenPosition getRecipeBookButtonPosition();
|
| 40 |
|
| 41 | private void initButton() {
|
| 42 | ScreenPosition buttonPos = this.getRecipeBookButtonPosition();
|
| 43 | this.addRenderableWidget(new ImageButton(buttonPos.x(), buttonPos.y(), 20, 18, RecipeBookComponent.RECIPE_BUTTON_SPRITES, button -> {
|
| 44 | this.recipeBookComponent.toggleVisibility();
|
| 45 | this.leftPos = this.recipeBookComponent.updateScreenPosition(this.width, this.imageWidth);
|
| 46 | ScreenPosition updatedButtonPos = this.getRecipeBookButtonPosition();
|
| 47 | button.setPosition(updatedButtonPos.x(), updatedButtonPos.y());
|
| 48 | this.onRecipeBookButtonClick();
|
| 49 | }));
|
| 50 | this.addWidget(this.recipeBookComponent);
|
| 51 | }
|
| 52 |
|
| 53 | protected void onRecipeBookButtonClick() {
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 58 | if (this.recipeBookComponent.isVisible() && this.widthTooNarrow) {
|
| 59 | this.extractBackground(graphics, mouseX, mouseY, a);
|
| 60 | } else {
|
| 61 | super.extractContents(graphics, mouseX, mouseY, a);
|
| 62 | }
|
| 63 |
|
| 64 | graphics.nextStratum();
|
| 65 | this.recipeBookComponent.extractRenderState(graphics, mouseX, mouseY, a);
|
| 66 | graphics.nextStratum();
|
| 67 | this.extractCarriedItem(graphics, mouseX, mouseY);
|
| 68 | this.extractTooltip(graphics, mouseX, mouseY);
|
| 69 | this.recipeBookComponent.extractTooltip(graphics, mouseX, mouseY, this.hoveredSlot);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | protected void extractSlots(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY) {
|
| 74 | super.extractSlots(graphics, mouseX, mouseY);
|
| 75 | this.recipeBookComponent.extractGhostRecipe(graphics, this.isBiggerResultSlot());
|
| 76 | }
|
| 77 |
|
| 78 | protected boolean isBiggerResultSlot() {
|
| 79 | return true;
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public boolean charTyped(final CharacterEvent event) {
|
| 84 | return this.recipeBookComponent.charTyped(event) ? true : super.charTyped(event);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public boolean keyPressed(final KeyEvent event) {
|
| 89 | return this.recipeBookComponent.keyPressed(event) ? true : super.keyPressed(event);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public boolean isInputCaptured() {
|
| 94 | return super.isInputCaptured() || this.recipeBookComponent.capturesInput();
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 99 | if (this.recipeBookComponent.mouseClicked(event, doubleClick)) {
|
| 100 | this.setFocused(this.recipeBookComponent);
|
| 101 | return true;
|
| 102 | } else {
|
| 103 | return this.widthTooNarrow && this.recipeBookComponent.isVisible() ? true : super.mouseClicked(event, doubleClick);
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public boolean mouseDragged(final MouseButtonEvent event, final double dx, final double dy) {
|
| 109 | return this.recipeBookComponent.mouseDragged(event, dx, dy) ? true : super.mouseDragged(event, dx, dy);
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected boolean isHovering(final int left, final int top, final int w, final int h, final double xm, final double ym) {
|
| 114 | return (!this.widthTooNarrow || !this.recipeBookComponent.isVisible()) && super.isHovering(left, top, w, h, xm, ym);
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected boolean hasClickedOutside(final double mx, final double my, final int xo, final int yo) {
|
| 119 | boolean clickedOutside = mx < xo || my < yo || mx >= xo + this.imageWidth || my >= yo + this.imageHeight;
|
| 120 | return this.recipeBookComponent.hasClickedOutside(mx, my, this.leftPos, this.topPos, this.imageWidth, this.imageHeight) && clickedOutside;
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | protected void slotClicked(final Slot slot, final int slotId, final int buttonNum, final ContainerInput containerInput) {
|
| 125 | super.slotClicked(slot, slotId, buttonNum, containerInput);
|
| 126 | this.recipeBookComponent.slotClicked(slot);
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public void containerTick() {
|
| 131 | super.containerTick();
|
| 132 | this.recipeBookComponent.tick();
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public void recipesUpdated() {
|
| 137 | this.recipeBookComponent.recipesUpdated();
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public void fillGhostRecipe(final RecipeDisplay display) {
|
| 142 | this.recipeBookComponent.fillGhostRecipe(display);
|
| 143 | }
|
| 144 | }
|
| 145 |
|