diff options
author | GlodBlock <1356392126@qq.com> | 2021-11-22 12:52:13 +0800 |
---|---|---|
committer | GlodBlock <1356392126@qq.com> | 2021-11-22 12:52:13 +0800 |
commit | 95f17298975372f5f09fbaa4c8c40502fcc8acdb (patch) | |
tree | 71484ac142d90828f0a2e2d58ce712e0ee77d4c3 /src | |
parent | 875afd0dc22915795ec74279f1708d79cab0cac8 (diff) | |
parent | 9d559f268a78564be4c49e35fc003346d6ae2a7c (diff) | |
download | GT5-Unofficial-95f17298975372f5f09fbaa4c8c40502fcc8acdb.tar.gz GT5-Unofficial-95f17298975372f5f09fbaa4c8c40502fcc8acdb.tar.bz2 GT5-Unofficial-95f17298975372f5f09fbaa4c8c40502fcc8acdb.zip |
Merge branch 'experimental' of https://github.com/GTNewHorizons/GT5-Unofficial into tankchest-rework
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/gregtech/api/util/GT_Recipe.java | 190 |
1 files changed, 103 insertions, 87 deletions
diff --git a/src/main/java/gregtech/api/util/GT_Recipe.java b/src/main/java/gregtech/api/util/GT_Recipe.java index 7b74f95a6b..c0ea06af07 100644 --- a/src/main/java/gregtech/api/util/GT_Recipe.java +++ b/src/main/java/gregtech/api/util/GT_Recipe.java @@ -376,115 +376,131 @@ public class GT_Recipe implements Comparable<GT_Recipe> { return isRecipeInputEqual(aDecreaseStacksizeBySuccess, false, aFluidInputs, aInputs); } + /** + * Okay, did some code archeology to figure out what's going on here. + * + * <p>This variable was added in + * <a href=https://github.com/GTNewHorizons/GT5-Unofficial/commit/9959ab7443982a19ad329bca424ab515493432e9>this commit,</a> + * in order to fix the issues mentioned in + * <a href=https://github.com/GTNewHorizons/GT5-Unofficial/pull/183>the PR</a>. + * + * <p>It looks like it controls checking NBT. At this point, since we are still using universal + * fluid cells which store their fluids in NBT, it probably will not be safe to disable the NBT + * checks in the near future. Data sticks may be another case. Anyway, we probably can't get rid + * of this without some significant changes to clean up recipe inputs. + */ public static boolean GTppRecipeHelper; + /** + * WARNING: Do not call this method with both {@code aDecreaseStacksizeBySuccess} and {@code aDontCheckStackSizes} set to {@code true}! + * You'll get weird behavior. + */ public boolean isRecipeInputEqual(boolean aDecreaseStacksizeBySuccess, boolean aDontCheckStackSizes, FluidStack[] aFluidInputs, ItemStack... aInputs) { - if (mInputs.length > 0 && aInputs == null) return false; if (mFluidInputs.length > 0 && aFluidInputs == null) return false; - int amt; - for (FluidStack tFluid : mFluidInputs) - if (tFluid != null) { - boolean temp = true; - amt = tFluid.amount; - for (FluidStack aFluid : aFluidInputs) - if (aFluid != null && aFluid.isFluidEqual(tFluid)) { - if (aDontCheckStackSizes) { - temp = false; - break; - } - amt -= aFluid.amount; - if (amt < 1) { - temp = false; - break; + + // We need to handle 0-size recipe inputs. These are for inputs that don't get consumed. + boolean inputFound; + int remainingCost; + + // Array tracking modified fluid amounts. For efficiency, we will lazily initialize this array. + // We use Integer so that we can have null as the default value, meaning unchanged. + Integer[] newFluidAmounts = null; + if (aFluidInputs != null) { + newFluidAmounts = new Integer[aFluidInputs.length]; + + for (FluidStack recipeFluidCost : mFluidInputs) { + if (recipeFluidCost != null) { + inputFound = false; + remainingCost = recipeFluidCost.amount; + + for (int i = 0; i < aFluidInputs.length; i++) { + FluidStack providedFluid = aFluidInputs[i]; + if (providedFluid != null && providedFluid.isFluidEqual(recipeFluidCost)) { + inputFound = true; + if (newFluidAmounts[i] == null) { + newFluidAmounts[i] = providedFluid.amount; + } + + if (aDontCheckStackSizes || newFluidAmounts[i] >= remainingCost) { + newFluidAmounts[i] -= remainingCost; + remainingCost = 0; + break; + } else { + remainingCost -= newFluidAmounts[i]; + newFluidAmounts[i] = 0; + } } } - if (temp) return false; + + if (remainingCost > 0 || !inputFound) { + // Cost not satisfied, or for non-consumed inputs, input not found. + return false; + } + } } + } - HashSet<Integer> isVisited = new HashSet<>(); - - for (ItemStack tStack : mInputs) { - ItemStack unified_tStack = GT_OreDictUnificator.get_nocopy(true, tStack); - if (unified_tStack != null) { - amt = tStack.stackSize; - boolean temp = true; - int it = 0; - for (ItemStack aStack : aInputs) { - it ++; - if (GT_OreDictUnificator.isInputStackEqual(aStack, unified_tStack) && !isVisited.contains(it)) { - isVisited.add(it); - if (GTppRecipeHelper) {//remove once the fix is out - if (GT_Utility.areStacksEqual(aStack, Ic2Items.FluidCell.copy(), true) || GT_Utility.areStacksEqual(aStack, ItemList.Tool_DataStick.get(1L), true) || GT_Utility.areStacksEqual(aStack, ItemList.Tool_DataOrb.get(1L), true)) { - if (!GT_Utility.areStacksEqual(aStack, tStack, false)) - continue; + // Array tracking modified item stack sizes. For efficiency, we will lazily initialize this array. + // We use Integer so that we can have null as the default value, meaning unchanged. + Integer[] newItemAmounts = null; + if (aInputs != null) { + newItemAmounts = new Integer[aInputs.length]; + + for (ItemStack recipeItemCost : mInputs) { + ItemStack unifiedItemCost = GT_OreDictUnificator.get_nocopy(true, recipeItemCost); + if (unifiedItemCost != null) { + inputFound = false; + remainingCost = recipeItemCost.stackSize; + + for (int i = 0; i < aInputs.length; i++) { + ItemStack providedItem = aInputs[i]; + if (GT_OreDictUnificator.isInputStackEqual(providedItem, unifiedItemCost)) { + if (GTppRecipeHelper) { // Please see JavaDoc on GTppRecipeHelper for why this is here. + if (GT_Utility.areStacksEqual(providedItem, Ic2Items.FluidCell.copy(), true) || GT_Utility.areStacksEqual(providedItem, ItemList.Tool_DataStick.get(1L), true) || GT_Utility.areStacksEqual(providedItem, ItemList.Tool_DataOrb.get(1L), true)) { + if (!GT_Utility.areStacksEqual(providedItem, recipeItemCost, false)) + continue; + } + } + + inputFound = true; + if (newItemAmounts[i] == null) { + newItemAmounts[i] = providedItem.stackSize; + } + + if (aDontCheckStackSizes || newItemAmounts[i] >= remainingCost) { + newItemAmounts[i] -= remainingCost; + remainingCost = 0; + break; + } else { + remainingCost -= newItemAmounts[i]; + newItemAmounts[i] = 0; } } - if (aDontCheckStackSizes) { - temp = false; - break; - } - amt -= aStack.stackSize; - if (amt < 1) { - temp = false; - break; - } + } + + if (remainingCost > 0 || !inputFound) { + // Cost not satisfied, or for non-consumed inputs, input not found. + return false; } } - if (temp) return false; } } + if (aDecreaseStacksizeBySuccess) { + // Copy modified amounts into the input stacks. if (aFluidInputs != null) { - for (FluidStack tFluid : mFluidInputs) { - if (tFluid != null) { - amt = tFluid.amount; - for (FluidStack aFluid : aFluidInputs) { - if (aFluid != null && aFluid.isFluidEqual(tFluid)) { - if (aDontCheckStackSizes) { - aFluid.amount -= amt; - break; - } - if (aFluid.amount < amt) { - amt -= aFluid.amount; - aFluid.amount = 0; - } else { - aFluid.amount -= amt; - amt = 0; - break; - } - } - } + for (int i = 0; i < aFluidInputs.length; i++) { + if (newFluidAmounts[i] != null) { + aFluidInputs[i].amount = newFluidAmounts[i]; } } } if (aInputs != null) { - for (ItemStack tStack : mInputs) { - if (tStack != null) { - amt = tStack.stackSize; - for (ItemStack aStack : aInputs) { - if ((GT_Utility.areUnificationsEqual(aStack, tStack, true) || GT_Utility.areUnificationsEqual(GT_OreDictUnificator.get(false, aStack), tStack, true))) { - if (GTppRecipeHelper) { - if (GT_Utility.areStacksEqual(aStack, Ic2Items.FluidCell.copy(), true) || GT_Utility.areStacksEqual(aStack, ItemList.Tool_DataStick.get(1L), true) || GT_Utility.areStacksEqual(aStack, ItemList.Tool_DataOrb.get(1L), true)) { - if (!GT_Utility.areStacksEqual(aStack, tStack, false)) - continue; - } - } - if (aDontCheckStackSizes){ - aStack.stackSize -= amt; - break; - } - if (aStack.stackSize < amt){ - amt -= aStack.stackSize; - aStack.stackSize = 0; - }else{ - aStack.stackSize -= amt; - amt = 0; - break; - } - } - } + for (int i = 0; i < aInputs.length; i++) { + if (newItemAmounts[i] != null) { + aInputs[i].stackSize = newItemAmounts[i]; } } } |