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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 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
5782881
Merge branch 'master' into cribs_super_recipe_check
Dream-Master Jan 12, 2025
cf86a32
review
lordIcocain Jan 12, 2025
da380f6
remove test
lordIcocain Jan 12, 2025
47469fe
Merge remote-tracking branch 'origin/cribs_super_recipe_check' into c…
lordIcocain Jan 12, 2025
f59d768
space
lordIcocain Jan 12, 2025
c5c3c2d
HashSet -> Set
lordIcocain Jan 12, 2025
6022ede
hashCode and equals
lordIcocain Jan 12, 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
73 changes: 41 additions & 32 deletions src/main/java/gregtech/api/logic/ProcessingLogic.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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;
Expand All @@ -20,6 +23,7 @@
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 @@ -32,8 +36,8 @@ public class ProcessingLogic extends AbstractProcessingLogic<ProcessingLogic> {
protected ItemStack[] inputItems;
protected FluidStack[] inputFluids;
protected boolean isRecipeLocked;
protected int cribsSlotHash;
protected Map<Integer, GTRecipe> cribsRecipeMap = new HashMap<>();
protected int craftingPatternHash;
protected Map<Integer, List<GTRecipe>> craftingPatternRecipeCache = new HashMap<>();
miozune marked this conversation as resolved.
Show resolved Hide resolved

public ProcessingLogic() {}

Expand Down Expand Up @@ -68,25 +72,35 @@ public ProcessingLogic setSpecialSlotItem(ItemStack specialSlotItem) {
return getThis();
}

public void setCribsSlotHash(int hash) {
this.cribsSlotHash = hash;
}

public boolean cribsHasRecipe(int hash) {
return cribsRecipeMap.containsKey(hash);
}

public boolean setCribsSlotRecipe(GTDualInputs inputs, int hash) {
GTRecipe tempRecipe = getRecipeByInputs(inputs.inputItems, inputs.inputFluid);
if (tempRecipe != null) {
cribsRecipeMap.put(hash, tempRecipe);
return true;
public boolean craftingPatternHandler(IDualInputInventory slot) {
int hash = slot.hashCode();
if (needWipeCraftingPatternRecipeCache) {
craftingPatternRecipeCache.clear();
needWipeCraftingPatternRecipeCache = false;
}
return false;
if (!craftingPatternRecipeCache.containsKey(hash)) {
miozune marked this conversation as resolved.
Show resolved Hide resolved
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);
miozune marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 resetCribsRecipeMap() {
cribsRecipeMap.clear();
public void removeEntryCraftingPatternRecipeCache(int hash) {
craftingPatternRecipeCache.remove(hash);
}

/**
Expand All @@ -111,7 +125,7 @@ public ProcessingLogic clear() {
this.calculatedEut = 0;
this.duration = 0;
this.calculatedParallels = 0;
this.cribsSlotHash = 0;
this.craftingPatternHash = 0;
return getThis();
}

Expand All @@ -133,11 +147,15 @@ public CheckRecipeResult process() {
inputFluids = new FluidStack[0];
}

if (cribsSlotHash != 0 && cribsRecipeMap.containsKey(cribsSlotHash)) {
if (cribsRecipeMap.get(cribsSlotHash)
.maxParallelCalculatedByInputs(1, inputFluids, inputItems) == 1) {
return validateAndCalculateRecipe(cribsRecipeMap.get(cribsSlotHash)).checkRecipeResult;
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;
}

Expand Down Expand Up @@ -171,15 +189,6 @@ public CheckRecipeResult process() {
return checkRecipeResult;
}

public GTRecipe getRecipeByInputs(ItemStack[] inItems, FluidStack[] inFluids) {
RecipeMap<?> map = preProcess();
if (map == null) return null;
return map.findRecipeQuery()
.items(inItems)
.fluids(inFluids)
.find();
}

/**
* Checks if supplied recipe is valid for process. This involves voltage check, output full check. If successful,
* additionally performs input consumption, output calculation with parallel, and overclock calculation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -887,18 +887,13 @@ protected CheckRecipeResult doCheckRecipe() {
// check crafting input hatches first
for (IDualInputHatch dualInputHatch : mDualInputHatches) {
ItemStack[] sharedItems = dualInputHatch.getSharedItems();
if (dualInputHatch.needClearRecipeMap()) processingLogic.resetCribsRecipeMap();
for (var it = dualInputHatch.inventories(); it.hasNext();) {
IDualInputInventory slot = it.next();
int slotHash = slot.hashCode();

if (!slot.isEmpty()) {
if (!processingLogic.cribsHasRecipe(slotHash)
&& !processingLogic.setCribsSlotRecipe(slot.getPatternInputs(), slotHash)) continue;
if (!slot.isEmpty() && processingLogic.craftingPatternHandler(slot)) {

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

CheckRecipeResult foundResult = processingLogic.process();
if (foundResult.wasSuccessful()) {
Expand Down Expand Up @@ -1727,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 @@ -2056,12 +2052,8 @@ public void getWailaBody(ItemStack itemStack, List<String> currentTip, IWailaDat
}
}
}
currentTip.add(
GTWaila.getMachineProgressString(
isActive,
tag.getBoolean("isAllowedToWork"),
miozune marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -2110,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());
miozune marked this conversation as resolved.
Show resolved Hide resolved
if (tileEntity.isActive()) {
if (mEUt < 0) tag.setLong("energyUsage", getActualEnergyUsage());
else tag.setLong("energyUsage", (long) -mEUt * mEfficiency / 10000);
Expand Down Expand Up @@ -2433,7 +2424,6 @@ public UITexture getMachineModeIcon(int index) {
@Override
public void setMachineMode(int index) {
machineMode = index;
processingLogic.resetCribsRecipeMap();
}

@Override
Expand Down
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 @@ -21,5 +23,5 @@ public interface IDualInputHatch {

ItemStack[] getSharedItems();

boolean needClearRecipeMap();
void setProcessingLogic(ProcessingLogic pl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
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;
Expand Down Expand Up @@ -221,6 +222,11 @@ public ICraftingPatternDetails getPatternDetails() {
return patternDetails;
}

@Override
public int hashCode() {
return Objects.hashCode(patternDetails);
Dream-Master marked this conversation as resolved.
Show resolved Hide resolved
}

public GTDualInputs getPatternInputs() {
miozune marked this conversation as resolved.
Show resolved Hide resolved
GTDualInputs dualInputs = new GTDualInputs();

Expand Down Expand Up @@ -350,7 +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 boolean needClearRecipeMap;
public ArrayList<ProcessingLogic> processingLogics = new ArrayList<>();
miozune marked this conversation as resolved.
Show resolved Hide resolved

// holds all internal inventories
private final PatternSlot[] internalInventory = new PatternSlot[MAX_PATTERN_COUNT];
Expand Down Expand Up @@ -812,7 +818,6 @@ private void onPatternChange(int index, ItemStack newItem) {
if (originalPattern.hasChanged(newItem, world)) {
try {
originalPattern.refund(getProxy(), getRequest());
needClearRecipeMap = true;
} catch (GridAccessException ignored) {}
internalInventory[index] = null;
needPatternSync = true;
Expand All @@ -839,6 +844,23 @@ public ItemStack[] getSharedItems() {
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;
miozune marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1011,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 @@ -1044,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))
miozune marked this conversation as resolved.
Show resolved Hide resolved
.build()
.setPos(7, 7));
return builder.build();
Expand Down Expand Up @@ -1098,11 +1127,6 @@ public List<ItemStack> getItemsForHoloGlasses() {
return list;
}

@Override
public boolean needClearRecipeMap() {
return needClearRecipeMap;
}

public void doublePatterns(int val) {
boolean fast = (val & 1) != 0;
boolean backwards = (val & 2) != 0;
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 @@ -298,7 +299,7 @@ public List<ItemStack> getItemsForHoloGlasses() {
}

@Override
public boolean needClearRecipeMap() {
return getMaster() != null && getMaster().needClearRecipeMap();
public void setProcessingLogic(ProcessingLogic pl) {
if (getMaster() != null) getMaster().setProcessingLogic(pl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -438,18 +438,13 @@ protected CheckRecipeResult doCheckRecipe() {
// check crafting input hatches first
for (IDualInputHatch dualInputHatch : mDualInputHatches) {
ItemStack[] sharedItems = dualInputHatch.getSharedItems();
if (dualInputHatch.needClearRecipeMap()) processingLogic.resetCribsRecipeMap();
for (var it = dualInputHatch.inventories(); it.hasNext();) {
IDualInputInventory slot = it.next();
int slotHash = slot.hashCode();

if (!slot.isEmpty()) {
if (!processingLogic.cribsHasRecipe(slotHash)
&& !processingLogic.setCribsSlotRecipe(slot.getPatternInputs(), slotHash)) continue;
if (!slot.isEmpty() && processingLogic.craftingPatternHandler(slot)) {

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

CheckRecipeResult foundResult = processingLogic.process();
miozune marked this conversation as resolved.
Show resolved Hide resolved
if (foundResult.wasSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,26 +534,6 @@ protected Stream<GTRecipe> findRecipeMatches(@Nullable RecipeMap<?> map) {
}
}

@Override
public GTRecipe getRecipeByInputs(ItemStack[] inItems, FluidStack[] inFluids) {
RecipeMap<?> map;
switch (getModeFromCircuit(inItems)) {
case MACHINEMODE_COMPRESSOR -> {
map = RecipeMaps.compressorRecipes;
}
case MACHINEMODE_BLACKHOLE -> {
map = RecipeMaps.neutroniumCompressorRecipes;
}
default -> {
return null;
}
}
return map.findRecipeQuery()
.items(inItems)
.fluids(inFluids)
.find();
}

@NotNull
@Override
protected OverclockCalculator createOverclockCalculator(@NotNull GTRecipe recipe) {
Expand Down