3232import net .minecraft .world .item .crafting .Ingredient ;
3333import net .minecraft .world .item .crafting .ShapelessRecipe ;
3434import net .minecraft .world .level .Level ;
35- import org .spongepowered .common .accessor .world .item .crafting .ShapelessRecipeAccessor ;
35+ import org .spongepowered .common .bridge .world .item .crafting .CraftingInputBridge ;
36+ import org .spongepowered .common .bridge .world .item .crafting .PlacementInfoBridge ;
3637import org .spongepowered .common .item .recipe .ingredient .SpongeIngredient ;
3738
38- import java .util .ArrayList ;
39- import java .util .Collection ;
40- import java .util .Comparator ;
41- import java .util .HashMap ;
42- import java .util .HashSet ;
4339import java .util .List ;
44- import java .util .Map ;
45- import java .util .Set ;
4640import java .util .function .Function ;
4741
4842/**
@@ -68,11 +62,19 @@ public SpongeShapelessRecipe(final String groupIn,
6862 }
6963
7064 @ Override
71- public boolean matches (final CraftingInput $$0 , final Level $$1 ) {
65+ public boolean matches (final CraftingInput input , final Level $$1 ) {
7266 if (this .onlyVanillaIngredients ) {
73- return super .matches ($$0 , $$1 );
67+ return super .matches (input , $$1 );
7468 }
75- return SpongeShapelessRecipe .matches ($$0 , ((ShapelessRecipeAccessor ) this ).accessor$ingredients ());
69+
70+ // Quick check to avoid complex calculations if possible
71+ if (input .ingredientCount () != this .placementInfo ().ingredients ().size ()) {
72+ // The amount of non-empty stacks doesn't match the amount of ingredients
73+ return false ;
74+ }
75+
76+ return ((CraftingInputBridge ) input ).bridge$getItemStackStackedContents ().tryPick (
77+ ((PlacementInfoBridge ) this .placementInfo ()).bridge$getStackIngredientInfos (), 1 , null );
7678 }
7779
7880 @ Override
@@ -91,65 +93,4 @@ public ItemStack assemble(final CraftingInput $$0, final HolderLookup.Provider $
9193 }
9294 return super .assemble ($$0 , $$1 );
9395 }
94-
95- private static boolean matches (final CraftingInput input , final List <Ingredient > ingredients ) {
96- final int elements = ingredients .size ();
97- if (input .ingredientCount () != elements ) {
98- // The amount of non-empty stacks doesn't match the amount of ingredients
99- return false ;
100- }
101-
102- // This can contain empty stacks
103- // Probably would be better to store non-empty stack list on CraftingInput
104- final List <ItemStack > stacks = input .items ();
105- // find matched stack -> ingredient list
106- final Map <Integer , List <Integer >> matchesMap = new HashMap <>();
107- for (int i = 0 ; i < elements ; i ++) {
108- final Ingredient ingredient = ingredients .get (i );
109- boolean noMatch = true ;
110- for (int j = 0 ; j < stacks .size (); j ++) {
111- final ItemStack stack = stacks .get (j );
112- if (!stack .isEmpty () && ingredient .test (stack )) {
113- matchesMap .computeIfAbsent (j , k -> new ArrayList <>()).add (i );
114- noMatch = false ;
115- }
116- }
117- if (noMatch ) {
118- // One ingredient had no matching stack
119- return false ;
120- }
121- }
122-
123- if (matchesMap .size () != elements ) {
124- // At least one stack had no matching ingredient
125- return false ;
126- }
127-
128- // Every ingredient had at least one matching stack
129- // Now check if each stack matches one ingredient
130- final List <Collection <Integer >> stackList = new ArrayList <>(matchesMap .values ());
131- stackList .sort (Comparator .comparingInt (Collection ::size ));
132- return SpongeShapelessRecipe .matchesRecursive (stackList , 0 , new HashSet <>());
133- }
134-
135- private static boolean matchesRecursive (List <Collection <Integer >> stackList , int d , Set <Integer > used ) {
136- if (d == stackList .size ()) {
137- return true ;
138- }
139-
140- final Collection <Integer > stacks = stackList .get (d );
141- for (Integer stack : stacks ) {
142- if (used .contains (stack )) {
143- // each stack is only used once
144- continue ;
145- }
146- final HashSet <Integer > copy = new HashSet <>(used );
147- copy .add (stack );
148- if (SpongeShapelessRecipe .matchesRecursive (stackList , d + 1 , copy )) {
149- return true ;
150- }
151- }
152- return false ;
153- }
154-
15596}
0 commit comments