Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cribs super recipe check #3608

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3fb4b52
ft
lordIcocain Nov 23, 2024
479381d
save
lordIcocain Nov 27, 2024
40242c4
save
lordIcocain Dec 5, 2024
337f1d9
seems works
lordIcocain Dec 6, 2024
094c88e
smol imrvmnt
lordIcocain Dec 6, 2024
fa6f276
less var
lordIcocain Dec 6, 2024
2d8f1fa
fix
lordIcocain Dec 6, 2024
ac7ada0
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Dec 6, 2024
51d237c
indicate if pattern not have recipe.
lordIcocain Dec 10, 2024
8992bcc
Merge remote-tracking branch 'origin/cribs_super_recipe_check' into c…
lordIcocain Dec 10, 2024
7c788e8
Merge branch 'master' into cribs_super_recipe_check
lordIcocain Dec 10, 2024
3359634
recipe indicator using FakeSyncWidget
lordIcocain Dec 10, 2024
297dcf2
Merge remote-tracking branch 'origin/cribs_super_recipe_check' into c…
lordIcocain Dec 10, 2024
59ff738
spotless
lordIcocain Dec 10, 2024
81d4423
as mode
lordIcocain Dec 11, 2024
140fa5b
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Dec 11, 2024
c27a227
get out sharedItems from PatternSlot.
lordIcocain Dec 12, 2024
6d25fd1
get out sharedItems from PatternSlot.
lordIcocain Dec 12, 2024
a4ec36a
Merge remote-tracking branch 'origin/cribs_super_recipe_check' into c…
lordIcocain Dec 12, 2024
f98f98f
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Dec 12, 2024
12e9d76
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Dec 13, 2024
ad5a450
re
lordIcocain Dec 14, 2024
4560720
Merge remote-tracking branch 'origin/cribs_super_recipe_check' into c…
lordIcocain Dec 14, 2024
837ad2f
cleanup
lordIcocain Dec 14, 2024
b3246c6
even more cleanup
lordIcocain Dec 14, 2024
ac8b451
a bit
lordIcocain Dec 14, 2024
4a4af59
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Dec 14, 2024
89cca94
Merge branch 'master' into cribs_super_recipe_check
lordIcocain Dec 21, 2024
7f00989
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Dec 21, 2024
fce62d9
Merge branch 'master' into cribs_super_recipe_check
serenibyss Dec 21, 2024
560b268
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Dec 21, 2024
7796601
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Dec 26, 2024
1a7681a
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Dec 31, 2024
6c469f9
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 4, 2025
fbf9201
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 4, 2025
1233161
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 4, 2025
a8f9b03
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 5, 2025
68f6c10
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 8, 2025
9c5eb8d
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 9, 2025
6b42af5
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 9, 2025
15c1a3d
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 10, 2025
b36752b
Merge branch 'master' into cribs_super_recipe_check
lordIcocain Jan 11, 2025
2515fa2
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 11, 2025
613e3e3
Merge branch 'master' into cribs_super_recipe_check
lordIcocain Jan 11, 2025
20768ef
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 11, 2025
3ccb3f5
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 11, 2025
92e376e
rework
lordIcocain Jan 11, 2025
219c870
Merge remote-tracking branch 'origin/cribs_super_recipe_check' into c…
lordIcocain Jan 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/gregtech/api/logic/AbstractProcessingLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public abstract class AbstractProcessingLogic<P extends AbstractProcessingLogic<
protected double speedBoost = 1.0;
protected boolean amperageOC = true;
protected boolean isCleanroom;
protected boolean needWipeCraftingPatternRecipeCache;

// #region Setters

Expand Down Expand Up @@ -213,6 +214,7 @@ protected RecipeMap<?> preProcess() {
recipeMap = recipeMapSupplier.get();
}
if (lastRecipeMap != recipeMap) {
if (lastRecipeMap != null) needWipeCraftingPatternRecipeCache = true;
lastRecipe = null;
lastRecipeMap = recipeMap;
}
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/gregtech/api/logic/ProcessingLogic.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package gregtech.api.logic;

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import javax.annotation.Nonnull;
Expand All @@ -10,13 +15,15 @@
import net.minecraftforge.fluids.FluidStack;

