RecipeManager .java

net.minecraft.world.item.crafting.RecipeManager (class, 255 lines): members: getSynchronizedStonecutterRecipes, getSynchronizedItemProperties, CachedCheck.

net.minecraft.world.item.craftingclass255 linescommon11.2 KB

Implements

API Surface (17)

void finalizeRecipeLoading(final FeatureFlagSet enabledFlags)
<I extends RecipeInput, T extends Recipe<I>> Optional<RecipeHolder<T>> getRecipeFor(final RecipeType<T> type, final I input, final Level level, final @Nullable ResourceKey<Recipe<?>> recipeHint)
<I extends RecipeInput, T extends Recipe<I>> Optional<RecipeHolder<T>> getRecipeFor(final RecipeType<T> type, final I input, final Level level, final @Nullable RecipeHolder<T> recipeHint)
<I extends RecipeInput, T extends Recipe<I>> Optional<RecipeHolder<T>> getRecipeFor(final RecipeType<T> type, final I input, final Level level)
Optional<RecipeHolder<?>> byKey(final ResourceKey<Recipe<?>> recipeId)
Map<ResourceKey<RecipePropertySet>, RecipePropertySet> getSynchronizedItemProperties()
SelectableRecipe.SingleInputSet<StonecutterRecipe> getSynchronizedStonecutterRecipes()
RecipePropertySet propertySet(final ResourceKey<RecipePropertySet> id)
SelectableRecipe.SingleInputSet<StonecutterRecipe> stonecutterRecipes()
Collection<RecipeHolder<?>> getRecipes()
Collection<RecipeHolder<?>> getLearnableRecipes()
void listDisplaysForRecipe(final ResourceKey<Recipe<?>> id, final Consumer<RecipeDisplayEntry> output)
<I extends RecipeInput, T extends Recipe<I>> RecipeManager.CachedCheck<I, T> createCheck(final RecipeType<T> type)
Optional<RecipeHolder<T>> getRecipeFor(final I input, final ServerLevel level)
void accept(final Recipe<?> recipe)
RecipePropertySet asPropertySet(final FeatureFlagSet enabledFeatures)
record ServerDisplayInfo(RecipeDisplayEntry display, RecipeHolder<?> parent)

Fields & Constants (10)

LOGGER
RECIPE_PROPERTY_SETS
RecipeInput
recipes
propertySets
stonecutterRecipes
allDisplays
recipeToDisplay
learnableRecipes
level

Full Source

