Skip to content

Commit b33fafb

Browse files
committed
put cache construction into context
1 parent 929f27c commit b33fafb

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,12 @@
4040
import net.minecraftforge.fluids.IFluidTank;
4141
import net.minecraftforge.items.IItemHandlerModifiable;
4242

43-
import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap;
4443
import org.jetbrains.annotations.MustBeInvokedByOverriders;
4544
import org.jetbrains.annotations.NotNull;
4645
import org.jetbrains.annotations.Nullable;
4746

4847
import java.util.ArrayList;
4948
import java.util.List;
50-
import java.util.Map;
5149

5250
import static gregtech.api.GTValues.ULV;
5351
import static gregtech.api.recipes.logic.OverclockingLogic.*;
@@ -75,18 +73,15 @@ public abstract class AbstractRecipeLogic extends MTETrait implements IWorkable,
7573
protected int maxProgressTime;
7674
protected long recipeEUt;
7775
protected List<FluidStack> fluidOutputs;
78-
protected final Map<FluidStack, Integer> fluidChancesCache = new Object2IntOpenCustomHashMap<>(
79-
FluidStackHashStrategy.builder()
80-
.compareFluid(true)
81-
.build());
8276
protected List<ItemStack> itemOutputs;
83-
protected final Map<ItemStack, Integer> itemChancesCache = new Object2IntOpenCustomHashMap<>(
84-
ItemStackHashStrategy.builder()
85-
.compareItem(true)
86-
.compareDamage(true)
87-
.build());
88-
private final RecipeContext<ItemStack> itemContext = new RecipeContext<>(itemChancesCache);
89-
private final RecipeContext<FluidStack> fluidContext = new RecipeContext<>(fluidChancesCache);
77+
78+
private final RecipeContext<ItemStack> itemContext = new RecipeContext<>(ItemStackHashStrategy.builder()
79+
.compareItem(true)
80+
.compareDamage(true)
81+
.build());
82+
private final RecipeContext<FluidStack> fluidContext = new RecipeContext<>(FluidStackHashStrategy.builder()
83+
.compareFluid(true)
84+
.build());
9085

9186
protected boolean isActive;
9287
protected boolean workingEnabled = true;
@@ -409,8 +404,8 @@ protected void trySearchNewRecipe() {
409404

410405
// we found a new recipe, clear the cache
411406
if (this.previousRecipe != null && !currentRecipe.equals(this.previousRecipe)) {
412-
this.itemChancesCache.clear();
413-
this.fluidChancesCache.clear();
407+
this.itemContext.getCache().clear();
408+
this.fluidContext.getCache().clear();
414409
}
415410
this.previousRecipe = currentRecipe;
416411
}
@@ -1237,13 +1232,13 @@ public NBTTagCompound serializeNBT() {
12371232
compound.setTag("FluidOutputs", fluidOutputsList);
12381233

12391234
NBTTagList itemCache = new NBTTagList();
1240-
for (var entry : itemChancesCache.entrySet()) {
1235+
for (var entry : itemContext.getCache().entrySet()) {
12411236
var tag = entry.getKey().serializeNBT();
12421237
tag.setInteger("CachedChance", entry.getValue());
12431238
itemCache.appendTag(tag);
12441239
}
12451240
NBTTagList fluidCache = new NBTTagList();
1246-
for (var entry : fluidChancesCache.entrySet()) {
1241+
for (var entry : fluidContext.getCache().entrySet()) {
12471242
var tag = entry.getKey().writeToNBT(new NBTTagCompound());
12481243
tag.setInteger("CachedChance", entry.getValue());
12491244
fluidCache.appendTag(tag);
@@ -1281,14 +1276,14 @@ public void deserializeNBT(@NotNull NBTTagCompound compound) {
12811276
for (int i = 0; i < itemCache.tagCount(); i++) {
12821277
var stack = itemCache.getCompoundTagAt(i);
12831278
int cache = stack.getInteger("CachedChance");
1284-
this.itemChancesCache.put(new ItemStack(stack), cache);
1279+
this.itemContext.updateCachedChance(new ItemStack(stack), cache);
12851280
}
12861281

12871282
NBTTagList fluidCache = compound.getTagList("FluidChanceCache", Constants.NBT.TAG_COMPOUND);
12881283
for (int i = 0; i < fluidCache.tagCount(); i++) {
12891284
var stack = fluidCache.getCompoundTagAt(i);
12901285
int cache = stack.getInteger("CachedChance");
1291-
this.fluidChancesCache.put(FluidStack.loadFluidStackFromNBT(stack), cache);
1286+
this.fluidContext.updateCachedChance(FluidStack.loadFluidStackFromNBT(stack), cache);
12921287
}
12931288
}
12941289
}

src/main/java/gregtech/api/recipes/RecipeContext.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import gregtech.api.recipes.chance.boost.ChanceBoostFunction;
55
import gregtech.api.recipes.chance.output.ChancedOutput;
66

7+
import it.unimi.dsi.fastutil.Hash;
8+
import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap;
9+
710
import java.util.Map;
811

912
public class RecipeContext<I> {
@@ -12,8 +15,8 @@ public class RecipeContext<I> {
1215
ChanceBoostFunction boostFunction;
1316
public int baseTier, machineTier;
1417

15-
public RecipeContext(Map<I, Integer> cache) {
16-
this.cache = cache;
18+
public RecipeContext(Hash.Strategy<I> strategy) {
19+
this.cache = new Object2IntOpenCustomHashMap<>(strategy);
1720
}
1821

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

3134
public void updateCachedChance(ChancedOutput<I> entry, int chance) {
35+
updateCachedChance(entry.getIngredient(), chance % entry.getMaxChance());
36+
}
37+
38+
public void updateCachedChance(I ingredient, int chance) {
3239
if (cache == null) return;
33-
cache.put(entry.getIngredient(), chance % entry.getMaxChance());
40+
cache.put(ingredient, chance);
3441
}
3542

3643
public int getCachedChance(ChancedOutput<I> entry) {
@@ -52,4 +59,8 @@ public int getChance(ChancedOutput<I> entry) {
5259
public int boostChance(BoostableChanceEntry<?> entry) {
5360
return boostFunction.getBoostedChance(entry, baseTier, machineTier);
5461
}
62+
63+
public Map<I, Integer> getCache() {
64+
return cache;
65+
}
5566
}

0 commit comments

Comments
 (0)