import gregtech.api.interfaces.tileentity.IRecipeLockable;
import gregtech.api.objects.GTDualInputs;
import gregtech.api.recipe.RecipeMap;
import gregtech.api.recipe.check.CheckRecipeResult;
import gregtech.api.recipe.check.CheckRecipeResultRegistry;
import gregtech.api.recipe.check.SingleRecipeCheck;
import gregtech.api.util.GTRecipe;
import gregtech.api.util.OverclockCalculator;
import gregtech.api.util.ParallelHelper;
import gregtech.common.tileentities.machines.IDualInputInventory;

/**
* Logic class to calculate result of recipe check from inputs, based on recipemap.
Expand All @@ -29,6 +36,8 @@ public class ProcessingLogic extends AbstractProcessingLogic<ProcessingLogic> {
protected ItemStack[] inputItems;
protected FluidStack[] inputFluids;
protected boolean isRecipeLocked;
protected int craftingPatternHash;
protected Map<Integer, List<GTRecipe>> craftingPatternRecipeCache = new HashMap<>();

public ProcessingLogic() {}

Expand Down Expand Up @@ -63,6 +72,37 @@ public ProcessingLogic setSpecialSlotItem(ItemStack specialSlotItem) {
return getThis();
}

public boolean craftingPatternHandler(IDualInputInventory slot) {
int hash = slot.hashCode();
if (needWipeCraftingPatternRecipeCache) {
craftingPatternRecipeCache.clear();
needWipeCraftingPatternRecipeCache = false;
}
if (!craftingPatternRecipeCache.containsKey(hash)) {
GTDualInputs inputs = slot.getPatternInputs();
setInputItems(inputs.inputItems);
setInputFluids(inputs.inputFluid);
List<GTRecipe> recipes = new ArrayList<>();
for (GTRecipe recipe : findRecipeMatches(preProcess()).collect(toList())) {
if (!recipes.contains(recipe)) {
recipes.add(recipe);
}
}
if (!recipes.isEmpty()) {
craftingPatternRecipeCache.put(hash, recipes);
craftingPatternHash = hash;
return true;
}
return false;
}
craftingPatternHash = hash;
return true;
}
Dream-Master marked this conversation as resolved.
Show resolved Hide resolved

public void removeEntryCraftingPatternRecipeCache(int hash) {
craftingPatternRecipeCache.remove(hash);
}

/**
* Enables single recipe locking mode.
*/
Expand All @@ -85,6 +125,7 @@ public ProcessingLogic clear() {
this.calculatedEut = 0;
this.duration = 0;
this.calculatedParallels = 0;
this.craftingPatternHash = 0;
return getThis();
}

Expand All @@ -106,6 +147,18 @@ public CheckRecipeResult process() {
inputFluids = new FluidStack[0];
}

if (craftingPatternHash != 0) {
List<GTRecipe> matchedRecipes = craftingPatternRecipeCache.get(craftingPatternHash);
for (GTRecipe matchedRecipe : matchedRecipes) {
if (matchedRecipe.maxParallelCalculatedByInputs(1, inputFluids, inputItems) == 1) {
CalculationResult foundResult = validateAndCalculateRecipe(matchedRecipe);
return foundResult.checkRecipeResult;
}
}
craftingPatternHash = 0;
return CheckRecipeResultRegistry.NO_RECIPE;
}

