3232import java .util .BitSet ;
3333import java .util .List ;
3434import java .util .Set ;
35- import java .util .function .Consumer ;
35+ import java .util .function .BiConsumer ;
3636
37- public class RecipeFinder <T > {
37+ public class RecipeFinder <T , I extends RecipeFinder . Ingredient < T > > {
3838 public final Reference2IntOpenHashMap <T > amounts = new Reference2IntOpenHashMap <>();
3939
4040 public boolean contains (T item ) {
@@ -56,11 +56,11 @@ public void put(T item, int amount) {
5656 this .amounts .addTo (item , amount );
5757 }
5858
59- public boolean findRecipe (List <Ingredient < T >> list , int maxCrafts , @ Nullable Consumer < T > output ) {
59+ public boolean findRecipe (List <I > list , int maxCrafts , @ Nullable BiConsumer < T , I > output ) {
6060 return new Filter (list ).tryPick (maxCrafts , output );
6161 }
6262
63- public int countRecipeCrafts (List <Ingredient < T >> list , int maxCrafts , @ Nullable Consumer < T > output ) {
63+ public int countRecipeCrafts (List <I > list , int maxCrafts , @ Nullable BiConsumer < T , I > output ) {
6464 return new Filter (list ).tryPickAll (maxCrafts , output );
6565 }
6666
@@ -69,14 +69,14 @@ public void clear() {
6969 }
7070
7171 class Filter {
72- private final List <Ingredient < T > > ingredients ;
72+ private final List <I > ingredients ;
7373 private final int ingredientCount ;
7474 private final List <T > items ;
7575 private final int itemCount ;
7676 private final BitSet data ;
7777 private final IntList path = new IntArrayList ();
7878
79- public Filter (final List <Ingredient < T > > list ) {
79+ public Filter (final List <I > list ) {
8080 this .ingredients = list ;
8181 this .ingredientCount = this .ingredients .size ();
8282 this .items = this .getUniqueAvailableIngredientItems ();
@@ -87,7 +87,7 @@ public Filter(final List<Ingredient<T>> list) {
8787
8888 private void setInitialConnections () {
8989 for (int i = 0 ; i < this .ingredientCount ; i ++) {
90- List <T > list = (( Ingredient < T >) this .ingredients .get (i ) ).elements ();
90+ List <T > list = this .ingredients .get (i ).elements ();
9191
9292 for (int j = 0 ; j < this .itemCount ; j ++) {
9393 if (list .contains (this .items .get (j ))) {
@@ -97,7 +97,7 @@ private void setInitialConnections() {
9797 }
9898 }
9999
100- public boolean tryPick (int maxCrafts , @ Nullable Consumer < T > output ) {
100+ public boolean tryPick (int maxCrafts , @ Nullable BiConsumer < T , I > output ) {
101101 if (maxCrafts <= 0 ) {
102102 return true ;
103103 } else {
@@ -117,7 +117,7 @@ public boolean tryPick(int maxCrafts, @Nullable Consumer<T> output) {
117117 this .unassign (m , l );
118118 put (this .items .get (m ), maxCrafts );
119119 if (bl2 ) {
120- output .accept (this .items .get (m ));
120+ output .accept (this .items .get (m ), this . ingredients . get ( l ) );
121121 }
122122 break ;
123123 }
@@ -362,7 +362,7 @@ private void clearRange(int i, int j) {
362362 this .data .clear (i , i + j );
363363 }
364364
365- public int tryPickAll (int i , @ Nullable Consumer < T > output ) {
365+ public int tryPickAll (int i , @ Nullable BiConsumer < T , I > output ) {
366366 int j = 0 ;
367367 int k = Math .min (i , this .getMinIngredientCount ()) + 1 ;
368368
@@ -403,6 +403,7 @@ private int getMinIngredientCount() {
403403 }
404404 }
405405
406- public record Ingredient <T >(List <T > elements ) {
406+ public interface Ingredient <T > {
407+ List <T > elements ();
407408 }
408409}
0 commit comments