Skip to content

Commit

Permalink
put cache construction into context
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude committed Nov 16, 2024
1 parent 929f27c commit b33fafb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
33 changes: 14 additions & 19 deletions src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.items.IItemHandlerModifiable;

import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static gregtech.api.GTValues.ULV;
import static gregtech.api.recipes.logic.OverclockingLogic.*;
Expand Down Expand Up @@ -75,18 +73,15 @@ public abstract class AbstractRecipeLogic extends MTETrait implements IWorkable,
protected int maxProgressTime;
protected long recipeEUt;
protected List<FluidStack> fluidOutputs;
protected final Map<FluidStack, Integer> fluidChancesCache = new Object2IntOpenCustomHashMap<>(
FluidStackHashStrategy.builder()
.compareFluid(true)
.build());
protected List<ItemStack> itemOutputs;
protected final Map<ItemStack, Integer> itemChancesCache = new Object2IntOpenCustomHashMap<>(
ItemStackHashStrategy.builder()
.compareItem(true)
.compareDamage(true)
.build());
private final RecipeContext<ItemStack> itemContext = new RecipeContext<>(itemChancesCache);
private final RecipeContext<FluidStack> fluidContext = new RecipeContext<>(fluidChancesCache);

private final RecipeContext<ItemStack> itemContext = new RecipeContext<>(ItemStackHashStrategy.builder()
.compareItem(true)
.compareDamage(true)
.build());
private final RecipeContext<FluidStack> fluidContext = new RecipeContext<>(FluidStackHashStrategy.builder()
.compareFluid(true)
.build());

protected boolean isActive;
protected boolean workingEnabled = true;
Expand Down Expand Up @@ -409,8 +404,8 @@ protected void trySearchNewRecipe() {

// we found a new recipe, clear the cache
if (this.previousRecipe != null && !currentRecipe.equals(this.previousRecipe)) {
this.itemChancesCache.clear();
this.fluidChancesCache.clear();
this.itemContext.getCache().clear();
this.fluidContext.getCache().clear();
}
this.previousRecipe = currentRecipe;
}
Expand Down Expand Up @@ -1237,13 +1232,13 @@ public NBTTagCompound serializeNBT() {
compound.setTag("FluidOutputs", fluidOutputsList);

NBTTagList itemCache = new NBTTagList();
for (var entry : itemChancesCache.entrySet()) {
for (var entry : itemContext.getCache().entrySet()) {
var tag = entry.getKey().serializeNBT();
tag.setInteger("CachedChance", entry.getValue());
itemCache.appendTag(tag);
}
NBTTagList fluidCache = new NBTTagList();
for (var entry : fluidChancesCache.entrySet()) {
for (var entry : fluidContext.getCache().entrySet()) {
var tag = entry.getKey().writeToNBT(new NBTTagCompound());
tag.setInteger("CachedChance", entry.getValue());
fluidCache.appendTag(tag);
Expand Down Expand Up @@ -1281,14 +1276,14 @@ public void deserializeNBT(@NotNull NBTTagCompound compound) {
for (int i = 0; i < itemCache.tagCount(); i++) {
var stack = itemCache.getCompoundTagAt(i);
int cache = stack.getInteger("CachedChance");
this.itemChancesCache.put(new ItemStack(stack), cache);
this.itemContext.updateCachedChance(new ItemStack(stack), cache);
}

NBTTagList fluidCache = compound.getTagList("FluidChanceCache", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < fluidCache.tagCount(); i++) {
var stack = fluidCache.getCompoundTagAt(i);
int cache = stack.getInteger("CachedChance");
this.fluidChancesCache.put(FluidStack.loadFluidStackFromNBT(stack), cache);
this.fluidContext.updateCachedChance(FluidStack.loadFluidStackFromNBT(stack), cache);
}
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/gregtech/api/recipes/RecipeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import gregtech.api.recipes.chance.boost.ChanceBoostFunction;
import gregtech.api.recipes.chance.output.ChancedOutput;

import it.unimi.dsi.fastutil.Hash;
import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap;

import java.util.Map;

public class RecipeContext<I> {
Expand All @@ -12,8 +15,8 @@ public class RecipeContext<I> {
ChanceBoostFunction boostFunction;
public int baseTier, machineTier;

public RecipeContext(Map<I, Integer> cache) {
this.cache = cache;
public RecipeContext(Hash.Strategy<I> strategy) {
this.cache = new Object2IntOpenCustomHashMap<>(strategy);
}

public RecipeContext() {
Expand All @@ -29,8 +32,12 @@ public RecipeContext<I> update(ChanceBoostFunction boostFunction,
}

public void updateCachedChance(ChancedOutput<I> entry, int chance) {
updateCachedChance(entry.getIngredient(), chance % entry.getMaxChance());
}

public void updateCachedChance(I ingredient, int chance) {
if (cache == null) return;
cache.put(entry.getIngredient(), chance % entry.getMaxChance());
cache.put(ingredient, chance);
}

public int getCachedChance(ChancedOutput<I> entry) {
Expand All @@ -52,4 +59,8 @@ public int getChance(ChancedOutput<I> entry) {
public int boostChance(BoostableChanceEntry<?> entry) {
return boostFunction.getBoostedChance(entry, baseTier, machineTier);
}

public Map<I, Integer> getCache() {
return cache;
}
}

0 comments on commit b33fafb

Please sign in to comment.