Skip to content

Commit

Permalink
feat: Add support for Vanila Recipe Book #56
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Oct 29, 2024
1 parent b8b4fbb commit f9af830
Show file tree
Hide file tree
Showing 15 changed files with 243 additions and 113 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.blay09.mods.craftingslots;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.craftingslots.container.ModMenus;
import net.blay09.mods.craftingslots.menu.ModMenus;
import net.blay09.mods.craftingslots.item.ModItems;
import net.blay09.mods.craftingslots.network.ModNetworking;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.blay09.mods.craftingslots.addon;

import net.blay09.mods.craftingslots.CraftingSlots;
import net.blay09.mods.craftingslots.container.InventoryCraftingMenu;
import net.blay09.mods.craftingslots.container.PortableCraftingMenu;
import net.blay09.mods.craftingslots.menu.InventoryCraftingMenu;
import net.blay09.mods.craftingslots.menu.PortableCraftingMenu;
import net.blay09.mods.craftingtweaks.api.*;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.blay09.mods.balm.api.client.screen.BalmScreens;
import net.blay09.mods.craftingslots.client.screen.InventoryCraftingScreen;
import net.blay09.mods.craftingslots.client.screen.PortableCraftingScreen;
import net.blay09.mods.craftingslots.container.ModMenus;
import net.blay09.mods.craftingslots.menu.ModMenus;

