diff options
author | Alkalus <3060479+draknyte1@users.noreply.github.com> | 2021-11-16 13:24:56 +0000 |
---|---|---|
committer | Alkalus <3060479+draknyte1@users.noreply.github.com> | 2021-11-16 13:24:56 +0000 |
commit | 4e2689d152f50a90e8c32c1229038d6e75e288ce (patch) | |
tree | 74b8987b253497fb1a0f5f6623f5521cc93e7ea1 /src | |
parent | 53e01ab8e1a7a75453f06f7fea5f5c76c09d03a3 (diff) | |
download | GT5-Unofficial-4e2689d152f50a90e8c32c1229038d6e75e288ce.tar.gz GT5-Unofficial-4e2689d152f50a90e8c32c1229038d6e75e288ce.tar.bz2 GT5-Unofficial-4e2689d152f50a90e8c32c1229038d6e75e288ce.zip |
Cleaned up the class a little.
Added Caching to Assembly Line output lookups.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java | 107 |
1 files changed, 64 insertions, 43 deletions
diff --git a/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java b/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java index 17a7583ebe..b423bfa91e 100644 --- a/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java +++ b/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java @@ -2,9 +2,12 @@ package gregtech.api.util; import static gregtech.GT_Mod.GT_FML_LOGGER; +import java.util.HashMap; + import cpw.mods.fml.common.FMLCommonHandler; import gregtech.api.enums.GT_Values; import gregtech.api.enums.ItemList; +import gregtech.api.objects.GT_ItemStack; import gregtech.api.util.GT_Recipe.GT_Recipe_AssemblyLine; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -16,6 +19,11 @@ import scala.actors.threadpool.Arrays; public class GT_AssemblyLineUtils { /** + * A cache of + */ + private static HashMap<GT_ItemStack, GT_Recipe_AssemblyLine> sRecipeCacheByOutput = new HashMap<GT_ItemStack, GT_Recipe_AssemblyLine>(); + + /** * Checks the DataStick for deprecated/invalid recipes, updating them as required. * @param aDataStick - The DataStick to process * @return Is this DataStick now valid with a current recipe? @@ -140,27 +148,60 @@ public class GT_AssemblyLineUtils { * @return First found GT_Recipe_AssemblyLine with matching output. */ public static GT_Recipe_AssemblyLine findAssemblyLineRecipeByOutput(ItemStack aOutput) { + if (aOutput == null) { + return null; + } + + // Check the cache + GT_ItemStack aCacheStack = new GT_ItemStack(aOutput); + GT_Recipe_AssemblyLine aRecipeFromCache = sRecipeCacheByOutput.get(aCacheStack); + if (aRecipeFromCache != null) { + return aRecipeFromCache; + } + + // Iterate all recipes and return the first matching based on Output. for (GT_Recipe_AssemblyLine aRecipe : GT_Recipe.GT_Recipe_AssemblyLine.sAssemblylineRecipes) { ItemStack aRecipeOutput = aRecipe.mOutput; if (GT_Utility.areStacksEqual(aRecipeOutput, aOutput)) { + // Cache it to prevent future iterations of all recipes + sRecipeCacheByOutput.put(aCacheStack, aRecipe); return aRecipe; } } return null; } + /** + * @param aRecipe - The recipe to generate a Recipe Hash String from. + * @return The Recipe Hash String. + */ + public static String generateRecipeHash(GT_Recipe_AssemblyLine aRecipe) { + String aHash = "Invalid.Recipe.Hash"; + if (aRecipe != null) { + aHash = "Hash."+aRecipe.hashCode(); + } + return aHash; + } /** - * Get the Output ItemStack from a Data Stick. - * @param aDataStick - The Data Stick to check. - * @return Output ItemStack contained on the Data Stick. + * @param aHash - Recipe hash String, may be null but will just be treated as invalid. + * @return Is this Recipe Hash String valid? */ - public static ItemStack getDataStickOutput(ItemStack aDataStick) { - if (doesDataStickHaveOutput(aDataStick)) { - ItemStack aOutput = GT_Utility.loadItem(aDataStick.getTagCompound(), "output"); - return aOutput; + public static boolean isValidHash(String aHash) { + if (aHash != null && aHash.length() > 0) { + if (!aHash.equals("Invalid.Recipe.Hash") && aHash.contains("Hash.")) { + return true; + } } - return null; + return false; + } + + /** + * @param aStack - The ItemStack to check. + * @return Is this ItemStack a Data Stick? + */ + public static boolean isItemDataStick(ItemStack aStack) { + return GT_Utility.isStackValid(aStack) && ItemList.Tool_DataStick.isStackEqual(aStack, false, true); } /** @@ -207,6 +248,19 @@ public class GT_AssemblyLineUtils { } /** + * Get the Output ItemStack from a Data Stick. + * @param aDataStick - The Data Stick to check. + * @return Output ItemStack contained on the Data Stick. + */ + public static ItemStack getDataStickOutput(ItemStack aDataStick) { + if (doesDataStickHaveOutput(aDataStick)) { + ItemStack aOutput = GT_Utility.loadItem(aDataStick.getTagCompound(), "output"); + return aOutput; + } + return null; + } + + /** * @param aDataStick - The Data Stick to procces. * @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> @@ -229,7 +283,7 @@ public class GT_AssemblyLineUtils { * @param aRecipeHash - The Recipe Hash String to update with. * @return Did we update the Recipe Hash String on the Data Stick? */ - public static boolean writeRecipeHashToDataStick(ItemStack aDataStick, String aRecipeHash) { + public static boolean setRecipeHashOnDataStick(ItemStack aDataStick, String aRecipeHash) { if (isItemDataStick(aDataStick) && aDataStick.hasTagCompound()) { NBTTagCompound aNBT = aDataStick.getTagCompound(); aNBT.setString("Data.Recipe.Hash", aRecipeHash); @@ -240,39 +294,6 @@ public class GT_AssemblyLineUtils { } /** - * @param aRecipe - The recipe to generate a Recipe Hash String from. - * @return The Recipe Hash String. - */ - public static String generateRecipeHash(GT_Recipe_AssemblyLine aRecipe) { - String aHash = "Invalid.Recipe.Hash"; - if (aRecipe != null) { - aHash = "Hash."+aRecipe.hashCode(); - } - return aHash; - } - - /** - * @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; - } - } - return false; - } - - /** - * @param aStack - The ItemStack to check. - * @return Is this ItemStack a Data Stick? - */ - public static boolean isItemDataStick(ItemStack aStack) { - return GT_Utility.isStackValid(aStack) && ItemList.Tool_DataStick.isStackEqual(aStack, false, true); - } - - /** * * @param aDataStick - The Data Stick to update. * @param aNewRecipe - The New GT_Recipe_AssemblyLine recipe to update it with. @@ -365,7 +386,7 @@ public class GT_AssemblyLineUtils { tNBT.setTag("pages", tNBTList); aDataStick.setTagCompound(tNBT); // Set recipe hash - writeRecipeHashToDataStick(aDataStick, generateRecipeHash(aNewRecipe)); + setRecipeHashOnDataStick(aDataStick, generateRecipeHash(aNewRecipe)); return true; } return false; |