diff --git a/CommonApi/src/main/java/mezz/jei/api/gui/ingredient/ICraftingGridHelper.java b/CommonApi/src/main/java/mezz/jei/api/gui/ingredient/ICraftingGridHelper.java index 4850d791e..6ad7a5e22 100644 --- a/CommonApi/src/main/java/mezz/jei/api/gui/ingredient/ICraftingGridHelper.java +++ b/CommonApi/src/main/java/mezz/jei/api/gui/ingredient/ICraftingGridHelper.java @@ -2,6 +2,7 @@ import java.util.List; +import com.mojang.datafixers.util.Pair; import mezz.jei.api.constants.VanillaTypes; import mezz.jei.api.gui.builder.IRecipeLayoutBuilder; import mezz.jei.api.gui.builder.IRecipeSlotBuilder; @@ -9,6 +10,7 @@ import mezz.jei.api.recipe.IFocusGroup; import mezz.jei.api.recipe.category.extensions.vanilla.crafting.ICraftingCategoryExtension; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.RecipeHolder; import org.jetbrains.annotations.Nullable; @@ -21,6 +23,22 @@ * to help them override the default behavior. */ public interface ICraftingGridHelper { + /** + * Create and place input ingredients onto the crafting grid in a consistent way. + * For shapeless recipes, use a width and height of 0. + * + * @since 19.16.2 + */ + List createAndSetNamedIngredients(IRecipeLayoutBuilder builder, List> namedIngredients, int width, int height); + + /** + * Create and place input ingredients onto the crafting grid in a consistent way. + * For shapeless recipes, use a width and height of 0. + * + * @since 19.16.2 + */ + void createAndSetIngredients(IRecipeLayoutBuilder builder, List ingredients, int width, int height); + /** * Create and place input ingredients onto the crafting grid in a consistent way. * For shapeless recipes, use a width and height of 0. diff --git a/Library/src/main/java/mezz/jei/library/gui/helpers/CraftingGridHelper.java b/Library/src/main/java/mezz/jei/library/gui/helpers/CraftingGridHelper.java index d1269c4b9..802445a4b 100644 --- a/Library/src/main/java/mezz/jei/library/gui/helpers/CraftingGridHelper.java +++ b/Library/src/main/java/mezz/jei/library/gui/helpers/CraftingGridHelper.java @@ -1,5 +1,6 @@ package mezz.jei.library.gui.helpers; +import com.mojang.datafixers.util.Pair; import mezz.jei.api.gui.builder.IRecipeLayoutBuilder; import mezz.jei.api.gui.builder.IRecipeSlotBuilder; import mezz.jei.api.gui.ingredient.ICraftingGridHelper; @@ -21,23 +22,43 @@ public class CraftingGridHelper implements ICraftingGridHelper { private CraftingGridHelper() {} + @Override + public List createAndSetNamedIngredients(IRecipeLayoutBuilder builder, List> namedIngredients, int width, int height) { + List inputSlots = createInputSlots(builder, width, height); + setNamedIngredients(inputSlots, namedIngredients, width, height); + return inputSlots; + } + + @Override + public void createAndSetIngredients(IRecipeLayoutBuilder builder, List ingredients, int width, int height) { + List inputSlots = createInputSlots(builder, width, height); + setIngredients(inputSlots, ingredients, width, height); + } + @Override public List createAndSetInputs(IRecipeLayoutBuilder builder, IIngredientType ingredientType, List<@Nullable List<@Nullable T>> inputs, int width, int height) { + List inputSlots = createInputSlots(builder, width, height); + setInputs(inputSlots, ingredientType, inputs, width, height); + return inputSlots; + } + + public void setIngredients(List slotBuilders, List ingredients, int width, int height) { if (width <= 0 || height <= 0) { - builder.setShapeless(); + width = height = getShapelessSize(ingredients.size()); } - - List inputSlots = new ArrayList<>(); - for (int y = 0; y < 3; ++y) { - for (int x = 0; x < 3; ++x) { - IRecipeSlotBuilder slot = builder.addSlot(RecipeIngredientRole.INPUT, x * 18 + 1, y * 18 + 1); - inputSlots.add(slot); - } + if (slotBuilders.size() < width * height) { + throw new IllegalArgumentException(String.format("There are not enough slots (%s) to hold a recipe of this size. (%sx%s)", slotBuilders.size(), width, height)); } - setInputs(inputSlots, ingredientType, inputs, width, height); + for (int i = 0; i < ingredients.size(); i++) { + int index = getCraftingIndex(i, width, height); + IRecipeSlotBuilder slot = slotBuilders.get(index); - return inputSlots; + Ingredient ingredient = ingredients.get(i); + if (ingredient != null) { + slot.addIngredients(ingredient); + } + } } @Override @@ -69,6 +90,41 @@ public IRecipeSlotBuilder createAndSetOutputs(IRecipeLayoutBuilder builder, return outputSlot; } + private static List createInputSlots(IRecipeLayoutBuilder builder, int width, int height) { + if (width <= 0 || height <= 0) { + builder.setShapeless(); + } + + List inputSlots = new ArrayList<>(); + for (int y = 0; y < 3; ++y) { + for (int x = 0; x < 3; ++x) { + IRecipeSlotBuilder slot = builder.addSlot(RecipeIngredientRole.INPUT, x * 18 + 1, y * 18 + 1); + inputSlots.add(slot); + } + } + return inputSlots; + } + + private static void setNamedIngredients(List slotBuilders, List> namedIngredients, int width, int height) { + if (width <= 0 || height <= 0) { + width = height = getShapelessSize(namedIngredients.size()); + } + if (slotBuilders.size() < width * height) { + throw new IllegalArgumentException(String.format("There are not enough slots (%s) to hold a recipe of this size. (%sx%s)", slotBuilders.size(), width, height)); + } + + for (int i = 0; i < namedIngredients.size(); i++) { + int index = getCraftingIndex(i, width, height); + IRecipeSlotBuilder slot = slotBuilders.get(index); + + Pair value = namedIngredients.get(i); + if (value != null) { + slot.setSlotName(value.getFirst()) + .addIngredients(value.getSecond()); + } + } + } + public static Map getGuiSlotToIngredientMap(RecipeHolder recipeHolder, int width, int height) { CraftingRecipe recipe = recipeHolder.value(); NonNullList ingredients = recipe.getIngredients(); diff --git a/Library/src/main/java/mezz/jei/library/plugins/vanilla/crafting/CraftingCategoryExtension.java b/Library/src/main/java/mezz/jei/library/plugins/vanilla/crafting/CraftingCategoryExtension.java index f2e9d89ef..761a0e903 100644 --- a/Library/src/main/java/mezz/jei/library/plugins/vanilla/crafting/CraftingCategoryExtension.java +++ b/Library/src/main/java/mezz/jei/library/plugins/vanilla/crafting/CraftingCategoryExtension.java @@ -8,11 +8,9 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.CraftingRecipe; -import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.RecipeHolder; import net.minecraft.world.item.crafting.ShapedRecipe; -import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -20,17 +18,12 @@ public class CraftingCategoryExtension implements ICraftingCategoryExtension recipeHolder, IRecipeLayoutBuilder builder, ICraftingGridHelper craftingGridHelper, IFocusGroup focuses) { CraftingRecipe recipe = recipeHolder.value(); - List> inputs = new ArrayList<>(); - for (Ingredient ingredient : recipe.getIngredients()) { - List items = List.of(ingredient.getItems()); - inputs.add(items); - } ItemStack resultItem = RecipeUtil.getResultItem(recipe); int width = getWidth(recipeHolder); int height = getHeight(recipeHolder); craftingGridHelper.createAndSetOutputs(builder, List.of(resultItem)); - craftingGridHelper.createAndSetInputs(builder, inputs, width, height); + craftingGridHelper.createAndSetIngredients(builder, recipe.getIngredients(), width, height); } @SuppressWarnings("removal") diff --git a/gradle.properties b/gradle.properties index 24c71b0c2..3fb89ac11 100644 --- a/gradle.properties +++ b/gradle.properties @@ -62,4 +62,4 @@ curseHomepageUrl=https://www.curseforge.com/minecraft/mc-mods/jei jUnitVersion=5.8.2 # Version -specificationVersion=19.16.1 +specificationVersion=19.16.2