Skip to content

Commit

Permalink
feat: improve vat recipe lookup performance
Browse files Browse the repository at this point in the history
  • Loading branch information
klikli-dev committed Aug 30, 2024
1 parent 8b4eaed commit 9adeb79
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.fluids.FluidStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
Expand All @@ -27,7 +28,11 @@ public class DigestionCachedCheck implements RecipeManager.CachedCheck<ItemHandl
private final RecipeType<DigestionRecipe> type;
private final RecipeManager.CachedCheck<ItemHandlerWithFluidRecipeInput, DigestionRecipe> internal;
@Nullable
private ResourceLocation lastRecipe;
private ResourceLocation lastRecipeForFluidStack;
@Nullable
private ResourceLocation lastRecipeForItemStack;
@Nullable
private ResourceLocation lastRecipeForItemStackCollection;

public DigestionCachedCheck(RecipeType<DigestionRecipe> type) {
this.type = type;
Expand Down Expand Up @@ -90,10 +95,10 @@ private Optional<RecipeHolder<DigestionRecipe>> getRecipeFor(FluidStack stack, L
* This only checks ingredients, including ingredients already present, not fluids
*/
public Optional<RecipeHolder<DigestionRecipe>> getRecipeFor(Collection<ItemStack> input, Level level) {
var optional = this.getRecipeFor(input, level, this.lastRecipe);
var optional = this.getRecipeFor(input, level, this.lastRecipeForItemStackCollection);
if (optional.isPresent()) {
var recipeHolder = optional.get();
this.lastRecipe = recipeHolder.id();
this.lastRecipeForItemStackCollection = recipeHolder.id();
return optional;
} else {
return Optional.empty();
Expand All @@ -104,10 +109,10 @@ public Optional<RecipeHolder<DigestionRecipe>> getRecipeFor(Collection<ItemStack
* This only checks ingredients, not fluids
*/
public Optional<RecipeHolder<DigestionRecipe>> getRecipeFor(ItemStack stack, Level level) {
var optional = this.getRecipeFor(stack, level, this.lastRecipe);
var optional = this.getRecipeFor(stack, level, this.lastRecipeForItemStack);
if (optional.isPresent()) {
var recipeHolder = optional.get();
this.lastRecipe = recipeHolder.id();
this.lastRecipeForItemStack = recipeHolder.id();
return optional;
} else {
return Optional.empty();
Expand All @@ -118,10 +123,10 @@ public Optional<RecipeHolder<DigestionRecipe>> getRecipeFor(ItemStack stack, Lev
* This only checks fluids, not ingredients
*/
public Optional<RecipeHolder<DigestionRecipe>> getRecipeFor(FluidStack stack, Level level) {
var optional = this.getRecipeFor(stack, level, this.lastRecipe);
var optional = this.getRecipeFor(stack, level, this.lastRecipeForFluidStack);
if (optional.isPresent()) {
var recipeHolder = optional.get();
this.lastRecipe = recipeHolder.id();
this.lastRecipeForFluidStack = recipeHolder.id();
return optional;
} else {
return Optional.empty();
Expand All @@ -132,12 +137,7 @@ public Optional<RecipeHolder<DigestionRecipe>> getRecipeFor(FluidStack stack, Le
* This checks full recipe validity: ingredients + fluids
*/
@Override
public Optional<RecipeHolder<DigestionRecipe>> getRecipeFor(ItemHandlerWithFluidRecipeInput container, Level level) {
var recipe = this.internal.getRecipeFor(container, level);
if (recipe.isPresent()) {
this.lastRecipe = recipe.get().id();
}

return recipe;
public @NotNull Optional<RecipeHolder<DigestionRecipe>> getRecipeFor(@NotNull ItemHandlerWithFluidRecipeInput container, @NotNull Level level) {
return this.internal.getRecipeFor(container, level);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.items.IItemHandlerModifiable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
Expand All @@ -32,7 +33,11 @@ public class FermentationCachedCheck implements RecipeManager.CachedCheck<ItemHa
private final RecipeType<FermentationRecipe> type;
private final RecipeManager.CachedCheck<ItemHandlerWithFluidRecipeInput, FermentationRecipe> internal;
@Nullable
private ResourceLocation lastRecipe;
private ResourceLocation lastRecipeForFluidStack;
@Nullable
private ResourceLocation lastRecipeForItemStack;
@Nullable
private ResourceLocation lastRecipeForItemStackCollection;

public FermentationCachedCheck(RecipeType<FermentationRecipe> type) {
this.type = type;
Expand Down Expand Up @@ -94,10 +99,10 @@ private Optional<RecipeHolder<FermentationRecipe>> getRecipeFor(FluidStack stack
* This only checks ingredients, including ingredients already present, not fluids
*/
public Optional<RecipeHolder<FermentationRecipe>> getRecipeFor(Collection<ItemStack> input, Level level) {
var optional = this.getRecipeFor(input, level, this.lastRecipe);
var optional = this.getRecipeFor(input, level, this.lastRecipeForItemStackCollection);
if (optional.isPresent()) {
var recipeHolder = optional.get();
this.lastRecipe = recipeHolder.id();
this.lastRecipeForItemStackCollection = recipeHolder.id();
return optional;
} else {
return Optional.empty();
Expand All @@ -108,10 +113,10 @@ public Optional<RecipeHolder<FermentationRecipe>> getRecipeFor(Collection<ItemSt
* This only checks ingredients, not fluids
*/
public Optional<RecipeHolder<FermentationRecipe>> getRecipeFor(ItemStack stack, Level level) {
var optional = this.getRecipeFor(stack, level, this.lastRecipe);
var optional = this.getRecipeFor(stack, level, this.lastRecipeForItemStack);
if (optional.isPresent()) {
var recipeHolder = optional.get();
this.lastRecipe = recipeHolder.id();
this.lastRecipeForItemStack = recipeHolder.id();
return optional;
} else {
return Optional.empty();
Expand All @@ -122,10 +127,10 @@ public Optional<RecipeHolder<FermentationRecipe>> getRecipeFor(ItemStack stack,
* This only checks fluids, not ingredients
*/
public Optional<RecipeHolder<FermentationRecipe>> getRecipeFor(FluidStack stack, Level level) {
var optional = this.getRecipeFor(stack, level, this.lastRecipe);
var optional = this.getRecipeFor(stack, level, this.lastRecipeForFluidStack);
if (optional.isPresent()) {
var recipeHolder = optional.get();
this.lastRecipe = recipeHolder.id();
this.lastRecipeForFluidStack = recipeHolder.id();
return optional;
} else {
return Optional.empty();
Expand All @@ -136,12 +141,7 @@ public Optional<RecipeHolder<FermentationRecipe>> getRecipeFor(FluidStack stack,
* This checks full recipe validity: ingredients + fluids
*/
@Override
public Optional<RecipeHolder<FermentationRecipe>> getRecipeFor(ItemHandlerWithFluidRecipeInput container, Level level) {
var recipe = this.internal.getRecipeFor(container, level);
if (recipe.isPresent()) {
this.lastRecipe = recipe.get().id();
}

return recipe;
public @NotNull Optional<RecipeHolder<FermentationRecipe>> getRecipeFor(@NotNull ItemHandlerWithFluidRecipeInput container, @NotNull Level level) {
return this.internal.getRecipeFor(container, level);
}
}

0 comments on commit 9adeb79

Please sign in to comment.