Skip to content

Commit d873e93

Browse files
committed
Merge branch '17.x-1.21.2' into 18.x-1.21.4
2 parents 9d3bb3c + 90de9de commit d873e93

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

api/src/main/java/me/shedaniel/rei/api/common/transfer/ItemRecipeFinder.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
import net.minecraft.world.item.ItemStack;
3434
import org.jetbrains.annotations.Nullable;
3535

36+
import java.util.ArrayList;
3637
import java.util.List;
38+
import java.util.function.BiConsumer;
3739
import java.util.function.Consumer;
3840

3941
public class ItemRecipeFinder {
4042
private final Interner<ItemKey> keys = Interners.newWeakInterner();
41-
private final RecipeFinder<ItemKey> finder = new RecipeFinder<>();
43+
private final RecipeFinder<ItemKey, Ingredient> finder = new RecipeFinder<>();
4244

4345
public boolean contains(ItemStack item) {
4446
return finder.contains(ofKey(item));
@@ -74,27 +76,54 @@ public void addItem(ItemStack itemStack, int i) {
7476
}
7577

7678
public boolean findRecipe(List<List<ItemStack>> list, int maxCrafts, @Nullable Consumer<ItemStack> output) {
77-
return finder.findRecipe(CollectionUtils.map(list, this::ofKeys), maxCrafts, itemKey -> {
79+
return finder.findRecipe(toIngredients(list), maxCrafts, flatten(itemStack -> {
7880
if (output != null) {
79-
output.accept(new ItemStack(itemKey.item(), 1, itemKey.patch()));
81+
output.accept(itemStack);
8082
}
81-
});
83+
}));
8284
}
8385

8486
public int countRecipeCrafts(List<List<ItemStack>> list, int maxCrafts, @Nullable Consumer<ItemStack> output) {
85-
return finder.countRecipeCrafts(CollectionUtils.map(list, this::ofKeys), maxCrafts, itemKey -> {
87+
return finder.countRecipeCrafts(toIngredients(list), maxCrafts, flatten(itemStack -> {
8688
if (output != null) {
87-
output.accept(new ItemStack(itemKey.item(), 1, itemKey.patch()));
89+
output.accept(itemStack);
8890
}
89-
});
91+
}));
9092
}
9193

9294
private ItemKey ofKey(ItemStack itemStack) {
9395
return keys.intern(new ItemKey(itemStack.getItemHolder(), itemStack.getComponentsPatch()));
9496
}
9597

96-
private RecipeFinder.Ingredient<ItemKey> ofKeys(List<ItemStack> itemStack) {
97-
return new RecipeFinder.Ingredient<>(CollectionUtils.map(itemStack, this::ofKey));
98+
private Ingredient ofKeys(int index, List<ItemStack> itemStack) {
99+
return new Ingredient(index, CollectionUtils.map(itemStack, this::ofKey));
100+
}
101+
102+
private List<Ingredient> toIngredients(List<List<ItemStack>> list) {
103+
List<Ingredient> ingredients = new ArrayList<>();
104+
105+
for (int i = 0; i < list.size(); i++) {
106+
List<ItemStack> stacks = list.get(i);
107+
if (!stacks.isEmpty()) {
108+
ingredients.add(ofKeys(i, stacks));
109+
}
110+
}
111+
112+
return ingredients;
113+
}
114+
115+
private static BiConsumer<ItemKey, Ingredient> flatten(Consumer<ItemStack> consumer) {
116+
int[] lastIndex = {-1};
117+
return (itemKey, ingredient) -> {
118+
for (int i = lastIndex[0] + 1; i < ingredient.index(); i++) {
119+
consumer.accept(ItemStack.EMPTY);
120+
}
121+
consumer.accept(new ItemStack(itemKey.item(), 1, itemKey.patch()));
122+
lastIndex[0] = ingredient.index();
123+
};
124+
}
125+
126+
private record Ingredient(int index, List<ItemKey> elements) implements RecipeFinder.Ingredient<ItemKey> {
98127
}
99128

100129
private record ItemKey(Holder<Item> item, DataComponentPatch patch) {

api/src/main/java/me/shedaniel/rei/api/common/transfer/RecipeFinder.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
import java.util.BitSet;
3333
import java.util.List;
3434
import 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

Comments
 (0)