diff --git a/src/main/java/mcjty/rftoolsutility/modules/crafter/blocks/CrafterBaseTE.java b/src/main/java/mcjty/rftoolsutility/modules/crafter/blocks/CrafterBaseTE.java index 9e397b09..1af06d03 100644 --- a/src/main/java/mcjty/rftoolsutility/modules/crafter/blocks/CrafterBaseTE.java +++ b/src/main/java/mcjty/rftoolsutility/modules/crafter/blocks/CrafterBaseTE.java @@ -39,6 +39,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.Recipe; +import net.minecraft.world.item.crafting.RecipeType; import net.minecraft.world.item.crafting.ShapedRecipe; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; @@ -120,9 +121,21 @@ private void clearCacheOrUpdateRecipe(Integer slot) { for (int i = 0; i < 9; i++) { workInventory.setItem(i, items.getStackInSlot(i + SLOT_CRAFTINPUT).copy()); } - Recipe recipe = CraftingRecipe.findRecipe(level, workInventory); - if (recipe != null) { - ItemStack result = BaseRecipe.assemble(recipe, workInventory, level); + Recipe matchingRecipe = null; + for (CraftingRecipe recipe : recipes) { + Recipe cachedRecipe = recipe.getCachedRecipe(level); + if (cachedRecipe != null && cachedRecipe.matches(workInventory, level)) { + matchingRecipe = cachedRecipe; + break; + } + } + if (matchingRecipe == null) { + matchingRecipe = level.getRecipeManager() + .getRecipeFor(RecipeType.CRAFTING, workInventory, level) + .orElse(null); + } + if (matchingRecipe != null) { + ItemStack result = BaseRecipe.assemble(matchingRecipe, workInventory, level); items.setStackInSlot(SLOT_CRAFTOUTPUT, result); } else { items.setStackInSlot(SLOT_CRAFTOUTPUT, ItemStack.EMPTY); diff --git a/src/main/java/mcjty/rftoolsutility/modules/crafter/data/CraftingRecipe.java b/src/main/java/mcjty/rftoolsutility/modules/crafter/data/CraftingRecipe.java index 15daddaa..9e780abc 100644 --- a/src/main/java/mcjty/rftoolsutility/modules/crafter/data/CraftingRecipe.java +++ b/src/main/java/mcjty/rftoolsutility/modules/crafter/data/CraftingRecipe.java @@ -1,16 +1,17 @@ package mcjty.rftoolsutility.modules.crafter.data; +import com.mojang.datafixers.util.Pair; import mcjty.lib.varia.InventoryTools; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.nbt.Tag; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.CraftingContainer; import net.minecraft.world.inventory.TransientCraftingContainer; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Recipe; -import net.minecraft.world.item.crafting.RecipeManager; import net.minecraft.world.item.crafting.RecipeType; import net.minecraft.world.level.Level; @@ -33,6 +34,7 @@ public ItemStack quickMoveStack(Player player, int slot) { }, 3, 3); private ItemStack result = ItemStack.EMPTY; + private ResourceLocation recipeId; private boolean recipePresent = false; private Recipe recipe = null; @@ -90,16 +92,6 @@ public List getCompressedIngredients() { return compressedIngredients; } - public static Recipe findRecipe(Level world, CraftingContainer inv) { - RecipeManager recipeManager = world.getRecipeManager(); - for (Recipe r : recipeManager.getRecipes()) { - if (r != null && RecipeType.CRAFTING.equals(r.getType()) && r.matches(inv, world)) { - return r; - } - } - return null; - } - public void readFromNBT(CompoundTag tagCompound) { ListTag nbtTagList = tagCompound.getList("Items", Tag.TAG_COMPOUND); for (int i = 0; i < nbtTagList.size(); i++) { @@ -109,6 +101,7 @@ public void readFromNBT(CompoundTag tagCompound) { result = ItemStack.of(resultCompound); keepOne = tagCompound.getBoolean("Keep") ? KeepMode.KEEP : KeepMode.ALL; craftMode = CraftMode.values()[tagCompound.getByte("Int")]; + recipeId = ResourceLocation.tryParse(tagCompound.getString("RecipeId")); recipePresent = false; } @@ -130,6 +123,9 @@ public void writeToNBT(CompoundTag tagCompound) { tagCompound.put("Items", nbtTagList); tagCompound.putBoolean("Keep", keepOne == KeepMode.KEEP); tagCompound.putByte("Int", (byte) craftMode.ordinal()); + if (recipe != null) { + tagCompound.putString("RecipeId", recipe.getId().toString()); + } } public void setRecipe(ItemStack[] items, ItemStack result) { @@ -155,7 +151,8 @@ public ItemStack getResult() { public Recipe getCachedRecipe(Level world) { if (!recipePresent) { recipePresent = true; - recipe = findRecipe(world, inv); + recipe = world.getRecipeManager().getRecipeFor(RecipeType.CRAFTING, inv, world, recipeId).map(Pair::getSecond).orElse(null); + recipeId = recipe != null ? recipe.getId() : null; compressedIngredients = null; } return recipe;