99import net .minecraftforge .fluids .FluidStack ;
1010import net .minecraftforge .items .IItemHandlerModifiable ;
1111import org .apache .commons .lang3 .Validate ;
12+ import org .apache .commons .lang3 .tuple .Pair ;
1213
1314import java .util .*;
1415import java .util .stream .Collectors ;
@@ -77,41 +78,83 @@ public Recipe(List<CountableIngredient> inputs, List<ItemStack> outputs, List<Ch
7778 this .inputs .sort (Comparator .comparing (CountableIngredient ::getCount ).reversed ());
7879 }
7980
81+ public final boolean matches (boolean consumeIfSuccessful , IItemHandlerModifiable inputs , IMultipleTankHandler fluidInputs , MatchingMode matchingMode ) {
82+ return matches (consumeIfSuccessful , GTUtility .itemHandlerToList (inputs ), GTUtility .fluidHandlerToList (fluidInputs ), matchingMode );
83+ }
84+
8085 public final boolean matches (boolean consumeIfSuccessful , IItemHandlerModifiable inputs , IMultipleTankHandler fluidInputs ) {
81- return matches (consumeIfSuccessful , GTUtility .itemHandlerToList (inputs ), GTUtility .fluidHandlerToList (fluidInputs ));
86+ return matches (consumeIfSuccessful , GTUtility .itemHandlerToList (inputs ), GTUtility .fluidHandlerToList (fluidInputs ), MatchingMode . DEFAULT );
8287 }
8388
8489 public boolean matches (boolean consumeIfSuccessful , List <ItemStack > inputs , List <FluidStack > fluidInputs ) {
85- int [] fluidAmountInTank = new int [ fluidInputs . size ()] ;
86- int [] itemAmountInSlot = new int [ inputs . size ()];
90+ return matches ( consumeIfSuccessful , inputs , fluidInputs , MatchingMode . DEFAULT ) ;
91+ }
8792
88- for (int i = 0 ; i < fluidAmountInTank .length ; i ++) {
89- FluidStack fluidInTank = fluidInputs .get (i );
90- fluidAmountInTank [i ] = fluidInTank == null ? 0 : fluidInTank .amount ;
93+ /**
94+ * This methods aim to verify if the current recipe matches the given inputs according to matchingMode mode.
95+ *
96+ * @param consumeIfSuccessful if true and matchingMode is equal to {@link MatchingMode#DEFAULT} will consume the inputs of the recipe.
97+ * @param inputs Items input or Collections.emptyList() if none.
98+ * @param fluidInputs Fluids input or Collections.emptyList() if none.
99+ * @param matchingMode How this method should check if inputs matches according to {@link MatchingMode} description.
100+ * @return true if the recipe matches the given inputs false otherwise.
101+ */
102+ public boolean matches (boolean consumeIfSuccessful , List <ItemStack > inputs , List <FluidStack > fluidInputs , MatchingMode matchingMode ) {
103+ Pair <Boolean , Integer []> fluids = null ;
104+ Pair <Boolean , Integer []> items = null ;
105+
106+ if (matchingMode == MatchingMode .IGNORE_FLUIDS ) {
107+ if (getInputs ().isEmpty ()) {
108+ return false ;
109+ }
110+ } else {
111+ fluids = matchesFluid (fluidInputs );
112+ if (!fluids .getKey ()) {
113+ return false ;
114+ }
91115 }
92- for (int i = 0 ; i < itemAmountInSlot .length ; i ++) {
93- ItemStack itemInSlot = inputs .get (i );
94- itemAmountInSlot [i ] = itemInSlot .isEmpty () ? 0 : itemInSlot .getCount ();
116+
117+ if (matchingMode == MatchingMode .IGNORE_ITEMS ) {
118+ if (getFluidInputs ().isEmpty ()) {
119+ return false ;
120+ }
121+ } else {
122+ items = matchesItems (inputs );
123+ if (!items .getKey ()) {
124+ return false ;
125+ }
95126 }
96127
97- for (FluidStack fluid : this .fluidInputs ) {
98- int fluidAmount = fluid .amount ;
99- boolean isNotConsumed = false ;
100- if (fluidAmount == 0 ) {
101- fluidAmount = 1 ;
102- isNotConsumed = true ;
128+ if (consumeIfSuccessful && matchingMode == MatchingMode .DEFAULT ) {
129+ Integer [] fluidAmountInTank = fluids .getValue ();
130+ Integer [] itemAmountInSlot = items .getValue ();
131+ for (int i = 0 ; i < fluidAmountInTank .length ; i ++) {
132+ FluidStack fluidStack = fluidInputs .get (i );
133+ int fluidAmount = fluidAmountInTank [i ];
134+ if (fluidStack == null || fluidStack .amount == fluidAmount )
135+ continue ;
136+ fluidStack .amount = fluidAmount ;
137+ if (fluidStack .amount == 0 )
138+ fluidInputs .set (i , null );
103139 }
104- for (int i = 0 ; i < fluidInputs .size (); i ++) {
105- FluidStack tankFluid = fluidInputs .get (i );
106- if (tankFluid == null || !tankFluid .isFluidEqual (fluid ))
140+ for (int i = 0 ; i < itemAmountInSlot .length ; i ++) {
141+ ItemStack itemInSlot = inputs .get (i );
142+ int itemAmount = itemAmountInSlot [i ];
143+ if (itemInSlot .isEmpty () || itemInSlot .getCount () == itemAmount )
107144 continue ;
108- int fluidAmountToConsume = Math .min (fluidAmountInTank [i ], fluidAmount );
109- fluidAmount -= fluidAmountToConsume ;
110- if (!isNotConsumed ) fluidAmountInTank [i ] -= fluidAmountToConsume ;
111- if (fluidAmount == 0 ) break ;
145+ itemInSlot .setCount (itemAmountInSlot [i ]);
112146 }
113- if (fluidAmount > 0 )
114- return false ;
147+ }
148+
149+ return true ;
150+ }
151+
152+ private Pair <Boolean , Integer []> matchesItems (List <ItemStack > inputs ) {
153+ Integer [] itemAmountInSlot = new Integer [inputs .size ()];
154+
155+ for (int i = 0 ; i < itemAmountInSlot .length ; i ++) {
156+ ItemStack itemInSlot = inputs .get (i );
157+ itemAmountInSlot [i ] = itemInSlot .isEmpty () ? 0 : itemInSlot .getCount ();
115158 }
116159
117160 for (CountableIngredient ingredient : this .inputs ) {
@@ -131,29 +174,40 @@ public boolean matches(boolean consumeIfSuccessful, List<ItemStack> inputs, List
131174 if (ingredientAmount == 0 ) break ;
132175 }
133176 if (ingredientAmount > 0 )
134- return false ;
177+ return Pair . of ( false , itemAmountInSlot ) ;
135178 }
136179
137- if (consumeIfSuccessful ) {
138- for (int i = 0 ; i < fluidAmountInTank .length ; i ++) {
139- FluidStack fluidStack = fluidInputs .get (i );
140- int fluidAmount = fluidAmountInTank [i ];
141- if (fluidStack == null || fluidStack .amount == fluidAmount )
142- continue ;
143- fluidStack .amount = fluidAmount ;
144- if (fluidStack .amount == 0 )
145- fluidInputs .set (i , null );
180+ return Pair .of (true , itemAmountInSlot );
181+ }
182+
183+ private Pair <Boolean , Integer []> matchesFluid (List <FluidStack > fluidInputs ) {
184+ Integer [] fluidAmountInTank = new Integer [fluidInputs .size ()];
185+
186+ for (int i = 0 ; i < fluidAmountInTank .length ; i ++) {
187+ FluidStack fluidInTank = fluidInputs .get (i );
188+ fluidAmountInTank [i ] = fluidInTank == null ? 0 : fluidInTank .amount ;
189+ }
190+
191+ for (FluidStack fluid : this .fluidInputs ) {
192+ int fluidAmount = fluid .amount ;
193+ boolean isNotConsumed = false ;
194+ if (fluidAmount == 0 ) {
195+ fluidAmount = 1 ;
196+ isNotConsumed = true ;
146197 }
147- for (int i = 0 ; i < itemAmountInSlot .length ; i ++) {
148- ItemStack itemInSlot = inputs .get (i );
149- int itemAmount = itemAmountInSlot [i ];
150- if (itemInSlot .isEmpty () || itemInSlot .getCount () == itemAmount )
198+ for (int i = 0 ; i < fluidInputs .size (); i ++) {
199+ FluidStack tankFluid = fluidInputs .get (i );
200+ if (tankFluid == null || !tankFluid .isFluidEqual (fluid ))
151201 continue ;
152- itemInSlot .setCount (itemAmountInSlot [i ]);
202+ int fluidAmountToConsume = Math .min (fluidAmountInTank [i ], fluidAmount );
203+ fluidAmount -= fluidAmountToConsume ;
204+ if (!isNotConsumed ) fluidAmountInTank [i ] -= fluidAmountToConsume ;
205+ if (fluidAmount == 0 ) break ;
153206 }
207+ if (fluidAmount > 0 )
208+ return Pair .of (false , fluidAmountInTank );
154209 }
155-
156- return true ;
210+ return Pair .of (true , fluidAmountInTank );
157211 }
158212
159213 ///////////////////
@@ -176,7 +230,7 @@ public List<ItemStack> getResultItemOutputs(int maxOutputSlots, Random random, i
176230 chancedOutputsList = chancedOutputsList .subList (0 , Math .max (0 , maxChancedSlots ));
177231 }
178232 for (ChanceEntry chancedOutput : chancedOutputsList ) {
179- int outputChance = RecipeMap .getChanceFunction ().chanceFor (chancedOutput .getChance (),chancedOutput .getBoostPerTier (), tier );
233+ int outputChance = RecipeMap .getChanceFunction ().chanceFor (chancedOutput .getChance (), chancedOutput .getBoostPerTier (), tier );
180234 if (random .nextInt (Recipe .getMaxChancedValue ()) <= outputChance ) {
181235 outputs .add (chancedOutput .getItemStack ().copy ());
182236 }
0 commit comments