diff options
author | Martin Robertz <dream-master@gmx.net> | 2021-11-20 09:42:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-20 09:42:13 +0100 |
commit | 36ab4741d560e11bc906d4173445a0a06a5b08da (patch) | |
tree | 0475fcc0f734f550b857660dd9958e4ee174f01e /src/main | |
parent | 602d32e4939ae74e99d683062c9d847d997fb9ad (diff) | |
parent | 5c7319323b5cc3601bd3fa43ec373ca40bb54a5d (diff) | |
download | GT5-Unofficial-36ab4741d560e11bc906d4173445a0a06a5b08da.tar.gz GT5-Unofficial-36ab4741d560e11bc906d4173445a0a06a5b08da.tar.bz2 GT5-Unofficial-36ab4741d560e11bc906d4173445a0a06a5b08da.zip |
Merge pull request #735 from GTNewHorizons/AssLine-Data-Stick-Fix
Assembly line data stick fix
Diffstat (limited to 'src/main')
4 files changed, 633 insertions, 188 deletions
diff --git a/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java b/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java new file mode 100644 index 0000000000..f35f1962d1 --- /dev/null +++ b/src/main/java/gregtech/api/util/GT_AssemblyLineUtils.java @@ -0,0 +1,440 @@ +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; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.nbt.NBTTagString; +import net.minecraftforge.fluids.FluidStack; +import scala.actors.threadpool.Arrays; + +public class GT_AssemblyLineUtils { + + /** + * A cache of Recipes using the Output as Key. + */ + private static HashMap<GT_ItemStack, GT_Recipe_AssemblyLine> sRecipeCacheByOutput = new HashMap<GT_ItemStack, GT_Recipe_AssemblyLine>(); + /** + * A cache of Recipes using the Recipe Hash String as Key. + */ + private static HashMap<String, GT_Recipe_AssemblyLine> sRecipeCacheByRecipeHash = new HashMap<String, 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? + */ + public static boolean processDataStick(ItemStack aDataStick) { + if (!isItemDataStick(aDataStick)) { + return false; + } + if (doesDataStickNeedUpdate(aDataStick)) { + ItemStack aStickOutput = getDataStickOutput(aDataStick); + if (aStickOutput != null) { + GT_Recipe_AssemblyLine aIntendedRecipe = findAssemblyLineRecipeByOutput(aStickOutput); + if (aIntendedRecipe != null) { + return setAssemblyLineRecipeOnDataStick(aDataStick, aIntendedRecipe); + } + } + return false; + } + return true; + } + + + /** + * Finds an Assembly Line recipe from a DataStick. + * @param aDataStick - The DataStick to check. + * @return The GT_Recipe_AssemblyLine recipe contained on the DataStick, if any. + */ + public static GT_Recipe_AssemblyLine findAssemblyLineRecipeFromDataStick(ItemStack aDataStick) { + return findAssemblyLineRecipeFromDataStick(aDataStick, false); + } + + /** + * Finds an Assembly Line recipe from a DataStick. + * @param aDataStick - The DataStick to check. + * @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) { + if (!isItemDataStick(aDataStick)) { + return null; + } + ItemStack[] aInputs = new ItemStack[15]; + ItemStack[] aOutputs = new ItemStack[1]; + FluidStack[] aFluidInputs = new FluidStack[4]; + + NBTTagCompound aTag = aDataStick.getTagCompound(); + if (aTag == null) { + return null; + } + + //Get From Cache + if (doesDataStickHaveRecipeHash(aDataStick)) { + GT_Recipe_AssemblyLine aRecipeFromCache = sRecipeCacheByRecipeHash.get(getHashFromDataStack(aDataStick)); + if (aRecipeFromCache != null) { + return aRecipeFromCache; + } + } + + for (int i = 0; i < 15; i++) { + int count = aTag.getInteger("a" + i); + if (!aTag.hasKey("" + i) && count <= 0) { + 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; + continue; + } + if (GT_Values.D1) { + GT_FML_LOGGER.info("Item " + i + " : " + aInputs[i].getUnlocalizedName()); + } + flag = false; + } + if (GT_Values.D1) { + GT_FML_LOGGER.info(i + (flag ? " not accepted" : " accepted")); + } + } + + if (GT_Values.D1) { + GT_FML_LOGGER.info("All Items done, start fluid check"); + } + 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; + if (GT_Values.D1) { + GT_FML_LOGGER.info("Fluid " + i + " " + aFluidInputs[i].getUnlocalizedName()); + } + if (GT_Values.D1) { + GT_FML_LOGGER.info(i + " accepted"); + } + } + 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; + } + if (GT_Values.D1) { + GT_FML_LOGGER.info("Found Data Stick recipe"); + } + + int aTime = aTag.getInteger("time"); + int aEU = aTag.getInteger("eu"); + + // 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; + } + + + 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; + } + } + } + } + return null; + } + + + /** + * Finds a GT_Recipe_AssemblyLine based on the expected output ItemStack. + * @param aOutput - The Output of a GT_Recipe_AssemblyLine. + * @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); + sRecipeCacheByRecipeHash.put(generateRecipeHash(aRecipe), 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; + } + + /** + * @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 check. + * @return Does this Data Stick have a valid output ItemStack? + */ + public static boolean doesDataStickHaveOutput(ItemStack aDataStick) { + if (isItemDataStick(aDataStick) && aDataStick.hasTagCompound() && aDataStick.getTagCompound().hasKey("output")) { + return true; + } + return false; + } + + /** + * @param aDataStick - The Data Stick to check. + * @return Does this Data Stick need recipe data updated. + */ + public static boolean doesDataStickNeedUpdate(ItemStack aDataStick) { + if (isItemDataStick(aDataStick) && doesDataStickHaveRecipeHash(aDataStick)) { + String aStickHash = getHashFromDataStack(aDataStick); + if (isValidHash(aStickHash) && doesDataStickHaveOutput(aDataStick)) { + ItemStack aStickOutput = getDataStickOutput(aDataStick); + GT_Recipe_AssemblyLine aIntendedRecipe = findAssemblyLineRecipeByOutput(aStickOutput); + if (aStickHash.equals(generateRecipeHash(aIntendedRecipe))) { + return false; + } + } + } + return true; + } + + /** + * @param aDataStick - The Data Stick to check. + * @return Does this have a Recipe Hash String at all? + */ + public static boolean doesDataStickHaveRecipeHash(ItemStack aDataStick) { + if (isItemDataStick(aDataStick) && aDataStick.hasTagCompound()) { + NBTTagCompound aNBT = aDataStick.getTagCompound(); + if (aNBT.hasKey("Data.Recipe.Hash")) { + return true; + } + } + return false; + } + + /** + * 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> + * 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"); + + } + } + return "Invalid.Recipe.Hash"; + } + + /** + * + * @param aDataStick - The Data Stick to update. + * @param aRecipeHash - The Recipe Hash String to update with. + * @return Did we update the Recipe Hash String on the Data Stick? + */ + public static boolean setRecipeHashOnDataStick(ItemStack aDataStick, String aRecipeHash) { + if (isItemDataStick(aDataStick) && aDataStick.hasTagCompound()) { + NBTTagCompound aNBT = aDataStick.getTagCompound(); + aNBT.setString("Data.Recipe.Hash", aRecipeHash); + aDataStick.setTagCompound(aNBT); + return true; + } + return false; + } + + /** + * + * @param aDataStick - The Data Stick to update. + * @param aNewRecipe - The New GT_Recipe_AssemblyLine recipe to update it with. + * @return Did we set the new recipe data & Recipe Hash String on the Data Stick? + */ + public static boolean setAssemblyLineRecipeOnDataStick(ItemStack aDataStick, GT_Recipe_AssemblyLine aNewRecipe) { + if (isItemDataStick(aDataStick)) { + String s = aNewRecipe.mOutput.getDisplayName(); + if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { + s = GT_Assemblyline_Server.lServerNames.get(aNewRecipe.mOutput.getDisplayName()); + if (s == null) { + s = aNewRecipe.mOutput.getDisplayName(); + } + } + + String aHash = generateRecipeHash(aNewRecipe); + if (GT_Values.D1) { + GT_Recipe_AssemblyLine aOldRecipe = findAssemblyLineRecipeFromDataStick(aDataStick, true); + GT_FML_LOGGER.info("Updating data stick: "+aDataStick.getDisplayName()+" | Old Recipe Hash: "+generateRecipeHash(aOldRecipe)+", New Recipe Hash: "+aHash); + } + + + //remove possible old NBTTagCompound + aDataStick.setTagCompound(new NBTTagCompound()); + if (GT_Values.D1) { + GT_Utility.ItemNBT.setBookTitle(aDataStick, s + " Construction Data ("+aHash+")"); + } + else { + GT_Utility.ItemNBT.setBookTitle(aDataStick, s + " Construction Data"); + } + + NBTTagCompound tNBT = aDataStick.getTagCompound(); + if (tNBT == null) { + tNBT = new NBTTagCompound(); + } + + tNBT.setTag("output", aNewRecipe.mOutput.writeToNBT(new NBTTagCompound())); + tNBT.setInteger("time", aNewRecipe.mDuration); + tNBT.setInteger("eu", aNewRecipe.mEUt); + for (int i = 0; i < aNewRecipe.mInputs.length; i++) { + tNBT.setTag("" + i, aNewRecipe.mInputs[i].writeToNBT(new NBTTagCompound())); + } + for (int i = 0; i < aNewRecipe.mOreDictAlt.length; i++) { + if (aNewRecipe.mOreDictAlt[i] != null && aNewRecipe.mOreDictAlt[i].length > 0) { + tNBT.setInteger("a" + i, aNewRecipe.mOreDictAlt[i].length); + for (int j = 0; j < aNewRecipe.mOreDictAlt[i].length; j++) { + tNBT.setTag("a" + i + ":" + j, aNewRecipe.mOreDictAlt[i][j].writeToNBT(new NBTTagCompound())); + } + } + } + for (int i = 0; i < aNewRecipe.mFluidInputs.length; i++) { + tNBT.setTag("f" + i, aNewRecipe.mFluidInputs[i].writeToNBT(new NBTTagCompound())); + } + tNBT.setString("author", "Assembling Line Recipe Generator"); + NBTTagList tNBTList = new NBTTagList(); + s = aNewRecipe.mOutput.getDisplayName(); + if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { + s = GT_Assemblyline_Server.lServerNames.get(aNewRecipe.mOutput.getDisplayName()); + if (s == null) + s = aNewRecipe.mOutput.getDisplayName(); + } + tNBTList.appendTag(new NBTTagString("Construction plan for " + aNewRecipe.mOutput.stackSize + " " + s + ". Needed EU/t: " + aNewRecipe.mEUt + " Production time: " + (aNewRecipe.mDuration / 20))); + for (int i = 0; i < aNewRecipe.mInputs.length; i++) { + if (aNewRecipe.mOreDictAlt[i] != null) { + int count = 0; + StringBuilder tBuilder = new StringBuilder("Input Bus " + (i + 1) + ": "); + for (ItemStack tStack : aNewRecipe.mOreDictAlt[i]) { + if (tStack != null) { + s = tStack.getDisplayName(); + if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { + s = GT_Assemblyline_Server.lServerNames.get(tStack.getDisplayName()); + if (s == null) + s = tStack.getDisplayName(); + } + + + tBuilder.append(count == 0 ? "" : "\nOr ").append(tStack.stackSize).append(" ").append(s); + count++; + } + } + if (count > 0) tNBTList.appendTag(new NBTTagString(tBuilder.toString())); + } else if (aNewRecipe.mInputs[i] != null) { + s = aNewRecipe.mInputs[i].getDisplayName(); + if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { + s = GT_Assemblyline_Server.lServerNames.get(aNewRecipe.mInputs[i].getDisplayName()); + if (s == null) + s = aNewRecipe.mInputs[i].getDisplayName(); + } + tNBTList.appendTag(new NBTTagString("Input Bus " + (i + 1) + ": " + aNewRecipe.mInputs[i].stackSize + " " + s)); + } + } + for (int i = 0; i < aNewRecipe.mFluidInputs.length; i++) { + if (aNewRecipe.mFluidInputs[i] != null) { + s = aNewRecipe.mFluidInputs[i].getLocalizedName(); + if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { + s = GT_Assemblyline_Server.lServerNames.get(aNewRecipe.mFluidInputs[i].getLocalizedName()); + if (s == null) + s = aNewRecipe.mFluidInputs[i].getLocalizedName(); + } + tNBTList.appendTag(new NBTTagString("Input Hatch " + (i + 1) + ": " + aNewRecipe.mFluidInputs[i].amount + "L " + s)); + } + } + tNBT.setTag("pages", tNBTList); + aDataStick.setTagCompound(tNBT); + // Set recipe hash + setRecipeHashOnDataStick(aDataStick, aHash); + return true; + } + return false; + } + +} diff --git a/src/main/java/gregtech/api/util/GT_Recipe.java b/src/main/java/gregtech/api/util/GT_Recipe.java index 1f13ff7b82..2c5b2df5bf 100644 --- a/src/main/java/gregtech/api/util/GT_Recipe.java +++ b/src/main/java/gregtech/api/util/GT_Recipe.java @@ -564,6 +564,84 @@ public class GT_Recipe implements Comparable<GT_Recipe> { mEUt = aEUt; mOreDictAlt = aAlt; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + GT_ItemStack[] thisInputs = new GT_ItemStack[this.mInputs.length]; + int totalInputStackSize = 0; + for (int i=0;i<this.mInputs.length;i++) { + thisInputs[i] = new GT_ItemStack(this.mInputs[i]); + totalInputStackSize += thisInputs[i].mStackSize; + } + int inputHash = Arrays.deepHashCode(thisInputs); + int inputFluidHash = Arrays.deepHashCode(this.mFluidInputs); + GT_ItemStack thisOutput = new GT_ItemStack(mOutput); + GT_ItemStack thisResearch = new GT_ItemStack(mResearchItem); + int miscRecipeDataHash = Arrays.deepHashCode(new Object[] { + totalInputStackSize, + mDuration, mEUt, + thisOutput, + thisResearch, + mResearchTime + }); + result = prime * result + inputFluidHash; + result = prime * result + inputHash; + result = prime * result + miscRecipeDataHash; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof GT_Recipe_AssemblyLine)) { + return false; + } + GT_Recipe_AssemblyLine other = (GT_Recipe_AssemblyLine) obj; + if (this.mInputs.length != other.mInputs.length) { + return false; + } + if (this.mFluidInputs.length != other.mFluidInputs.length) { + return false; + } + // Check Outputs Match + GT_ItemStack output1 = new GT_ItemStack(this.mOutput); + GT_ItemStack output2 = new GT_ItemStack(other.mOutput); + if (!output1.equals(output2)) { + return false; + } + // Check Scanned Item Match + GT_ItemStack scan1 = new GT_ItemStack(this.mResearchItem); + GT_ItemStack scan2 = new GT_ItemStack(other.mResearchItem); + if (!scan1.equals(scan2)) { + return false; + } + // Check Items Match + GT_ItemStack[] thisInputs = new GT_ItemStack[this.mInputs.length]; + GT_ItemStack[] otherInputs = new GT_ItemStack[other.mInputs.length]; + for (int i=0;i<thisInputs.length;i++) { + thisInputs[i] = new GT_ItemStack(this.mInputs[i]); + otherInputs[i] = new GT_ItemStack(other.mInputs[i]); + } + for (int i=0;i<thisInputs.length;i++) { + if (!thisInputs[i].equals(otherInputs[i]) || thisInputs[i].mStackSize != otherInputs[i].mStackSize) { + return false; + } + } + // Check Fluids Match + for (int i=0;i<this.mFluidInputs.length;i++) { + if (!this.mFluidInputs[i].isFluidStackIdentical(other.mFluidInputs[i])) { + return false; + } + } + + return this.mDuration == other.mDuration + && this.mEUt == other.mEUt + && this.mResearchTime == other.mResearchTime; + } } diff --git a/src/main/java/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_Scanner.java b/src/main/java/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_Scanner.java index eba61ad1b1..1573c951eb 100644 --- a/src/main/java/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_Scanner.java +++ b/src/main/java/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_Scanner.java @@ -15,6 +15,7 @@ import gregtech.api.metatileentity.MetaTileEntity; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_BasicMachine; import gregtech.api.objects.ItemData; import gregtech.api.render.TextureFactory; +import gregtech.api.util.GT_AssemblyLineUtils; import gregtech.api.util.GT_Assemblyline_Server; import gregtech.api.util.GT_Log; import gregtech.api.util.GT_ModHandler; @@ -253,6 +254,7 @@ public class GT_MetaTileEntity_Scanner extends GT_MetaTileEntity_BasicMachine { if (failScanner) { return FOUND_RECIPE_BUT_DID_NOT_MEET_REQUIREMENTS; } + String s = tRecipe.mOutput.getDisplayName(); if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { @@ -261,92 +263,19 @@ public class GT_MetaTileEntity_Scanner extends GT_MetaTileEntity_BasicMachine { s = tRecipe.mOutput.getDisplayName(); } this.mOutputItems[0] = GT_Utility.copyAmount(1L, getSpecialSlot()); - //remove possible old NBTTagCompound - this.mOutputItems[0].setTagCompound(new NBTTagCompound()); - GT_Utility.ItemNBT.setBookTitle(this.mOutputItems[0], s + " Construction Data"); + - NBTTagCompound tNBT = this.mOutputItems[0].getTagCompound(); - if (tNBT == null) { - tNBT = new NBTTagCompound(); - } - - tNBT.setTag("output", tRecipe.mOutput.writeToNBT(new NBTTagCompound())); - tNBT.setInteger("time", tRecipe.mDuration); - tNBT.setInteger("eu", tRecipe.mEUt); - for (int i = 0; i < tRecipe.mInputs.length; i++) { - tNBT.setTag("" + i, tRecipe.mInputs[i].writeToNBT(new NBTTagCompound())); - } - for (int i = 0; i < tRecipe.mOreDictAlt.length; i++) { - if (tRecipe.mOreDictAlt[i] != null && tRecipe.mOreDictAlt[i].length > 0) { - tNBT.setInteger("a" + i, tRecipe.mOreDictAlt[i].length); - for (int j = 0; j < tRecipe.mOreDictAlt[i].length; j++) { - tNBT.setTag("a" + i + ":" + j, tRecipe.mOreDictAlt[i][j].writeToNBT(new NBTTagCompound())); - } - } - } - for (int i = 0; i < tRecipe.mFluidInputs.length; i++) { - tNBT.setTag("f" + i, tRecipe.mFluidInputs[i].writeToNBT(new NBTTagCompound())); - } - tNBT.setString("author", "Assembling Line Recipe Generator"); - NBTTagList tNBTList = new NBTTagList(); - s = tRecipe.mOutput.getDisplayName(); - if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { - s = GT_Assemblyline_Server.lServerNames.get(tRecipe.mOutput.getDisplayName()); - if (s == null) - s = tRecipe.mOutput.getDisplayName(); - } - tNBTList.appendTag(new NBTTagString("Construction plan for " + tRecipe.mOutput.stackSize + " " + s + ". Needed EU/t: " + tRecipe.mEUt + " Production time: " + (tRecipe.mDuration / 20))); - for (int i = 0; i < tRecipe.mInputs.length; i++) { - if (tRecipe.mOreDictAlt[i] != null) { - int count = 0; - StringBuilder tBuilder = new StringBuilder("Input Bus " + (i + 1) + ": "); - for (ItemStack tStack : tRecipe.mOreDictAlt[i]) { - if (tStack != null) { - s = tStack.getDisplayName(); - if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { - s = GT_Assemblyline_Server.lServerNames.get(tStack.getDisplayName()); - if (s == null) - s = tStack.getDisplayName(); - } - - - tBuilder.append(count == 0 ? "" : "\nOr ").append(tStack.stackSize).append(" ").append(s); - count++; - } - } - if (count > 0) tNBTList.appendTag(new NBTTagString(tBuilder.toString())); - } else if (tRecipe.mInputs[i] != null) { - s = tRecipe.mInputs[i].getDisplayName(); - if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { - s = GT_Assemblyline_Server.lServerNames.get(tRecipe.mInputs[i].getDisplayName()); - if (s == null) - s = tRecipe.mInputs[i].getDisplayName(); - } - tNBTList.appendTag(new NBTTagString("Input Bus " + (i + 1) + ": " + tRecipe.mInputs[i].stackSize + " " + s)); - } - } - for (int i = 0; i < tRecipe.mFluidInputs.length; i++) { - if (tRecipe.mFluidInputs[i] != null) { - s = tRecipe.mFluidInputs[i].getLocalizedName(); - if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { - s = GT_Assemblyline_Server.lServerNames.get(tRecipe.mFluidInputs[i].getLocalizedName()); - if (s == null) - s = tRecipe.mFluidInputs[i].getLocalizedName(); - } - tNBTList.appendTag(new NBTTagString("Input Hatch " + (i + 1) + ": " + tRecipe.mFluidInputs[i].amount + "L " + s)); - } + // Use Assline Utils + if (GT_AssemblyLineUtils.setAssemblyLineRecipeOnDataStick(this.mOutputItems[0], tRecipe)) { + aStack.stackSize -= 1; + calculateOverclockedNess(30, tRecipe.mResearchTime); + //In case recipe is too OP for that machine + if (mMaxProgresstime == Integer.MAX_VALUE - 1 && mEUt == Integer.MAX_VALUE - 1) + return FOUND_RECIPE_BUT_DID_NOT_MEET_REQUIREMENTS; + getSpecialSlot().stackSize -= 1; + return 2; } - tNBT.setTag("pages", tNBTList); - - this.mOutputItems[0].setTagCompound(tNBT); - - aStack.stackSize -= 1; - calculateOverclockedNess(30, tRecipe.mResearchTime); - //In case recipe is too OP for that machine - if (mMaxProgresstime == Integer.MAX_VALUE - 1 && mEUt == Integer.MAX_VALUE - 1) - return FOUND_RECIPE_BUT_DID_NOT_MEET_REQUIREMENTS; - getSpecialSlot().stackSize -= 1; - return 2; + } } } diff --git a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_AssemblyLine.java b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_AssemblyLine.java index 0a19536bf1..c2460213c8 100644 --- a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_AssemblyLine.java +++ b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_AssemblyLine.java @@ -17,8 +17,10 @@ import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch_DataAccess; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch_Input; import gregtech.api.render.TextureFactory; +import gregtech.api.util.GT_AssemblyLineUtils; import gregtech.api.util.GT_Multiblock_Tooltip_Builder; import gregtech.api.util.GT_Recipe; +import gregtech.api.util.GT_Recipe.GT_Recipe_AssemblyLine; import gregtech.api.util.GT_Utility; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; @@ -156,117 +158,111 @@ public class GT_MetaTileEntity_AssemblyLine extends GT_MetaTileEntity_EnhancedMu @Override public boolean checkRecipe(ItemStack aStack) { - if (GT_Values.D1) - GT_FML_LOGGER.info("Start ALine recipe check"); + if (GT_Values.D1) { + GT_FML_LOGGER.info("Start ALine recipe check"); + } ArrayList<ItemStack> tDataStickList = getDataItems(2); - if (tDataStickList.isEmpty()) return false; - if (GT_Values.D1) - GT_FML_LOGGER.info("Stick accepted, " + tDataStickList.size() + " Data Sticks found"); + if (tDataStickList.isEmpty()) { + return false; + } + if (GT_Values.D1) { + GT_FML_LOGGER.info("Stick accepted, " + tDataStickList.size() + " Data Sticks found"); + } - ItemStack[] tStack = new ItemStack[15]; - FluidStack[] tFluids = new FluidStack[4]; + ItemStack[] tStack = null; + FluidStack[] tFluids = null; boolean findRecipe = false; - nextDS: + + nextDataStick: for (ItemStack tDataStick : tDataStickList) { - NBTTagCompound tTag = tDataStick.getTagCompound(); - if (tTag == null) - continue; + GT_Recipe_AssemblyLine aFoundRecipe = GT_AssemblyLineUtils.findAssemblyLineRecipeFromDataStick(tDataStick); + + // Check if the recipe on the data stick is the current recipe for it's given output, if not we update it and continue to next. + if (!GT_AssemblyLineUtils.processDataStick(tDataStick)) { + continue; + } + + tStack = aFoundRecipe.mInputs; + tFluids = aFoundRecipe.mFluidInputs; + + // So here we check against the recipe found on the data stick. + // If we run into missing buses/hatches or bad inputs, we go to the next data stick. + // This check only happens if we have a valid up to date data stick. + + // Check Inputs allign + int aItemCount = aFoundRecipe.mInputs.length; for (int i = 0; i < 15; i++) { - int count = tTag.getInteger("a" + i); - if (!tTag.hasKey("" + i) && count <= 0) - continue; - if (mInputBusses.get(i) == null) { - continue nextDS; - } - - ItemStack stackInSlot = mInputBusses.get(i).getBaseMetaTileEntity().getStackInSlot(0); - boolean flag = true; - if (count > 0) { - for (int j = 0; j < count; j++) { - tStack[i] = GT_Utility.loadItem(tTag, "a" + i + ":" + j); - if (tStack[i] == null) continue; - if (GT_Values.D1) - GT_FML_LOGGER.info("Item " + i + " : " + tStack[i].getUnlocalizedName()); - if (GT_Utility.areStacksEqual(tStack[i], stackInSlot, true) && tStack[i].stackSize <= stackInSlot.stackSize) { - flag = false; - break; - } - } - } - if (flag) { - tStack[i] = GT_Utility.loadItem(tTag, "" + i); - if (tStack[i] == null) { - flag = false; - continue; - } - if (GT_Values.D1) - GT_FML_LOGGER.info("Item " + i + " : " + tStack[i].getUnlocalizedName()); - if (GT_Utility.areStacksEqual(tStack[i], stackInSlot, true) && tStack[i].stackSize <= stackInSlot.stackSize) { - flag = false; + if (i >= aItemCount) { + continue; + } + else { + if (mInputBusses.get(i) == null) { + continue nextDataStick; } - } - if (GT_Values.D1) - GT_FML_LOGGER.info(i + (flag ? " not accepted" : " accepted")); - if (flag) - continue nextDS; - } - - if (GT_Values.D1) GT_FML_LOGGER.info("All Items done, start fluid check"); - for (int i = 0; i < 4; i++) { - if (!tTag.hasKey("f" + i)) continue; - tFluids[i] = GT_Utility.loadFluid(tTag, "f" + i); - if (tFluids[i] == null) continue; - if (GT_Values.D1) - GT_FML_LOGGER.info("Fluid " + i + " " + tFluids[i].getUnlocalizedName()); - if (mInputHatches.get(i) == null) { - continue nextDS; - } - FluidStack fluidInHatch = mInputHatches.get(i).mFluid; - if (!GT_Utility.areFluidsEqual(fluidInHatch, tFluids[i], true) || fluidInHatch.amount < tFluids[i].amount) { - if (GT_Values.D1) - GT_FML_LOGGER.info(i + " not accepted"); - continue nextDS; - } - if (GT_Values.D1) - GT_FML_LOGGER.info(i + " accepted"); + else { + ItemStack stackInSlot = mInputBusses.get(i).getBaseMetaTileEntity().getStackInSlot(0); + if (!GT_Utility.areStacksEqual(tStack[i], stackInSlot, true) || tStack[i].stackSize <= stackInSlot.stackSize) { + continue nextDataStick; + } + if (GT_Values.D1) { + GT_FML_LOGGER.info("Item: " + i + " accepted"); + } + } + } } - - if (GT_Values.D1) - GT_FML_LOGGER.info("Input accepted, check other values"); - if (!tTag.hasKey("output")) - continue; - mOutputItems = new ItemStack[]{GT_Utility.loadItem(tTag, "output")}; - if (mOutputItems[0] == null || !GT_Utility.isStackValid(mOutputItems[0])) - continue; - - if (!tTag.hasKey("time")) - continue; - int tMaxProgressTime = tTag.getInteger("time"); - if (tMaxProgressTime <= 0) - continue; - - if (!tTag.hasKey("eu")) - continue; - - if (GT_Values.D1) GT_FML_LOGGER.info("Check overclock"); - - calculateOverclockedNessMulti(tTag.getInteger("eu"), tMaxProgressTime, 1, getMaxInputVoltage()); - //In case recipe is too OP for that machine - if (mMaxProgresstime == Integer.MAX_VALUE - 1 && mEUt == Integer.MAX_VALUE - 1) { - if (GT_Values.D1) GT_FML_LOGGER.info("Recipe too OP"); - continue; + + // Check Fluid Inputs allign + int aFluidCount = aFoundRecipe.mFluidInputs.length; + for (int i = 0; i < 4; i++){ + if (i >= aFluidCount) { + continue; + } + else { + if (mInputHatches.get(i) == null) { + continue nextDataStick; + } + else { + FluidStack fluidInHatch = mInputHatches.get(i).mFluid; + if (!GT_Utility.areFluidsEqual(fluidInHatch, tFluids[i], true) || fluidInHatch.amount < tFluids[i].amount) { + continue nextDataStick; + } + if (GT_Values.D1) { + GT_FML_LOGGER.info("Fluid:" + i + " accepted"); + } + } + } } - - if (GT_Values.D1) GT_FML_LOGGER.info("Find avaiable recipe"); - findRecipe = true; - break; + + if (GT_Values.D1) { + GT_FML_LOGGER.info("Check overclock"); + } + calculateOverclockedNessMulti(aFoundRecipe.mEUt, aFoundRecipe.mDuration, 1, getMaxInputVoltage()); + //In case recipe is too OP for that machine + if (mMaxProgresstime == Integer.MAX_VALUE - 1 && mEUt == Integer.MAX_VALUE - 1) { + if (GT_Values.D1) { + GT_FML_LOGGER.info("Recipe too OP"); + } + continue; + } + if (GT_Values.D1) { + GT_FML_LOGGER.info("Find available recipe"); + } + findRecipe = true; } - if (!findRecipe) return false; + + // Best not to run this recipe. + if (!findRecipe || tStack == null || tStack.length <= 0 || tFluids == null || tFluids.length <= 0) { + return false; + } + - if (GT_Values.D1) GT_FML_LOGGER.info("All checked start consuming inputs"); + if (GT_Values.D1) { + GT_FML_LOGGER.info("All checked start consuming inputs"); + } for (int i = 0; i < 15; i++) { - if (tStack[i] == null) - continue; + if (tStack[i] == null) { + continue; + } ItemStack stackInSlot = mInputBusses.get(i).getBaseMetaTileEntity().getStackInSlot(0); stackInSlot.stackSize -= tStack[i].stackSize; } @@ -280,13 +276,15 @@ public class GT_MetaTileEntity_AssemblyLine extends GT_MetaTileEntity_EnhancedMu } } - if (this.mEUt > 0) - this.mEUt = -this.mEUt; + if (this.mEUt > 0) { + this.mEUt = -this.mEUt; + } this.mEfficiency = (10000 - (getIdealStatus() - getRepairStatus()) * 1000); this.mEfficiencyIncrease = 10000; updateSlots(); - if (GT_Values.D1) - GT_FML_LOGGER.info("Recipe sucessfull"); + if (GT_Values.D1) { + GT_FML_LOGGER.info("Recipe sucessfull"); + } return true; } |