1111import java .util .Collections ;
1212import java .util .List ;
1313import java .util .Objects ;
14- import java .util .function .IntFunction ;
15- import java .util .function .UnaryOperator ;
1614import mekanism .api .Action ;
1715import mekanism .api .AutomationType ;
1816import mekanism .api .IContentsListener ;
@@ -289,31 +287,30 @@ private void useInput(IInventorySlot inputSlot) {
289287 */
290288 public void emptyTo (boolean toPlayerInv , List <HotBarSlot > hotBarSlots , List <MainInventorySlot > mainInventorySlots ) {
291289 if (toPlayerInv ) {
292- emptyTo (toTransfer -> {
293- ItemStack remainder = MekanismContainer .insertItem (hotBarSlots , toTransfer , true , windowData );
294- remainder = MekanismContainer .insertItem (mainInventorySlots , remainder , true , windowData );
295- remainder = MekanismContainer .insertItem (hotBarSlots , remainder , false , windowData );
296- return MekanismContainer .insertItem (mainInventorySlots , remainder , false , windowData );
297- });
290+ for (CraftingWindowInventorySlot inputSlot : inputSlots ) {
291+ if (!inputSlot .isEmpty ()) {
292+ ItemStack toTransfer = inputSlot .extractItem (inputSlot .getCount (), Action .SIMULATE , AutomationType .INTERNAL );
293+ if (!toTransfer .isEmpty ()) {
294+ ItemStack remainder = MekanismContainer .insertItem (hotBarSlots , toTransfer , true , windowData );
295+ remainder = MekanismContainer .insertItem (mainInventorySlots , remainder , true , windowData );
296+ remainder = MekanismContainer .insertItem (hotBarSlots , remainder , false , windowData );
297+ remainder = MekanismContainer .insertItem (mainInventorySlots , remainder , false , windowData );
298+ inputSlot .extractItem (toTransfer .getCount () - remainder .getCount (), Action .EXECUTE , AutomationType .INTERNAL );
299+ }
300+ }
301+ }
298302 } else {
299303 QIOFrequency frequency = holder .getFrequency ();
300304 //NO-OP if the frequency is null and that is the target
301305 if (frequency != null ) {
302- emptyTo (frequency ::addItem );
303- }
304- }
305- }
306-
307- /**
308- * @param inserter Unary operator that takes the stack to transfer and returns remainder
309- */
310- private void emptyTo (UnaryOperator <ItemStack > inserter ) {
311- for (CraftingWindowInventorySlot inputSlot : inputSlots ) {
312- if (!inputSlot .isEmpty ()) {
313- ItemStack toTransfer = inputSlot .extractItem (inputSlot .getCount (), Action .SIMULATE , AutomationType .INTERNAL );
314- if (!toTransfer .isEmpty ()) {
315- ItemStack remainder = inserter .apply (toTransfer );
316- inputSlot .extractItem (toTransfer .getCount () - remainder .getCount (), Action .EXECUTE , AutomationType .INTERNAL );
306+ for (CraftingWindowInventorySlot inputSlot : inputSlots ) {
307+ if (!inputSlot .isEmpty ()) {
308+ ItemStack toTransfer = inputSlot .extractItem (inputSlot .getCount (), Action .SIMULATE , AutomationType .INTERNAL );
309+ if (!toTransfer .isEmpty ()) {
310+ ItemStack remainder = frequency .addItem (toTransfer );
311+ inputSlot .extractItem (toTransfer .getCount () - remainder .getCount (), Action .EXECUTE , AutomationType .INTERNAL );
312+ }
313+ }
317314 }
318315 }
319316 }
@@ -904,24 +901,25 @@ private void mapRecipe(int index, ItemStack used) {
904901 }
905902 //Ensure our remainder helper has been initialized as we will make use of it in validation
906903 remainderHelper .updateInputsWithReplacement (index , used );
907- IntFunction <ItemStack > itemGetter = i -> {
908- if (i == index ) {
909- return used ;
910- } else if (i >= 0 && i < inputSlots .length ) {
911- return inputSlots [i ].getStack ();
912- }
913- return ItemStack .EMPTY ;
914- };
915904 if (lastRecipe .value () instanceof IShapedRecipe <?> shapedRecipe ) {
916905 //It is a shaped recipe, make use of this information to attempt to find the proper match
917- mapShapedRecipe (shapedRecipe , ingredients , itemGetter );
906+ mapShapedRecipe (shapedRecipe , ingredients , index , used );
918907 } else {
919- mapShapelessRecipe (ingredients , itemGetter );
908+ mapShapelessRecipe (ingredients , index , used );
920909 }
921910 }
922911 }
923912
924- private void mapShapedRecipe (IShapedRecipe <?> shapedRecipe , NonNullList <Ingredient > ingredients , IntFunction <ItemStack > itemGetter ) {
913+ private ItemStack getItem (int i , int index , ItemStack used ) {
914+ if (i == index ) {
915+ return used ;
916+ } else if (i >= 0 && i < inputSlots .length ) {
917+ return inputSlots [i ].getStack ();
918+ }
919+ return ItemStack .EMPTY ;
920+ }
921+
922+ private void mapShapedRecipe (IShapedRecipe <?> shapedRecipe , NonNullList <Ingredient > ingredients , int index , ItemStack used ) {
925923 int recipeWidth = shapedRecipe .getRecipeWidth ();
926924 int recipeHeight = shapedRecipe .getRecipeHeight ();
927925 for (int columnStart = 0 ; columnStart <= 3 - recipeWidth ; columnStart ++) {
@@ -932,8 +930,8 @@ private void mapShapedRecipe(IShapedRecipe<?> shapedRecipe, NonNullList<Ingredie
932930 // be so that it matches when mirrored, and if it does, the ingredients still should be close enough
933931 // for the various spots given this is more of a heuristic than actually having to match no matter what,
934932 // because we will end up testing the recipe with the item we try to use anyway at the end before moving it.
935- if (mapShapedRecipe (ingredients , columnStart , rowStart , recipeWidth , recipeHeight , true , itemGetter ) ||
936- mapShapedRecipe (ingredients , columnStart , rowStart , recipeWidth , recipeHeight , false , itemGetter )) {
933+ if (mapShapedRecipe (ingredients , columnStart , rowStart , recipeWidth , recipeHeight , true , index , used ) ||
934+ mapShapedRecipe (ingredients , columnStart , rowStart , recipeWidth , recipeHeight , false , index , used )) {
937935 return ;
938936 }
939937 }
@@ -943,7 +941,7 @@ private void mapShapedRecipe(IShapedRecipe<?> shapedRecipe, NonNullList<Ingredie
943941 }
944942
945943 private boolean mapShapedRecipe (NonNullList <Ingredient > ingredients , int columnStart , int rowStart , int recipeWidth , int recipeHeight , boolean mirrored ,
946- IntFunction < ItemStack > itemGetter ) {
944+ int index , ItemStack used ) {
947945 for (int actualColumn = 0 ; actualColumn < 3 ; actualColumn ++) {
948946 for (int actualRow = 0 ; actualRow < 3 ; actualRow ++) {
949947 int column = actualColumn - columnStart ;
@@ -956,10 +954,10 @@ private boolean mapShapedRecipe(NonNullList<Ingredient> ingredients, int columnS
956954 ingredient = ingredients .get (column + row * recipeWidth );
957955 }
958956 }
959- int index = actualColumn + actualRow * 3 ;
960- if (ingredient .test (itemGetter . apply ( index ))) {
957+ int i = actualColumn + actualRow * 3 ;
958+ if (ingredient .test (getItem ( i , index , used ))) {
961959 //If the ingredient matches, add it to our map
962- slotIngredients .put (index , ingredient );
960+ slotIngredients .put (i , ingredient );
963961 } else {
964962 //Otherwise, if the ingredient doesn't match, clear our stored ingredients
965963 // as they were empty to start and return there is no match
@@ -971,21 +969,22 @@ private boolean mapShapedRecipe(NonNullList<Ingredient> ingredients, int columnS
971969 return true ;
972970 }
973971
974- private void mapShapelessRecipe (NonNullList <Ingredient > ingredients , IntFunction < ItemStack > itemGetter ) {
972+ private void mapShapelessRecipe (NonNullList <Ingredient > ingredients , int index , ItemStack used ) {
975973 //Note: We don't make use of the "simple" way of looking the ingredients up that Vanilla's Shapeless recipe uses,
976974 // when all the ingredients are simple, as we care about which slot the various things happens in, which is much
977975 // easier to grab from forge's RecipeMatcher which works for simple ingredients as well, and is just not used
978976 // normally as it has slightly more overhead
979977 Int2IntMap actualLookup = new Int2IntArrayMap (inputSlots .length );
980978 List <ItemStack > inputs = new ArrayList <>(inputSlots .length );
981- for (int index = 0 ; index < inputSlots .length ; index ++) {
982- ItemStack stack = itemGetter . apply ( index );
979+ for (int i = 0 ; i < inputSlots .length ; i ++) {
980+ ItemStack stack = getItem ( i , index , used );
983981 if (!stack .isEmpty ()) {
984- actualLookup .put (inputs .size (), index );
982+ actualLookup .put (inputs .size (), i );
985983 inputs .add (stack );
986984 }
987985 }
988986 int [] matches = RecipeMatcher .findMatches (inputs , ingredients );
987+ //noinspection ConstantValue,UnreachableCode - It can return null values
989988 if (matches != null ) {
990989 for (int ingredientIndex = 0 ; ingredientIndex < matches .length ; ingredientIndex ++) {
991990 int actualSlot = actualLookup .getOrDefault (matches [ingredientIndex ], -1 );
0 commit comments