Skip to content

Commit 763f8a1

Browse files
committed
Continued removal of capturing lambdas
1 parent b61e724 commit 763f8a1

File tree

3 files changed

+59
-62
lines changed

3 files changed

+59
-62
lines changed

src/main/java/mekanism/common/content/qio/QIOCraftingWindow.java

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import java.util.Collections;
1212
import java.util.List;
1313
import java.util.Objects;
14-
import java.util.function.IntFunction;
15-
import java.util.function.UnaryOperator;
1614
import mekanism.api.Action;
1715
import mekanism.api.AutomationType;
1816
import 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);

src/main/java/mekanism/common/tile/machine/TileEntityOredictionificator.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,27 @@ public void setRemoved() {
111111

112112
private static List<ResourceLocation> getFilterableTags(ItemStack stack) {
113113
//TODO: Cache this and hasFilterableTags?
114-
Map<String, List<String>> possibleFilters = MekanismConfig.general.validOredictionificatorFilters.get();
115114
//For each tag that matches a tag that is filterable, add it to the resulting list
116115
return stack.getTags()
117116
.map(TagKey::location)
118-
.filter(resource -> {
119-
for (String pre : possibleFilters.getOrDefault(resource.getNamespace(), Collections.emptyList())) {
120-
if (resource.getPath().startsWith(pre)) {
121-
return true;
122-
}
123-
}
124-
return false;
125-
}).toList();
117+
.filter(TileEntityOredictionificator::isPossibleFilter)
118+
.toList();
126119
}
127120

128121
private boolean hasFilterableTags(ItemStack stack) {
122+
return stack.getTags().anyMatch(tag -> isPossibleFilter(tag.location()));
123+
}
124+
125+
private static boolean isPossibleFilter(ResourceLocation resource) {
126+
//Note: We get the possible filters inside the stream so that we don't have to capture it
127+
// as while we look it for every item, it is cached on the config value, so it becomes a simple getter
129128
Map<String, List<String>> possibleFilters = MekanismConfig.general.validOredictionificatorFilters.get();
130-
return stack.getTags().anyMatch(tag -> {
131-
ResourceLocation resource = tag.location();
132-
for (String pre : possibleFilters.getOrDefault(resource.getNamespace(), Collections.emptyList())) {
133-
if (resource.getPath().startsWith(pre)) {
134-
return true;
135-
}
129+
for (String pre : possibleFilters.getOrDefault(resource.getNamespace(), Collections.emptyList())) {
130+
if (resource.getPath().startsWith(pre)) {
131+
return true;
136132
}
137-
return false;
138-
});
133+
}
134+
return false;
139135
}
140136

141137
public static boolean isValidTarget(ResourceLocation tag) {

src/main/java/mekanism/common/tile/qio/TileEntityQIOImporter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class TileEntityQIOImporter extends TileEntityQIOFilterHandler {
3636

3737
private static final int MAX_DELAY = MekanismUtils.TICKS_PER_HALF_SECOND;
3838

39+
private final Predicate<ItemStack> FILTER_ENABLED = stack -> getFilterManager().anyEnabledMatch(stack, (filter, s) -> filter.getFinder().test(s));
40+
3941
@Nullable
4042
private BlockCapabilityCache<IItemHandler, @Nullable Direction> backInventory;
4143
private int delay = 0;
@@ -77,7 +79,7 @@ private void tryImport(QIOFrequency freq) {
7779

7880
Predicate<ItemStack> canFilter;
7981
if (getFilterManager().hasEnabledFilters()) {
80-
canFilter = stack -> getFilterManager().anyEnabledMatch(stack, (filter, s) -> filter.getFinder().test(s));
82+
canFilter = FILTER_ENABLED;
8183
} else if (importWithoutFilter) {
8284
// return true if we don't have any enabled filters installed, and we allow for filterless importing
8385
canFilter = ConstantPredicates.alwaysTrue();

0 commit comments

Comments
 (0)