From fc87d83d73e7b6f202c55add758d55e2d7b81307 Mon Sep 17 00:00:00 2001 From: LexManos Date: Tue, 14 Nov 2017 00:10:33 -0800 Subject: [PATCH] Make Ingredient explode wildcard values like OreIngredient. Fixes some issues related to Grums order fix for Shapeless recipes. Other more advanced usages need to be documented and tested. --- .../item/crafting/Ingredient.java.patch | 54 +++++++++++++++++-- .../common/crafting/CraftingHelper.java | 14 ++--- .../assets/forge/recipes/_constants.json | 15 ++++++ .../forge/recipes/shapeless_constant.json | 15 ++++++ .../forge/recipes/shapeless_wildcard.json | 13 +++++ 5 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 src/test/resources/assets/forge/recipes/_constants.json create mode 100644 src/test/resources/assets/forge/recipes/shapeless_constant.json create mode 100644 src/test/resources/assets/forge/recipes/shapeless_wildcard.json diff --git a/patches/minecraft/net/minecraft/item/crafting/Ingredient.java.patch b/patches/minecraft/net/minecraft/item/crafting/Ingredient.java.patch index 4027bd8638..697a870b61 100644 --- a/patches/minecraft/net/minecraft/item/crafting/Ingredient.java.patch +++ b/patches/minecraft/net/minecraft/item/crafting/Ingredient.java.patch @@ -9,8 +9,11 @@ public static final Ingredient field_193370_a = new Ingredient(new ItemStack[0]) { public boolean apply(@Nullable ItemStack p_apply_1_) -@@ -23,12 +25,17 @@ +@@ -21,17 +23,34 @@ + } + }; private final ItemStack[] field_193371_b; ++ private final ItemStack[] matchingStacksExploded; private IntList field_194140_c; + protected Ingredient(int size) @@ -21,14 +24,41 @@ protected Ingredient(ItemStack... p_i47503_1_) { this.field_193371_b = p_i47503_1_; ++ net.minecraft.util.NonNullList lst = net.minecraft.util.NonNullList.func_191196_a(); ++ for (ItemStack s : p_i47503_1_) ++ { ++ if (s.func_190926_b()) ++ continue; ++ if (s.func_77960_j() == net.minecraftforge.oredict.OreDictionary.WILDCARD_VALUE) ++ s.func_77973_b().func_150895_a(net.minecraft.creativetab.CreativeTabs.field_78027_g, lst); ++ else ++ lst.add(s); ++ } ++ this.matchingStacksExploded = lst.toArray(new ItemStack[lst.size()]); + Ingredient.INSTANCES.add(this); } - @SideOnly(Side.CLIENT) public ItemStack[] func_193365_a() { - return this.field_193371_b; -@@ -76,6 +83,18 @@ +- return this.field_193371_b; ++ return this.matchingStacksExploded; + } + + public boolean apply(@Nullable ItemStack p_apply_1_) +@@ -63,9 +82,9 @@ + { + if (this.field_194140_c == null) + { +- this.field_194140_c = new IntArrayList(this.field_193371_b.length); ++ this.field_194140_c = new IntArrayList(this.matchingStacksExploded.length); + +- for (ItemStack itemstack : this.field_193371_b) ++ for (ItemStack itemstack : this.matchingStacksExploded) + { + this.field_194140_c.add(RecipeItemHelper.func_194113_b(itemstack)); + } +@@ -76,6 +95,18 @@ return this.field_194140_c; } @@ -47,3 +77,21 @@ public static Ingredient func_193367_a(Item p_193367_0_) { return func_193369_a(new ItemStack(p_193367_0_, 1, 32767)); +@@ -108,4 +139,17 @@ + + return field_193370_a; + } ++ ++ // Merges several vanilla Ingredients together. As a qwerk of how the json is structured, we can't tell if its a single Ingredient type or multiple so we split per item and remerge here. ++ //Only public for internal use, so we can access a private field in here. ++ public static Ingredient merge(java.util.Collection parts) ++ { ++ net.minecraft.util.NonNullList lst = net.minecraft.util.NonNullList.func_191196_a(); ++ for (Ingredient part : parts) ++ { ++ for (ItemStack stack : part.field_193371_b) ++ lst.add(stack); ++ } ++ return new Ingredient(lst.toArray(new ItemStack[lst.size()])); ++ } + } diff --git a/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java b/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java index f4479c2e61..ec1923a0c5 100644 --- a/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java +++ b/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java @@ -143,14 +143,15 @@ public class CraftingHelper { if (json.isJsonArray()) { List ingredients = Lists.newArrayList(); - List vanilla = Lists.newArrayList(); - json.getAsJsonArray().forEach((ele) -> { + List vanilla = Lists.newArrayList(); + json.getAsJsonArray().forEach((ele) -> + { Ingredient ing = CraftingHelper.getIngredient(ele, context); - if (ing.getClass() == Ingredient.class) { + if (ing.getClass() == Ingredient.class) + { //Vanilla, Due to how we read it splits each itemstack, so we pull out to re-merge later - for (ItemStack stack : ing.getMatchingStacks()) - vanilla.add(stack); + vanilla.add(ing); } else { @@ -160,8 +161,7 @@ public class CraftingHelper { if (!vanilla.isEmpty()) { - ItemStack[] items = vanilla.toArray(new ItemStack[vanilla.size()]); - ingredients.add(Ingredient.fromStacks(items)); + ingredients.add(Ingredient.merge(vanilla)); } if (ingredients.size() == 0) diff --git a/src/test/resources/assets/forge/recipes/_constants.json b/src/test/resources/assets/forge/recipes/_constants.json new file mode 100644 index 0000000000..a0e2377be8 --- /dev/null +++ b/src/test/resources/assets/forge/recipes/_constants.json @@ -0,0 +1,15 @@ +[ + { + "name": "forge:some_wool", + "ingredient": [ + { + "item": "minecraft:wool", + "data": 1 + }, + { + "item": "minecraft:wool", + "data": 2 + } + ] + } +] \ No newline at end of file diff --git a/src/test/resources/assets/forge/recipes/shapeless_constant.json b/src/test/resources/assets/forge/recipes/shapeless_constant.json new file mode 100644 index 0000000000..f072c7d8aa --- /dev/null +++ b/src/test/resources/assets/forge/recipes/shapeless_constant.json @@ -0,0 +1,15 @@ +{ + "type": "minecraft:crafting_shapeless", + "result": { + "item": "minecraft:stone", + "data": 0 + }, + "ingredients": [ + { + "item": "#forge:some_wool" + }, + { + "item": "#forge:some_wool" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/assets/forge/recipes/shapeless_wildcard.json b/src/test/resources/assets/forge/recipes/shapeless_wildcard.json new file mode 100644 index 0000000000..9795984193 --- /dev/null +++ b/src/test/resources/assets/forge/recipes/shapeless_wildcard.json @@ -0,0 +1,13 @@ +{ + "result": { + "item": "minecraft:dirt", + "data": 1 + }, + "ingredients": [ + { + "item": "minecraft:stone", + "data": 32767 + } + ], + "type": "minecraft:crafting_shapeless" +} \ No newline at end of file