From 068a827c1087be5379a5f59298e416bd52a27023 Mon Sep 17 00:00:00 2001 From: BlueWeabo <76872108+BlueWeabo@users.noreply.github.com> Date: Thu, 9 Feb 2023 22:45:04 +0200 Subject: [PATCH] working recipes! --- .../multiblock/base/MultiBlockController.java | 112 +++++++++++++++++- .../multiblock/base/MultiBlock_Stackable.java | 5 - .../multiblock/MultiBlock_Macerator.java | 53 ++++++++- 3 files changed, 159 insertions(+), 11 deletions(-) diff --git a/src/main/java/gregtech/api/multitileentity/multiblock/base/MultiBlockController.java b/src/main/java/gregtech/api/multitileentity/multiblock/base/MultiBlockController.java index 579d0b1759e..8da2107ab9e 100644 --- a/src/main/java/gregtech/api/multitileentity/multiblock/base/MultiBlockController.java +++ b/src/main/java/gregtech/api/multitileentity/multiblock/base/MultiBlockController.java @@ -93,11 +93,14 @@ public abstract class MultiBlockController> ex protected Map multiBlockInputInventory = new LinkedHashMap<>(); protected Map multiBlockOutputInventory = new LinkedHashMap<>(); - private int mMaxProgressTime = 0, mProgressTime = 0; + protected int mMaxProgressTime = 0; + private int mProgressTime = 0; private boolean mStructureOkay = false, mStructureChanged = false; - private boolean mWorks = true, mWorkUpdate = false, mWasShutdown = false, mActive = false; + private boolean mWorks = true, mWorkUpdate = false, mWasShutdown = false, mActive = false, mSeparateInputs = true; private ExtendedFacing mExtendedFacing = ExtendedFacing.DEFAULT; private IAlignmentLimits mLimits = getInitialAlignmentLimits(); + private ItemStack[] mItemsToOutput; + private String mInventory; // A list of sides // Each side has a list of parts that have a cover that need to be ticked @@ -435,9 +438,42 @@ public void onPostTick(long aTick, boolean aIsServerSide) { // Recheck the structure every 30 seconds or so if (!checkStructure(false)) checkStructure(true); } + if (mStructureOkay) { + runMachine(aTick); + } else { + stopMachine(); + } + } + } + + protected void runMachine(long aTick) { + if (mMaxProgressTime > 0) { + markDirty(); + if (mMaxProgressTime > 0 && ++mProgressTime >= mMaxProgressTime) { + mProgressTime = 0; + mMaxProgressTime = 0; + outputItems(); + if (isAllowedToWork()) { + checkRecipe(); + } + } + } else { + if (aTick % 100 == 0 || hasWorkJustBeenEnabled() || hasInventoryBeenModified()) { + + if (isAllowedToWork()) { + if (checkRecipe()) { + markDirty(); + } + } + } } } + protected boolean checkRecipe() { + boolean result = checkRecipe(null); + return result; + } + protected void clearSpecialLists() { mUpgradeCasings.clear(); } @@ -538,6 +574,14 @@ public void setActive(boolean aActive) { mActive = aActive; } + public boolean isSeparateInputs() { + return mSeparateInputs; + } + + public void setSeparateInputs(boolean aSeparateInputs) { + mSeparateInputs = aSeparateInputs; + } + @Override public boolean wasShutdown() { return mWasShutdown; @@ -1165,7 +1209,69 @@ public boolean isItemValidForSlot(MultiBlockPart aPart, int aSlot, ItemStack aSt return isItemValidForSlot(aSlot, aStack); } - /** + /* + * Helper Methods For Recipe checking + */ + + protected ItemStack[] getAllItemInputs() { + return getInventoriesForInput().getStacks().toArray(new ItemStack[0]); + } + + protected Iterable> getItemInputsForEachInventory() { + Iterable> tIterator = new Iterable>() { + + @Override + public Iterator> iterator() { + return new Iterator>() { + + int i = 0; + + @Override + public boolean hasNext() { + if (i >= multiBlockInputInventory.values().size()) { + return false; + } + return true; + } + + @Override + public Pair next() { + return Pair.of( + multiBlockInputInventory.values().toArray(new IItemHandlerModifiable[0])[i].getStacks() + .toArray(new ItemStack[0]), + multiBlockInputInventory.keySet().toArray(new String[0])[i++]); + } + }; + } + + }; + return tIterator; + } + + protected void setItemOutputs(ItemStack[] aItemOutputs, String aInventory) { + mItemsToOutput = aItemOutputs; + mInventory = aInventory; + } + + private void outputItems() { + int index = 0; + if (mItemsToOutput == null) { + return; + } + if (mInventory != null) { + for (ItemStack tItem : mItemsToOutput) { + multiBlockOutputInventory.getOrDefault(mInventory, getInventoriesForOutput()) + .insertItem(index++, tItem.copy(), false); + } + } else { + for (ItemStack tItem : mItemsToOutput) { + getInventoriesForOutput().insertItem(index++, tItem.copy(), false); + } + } + mItemsToOutput = null; + } + + /* * GUI Work - Multiblock GUI related methods */ @Override diff --git a/src/main/java/gregtech/api/multitileentity/multiblock/base/MultiBlock_Stackable.java b/src/main/java/gregtech/api/multitileentity/multiblock/base/MultiBlock_Stackable.java index 9dfd4c0cc67..f40a9937387 100644 --- a/src/main/java/gregtech/api/multitileentity/multiblock/base/MultiBlock_Stackable.java +++ b/src/main/java/gregtech/api/multitileentity/multiblock/base/MultiBlock_Stackable.java @@ -105,9 +105,4 @@ public boolean checkMachine() { buildState.addOffset(getAfterLastStackOffset()); return checkPiece(STACKABLE_TOP, buildState.stopBuilding()); } - - @Override - public boolean checkRecipe(ItemStack aStack) { - return false; - } } diff --git a/src/main/java/gregtech/common/tileentities/machines/multiblock/MultiBlock_Macerator.java b/src/main/java/gregtech/common/tileentities/machines/multiblock/MultiBlock_Macerator.java index fa0a6298f67..d4810aa1959 100644 --- a/src/main/java/gregtech/common/tileentities/machines/multiblock/MultiBlock_Macerator.java +++ b/src/main/java/gregtech/common/tileentities/machines/multiblock/MultiBlock_Macerator.java @@ -3,8 +3,10 @@ import static com.gtnewhorizon.structurelib.structure.StructureUtility.ofChain; import static com.gtnewhorizon.structurelib.structure.StructureUtility.transpose; import static gregtech.api.enums.Textures.BlockIcons.MACHINE_CASINGS; +import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_FRONT_ELECTRIC_BLAST_FURNACE; import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_FRONT_ELECTRIC_BLAST_FURNACE_ACTIVE; import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_FRONT_ELECTRIC_BLAST_FURNACE_ACTIVE_GLOW; +import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_FRONT_ELECTRIC_BLAST_FURNACE_GLOW; import static gregtech.api.multitileentity.multiblock.base.MultiBlockPart.ENERGY_IN; import static gregtech.api.multitileentity.multiblock.base.MultiBlockPart.FLUID_IN; import static gregtech.api.multitileentity.multiblock.base.MultiBlockPart.FLUID_OUT; @@ -13,15 +15,21 @@ import static gregtech.api.multitileentity.multiblock.base.MultiBlockPart.NOTHING; import net.minecraft.block.Block; +import net.minecraft.item.ItemStack; + +import org.apache.commons.lang3.tuple.Pair; import com.gtnewhorizon.structurelib.structure.IStructureDefinition; import com.gtnewhorizon.structurelib.structure.StructureDefinition; import com.gtnewhorizon.structurelib.util.Vec3Impl; +import gregtech.api.enums.TierEU; import gregtech.api.interfaces.ITexture; import gregtech.api.multitileentity.multiblock.base.MultiBlock_Stackable; import gregtech.api.render.TextureFactory; import gregtech.api.util.GT_Multiblock_Tooltip_Builder; +import gregtech.api.util.GT_Recipe; +import gregtech.api.util.GT_Recipe.GT_Recipe_Map; public class MultiBlock_Macerator extends MultiBlock_Stackable { @@ -135,12 +143,51 @@ public ITexture[] getTexture(Block aBlock, byte aSide, boolean isActive, int aRe // Base Texture MACHINE_CASINGS[1][0], // Active - TextureFactory.builder().addIcon(OVERLAY_FRONT_ELECTRIC_BLAST_FURNACE_ACTIVE).extFacing().build(), + isActive() + ? TextureFactory.builder().addIcon(OVERLAY_FRONT_ELECTRIC_BLAST_FURNACE_ACTIVE).extFacing() + .build() + : TextureFactory.builder().addIcon(OVERLAY_FRONT_ELECTRIC_BLAST_FURNACE).extFacing() + .build(), // Active Glow - TextureFactory.builder().addIcon(OVERLAY_FRONT_ELECTRIC_BLAST_FURNACE_ACTIVE_GLOW).extFacing() - .glow().build() }; + isActive() + ? TextureFactory.builder().addIcon(OVERLAY_FRONT_ELECTRIC_BLAST_FURNACE_ACTIVE_GLOW) + .extFacing().glow().build() + : TextureFactory.builder().addIcon(OVERLAY_FRONT_ELECTRIC_BLAST_FURNACE_GLOW).extFacing() + .glow().build() }; } // Base Texture return new ITexture[] { MACHINE_CASINGS[1][0] }; } + + @Override + public boolean checkRecipe(ItemStack aStack) { + if (isSeparateInputs()) { + for (Pair tItemInputs : getItemInputsForEachInventory()) { + if (processRecipe(aStack, tItemInputs.getLeft(), tItemInputs.getRight())) { + return true; + } + } + return false; + } else { + ItemStack[] tItemInputs = getInventoriesForInput().getStacks().toArray(new ItemStack[0]); + return processRecipe(aStack, tItemInputs, null); + } + } + + private boolean processRecipe(ItemStack aStack, ItemStack[] aItemInputs, String aInventory) { + GT_Recipe_Map tRecipeMap = GT_Recipe_Map.sMaceratorRecipes; + GT_Recipe tRecipe = tRecipeMap.findRecipe(this, false, TierEU.IV, null, aItemInputs); + if (tRecipe == null) { + return false; + } + + if (!tRecipe.isRecipeInputEqual(true, false, 1, null, aItemInputs)) { + return false; + } + + mMaxProgressTime = tRecipe.mDuration; + + setItemOutputs(tRecipe.mOutputs, aInventory); + return true; + } }