Skip to content

Commit

Permalink
add recipe book to furniture workbench
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMCLoveMan committed Apr 1, 2024
1 parent b035f2c commit 76ef575
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.recipebook.RecipeBookComponent;
import net.minecraft.client.gui.screens.recipebook.RecipeUpdateListener;
import net.minecraft.client.renderer.GameRenderer;
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.Slot;
import net.themcbrothers.interiormod.container.FurnitureWorkbenchMenu;

public class FurnitureWorkbenchScreen extends AbstractContainerScreen<FurnitureWorkbenchMenu> {
public class FurnitureWorkbenchScreen extends AbstractContainerScreen<FurnitureWorkbenchMenu> implements RecipeUpdateListener {
private static final ResourceLocation CRAFTING_TABLE_GUI_TEXTURES = new ResourceLocation("textures/gui/container/crafting_table.png");
private static final ResourceLocation RECIPE_BUTTON_LOCATION = new ResourceLocation("textures/gui/recipe_button.png");

private final RecipeBookComponent recipeBookComponent = new RecipeBookComponent();
private boolean widthTooNarrow;

public FurnitureWorkbenchScreen(FurnitureWorkbenchMenu screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn);
Expand All @@ -20,13 +29,34 @@ public FurnitureWorkbenchScreen(FurnitureWorkbenchMenu screenContainer, Inventor
protected void init() {
super.init();
this.titleLabelX = 29;
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, 0, 0, 19, RECIPE_BUTTON_LOCATION, (p_98484_) -> {
this.recipeBookComponent.toggleVisibility();
this.leftPos = this.recipeBookComponent.updateScreenPosition(this.width, this.imageWidth);
((ImageButton) p_98484_).setPosition(this.leftPos + 5, this.height / 2 - 49);
}));
this.addWidget(this.recipeBookComponent);
this.setInitialFocus(this.recipeBookComponent);
}

@Override
public void render(PoseStack matrixStack, int mouseX, int mouseY, float partialTicks) {
this.renderBackground(matrixStack);
super.render(matrixStack, mouseX, mouseY, partialTicks);

if (this.recipeBookComponent.isVisible() && this.widthTooNarrow) {
this.renderBg(matrixStack, partialTicks, mouseX, mouseY);
this.recipeBookComponent.render(matrixStack, mouseX, mouseY, partialTicks);
} else {
this.recipeBookComponent.render(matrixStack, mouseX, mouseY, partialTicks);
super.render(matrixStack, mouseX, mouseY, partialTicks);
this.recipeBookComponent.renderGhostRecipe(matrixStack, this.leftPos, this.topPos, true, partialTicks);
}

this.renderTooltip(matrixStack, mouseX, mouseY);
this.recipeBookComponent.renderTooltip(matrixStack, this.leftPos, this.topPos, mouseX, mouseY);
}

@Override
Expand All @@ -38,4 +68,53 @@ protected void renderBg(PoseStack matrixStack, float partialTicks, int x, int y)
int j = (this.height - this.imageHeight) / 2;
this.blit(matrixStack, i, j, 0, 0, this.imageWidth, this.imageHeight);
}

@Override
protected void containerTick() {
super.containerTick();
this.recipeBookComponent.tick();
}

@Override
protected boolean isHovering(int pX, int pY, int pWidth, int pHeight, double pMouseX, double pMouseY) {
return (!this.widthTooNarrow || !this.recipeBookComponent.isVisible()) && super.isHovering(pX, pY, pWidth, pHeight, pMouseX, pMouseY);
}

@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() || super.mouseClicked(mouseX, mouseY, button);
}
}

@Override
protected boolean hasClickedOutside(double mouseX, double mouseY, int guiLeft, int guiTop, int mouseButton) {
boolean flag = mouseX < (double) guiLeft || mouseY < (double) guiTop || mouseX >= (double) (guiLeft + this.imageWidth) || mouseY >= (double) (guiTop + this.imageHeight);
return this.recipeBookComponent.hasClickedOutside(mouseX, mouseY, this.leftPos, this.topPos, this.imageWidth, this.imageHeight, mouseButton) && flag;
}

@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 void removed() {
this.recipeBookComponent.removed();
super.removed();
}

@Override
public RecipeBookComponent getRecipeBookComponent() {
return this.recipeBookComponent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.world.inventory.*;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.Level;
import net.themcbrothers.interiormod.container.slot.FurnitureResultSlot;
import net.themcbrothers.interiormod.init.InteriorBlocks;
import net.themcbrothers.interiormod.init.InteriorContainers;
import net.themcbrothers.interiormod.init.InteriorRecipeBookExtensions;
import net.themcbrothers.interiormod.init.InteriorRecipeTypes;

import java.util.Optional;

/**
* @author TheMCBrothers
*/
public class FurnitureWorkbenchMenu extends AbstractContainerMenu {
public class FurnitureWorkbenchMenu extends RecipeBookMenu<CraftingContainer> {

private final CraftingContainer craftSlots = new CraftingContainer(this, 3, 3);
private final ResultContainer resultSlots = new ResultContainer();
Expand Down Expand Up @@ -145,4 +148,50 @@ public ItemStack quickMoveStack(Player player, int index) {
public boolean canTakeItemForPickAll(ItemStack stack, Slot slot) {
return slot.container != this.resultSlots && super.canTakeItemForPickAll(stack, slot);
}

@Override
public void fillCraftSlotsStackedContents(StackedContents stackedContents) {
this.craftSlots.fillStackedContents(stackedContents);
}

@Override
public void clearCraftingContent() {
this.craftSlots.clearContent();
this.resultSlots.clearContent();
}

@Override
public boolean recipeMatches(Recipe<? super CraftingContainer> recipe) {
return recipe.matches(this.craftSlots, this.player.level);
}

@Override
public int getResultSlotIndex() {
return 0;
}

@Override
public int getGridWidth() {
return this.craftSlots.getWidth();
}

@Override
public int getGridHeight() {
return this.craftSlots.getHeight();
}

@Override
public int getSize() {
return 10;
}

@Override
public RecipeBookType getRecipeBookType() {
return InteriorRecipeBookExtensions.FURNITURE_WORKBENCH_TYPE;
}

@Override
public boolean shouldMoveToInventory(int index) {
return index != this.getResultSlotIndex();
}
}

0 comments on commit 76ef575

Please sign in to comment.