Skip to content

Commit

Permalink
working recipes!
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueWeabo committed Feb 11, 2023
1 parent 136a147 commit 068a827
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ public abstract class MultiBlockController<T extends MultiBlockController<T>> ex
protected Map<String, IItemHandlerModifiable> multiBlockInputInventory = new LinkedHashMap<>();
protected Map<String, IItemHandlerModifiable> 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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Pair<ItemStack[], String>> getItemInputsForEachInventory() {
Iterable<Pair<ItemStack[], String>> tIterator = new Iterable<Pair<ItemStack[], String>>() {

@Override
public Iterator<Pair<ItemStack[], String>> iterator() {
return new Iterator<Pair<ItemStack[], String>>() {

int i = 0;

@Override
public boolean hasNext() {
if (i >= multiBlockInputInventory.values().size()) {
return false;
}
return true;
}

@Override
public Pair<ItemStack[], String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,4 @@ public boolean checkMachine() {
buildState.addOffset(getAfterLastStackOffset());
return checkPiece(STACKABLE_TOP, buildState.stopBuilding());
}

@Override
public boolean checkRecipe(ItemStack aStack) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<MultiBlock_Macerator> {

Expand Down Expand Up @@ -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<ItemStack[], String> 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;
}
}

0 comments on commit 068a827

Please sign in to comment.