diff options
author | Glease <4586901+Glease@users.noreply.github.com> | 2021-12-09 03:26:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-08 20:26:50 +0100 |
commit | 0b70f6cd6ff8b224600fc8e47816912aa8fce223 (patch) | |
tree | 65e624cdfd3b3ccfcceb5f93038b0604b713dd66 /src/main/java/gregtech/api | |
parent | 8ad71b7500941a1e923c87d24b0d31b4b99c8210 (diff) | |
download | GT5-Unofficial-0b70f6cd6ff8b224600fc8e47816912aa8fce223.tar.gz GT5-Unofficial-0b70f6cd6ff8b224600fc8e47816912aa8fce223.tar.bz2 GT5-Unofficial-0b70f6cd6ff8b224600fc8e47816912aa8fce223.zip |
Fix assline oredict support (#786)
* Fix assline oredict support
* Fix assline hash generation depending on transient states
Diffstat (limited to 'src/main/java/gregtech/api')
-rw-r--r-- | src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java | 176 | ||||
-rw-r--r-- | src/main/java/gregtech/api/util/GT_Recipe.java | 15 | ||||
-rw-r--r-- | src/main/java/gregtech/api/util/GT_Utility.java | 52 |
3 files changed, 182 insertions, 61 deletions
diff --git a/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java b/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java index 62238a8112..25f4b35ab3 100644 --- a/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java +++ b/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java @@ -2,8 +2,11 @@ package gregtech.api.util; import static gregtech.GT_Mod.GT_FML_LOGGER; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.List; +import java.util.Objects; import cpw.mods.fml.common.FMLCommonHandler; import gregtech.api.enums.GT_Values; @@ -16,6 +19,8 @@ import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagString; import net.minecraftforge.fluids.FluidStack; +import javax.annotation.Nonnull; + public class GT_AssemblyLineUtils { /** @@ -34,21 +39,19 @@ public class GT_AssemblyLineUtils { * @param aDataStick - The DataStick to process * @return Is this DataStick now valid with a current recipe? */ - public static boolean processDataStick(ItemStack aDataStick) { + public static GT_Recipe_AssemblyLine processDataStick(ItemStack aDataStick) { if (!isItemDataStick(aDataStick)) { - return false; + return null; } if (doesDataStickNeedUpdate(aDataStick)) { ItemStack aStickOutput = getDataStickOutput(aDataStick); if (aStickOutput != null) { GT_Recipe_AssemblyLine aIntendedRecipe = findAssemblyLineRecipeByOutput(aStickOutput); - if (aIntendedRecipe != null) { - return setAssemblyLineRecipeOnDataStick(aDataStick, aIntendedRecipe); - } + if (aIntendedRecipe != null && setAssemblyLineRecipeOnDataStick(aDataStick, aIntendedRecipe)) + return aIntendedRecipe; } - return false; } - return true; + return null; } @@ -58,7 +61,7 @@ public class GT_AssemblyLineUtils { * @return The GT_Recipe_AssemblyLine recipe contained on the DataStick, if any. */ public static GT_Recipe_AssemblyLine findAssemblyLineRecipeFromDataStick(ItemStack aDataStick) { - return findAssemblyLineRecipeFromDataStick(aDataStick, false); + return findAssemblyLineRecipeFromDataStick(aDataStick, false).getRecipe(); } /** @@ -67,24 +70,26 @@ public class GT_AssemblyLineUtils { * @param aReturnBuiltRecipe - Do we return a GT_Recipe_AssemblyLine built from the data on the Data Stick instead of searching the Recipe Map? * @return The GT_Recipe_AssemblyLine recipe contained on the DataStick, if any. */ - public static GT_Recipe_AssemblyLine findAssemblyLineRecipeFromDataStick(ItemStack aDataStick, boolean aReturnBuiltRecipe) { + @Nonnull + public static LookupResult findAssemblyLineRecipeFromDataStick(ItemStack aDataStick, boolean aReturnBuiltRecipe) { if (!isItemDataStick(aDataStick)) { - return null; + return LookupResultType.INVALID_STICK.getResult(); } - ItemStack[] aInputs = new ItemStack[15]; - ItemStack[] aOutputs = new ItemStack[1]; - FluidStack[] aFluidInputs = new FluidStack[4]; + List<ItemStack> aInputs = new ArrayList<>(15); + ItemStack aOutput = null; + List<List<ItemStack>> mOreDictAlt = new ArrayList<>(15); + List<FluidStack> aFluidInputs = new ArrayList<>(4); NBTTagCompound aTag = aDataStick.getTagCompound(); if (aTag == null) { - return null; + return LookupResultType.INVALID_STICK.getResult(); } //Get From Cache if (doesDataStickHaveRecipeHash(aDataStick)) { GT_Recipe_AssemblyLine aRecipeFromCache = sRecipeCacheByRecipeHash.get(getHashFromDataStack(aDataStick)); if (aRecipeFromCache != null) { - return aRecipeFromCache; + return LookupResultType.VALID_STACK_AND_VALID_HASH.getResult(aRecipeFromCache); } } @@ -94,33 +99,25 @@ public class GT_AssemblyLineUtils { continue; } - boolean flag = true; - if (count > 0) { - for (int j = 0; j < count; j++) { - aInputs[i] = GT_Utility.loadItem(aTag, "a" + i + ":" + j); - if (aInputs[i] == null) { - continue; - } - if (GT_Values.D1) { - GT_FML_LOGGER.info("Item " + i + " : " + aInputs[i].getUnlocalizedName()); - } - flag = false; - break; - } - } - if (flag) { - aInputs[i] = GT_Utility.loadItem(aTag, "" + i); - if (aInputs[i] == null) { - flag = false; + List<ItemStack> tAltCurrent = new ArrayList<>(); + for (int j = 0; j < count; j++) { + ItemStack tLoaded = GT_Utility.loadItem(aTag, "a" + i + ":" + j); + if (tLoaded == null) { continue; } + tAltCurrent.add(tLoaded); if (GT_Values.D1) { - GT_FML_LOGGER.info("Item " + i + " : " + aInputs[i].getUnlocalizedName()); + GT_FML_LOGGER.info("Item Alt " + i + " : " + tLoaded.getUnlocalizedName()); } - flag = false; } + mOreDictAlt.add(tAltCurrent); + ItemStack tLoaded = GT_Utility.loadItem(aTag, "" + i); + if (tLoaded == null) { + continue; + } + aInputs.add(tLoaded); if (GT_Values.D1) { - GT_FML_LOGGER.info(i + (flag ? " not accepted" : " accepted")); + GT_FML_LOGGER.info("Item " + i + " : " + tLoaded.getUnlocalizedName()); } } @@ -129,18 +126,16 @@ public class GT_AssemblyLineUtils { } for (int i = 0; i < 4; i++) { if (!aTag.hasKey("f" + i)) continue; - aFluidInputs[i] = GT_Utility.loadFluid(aTag, "f" + i); - if (aFluidInputs[i] == null) continue; + FluidStack tLoaded = GT_Utility.loadFluid(aTag, "f" + i); + if (tLoaded == null) continue; + aFluidInputs.add(tLoaded); if (GT_Values.D1) { - GT_FML_LOGGER.info("Fluid " + i + " " + aFluidInputs[i].getUnlocalizedName()); - } - if (GT_Values.D1) { - GT_FML_LOGGER.info(i + " accepted"); + GT_FML_LOGGER.info("Fluid " + i + " " + tLoaded.getUnlocalizedName()); } } - aOutputs = new ItemStack[]{GT_Utility.loadItem(aTag, "output")}; - if (!aTag.hasKey("output") || !aTag.hasKey("time") || aTag.getInteger("time") <= 0 || !aTag.hasKey("eu") || aOutputs[0] == null || !GT_Utility.isStackValid(aOutputs[0])) { - return null; + aOutput = GT_Utility.loadItem(aTag, "output"); + if (!aTag.hasKey("output") || !aTag.hasKey("time") || aTag.getInteger("time") <= 0 || !aTag.hasKey("eu") || !GT_Utility.isStackValid(aOutput)) { + return LookupResultType.INVALID_STICK.getResult(); } if (GT_Values.D1) { GT_FML_LOGGER.info("Found Data Stick recipe"); @@ -151,25 +146,41 @@ public class GT_AssemblyLineUtils { // Try build a recipe instance if (aReturnBuiltRecipe) { - GT_Recipe_AssemblyLine aBuiltRecipe = new GT_Recipe_AssemblyLine(null, 0, aInputs, aFluidInputs, aOutputs[0], aTime, aEU); - return aBuiltRecipe; + return LookupResultType.VALID_STACK_AND_VALID_HASH.getResult(new GT_Recipe_AssemblyLine(null, 0, aInputs.toArray(new ItemStack[0]), aFluidInputs.toArray(new FluidStack[0]), aOutput, aTime, aEU)); } for (GT_Recipe_AssemblyLine aRecipe : GT_Recipe.GT_Recipe_AssemblyLine.sAssemblylineRecipes) { - if (aRecipe.mEUt == aEU && aRecipe.mDuration == aTime) { - if (GT_Utility.areStacksEqual(aOutputs[0], aRecipe.mOutput, true)) { - if (Arrays.equals(aRecipe.mInputs, aInputs) && Arrays.equals(aRecipe.mFluidInputs, aFluidInputs)) { - // Cache it - String aRecipeHash = generateRecipeHash(aRecipe); - sRecipeCacheByRecipeHash.put(aRecipeHash, aRecipe); - sRecipeCacheByOutput.put(new GT_ItemStack(aRecipe.mOutput), aRecipe); - return aRecipe; - } - } + if (aRecipe.mEUt != aEU || aRecipe.mDuration != aTime) continue; + if (!GT_Utility.areStacksEqual(aOutput, aRecipe.mOutput, true)) continue; + if (!GT_Utility.areStackListsEqual(Arrays.asList(aRecipe.mInputs), aInputs, false, true)) continue; + if (!Objects.equals(Arrays.asList(aRecipe.mFluidInputs), aFluidInputs)) continue; + if (!areStacksEqual(aRecipe.mOreDictAlt, mOreDictAlt)) continue; + + // Cache it + String aRecipeHash = generateRecipeHash(aRecipe); + sRecipeCacheByRecipeHash.put(aRecipeHash, aRecipe); + sRecipeCacheByOutput.put(new GT_ItemStack(aRecipe.mOutput), aRecipe); + if (doesDataStickHaveRecipeHash(aDataStick)) { + String aStickHash = getHashFromDataStack(aDataStick); + if (aRecipeHash.equals(aStickHash)) + return LookupResultType.VALID_STACK_AND_VALID_HASH.getResult(aRecipe); } + return LookupResultType.VALID_STACK_AND_VALID_RECIPE.getResult(aRecipe); } - return null; + return LookupResultType.VALID_STACK_BUT_INVALID_RECIPE.getResult(); + } + + private static boolean areStacksEqual(ItemStack[][] lhs, List<List<ItemStack>> rhs) { + for (int i = 0; i < lhs.length; i++) { + if (!areStacksEqual(lhs[i], rhs.get(i))) + return false; + } + return true; + } + + private static boolean areStacksEqual(ItemStack[] lhs, List<ItemStack> rhs) { + return lhs == null ? rhs.isEmpty() : !rhs.isEmpty() && GT_Utility.areStackListsEqual(Arrays.asList(lhs), rhs, false, true); } @@ -210,7 +221,7 @@ public class GT_AssemblyLineUtils { public static String generateRecipeHash(GT_Recipe_AssemblyLine aRecipe) { String aHash = "Invalid.Recipe.Hash"; if (aRecipe != null) { - aHash = "Hash."+aRecipe.hashCode(); + aHash = "Hash."+aRecipe.getPersistentHash(); } return aHash; } @@ -343,7 +354,7 @@ public class GT_AssemblyLineUtils { String aHash = generateRecipeHash(aNewRecipe); if (GT_Values.D1) { - GT_Recipe_AssemblyLine aOldRecipe = findAssemblyLineRecipeFromDataStick(aDataStick, true); + GT_Recipe_AssemblyLine aOldRecipe = findAssemblyLineRecipeFromDataStick(aDataStick, true).recipe; GT_FML_LOGGER.info("Updating data stick: "+aDataStick.getDisplayName()+" | Old Recipe Hash: "+generateRecipeHash(aOldRecipe)+", New Recipe Hash: "+aHash); } @@ -437,4 +448,49 @@ public class GT_AssemblyLineUtils { return false; } + public enum LookupResultType { + INVALID_STICK(true), + VALID_STACK_BUT_INVALID_RECIPE(true), + VALID_STACK_AND_VALID_RECIPE(false), + VALID_STACK_AND_VALID_HASH(false); + + private final boolean recipeNull; + private LookupResult singletonResult; + + LookupResultType(boolean recipeNull) { + this.recipeNull = recipeNull; + } + + public LookupResult getResult() { + if (!recipeNull) + throw new IllegalArgumentException("This result type require a nonnull recipe"); + if (singletonResult == null) + singletonResult = new LookupResult(null, this); + return singletonResult; + } + + public LookupResult getResult(GT_Recipe_AssemblyLine recipe) { + if ((recipe == null) != recipeNull) + throw new IllegalArgumentException("This result type does not allow given input"); + return new LookupResult(recipe, this); + } + } + + public static class LookupResult { + private final GT_Recipe_AssemblyLine recipe; + private final LookupResultType type; + + LookupResult(GT_Recipe_AssemblyLine recipe, LookupResultType type) { + this.recipe = recipe; + this.type = type; + } + + public GT_Recipe_AssemblyLine getRecipe() { + return recipe; + } + + public LookupResultType getType() { + return type; + } + } } diff --git a/src/main/java/gregtech/api/util/GT_Recipe.java b/src/main/java/gregtech/api/util/GT_Recipe.java index 7d475b4dfd..1b54e10795 100644 --- a/src/main/java/gregtech/api/util/GT_Recipe.java +++ b/src/main/java/gregtech/api/util/GT_Recipe.java @@ -565,6 +565,7 @@ public class GT_Recipe implements Comparable<GT_Recipe> { public int mDuration; public int mEUt; public ItemStack[][] mOreDictAlt; + private int mPersistentHash; public GT_Recipe_AssemblyLine(ItemStack aResearchItem, int aResearchTime, ItemStack[] aInputs, FluidStack[] aFluidInputs, ItemStack aOutput, int aDuration, int aEUt) { this(aResearchItem, aResearchTime, aInputs, aFluidInputs, aOutput, aDuration, aEUt, new ItemStack[aInputs.length][]); @@ -658,7 +659,19 @@ public class GT_Recipe implements Comparable<GT_Recipe> { && this.mEUt == other.mEUt && this.mResearchTime == other.mResearchTime; } - + + public int getPersistentHash() { + return mPersistentHash; + } + + public void setPersistentHash(int aPersistentHash) { + if (this.mPersistentHash != 0) + throw new IllegalStateException("Cannot set persistent hash twice!"); + if (aPersistentHash == 0) + this.mPersistentHash = 1; + else + this.mPersistentHash = aPersistentHash; + } } public static class GT_Recipe_Map { diff --git a/src/main/java/gregtech/api/util/GT_Utility.java b/src/main/java/gregtech/api/util/GT_Utility.java index 3b0ec94a92..07e5fcec73 100644 --- a/src/main/java/gregtech/api/util/GT_Utility.java +++ b/src/main/java/gregtech/api/util/GT_Utility.java @@ -9,6 +9,7 @@ import com.gtnewhorizon.structurelib.alignment.IAlignment; import com.gtnewhorizon.structurelib.alignment.IAlignmentProvider; import com.mojang.authlib.GameProfile; import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.registry.GameRegistry; import gregtech.api.GregTech_API; import gregtech.api.damagesources.GT_DamageSources; import gregtech.api.damagesources.GT_DamageSources.DamageSourceHotItem; @@ -965,6 +966,30 @@ public class GT_Utility { && (Items.feather.getDamage(aStack1) == Items.feather.getDamage(aStack2) || Items.feather.getDamage(aStack1) == W || Items.feather.getDamage(aStack2) == W); } + /** + * Treat both null list, or both null item stack at same list position as equal. + * + * Since ItemStack doesn't override equals and hashCode, you cannot just use Objects.equals + */ + public static boolean areStackListsEqual(List<ItemStack> lhs, List<ItemStack> rhs, boolean ignoreStackSize, boolean ignoreNBT) { + if (lhs == null) return rhs == null; + if (rhs == null) return false; + if (lhs.size() != rhs.size()) return false; + for (Iterator<ItemStack> it1 = lhs.iterator(), it2 = rhs.iterator(); it1.hasNext() && it2.hasNext(); ) { + if (!areStacksEqualExtended(it1.next(), it2.next(), ignoreStackSize, ignoreNBT)) + return false; + } + return true; + } + + private static boolean areStacksEqualExtended(ItemStack lhs, ItemStack rhs, boolean ignoreStackSize, boolean ignoreNBT) { + if (lhs == null) return rhs == null; + if (rhs == null) return false; + return lhs.getItem() == rhs.getItem() && + (ignoreNBT || Objects.equals(lhs.stackTagCompound, rhs.stackTagCompound)) && + (ignoreStackSize || lhs.stackSize == rhs.stackSize); + } + public static boolean areUnificationsEqual(ItemStack aStack1, ItemStack aStack2) { return areUnificationsEqual(aStack1, aStack2, false); } @@ -3014,4 +3039,31 @@ public class GT_Utility { public static int clamp(int val, int lo, int hi) { return val > hi ? hi : val < lo ? lo : val; } + + /** + * Hash an item stack for the purpose of storing hash across launches + */ + public static int persistentHash(ItemStack aStack, boolean aUseStackSize, boolean aUseNBT) { + if (aStack == null) + return 0; + int result = Objects.hashCode(GameRegistry.findUniqueIdentifierFor(aStack.getItem())); + result = result * 31 + Items.feather.getDamage(aStack); + + if (aUseStackSize) result = result * 31 + aStack.stackSize; + if (aUseNBT) result = result * 31 + Objects.hashCode(aStack.stackTagCompound); + return result; + } + + /** + * Hash an item stack for the purpose of storing hash across launches + */ + public static int persistentHash(FluidStack aStack, boolean aUseStackSize, boolean aUseNBT) { + if (aStack == null) + return 0; + int base = Objects.hashCode(aStack.getFluid().getName()); + + if (aUseStackSize) base = base * 31 + aStack.amount; + if (aUseNBT) base = base * 31 + Objects.hashCode(aStack.tag); + return base; + } } |