RecipeManager.java255 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.crafting;
2
3
import com.mojang.logging.LogUtils;
4
import it.unimi.dsi.fastutil.objects.Object2IntMap;
5
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
6
import java.util.ArrayList;
7
import java.util.Collection;
8
import java.util.IdentityHashMap;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Optional;
12
import java.util.OptionalInt;
13
import java.util.function.Consumer;
14
import java.util.stream.Collectors;
15
import net.fabricmc.fabric.api.recipe.v1.FabricRecipeManager;
16
import net.minecraft.core.HolderLookup;
17
import net.minecraft.core.registries.Registries;
18
import net.minecraft.resources.ResourceKey;
19
import net.minecraft.server.level.ServerLevel;
20
import net.minecraft.world.flag.FeatureFlagSet;
21
import net.minecraft.world.item.crafting.display.RecipeDisplay;
22
import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
23
import net.minecraft.world.item.crafting.display.RecipeDisplayId;
24
import net.minecraft.world.level.Level;
25
import org.jspecify.annotations.Nullable;
26
import org.slf4j.Logger;
27
28
public class RecipeManager implements RecipeAccess, FabricRecipeManager {
29
	private static final Logger LOGGER = LogUtils.getLogger();
30
	private static final Map<ResourceKey<RecipePropertySet>, RecipeManager.IngredientExtractor> RECIPE_PROPERTY_SETS = Map.of(
31
		RecipePropertySet.SMITHING_ADDITION,
32
		recipe -> recipe instanceof SmithingRecipe smithingRecipe ? smithingRecipe.additionIngredient() : Optional.empty(),
33
		RecipePropertySet.SMITHING_BASE,
34
		recipe -> recipe instanceof SmithingRecipe smithingRecipe ? Optional.of(smithingRecipe.baseIngredient()) : Optional.empty(),
35
		RecipePropertySet.SMITHING_TEMPLATE,
36
		recipe -> recipe instanceof SmithingRecipe smithingRecipe ? smithingRecipe.templateIngredient() : Optional.empty(),
37
		RecipePropertySet.FURNACE_INPUT,
38
		forSingleInput(RecipeType.SMELTING),
39
		RecipePropertySet.BLAST_FURNACE_INPUT,
40
		forSingleInput(RecipeType.BLASTING),
41
		RecipePropertySet.SMOKER_INPUT,
42
		forSingleInput(RecipeType.SMOKING),
43
		RecipePropertySet.CAMPFIRE_INPUT,
44
		forSingleInput(RecipeType.CAMPFIRE_COOKING),
45
		RecipePropertySet.BREWING_INPUTS,
46
		recipe -> recipe instanceof BrewingRecipe brewingRecipe ? Optional.of(brewingRecipe.getInput().ingredient()) : Optional.empty(),
47
		RecipePropertySet.BREWING_REAGENTS,
48
		recipe -> recipe instanceof BrewingRecipe brewingRecipe ? Optional.of(brewingRecipe.getReagent().ingredient()) : Optional.empty()
49
	);
50
	private final RecipeMap recipes;
51
	private Map<ResourceKey<RecipePropertySet>, RecipePropertySet> propertySets = Map.of();
52
	private SelectableRecipe.SingleInputSet<StonecutterRecipe> stonecutterRecipes = SelectableRecipe.SingleInputSet.empty();
53
	private List<RecipeManager.ServerDisplayInfo> allDisplays = List.of();
54
	private Map<ResourceKey<Recipe<?>>, List<RecipeManager.ServerDisplayInfo>> recipeToDisplay = Map.of();
55
	private final Collection<RecipeHolder<?>> learnableRecipes;
56
57
	public RecipeManager(final HolderLookup.Provider registries) {
58
		HolderLookup.RegistryLookup<Recipe<?>> recipeRegistries = registries.lookupOrThrow(Registries.RECIPE);
59
		this.recipes = RecipeMap.create(recipeRegistries);
60
		this.learnableRecipes = this.recipes.values().stream().filter(r -> !r.value().isSpecial()).collect(Collectors.toUnmodifiableList());
61
	}
62
63
	public void finalizeRecipeLoading(final FeatureFlagSet enabledFlags) {
64
		List<SelectableRecipe.SingleInputEntry<StonecutterRecipe>> stonecutterRecipes = new ArrayList<>();
65
		List<RecipeManager.IngredientCollector> propertySetCollectors = RECIPE_PROPERTY_SETS.entrySet()
66
			.stream()
67
			.map(e -> new RecipeManager.IngredientCollector(e.getKey(), e.getValue()))
68
			.toList();
69
		this.recipes
70
			.values()
71
			.forEach(
72
				recipeHolder -> {
73
					Recipe<?> recipe = recipeHolder.value();
74
					if (!recipe.isSpecial() && recipe.placementInfo().isImpossibleToPlace()) {
75
						LOGGER.warn("Recipe {} can't be placed due to empty ingredients and will be ignored", recipeHolder.id().identifier());
76
					} else {
77
						propertySetCollectors.forEach(c -> c.accept(recipe));
78
						if (recipe instanceof StonecutterRecipe stonecutterRecipe) {
79
							RecipeHolder<StonecutterRecipe> castHolder = (RecipeHolder<StonecutterRecipe>)recipeHolder;
80
							if (isIngredientEnabled(enabledFlags, stonecutterRecipe.input()) && stonecutterRecipe.resultDisplay().isEnabled(enabledFlags)) {
81
								stonecutterRecipes.add(
82
									new SelectableRecipe.SingleInputEntry<>(stonecutterRecipe.input(), new SelectableRecipe<>(stonecutterRecipe.resultDisplay(), Optional.of(castHolder)))
83
								);
84
							}
85
						}
86
					}
87
				}
88
			);
89
		this.propertySets = propertySetCollectors.stream().collect(Collectors.toUnmodifiableMap(c -> c.key, c -> c.asPropertySet(enabledFlags)));
90
		this.stonecutterRecipes = new SelectableRecipe.SingleInputSet<>(stonecutterRecipes);
91
		this.allDisplays = unpackRecipeInfo(this.recipes.values(), enabledFlags);
92
		this.recipeToDisplay = this.allDisplays.stream().collect(Collectors.groupingBy(r -> r.parent.id(), IdentityHashMap::new, Collectors.toList()));
93
	}
94
95
	private static List<Ingredient> filterDisabled(final FeatureFlagSet enabledFlags, final List<Ingredient> ingredients) {
96
		ingredients.removeIf(e -> !isIngredientEnabled(enabledFlags, e));
97
		return ingredients;
98
	}
99
100
	private static boolean isIngredientEnabled(final FeatureFlagSet enabledFlags, final Ingredient ingredient) {
101
		return ingredient.items().allMatch(i -> i.value().isEnabled(enabledFlags));
102
	}
103
104
	public <I extends RecipeInput, T extends Recipe<I>> Optional<RecipeHolder<T>> getRecipeFor(
105
		final RecipeType<T> type, final I input, final Level level, final @Nullable ResourceKey<Recipe<?>> recipeHint
106
	) {
107
		RecipeHolder<T> hintedRecipe = recipeHint != null ? this.byKeyTyped(type, recipeHint) : null;
108
		return this.getRecipeFor(type, input, level, hintedRecipe);
109
	}
110
111
	public <I extends RecipeInput, T extends Recipe<I>> Optional<RecipeHolder<T>> getRecipeFor(
112
		final RecipeType<T> type, final I input, final Level level, final @Nullable RecipeHolder<T> recipeHint
113
	) {
114
		return recipeHint != null && recipeHint.value().matches(input, level) ? Optional.of(recipeHint) : this.getRecipeFor(type, input, level);
115
	}
116
117
	public <I extends RecipeInput, T extends Recipe<I>> Optional<RecipeHolder<T>> getRecipeFor(final RecipeType<T> type, final I input, final Level level) {
118
		return this.recipes.getRecipesFor(type, input, level).findFirst();
119
	}
120
121
	public Optional<RecipeHolder<?>> byKey(final ResourceKey<Recipe<?>> recipeId) {
122
		return Optional.ofNullable(this.recipes.byKey(recipeId));
123
	}
124
125
	private <T extends Recipe<?>> @Nullable RecipeHolder<T> byKeyTyped(final RecipeType<T> type, final ResourceKey<Recipe<?>> recipeId) {
126
		RecipeHolder<?> recipe = this.recipes.byKey(recipeId);
127
		return (RecipeHolder<T>)(recipe != null && recipe.value().getType().equals(type) ? recipe : null);
128
	}
129
130
	public Map<ResourceKey<RecipePropertySet>, RecipePropertySet> getSynchronizedItemProperties() {
131
		return this.propertySets;
132
	}
133
134
	public SelectableRecipe.SingleInputSet<StonecutterRecipe> getSynchronizedStonecutterRecipes() {
135
		return this.stonecutterRecipes;
136
	}
137
138
	@Override
139
	public RecipePropertySet propertySet(final ResourceKey<RecipePropertySet> id) {
140
		return this.propertySets.getOrDefault(id, RecipePropertySet.EMPTY);
141
	}
142
143
	@Override
144
	public SelectableRecipe.SingleInputSet<StonecutterRecipe> stonecutterRecipes() {
145
		return this.stonecutterRecipes;
146
	}
147
148
	public Collection<RecipeHolder<?>> getRecipes() {
149
		return this.recipes.values();
150
	}
151
152
	public Collection<RecipeHolder<?>> getLearnableRecipes() {
153
		return this.learnableRecipes;
154
	}
155
156
	public RecipeManager.@Nullable ServerDisplayInfo getRecipeFromDisplay(final RecipeDisplayId id) {
157
		int index = id.index();
158
		return index >= 0 && index < this.allDisplays.size() ? this.allDisplays.get(index) : null;
159
	}
160
161
	public void listDisplaysForRecipe(final ResourceKey<Recipe<?>> id, final Consumer<RecipeDisplayEntry> output) {
162
		List<RecipeManager.ServerDisplayInfo> recipes = this.recipeToDisplay.get(id);
163
		if (recipes != null) {
164
			recipes.forEach(e -> output.accept(e.display));
165
		}
166
	}
167
168
	public static <I extends RecipeInput, T extends Recipe<I>> RecipeManager.CachedCheck<I, T> createCheck(final RecipeType<T> type) {
169
		return new RecipeManager.CachedCheck<I, T>() {
170
			private @Nullable ResourceKey<Recipe<?>> lastRecipe;
171
172
			@Override
173
			public Optional<RecipeHolder<T>> getRecipeFor(final I input, final ServerLevel level) {
174
				RecipeManager recipeManager = level.recipeAccess();
175
				Optional<RecipeHolder<T>> result = recipeManager.getRecipeFor(type, input, level, this.lastRecipe);
176
				if (result.isPresent()) {
177
					RecipeHolder<T> unpackedResult = result.get();
178
					this.lastRecipe = unpackedResult.id();
179
					return Optional.of(unpackedResult);
180
				} else {
181
					return Optional.empty();
182
				}
183
			}
184
		};
185
	}
186
187
	private static List<RecipeManager.ServerDisplayInfo> unpackRecipeInfo(final Iterable<RecipeHolder<?>> recipes, final FeatureFlagSet enabledFeatures) {
188
		List<RecipeManager.ServerDisplayInfo> result = new ArrayList<>();
189
		Object2IntMap<String> recipeGroups = new Object2IntOpenHashMap<>();
190
191
		for (RecipeHolder<?> recipeHolder : recipes) {
192
			Recipe<?> recipe = recipeHolder.value();
193
			OptionalInt groupId;
194
			if (recipe.group().isEmpty()) {
195
				groupId = OptionalInt.empty();
196
			} else {
197
				groupId = OptionalInt.of(recipeGroups.computeIfAbsent(recipe.group(), idx -> recipeGroups.size()));
198
			}
199
200
			Optional<List<Ingredient>> placementCheck;
201
			if (recipe.isSpecial()) {
202
				placementCheck = Optional.empty();
203
			} else {
204
				placementCheck = Optional.of(recipe.placementInfo().ingredients());
205
			}
206
207
			for (RecipeDisplay recipeDisplay : recipe.display()) {
208
				if (recipeDisplay.isEnabled(enabledFeatures)) {
209
					int nextDisplayId = result.size();
210
					RecipeDisplayId id = new RecipeDisplayId(nextDisplayId);
211
					RecipeDisplayEntry entry = new RecipeDisplayEntry(id, recipeDisplay, groupId, recipe.recipeBookCategory(), placementCheck);
212
					result.add(new RecipeManager.ServerDisplayInfo(entry, recipeHolder));
213
				}
214
			}
215
		}
216
217
		return result;
218
	}
219
220
	private static RecipeManager.IngredientExtractor forSingleInput(final RecipeType<? extends SingleItemRecipe> type) {
221
		return recipe -> recipe.getType() == type && recipe instanceof SingleItemRecipe singleItemRecipe ? Optional.of(singleItemRecipe.input()) : Optional.empty();
222
	}
223
224
	public interface CachedCheck<I extends RecipeInput, T extends Recipe<I>> {
225
		Optional<RecipeHolder<T>> getRecipeFor(I input, ServerLevel level);
226
	}
227
228
	public static class IngredientCollector implements Consumer<Recipe<?>> {
229
		private final ResourceKey<RecipePropertySet> key;
230
		private final RecipeManager.IngredientExtractor extractor;
231
		private final List<Ingredient> ingredients = new ArrayList<>();
232
233
		protected IngredientCollector(final ResourceKey<RecipePropertySet> key, final RecipeManager.IngredientExtractor extractor) {
234
			this.key = key;
235
			this.extractor = extractor;
236
		}
237
238
		public void accept(final Recipe<?> recipe) {
239
			this.extractor.apply(recipe).ifPresent(this.ingredients::add);
240
		}
241
242
		public RecipePropertySet asPropertySet(final FeatureFlagSet enabledFeatures) {
243
			return RecipePropertySet.create(RecipeManager.filterDisabled(enabledFeatures, this.ingredients));
244
		}
245
	}
246
247
	@FunctionalInterface
248
	public interface IngredientExtractor {
249
		Optional<Ingredient> apply(Recipe<?> recipe);
250
	}
251
252
	public record ServerDisplayInfo(RecipeDisplayEntry display, RecipeHolder<?> parent) {
253
	}
254
}
255

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