Skip to content

Commit

Permalink
chore: add utility method for matching recipes with the last know rec…
Browse files Browse the repository at this point in the history
…ipe id first before doing a lookup
  • Loading branch information
Elenterius committed Oct 4, 2024
1 parent e6656c5 commit 707840e
Showing 1 changed file with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.elenterius.biomancy.crafting.recipe;

import com.mojang.datafixers.util.Pair;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
Expand All @@ -10,6 +11,7 @@
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Optional;

public class SimpleRecipeType<T extends Recipe<?>> implements RecipeType<T> {
Expand Down Expand Up @@ -50,18 +52,36 @@ public Optional<R> getRecipeFromContainer(Level level, Container inputInventory)
return (R) recipe;
}

private boolean matches(R recipe, ItemStack stack) {
for (Ingredient ingredient : recipe.getIngredients()) {
if (ingredient.test(stack)) return true;
}
return false;
}

public Optional<R> getRecipeForIngredient(Level level, ItemStack stack) {
RecipeManager recipeManager = level.getRecipeManager();
return recipeManager.byType(this).values().stream()
.filter(recipe -> {
for (Ingredient ingredient : recipe.getIngredients()) {
if (ingredient.test(stack)) return true;
}
return false;
})
.filter(recipe -> matches(recipe, stack))
.findFirst().map(this::castRecipe);
}

public Optional<Pair<ResourceLocation, R>> getRecipeForIngredient(Level level, ItemStack stack, @Nullable ResourceLocation lastRecipeId) {
RecipeManager recipeManager = level.getRecipeManager();

Map<ResourceLocation, R> map = recipeManager.byType(this);
if (lastRecipeId != null) {
R recipe = map.get(lastRecipeId);
if (recipe != null && matches(recipe, stack)) {
return Optional.of(Pair.of(lastRecipeId, recipe));
}
}

return map.entrySet().stream()
.filter(entry -> matches(entry.getValue(), stack))
.findFirst()
.map(entry -> Pair.of(entry.getKey(), entry.getValue()));
}
}

}

0 comments on commit 707840e

Please sign in to comment.