Skip to content

Commit aabe723

Browse files
committed
remove unused method
move ingredient list to CachedRecipeData apply the ingredient to the stack when extracting
1 parent 36d713c commit aabe723

File tree

2 files changed

+35
-54
lines changed

2 files changed

+35
-54
lines changed

src/main/java/gregtech/common/metatileentities/storage/CachedRecipeData.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package gregtech.common.metatileentities.storage;
22

3+
import gregtech.api.util.GTLog;
4+
35
import net.minecraft.inventory.InventoryCrafting;
6+
import net.minecraft.item.ItemStack;
47
import net.minecraft.item.crafting.IRecipe;
8+
import net.minecraft.item.crafting.Ingredient;
59
import net.minecraft.world.World;
610

711
import org.jetbrains.annotations.Nullable;
812

13+
import java.util.ArrayList;
14+
import java.util.List;
15+
916
public class CachedRecipeData {
1017

1118
private IRecipe recipe;
1219
private IRecipe previousRecipe;
20+
private final List<Ingredient> recipeIngredients = new ArrayList<>();
1321

1422
public CachedRecipeData() {
1523
this(null);
@@ -29,6 +37,11 @@ public boolean matches(InventoryCrafting inventoryCrafting, World world) {
2937
public void setRecipe(IRecipe newRecipe) {
3038
this.previousRecipe = this.recipe;
3139
this.recipe = newRecipe;
40+
this.recipeIngredients.clear();
41+
if (newRecipe != null) {
42+
this.recipeIngredients.addAll(newRecipe.getIngredients());
43+
this.recipeIngredients.removeIf(ing -> ing == Ingredient.EMPTY);
44+
}
3245
}
3346

3447
public IRecipe getRecipe() {
@@ -38,4 +51,14 @@ public IRecipe getRecipe() {
3851
public IRecipe getPreviousRecipe() {
3952
return previousRecipe;
4053
}
54+
55+
public boolean canIngredientApply(int index, ItemStack stack) {
56+
if (this.recipeIngredients.isEmpty()) return false;
57+
if (index < 0 || index >= this.recipeIngredients.size()) {
58+
GTLog.logger.warn("Compacted index \"{}\" is out of bounds for list size \"{}\"", index,
59+
this.recipeIngredients.size());
60+
return false;
61+
}
62+
return this.recipeIngredients.get(index).apply(stack);
63+
}
4164
}

src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeLogic.java

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import gregtech.api.items.toolitem.ItemGTToolbelt;
44
import gregtech.api.util.DummyContainer;
5-
import gregtech.api.util.GTLog;
65
import gregtech.api.util.GTTransferUtils;
76
import gregtech.api.util.GTUtility;
87
import 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

Comments
 (0)