RecipeCache .java

net.minecraft.world.item.crafting.RecipeCache (class): members: cachedRecipeManager, entries, Entry, get. imports: NonNullList, ItemStack, ServerLevel.

net.minecraft.world.item.craftingclass86 linescommon2.7 KB

API Surface (2)

Optional<RecipeHolder<CraftingRecipe>> get(final ServerLevel level, final CraftingInput input)
boolean matches(final CraftingInput input)

Fields & Constants (2)

entries
cachedRecipeManager

Full Source

RecipeCache.java86 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.crafting;
2
3
import java.lang.ref.WeakReference;
4
import java.util.Arrays;
5
import java.util.Optional;
6
import net.minecraft.core.NonNullList;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.world.item.ItemStack;
9
import org.jspecify.annotations.Nullable;
10
11
public class RecipeCache {
12
	private final RecipeCache.@Nullable Entry[] entries;
13
	private WeakReference<@Nullable RecipeManager> cachedRecipeManager = new WeakReference<>(null);
14
15
	public RecipeCache(final int capacity) {
16
		this.entries = new RecipeCache.Entry[capacity];
17
	}
18
19
	public Optional<RecipeHolder<CraftingRecipe>> get(final ServerLevel level, final CraftingInput input) {
20
		if (input.isEmpty()) {
21
			return Optional.empty();
22
		}
23
24
		this.validateRecipeManager(level);
25
26
		for (int i = 0; i < this.entries.length; i++) {
27
			RecipeCache.Entry entry = this.entries[i];
28
			if (entry != null && entry.matches(input)) {
29
				this.moveEntryToFront(i);
30
				return Optional.ofNullable(entry.value());
31
			}
32
		}
33
34
		return this.compute(input, level);
35
	}
36
37
	private void validateRecipeManager(final ServerLevel level) {
38
		RecipeManager recipeManager = level.recipeAccess();
39
		if (recipeManager != this.cachedRecipeManager.get()) {
40
			this.cachedRecipeManager = new WeakReference<>(recipeManager);
41
			Arrays.fill(this.entries, null);
42
		}
43
	}
44
45
	private Optional<RecipeHolder<CraftingRecipe>> compute(final CraftingInput input, final ServerLevel level) {
46
		Optional<RecipeHolder<CraftingRecipe>> recipe = level.recipeAccess().getRecipeFor(RecipeType.CRAFTING, input, level);
47
		this.insert(input, recipe.orElse(null));
48
		return recipe;
49
	}
50
51
	private void moveEntryToFront(final int index) {
52
		if (index > 0) {
53
			RecipeCache.Entry entry = this.entries[index];
54
			System.arraycopy(this.entries, 0, this.entries, 1, index);
55
			this.entries[0] = entry;
56
		}
57
	}
58
59
	private void insert(final CraftingInput input, final @Nullable RecipeHolder<CraftingRecipe> recipe) {
60
		NonNullList<ItemStack> key = NonNullList.withSize(input.size(), ItemStack.EMPTY);
61
62
		for (int i = 0; i < input.size(); i++) {
63
			key.set(i, input.getItem(i).copyWithCount(1));
64
		}
65
66
		System.arraycopy(this.entries, 0, this.entries, 1, this.entries.length - 1);
67
		this.entries[0] = new RecipeCache.Entry(key, input.width(), input.height(), recipe);
68
	}
69
70
	private record Entry(NonNullList<ItemStack> key, int width, int height, @Nullable RecipeHolder<CraftingRecipe> value) {
71
		public boolean matches(final CraftingInput input) {
72
			if (this.width == input.width() && this.height == input.height()) {
73
				for (int i = 0; i < this.key.size(); i++) {
74
					if (!ItemStack.isSameItemSameComponents(this.key.get(i), input.getItem(i))) {
75
						return false;
76
					}
77
				}
78
79
				return true;
80
			} else {
81
				return false;
82
			}
83
		}
84
	}
85
}
86

Decompiled Minecraft 26.3 source of RecipeCache (net/minecraft/world/item/crafting/RecipeCache.java) with Mojang official mappings, stored verbatim on the site.