22
33import gregtech .api .items .toolitem .ItemGTToolbelt ;
44import gregtech .api .util .DummyContainer ;
5- import gregtech .api .util .GTLog ;
65import gregtech .api .util .GTTransferUtils ;
76import gregtech .api .util .GTUtility ;
87import gregtech .api .util .ItemStackHashStrategy ;
@@ -64,7 +63,7 @@ public class CraftingRecipeLogic extends SyncHandler {
6463
6564 /**
6665 * List of items needed to complete the crafting recipe, filled by
67- * {@link CraftingRecipeLogic#getIngredientEquivalent(CraftingInputSlot )} )}
66+ * {@link CraftingRecipeLogic#detectAndSendChanges(boolean )} )}
6867 **/
6968 private final Map <ItemStack , Integer > requiredItems = new Object2IntOpenCustomHashMap <>(
7069 this .strategy );
@@ -188,43 +187,6 @@ protected boolean consumeRecipeItems() {
188187 return extracted ;
189188 }
190189
191- /**
192- * <p>
193- * Searches all connected inventories for the slot's stack, and uses
194- * {@link CraftingRecipeLogic#findSubstitute(int, ItemStack)} to look for valid substitutes
195- * </p>
196- * <br/>
197- * <p>
198- * This method also fills out {@link CraftingRecipeLogic#requiredItems} for use in
199- * {@link CraftingRecipeLogic#consumeRecipeItems()}
200- * </p>
201- *
202- * @param slot slot whose current stack to find a substitute for
203- * @return true if the stack in the slot can be extracted or has a valid substitute
204- */
205- public boolean getIngredientEquivalent (CraftingInputSlot slot ) {
206- ItemStack currentStack = slot .getStack ();
207- if (currentStack .isEmpty ()) {
208- return true ; // stack is empty, nothing to return
209- }
210-
211- int count = requiredItems .getOrDefault (currentStack , 0 );
212- if (simulateExtractItem (currentStack , count + 1 )) {
213- requiredItems .put (currentStack , ++count );
214- return true ;
215- }
216-
217- ItemStack substitute = findSubstitute (slot .getIndex (), currentStack );
218- if (substitute .isEmpty ()) return false ;
219-
220- count = requiredItems .getOrDefault (substitute , 0 );
221- if (simulateExtractItem (substitute , count + 1 )) {
222- requiredItems .put (substitute , ++count );
223- return true ;
224- }
225- return false ;
226- }
227-
228190 /**
229191 * <p>
230192 * Searches through all connected inventories for a replacement stack that can be used in the recipe
@@ -241,8 +203,6 @@ public ItemStack findSubstitute(int craftingIndex, ItemStack stack) {
241203 ItemStack substitute = ItemStack .EMPTY ;
242204
243205 var recipe = getCachedRecipe ();
244- List <Ingredient > ingredients = new ArrayList <>(recipe .getIngredients ());
245- ingredients .removeIf (ingredient -> ingredient == Ingredient .EMPTY );
246206 int index = compactedIndexes .get (craftingIndex );
247207
248208 // iterate stored items to find equivalent
@@ -273,7 +233,7 @@ public ItemStack findSubstitute(int craftingIndex, ItemStack stack) {
273233 // take the stack
274234 boolean matched = false ;
275235 if (!(recipe instanceof IShapedRecipe )) {
276- for (Ingredient ing : ingredients ) {
236+ for (Ingredient ing : recipe . getIngredients () ) {
277237 if (ing .apply (itemStack )) {
278238 matched = true ;
279239 break ;
@@ -282,12 +242,7 @@ public ItemStack findSubstitute(int craftingIndex, ItemStack stack) {
282242 } else {
283243 // for shaped recipes, check the exact ingredient instead
284244 // ingredients should be in the correct order
285- if (index >= 0 && index < ingredients .size ())
286- matched = ingredients .get (index ).apply (itemStack );
287- else {
288- GTLog .logger .warn ("Compacted index \" {}\" is out of bounds for list size \" {}\" " , index ,
289- ingredients .size ());
290- }
245+ matched = cachedRecipeData .canIngredientApply (index , itemStack );
291246 }
292247 if (!matched ) {
293248 map .put (GTUtility .copy (1 , itemStack ), false );
@@ -319,10 +274,11 @@ public ItemStack findSubstitute(int craftingIndex, ItemStack stack) {
319274 /**
320275 * Attempts to extract the given stack from connected inventories
321276 *
322- * @param itemStack stack from the crafting matrix
277+ * @param craftingIndex current crafting index
278+ * @param itemStack stack from the crafting matrix
323279 * @return true if the stack was successfully extracted or the stack is empty
324280 */
325- private boolean simulateExtractItem (ItemStack itemStack , int count ) {
281+ private boolean simulateExtractItem (int craftingIndex , ItemStack itemStack , int count ) {
326282 if (itemStack .isEmpty ()) return true ;
327283 if (!stackLookupMap .containsKey (itemStack )) return false ;
328284
@@ -334,8 +290,10 @@ private boolean simulateExtractItem(ItemStack itemStack, int count) {
334290 if (slotStack .getItem () instanceof ItemGTToolbelt ) {
335291 ItemGTToolbelt .setCraftingSlot (slotMap .get (slot ), (EntityPlayerMP ) getSyncManager ().getPlayer ());
336292 }
337- extracted += slotStack .getCount ();
338- if (extracted >= count ) return true ;
293+ if (cachedRecipeData .canIngredientApply (compactedIndexes .get (craftingIndex ), slotStack )) {
294+ extracted += slotStack .getCount ();
295+ if (extracted >= count ) return true ;
296+ }
339297 }
340298
341299 return false ;
@@ -391,7 +349,7 @@ public void detectAndSendChanges(boolean init) {
391349
392350 compactedIndexes .put (slot .getIndex (), next ++);
393351 int count = requiredItems .getOrDefault (slotStack , 0 ) + 1 ;
394- slot .hasIngredients = simulateExtractItem (slotStack , count );
352+ slot .hasIngredients = simulateExtractItem (slot . getIndex (), slotStack , count );
395353
396354 if (slot .hasIngredients ) {
397355 requiredItems .put (GTUtility .copy (1 , slotStack ), count );
@@ -400,7 +358,7 @@ public void detectAndSendChanges(boolean init) {
400358 ItemStack substitute = findSubstitute (slot .getIndex (), slotStack );
401359 if (!substitute .isEmpty ()) {
402360 count = requiredItems .getOrDefault (substitute , 0 ) + 1 ;
403- slot .hasIngredients = simulateExtractItem (substitute , count );
361+ slot .hasIngredients = simulateExtractItem (slot . getIndex (), substitute , count );
404362 requiredItems .put (GTUtility .copy (1 , substitute ), count );
405363 }
406364 }
0 commit comments