Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More recipe caching #197

Open
wants to merge 2 commits into
base: 1.20
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -90,16 +92,6 @@ public List<CompressedIngredient> 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++) {
Expand All @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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;
Expand Down