aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorMartin Robertz <dream-master@gmx.net>2021-11-21 15:03:21 +0100
committerGitHub <noreply@github.com>2021-11-21 15:03:21 +0100
commit9d559f268a78564be4c49e35fc003346d6ae2a7c (patch)
treee17373eed22b96a8f6632171a353ffb3690548ed /src/main
parent81f0b4801dfb0bd2d2a863e7a60f27af0daba463 (diff)
parenta4d6e33b06059629b3fb86465c9c8c146407fb82 (diff)
downloadGT5-Unofficial-9d559f268a78564be4c49e35fc003346d6ae2a7c.tar.gz
GT5-Unofficial-9d559f268a78564be4c49e35fc003346d6ae2a7c.tar.bz2
GT5-Unofficial-9d559f268a78564be4c49e35fc003346d6ae2a7c.zip
Merge pull request #746 from D-Cysteine/experimental
Fix recipe input check
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/gregtech/api/util/GT_Recipe.java190
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];
}
}
}