if (isRecipeLocked && recipeLockableMachine != null && recipeLockableMachine.getSingleRecipeCheck() != null) {
// Recipe checker is already built, we'll use it
SingleRecipeCheck singleRecipeCheck = recipeLockableMachine.getSingleRecipeCheck();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -883,16 +883,18 @@ protected boolean supportsCraftingMEBuffer() {
@Nonnull
protected CheckRecipeResult doCheckRecipe() {
CheckRecipeResult result = CheckRecipeResultRegistry.NO_RECIPE;

// check crafting input hatches first
if (supportsCraftingMEBuffer()) {
for (IDualInputHatch dualInputHatch : mDualInputHatches) {
for (var it = dualInputHatch.inventories(); it.hasNext();) {
IDualInputInventory slot = it.next();
// Reverse order of input items for consistent behavior with standard input buses.
ItemStack[] inputItems = slot.getItemInputs();
ArrayUtils.reverse(inputItems);
processingLogic.setInputItems(inputItems);
for (IDualInputHatch dualInputHatch : mDualInputHatches) {
ItemStack[] sharedItems = dualInputHatch.getSharedItems();
for (var it = dualInputHatch.inventories(); it.hasNext();) {
IDualInputInventory slot = it.next();

if (!slot.isEmpty() && processingLogic.craftingPatternHandler(slot)) {

processingLogic.setInputItems(ArrayUtils.addAll(sharedItems, slot.getItemInputs()));
processingLogic.setInputFluids(slot.getFluidInputs());

CheckRecipeResult foundResult = processingLogic.process();
if (foundResult.wasSuccessful()) {
return foundResult;
Expand Down Expand Up @@ -1720,6 +1722,7 @@ public boolean addToMachineList(IGregTechTileEntity aTileEntity, int aBaseCasing
}
if (aMetaTileEntity instanceof IDualInputHatch hatch) {
hatch.updateCraftingIcon(this.getMachineCraftingIcon());
hatch.setProcessingLogic(processingLogic);
return mDualInputHatches.add(hatch);
}
if (aMetaTileEntity instanceof ISmartInputHatch hatch) {
Expand Down Expand Up @@ -2049,12 +2052,8 @@ public void getWailaBody(ItemStack itemStack, List<String> currentTip, IWailaDat
}
}
}
currentTip.add(
GTWaila.getMachineProgressString(
isActive,
tag.getBoolean("isAllowedToWork"),
tag.getInteger("maxProgress"),
tag.getInteger("progress")));
currentTip
.add(GTWaila.getMachineProgressString(isActive, tag.getInteger("maxProgress"), tag.getInteger("progress")));
// Show ns on the tooltip
if (GTMod.gregtechproxy.wailaAverageNS && tag.hasKey("averageNS")) {
int tAverageTime = tag.getInteger("averageNS");
Expand Down Expand Up @@ -2103,7 +2102,6 @@ public void getWailaNBTData(EntityPlayerMP player, TileEntity tile, NBTTagCompou
final IGregTechTileEntity tileEntity = getBaseMetaTileEntity();
if (tileEntity != null) {
tag.setBoolean("isActive", tileEntity.isActive());
tag.setBoolean("isAllowedToWork", tileEntity.isAllowedToWork());
if (tileEntity.isActive()) {
if (mEUt < 0) tag.setLong("energyUsage", getActualEnergyUsage());
else tag.setLong("energyUsage", (long) -mEUt * mEfficiency / 10000);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/gregtech/api/objects/GTDualInputs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gregtech.api.objects;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

public class GTDualInputs {

public ItemStack[] inputItems;
public FluidStack[] inputFluid;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import net.minecraft.item.ItemStack;

import gregtech.api.logic.ProcessingLogic;

public interface IDualInputHatch {

boolean justUpdated();
Expand All @@ -18,4 +20,8 @@ public interface IDualInputHatch {
Optional<IDualInputInventory> getFirstNonEmptyInventory();

boolean supportsFluids();

ItemStack[] getSharedItems();

void setProcessingLogic(ProcessingLogic pl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

import gregtech.api.objects.GTDualInputs;

public interface IDualInputInventory {

boolean isEmpty();

ItemStack[] getItemInputs();

FluidStack[] getFluidInputs();

GTDualInputs getPatternInputs();

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.NotNull;

import com.glodblock.github.common.item.ItemFluidDrop;
import com.glodblock.github.common.item.ItemFluidPacket;
import com.google.common.collect.ImmutableList;
import com.gtnewhorizons.modularui.api.math.Alignment;
Expand Down Expand Up @@ -91,8 +92,10 @@
import gregtech.api.interfaces.modularui.IAddGregtechLogo;
import gregtech.api.interfaces.modularui.IAddUIWidgets;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.logic.ProcessingLogic;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.implementations.MTEHatchInputBus;
import gregtech.api.objects.GTDualInputs;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTUtility;
import gregtech.api.util.extensions.ArrayExt;
Expand Down Expand Up @@ -198,14 +201,15 @@ public boolean isFluidEmpty() {
return fluidInventory.isEmpty();
}

@Override
public boolean isEmpty() {
return isItemEmpty() && isFluidEmpty();
}

@Override
public ItemStack[] getItemInputs() {
if (isEmpty()) return new ItemStack[0];
return ArrayUtils.addAll(itemInventory.toArray(new ItemStack[0]), sharedItemGetter.getSharedItem());
if (isItemEmpty()) return new ItemStack[0];
return itemInventory.toArray(new ItemStack[0]);
}

@Override
Expand All @@ -218,6 +222,34 @@ public ICraftingPatternDetails getPatternDetails() {
return patternDetails;
}

@Override
public int hashCode() {
return Objects.hashCode(patternDetails);
}

public GTDualInputs getPatternInputs() {
GTDualInputs dualInputs = new GTDualInputs();

ItemStack[] inputItems = this.sharedItemGetter.getSharedItem();
FluidStack[] inputFluids = new FluidStack[0];

for (IAEItemStack singleInput : this.getPatternDetails()
.getInputs()) {
if (singleInput == null) continue;
ItemStack singleInputItemStack = singleInput.getItemStack();
if (singleInputItemStack.getItem() instanceof ItemFluidDrop) {
FluidStack fluidStack = ItemFluidDrop.getFluidStack(singleInputItemStack);
if (fluidStack != null) inputFluids = ArrayUtils.addAll(inputFluids, fluidStack);
} else {
inputItems = ArrayUtils.addAll(inputItems, singleInputItemStack);
}
}

dualInputs.inputItems = inputItems;
dualInputs.inputFluid = inputFluids;
return dualInputs;
}

public void refund(AENetworkProxy proxy, BaseActionSource src) throws GridAccessException {
IMEMonitor<IAEItemStack> sg = proxy.getStorage()
.getItemInventory();
Expand Down Expand Up @@ -324,6 +356,7 @@ public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
private static final int MANUAL_SLOT_WINDOW = 10;
private BaseActionSource requestSource = null;
private @Nullable AENetworkProxy gridProxy = null;
public ArrayList<ProcessingLogic> processingLogics = new ArrayList<>();

// holds all internal inventories
private final PatternSlot[] internalInventory = new PatternSlot[MAX_PATTERN_COUNT];
Expand Down Expand Up @@ -803,13 +836,31 @@ private void onPatternChange(int index, ItemStack newItem) {
needPatternSync = true;
}

@Override
public ItemStack[] getSharedItems() {
ItemStack[] sharedItems = new ItemStack[SLOT_MANUAL_SIZE + 1];
sharedItems[0] = mInventory[SLOT_CIRCUIT];
System.arraycopy(mInventory, SLOT_MANUAL_START, sharedItems, 1, SLOT_MANUAL_SIZE);
return ArrayExt.withoutNulls(sharedItems, ItemStack[]::new);
}

@Override
public void setProcessingLogic(ProcessingLogic pl) {
if (!processingLogics.contains(pl)) {
processingLogics.add(pl);
}
}

private void resetCraftingInputRecipeMap() {
for (ProcessingLogic pl : processingLogics) {
if (pl == null) continue;
for (PatternSlot slot : internalInventory) {
if (slot == null) continue;
pl.removeEntryCraftingPatternRecipeCache(slot.hashCode());
}
}
}

@Override
public void getWailaBody(ItemStack itemStack, List<String> currenttip, IWailaDataAccessor accessor,
IWailaConfigHandler config) {
Expand Down Expand Up @@ -982,6 +1033,12 @@ public ItemStack getCrafterIcon() {
return getMachineCraftingIcon();
}

@Override
public void markDirty() {
super.markDirty();
resetCraftingInputRecipeMap();
}

private boolean postMEPatternChange() {
// don't post until it's active
if (!getProxy().isActive()) return false;
Expand Down Expand Up @@ -1015,6 +1072,7 @@ protected ModularWindow createSlotManualWindow(final EntityPlayer player) {
.endAtSlot(SLOT_MANUAL_START + SLOT_MANUAL_SIZE - 1)
.phantom(false)
.background(getGUITextureSet().getItemSlot())
.widgetCreator(slot -> new SlotWidget(slot).setChangeListener(this::resetCraftingInputRecipeMap))
.build()
.setPos(7, 7));
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import gregtech.api.interfaces.IDataCopyable;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.logic.ProcessingLogic;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.implementations.MTEHatchInputBus;
import gregtech.api.render.TextureFactory;
Expand Down Expand Up @@ -163,6 +164,11 @@ public boolean supportsFluids() {
return getMaster() != null && getMaster().supportsFluids();
}

@Override
public ItemStack[] getSharedItems() {
return getMaster() != null ? getMaster().getSharedItems() : new ItemStack[0];
}

@Override
public boolean justUpdated() {
return getMaster() != null && getMaster().justUpdated();
Expand Down Expand Up @@ -291,4 +297,9 @@ public void getWailaNBTData(EntityPlayerMP player, TileEntity tile, NBTTagCompou
public List<ItemStack> getItemsForHoloGlasses() {
return getMaster() != null ? getMaster().getItemsForHoloGlasses() : null;
}

@Override
public void setProcessingLogic(ProcessingLogic pl) {
if (getMaster() != null) getMaster().setProcessingLogic(pl);
}
}
Loading
Loading