diff options
Diffstat (limited to 'src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java')
-rw-r--r-- | src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java b/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java index 25f4b35ab3..07353882ec 100644 --- a/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java +++ b/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java @@ -17,6 +17,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagString; +import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.fluids.FluidStack; import javax.annotation.Nonnull; @@ -72,11 +73,11 @@ public class GT_AssemblyLineUtils { */ @Nonnull public static LookupResult findAssemblyLineRecipeFromDataStick(ItemStack aDataStick, boolean aReturnBuiltRecipe) { - if (!isItemDataStick(aDataStick)) { + if (!isItemDataStick(aDataStick) || doesDataStickHaveOutput(aDataStick)) { return LookupResultType.INVALID_STICK.getResult(); } List<ItemStack> aInputs = new ArrayList<>(15); - ItemStack aOutput = null; + ItemStack aOutput = getDataStickOutput(aDataStick); List<List<ItemStack>> mOreDictAlt = new ArrayList<>(15); List<FluidStack> aFluidInputs = new ArrayList<>(4); @@ -88,9 +89,9 @@ public class GT_AssemblyLineUtils { //Get From Cache if (doesDataStickHaveRecipeHash(aDataStick)) { GT_Recipe_AssemblyLine aRecipeFromCache = sRecipeCacheByRecipeHash.get(getHashFromDataStack(aDataStick)); - if (aRecipeFromCache != null) { + if (aRecipeFromCache != null && GT_Utility.areStacksEqual(aOutput, aRecipeFromCache.mOutput)) { return LookupResultType.VALID_STACK_AND_VALID_HASH.getResult(aRecipeFromCache); - } + } // else: no cache, or the old recipe run into a hash collision with a different new recipe } for (int i = 0; i < 15; i++) { @@ -133,7 +134,6 @@ public class GT_AssemblyLineUtils { GT_FML_LOGGER.info("Fluid " + i + " " + tLoaded.getUnlocalizedName()); } } - 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(); } @@ -227,14 +227,29 @@ public class GT_AssemblyLineUtils { } /** + * @param aRecipe - The recipe to add to internal caches + * @throws IllegalArgumentException if given recipe collide with any existing recipe in the cache + */ + public static void addRecipeToCache(GT_Recipe_AssemblyLine aRecipe) { + if (aRecipe != null) { + String aHash = "Hash." + aRecipe.getPersistentHash(); + GT_Recipe_AssemblyLine existing = sRecipeCacheByOutput.put(new GT_ItemStack(aRecipe.mOutput), aRecipe); + if (existing != null) + throw new IllegalArgumentException("Duplicate assline recipe for " + aRecipe.mOutput); + existing = sRecipeCacheByRecipeHash.put(aHash, aRecipe); + if (existing != null && !existing.equals(aRecipe)) + throw new IllegalArgumentException("Recipe hash collision for " + aRecipe + " and " + existing); + } + } + + /** * @param aHash - Recipe hash String, may be null but will just be treated as invalid. * @return Is this Recipe Hash String valid? */ public static boolean isValidHash(String aHash) { if (aHash != null && aHash.length() > 0) { - if (!aHash.equals("Invalid.Recipe.Hash") && aHash.contains("Hash.")) { - return true; - } + // persistent hash can never be 0 + return !aHash.equals("Invalid.Recipe.Hash") && !aHash.equals("Hash.0"); } return false; } @@ -283,7 +298,7 @@ public class GT_AssemblyLineUtils { public static boolean doesDataStickHaveRecipeHash(ItemStack aDataStick) { if (isItemDataStick(aDataStick) && aDataStick.hasTagCompound()) { NBTTagCompound aNBT = aDataStick.getTagCompound(); - if (aNBT.hasKey("Data.Recipe.Hash")) { + if (aNBT.hasKey("Data.Recipe.Hash") && aNBT.getString("Data.Recipe.Hash").equals("Hash.0")) { return true; } } @@ -304,17 +319,18 @@ public class GT_AssemblyLineUtils { } /** - * @param aDataStick - The Data Stick to procces. + * @param aDataStick - The Data Stick to process. * @return The stored Recipe Hash String on the Data Stick, will return an invalid Hash if one is not found. <p> - * Check with isValidHash(). <p> + * The hash will be guaranteed to pass isValidHash(). <p> * Will not return Null. */ public static String getHashFromDataStack(ItemStack aDataStick) { if (isItemDataStick(aDataStick) && aDataStick.hasTagCompound()) { NBTTagCompound aNBT = aDataStick.getTagCompound(); - if (aNBT.hasKey("Data.Recipe.Hash")) { - return aNBT.getString("Data.Recipe.Hash"); - + if (aNBT.hasKey("Data.Recipe.Hash", NBT.TAG_STRING)) { + String hash = aNBT.getString("Data.Recipe.Hash"); + if (isValidHash(hash)) + return hash; } } return "Invalid.Recipe.Hash"; |