public class ModScreens {
public static void initialize(BalmScreens screens) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package net.blay09.mods.craftingslots.client.screen;

import net.blay09.mods.craftingslots.CraftingSlots;
import net.blay09.mods.craftingslots.container.InventoryCraftingMenu;
import net.blay09.mods.craftingslots.menu.InventoryCraftingMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.navigation.ScreenPosition;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.inventory.AbstractRecipeBookScreen;
import net.minecraft.client.gui.screens.recipebook.CraftingRecipeBookComponent;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

public class InventoryCraftingScreen extends AbstractContainerScreen<InventoryCraftingMenu> {
public class InventoryCraftingScreen extends AbstractRecipeBookScreen<InventoryCraftingMenu> {

private final ResourceLocation texture = ResourceLocation.fromNamespaceAndPath(CraftingSlots.MOD_ID, "textures/gui/portable_crafting.png");

public InventoryCraftingScreen(InventoryCraftingMenu container, Inventory playerInventory, Component displayName) {
super(container, playerInventory, displayName);
imageWidth = 218;
private final int actualImageWidth = 218;

public InventoryCraftingScreen(InventoryCraftingMenu menu, Inventory playerInventory, Component displayName) {
super(menu, new CraftingRecipeBookComponent(menu), playerInventory, displayName);
imageHeight = 102;
}

@Override
protected ScreenPosition getRecipeBookButtonPosition() {
return new ScreenPosition(leftPos + imageWidth + 8, topPos + 8);
}

@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
super.render(guiGraphics, mouseX, mouseY, partialTicks);
Expand All @@ -32,7 +41,7 @@ protected void renderLabels(GuiGraphics guiGraphics, int i, int j) {

@Override
protected void renderBg(GuiGraphics guiGraphics, float f, int i, int j) {
guiGraphics.blit(RenderType::guiTextured, texture, leftPos, topPos, 0, 0, imageWidth, imageHeight, 256, 256);
guiGraphics.blit(RenderType::guiTextured, texture, leftPos, topPos, 0, 0, actualImageWidth, imageHeight, 256, 256);
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package net.blay09.mods.craftingslots.client.screen;

import net.blay09.mods.craftingslots.container.PortableCraftingMenu;
import net.blay09.mods.craftingslots.menu.PortableCraftingMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.navigation.ScreenPosition;
import net.minecraft.client.gui.screens.inventory.AbstractRecipeBookScreen;
import net.minecraft.client.gui.screens.recipebook.CraftingRecipeBookComponent;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

public class PortableCraftingScreen extends AbstractContainerScreen<PortableCraftingMenu> {
public class PortableCraftingScreen extends AbstractRecipeBookScreen<PortableCraftingMenu> {

private static final ResourceLocation texture = ResourceLocation.withDefaultNamespace("textures/gui/container/crafting_table.png");

public PortableCraftingScreen(PortableCraftingMenu container, Inventory playerInventory, Component displayName) {
super(container, playerInventory, displayName);
public PortableCraftingScreen(PortableCraftingMenu menu, Inventory playerInventory, Component displayName) {
super(menu, new CraftingRecipeBookComponent(menu), playerInventory, displayName);
}

@Override
protected ScreenPosition getRecipeBookButtonPosition() {
return new ScreenPosition(leftPos + imageWidth - 25, topPos + 5);
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.blay09.mods.craftingslots.item;

import net.blay09.mods.craftingslots.container.InventoryCraftingMenu;
import net.blay09.mods.craftingslots.menu.InventoryCraftingMenu;
import net.minecraft.world.MenuProvider;

public class InventoryCraftingItem extends PortableCraftingItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.blay09.mods.craftingslots.item;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.craftingslots.container.PortableCraftingMenu;
import net.blay09.mods.craftingslots.menu.PortableCraftingMenu;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package net.blay09.mods.craftingslots.menu;

import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.recipebook.ServerPlaceRecipe;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
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.StackedItemContents;
import net.minecraft.world.inventory.*;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeType;

import javax.annotation.Nullable;
import java.util.List;

public abstract class CustomCraftingMenu extends AbstractCraftingMenu {

private static final int WIDTH = 3;
private static final int HEIGHT = 3;
private final Inventory playerInventory;
private boolean placingRecipe;

protected CustomCraftingMenu(MenuType<?> type, int id, Inventory playerInventory) {
super(type, id, WIDTH, HEIGHT);
this.playerInventory = playerInventory;
}

@Override
public RecipeBookMenu.PostPlaceAction handlePlacement(boolean useMaxItems, boolean b, RecipeHolder<?> recipeHolder, ServerLevel level, Inventory inventory) {
RecipeHolder<CraftingRecipe> craftingRecipeHolder = (RecipeHolder<CraftingRecipe>) recipeHolder;
beginPlacingRecipe();

RecipeBookMenu.PostPlaceAction result;
try {
List<Slot> list = getInputGridSlots();
result = ServerPlaceRecipe.placeRecipe(new ServerPlaceRecipe.CraftingMenuAccess<>() {
public void fillCraftSlotsStackedContents(StackedItemContents stackedItemContents) {
CustomCraftingMenu.this.fillCraftSlotsStackedContents(stackedItemContents);
}

public void clearCraftingContent() {
CustomCraftingMenu.this.getResultContainer().clearContent();
CustomCraftingMenu.this.getCraftingContainer().clearContent();
}

public boolean recipeMatches(RecipeHolder<CraftingRecipe> recipeHolder) {
return recipeHolder.value().matches(CustomCraftingMenu.this.getCraftingContainer().asCraftInput(), CustomCraftingMenu.this.owner().level());
}
}, WIDTH, HEIGHT, list, list, inventory, craftingRecipeHolder, useMaxItems, b);
} finally {
finishPlacingRecipe(level, craftingRecipeHolder);
}

return result;
}

public abstract Slot getResultSlot();

public abstract List<Slot> getInputGridSlots();

protected abstract CraftingContainer getCraftingContainer();

protected abstract ResultContainer getResultContainer();

protected void slotChangedCraftingGrid(AbstractContainerMenu menu, ServerLevel level, Player player, CraftingContainer craftingContainer, ResultContainer resultContainer, @Nullable RecipeHolder<CraftingRecipe> recipeHolder) {
final var craftInput = craftingContainer.asCraftInput();
final var serverPlayer = (ServerPlayer) player;
var itemStack = ItemStack.EMPTY;
final var foundRecipe = level.getServer()
.getRecipeManager()
.getRecipeFor(RecipeType.CRAFTING, craftInput, level, recipeHolder);
if (foundRecipe.isPresent()) {
final var foundRecipeHolder = foundRecipe.get();
final var craftingRecipe = foundRecipeHolder.value();
if (resultContainer.setRecipeUsed(serverPlayer, foundRecipeHolder)) {
final var assembledStack = craftingRecipe.assemble(craftInput, level.registryAccess());
if (assembledStack.isItemEnabled(level.enabledFeatures())) {
itemStack = assembledStack;
}
}
}

resultContainer.setItem(getResultSlot().getContainerSlot(), itemStack);
menu.setRemoteSlot(getResultSlot().index, itemStack);
serverPlayer.connection.send(new ClientboundContainerSetSlotPacket(menu.containerId, menu.incrementStateId(), getResultSlot().index, itemStack));
}

@Override
public void slotsChanged(Container container) {
if (!placingRecipe) {
if (playerInventory.player.level() instanceof ServerLevel serverLevel) {
slotChangedCraftingGrid(this, serverLevel, playerInventory.player, getCraftingContainer(), getResultContainer(), null);
}
}
}

public void beginPlacingRecipe() {
this.placingRecipe = true;
}

public void finishPlacingRecipe(ServerLevel level, RecipeHolder<CraftingRecipe> recipeHolder) {
this.placingRecipe = false;
slotChangedCraftingGrid(this, level, playerInventory.player, getCraftingContainer(), getResultContainer(), recipeHolder);
}

@Override
public boolean canTakeItemForPickAll(ItemStack itemStack, Slot slot) {
return slot.container != getResultContainer() && super.canTakeItemForPickAll(itemStack, slot);
}

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

protected Player owner() {
return playerInventory.player;
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package net.blay09.mods.craftingslots.container;
package net.blay09.mods.craftingslots.menu;

import net.minecraft.core.NonNullList;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.world.entity.player.StackedItemContents;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.TransientCraftingContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;

import java.util.Iterator;
import java.util.List;

public class InventoryCraftingContainer extends TransientCraftingContainer {
Expand Down
Loading

0 comments on commit f9af830

Please sign in to comment.