| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.ImmutableMultimap;
|
| 5 | import com.google.common.collect.Multimap;
|
| 6 | import com.google.common.collect.ImmutableMultimap.Builder;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.stream.Stream;
|
| 10 | import net.minecraft.core.HolderLookup;
|
| 11 | import net.minecraft.resources.ResourceKey;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class RecipeMap {
|
| 16 | public static final RecipeMap EMPTY = new RecipeMap(ImmutableMultimap.of(), Map.of());
|
| 17 | private final Multimap<RecipeType<?>, RecipeHolder<?>> byType;
|
| 18 | private final Map<ResourceKey<Recipe<?>>, RecipeHolder<?>> byKey;
|
| 19 |
|
| 20 | private RecipeMap(final Multimap<RecipeType<?>, RecipeHolder<?>> byType, final Map<ResourceKey<Recipe<?>>, RecipeHolder<?>> byKey) {
|
| 21 | this.byType = byType;
|
| 22 | this.byKey = byKey;
|
| 23 | }
|
| 24 |
|
| 25 | public static RecipeMap create(final HolderLookup<Recipe<?>> recipes) {
|
| 26 | Builder<RecipeType<?>, RecipeHolder<?>> byType = ImmutableMultimap.builder();
|
| 27 | com.google.common.collect.ImmutableMap.Builder<ResourceKey<Recipe<?>>, RecipeHolder<?>> byKey = ImmutableMap.builder();
|
| 28 | recipes.listElements().forEach(recipe -> {
|
| 29 | RecipeHolder<? extends Recipe<?>> legacyRecipeHolder = new RecipeHolder<>(recipe.key(), recipe.value());
|
| 30 | byType.put(recipe.value().getType(), legacyRecipeHolder);
|
| 31 | byKey.put(recipe.key(), legacyRecipeHolder);
|
| 32 | });
|
| 33 | return new RecipeMap(byType.build(), byKey.build());
|
| 34 | }
|
| 35 |
|
| 36 | public <I extends RecipeInput, T extends Recipe<I>> Collection<RecipeHolder<T>> byType(final RecipeType<T> type) {
|
| 37 | return (Collection<RecipeHolder<T>>)this.byType.get(type);
|
| 38 | }
|
| 39 |
|
| 40 | public Collection<RecipeHolder<?>> values() {
|
| 41 | return this.byKey.values();
|
| 42 | }
|
| 43 |
|
| 44 | public @Nullable RecipeHolder<?> byKey(final ResourceKey<Recipe<?>> recipeId) {
|
| 45 | return this.byKey.get(recipeId);
|
| 46 | }
|
| 47 |
|
| 48 | public <I extends RecipeInput, T extends Recipe<I>> Stream<RecipeHolder<T>> getRecipesFor(final RecipeType<T> type, final I container, final Level level) {
|
| 49 | return container.isEmpty() ? Stream.empty() : this.byType(type).stream().filter(r -> r.value().matches(container, level));
|
| 50 | }
|
| 51 | }
|
| 52 |
|