52 lines
2 KiB
Java
52 lines
2 KiB
Java
package net.minecraft.world.item.crafting;
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
import com.google.common.collect.ImmutableMultimap;
|
|
import com.google.common.collect.Multimap;
|
|
import com.google.common.collect.ImmutableMultimap.Builder;
|
|
import java.util.Collection;
|
|
import java.util.Map;
|
|
import java.util.stream.Stream;
|
|
import net.minecraft.resources.ResourceKey;
|
|
import net.minecraft.world.level.Level;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public class RecipeMap {
|
|
public static final RecipeMap EMPTY = new RecipeMap(ImmutableMultimap.of(), Map.of());
|
|
private final Multimap<RecipeType<?>, RecipeHolder<?>> byType;
|
|
private final Map<ResourceKey<Recipe<?>>, RecipeHolder<?>> byKey;
|
|
|
|
private RecipeMap(Multimap<RecipeType<?>, RecipeHolder<?>> byType, Map<ResourceKey<Recipe<?>>, RecipeHolder<?>> byKey) {
|
|
this.byType = byType;
|
|
this.byKey = byKey;
|
|
}
|
|
|
|
public static RecipeMap create(Iterable<RecipeHolder<?>> recipes) {
|
|
Builder<RecipeType<?>, RecipeHolder<?>> builder = ImmutableMultimap.builder();
|
|
com.google.common.collect.ImmutableMap.Builder<ResourceKey<Recipe<?>>, RecipeHolder<?>> builder2 = ImmutableMap.builder();
|
|
|
|
for (RecipeHolder<?> recipeHolder : recipes) {
|
|
builder.put(recipeHolder.value().getType(), recipeHolder);
|
|
builder2.put(recipeHolder.id(), recipeHolder);
|
|
}
|
|
|
|
return new RecipeMap(builder.build(), builder2.build());
|
|
}
|
|
|
|
public <I extends RecipeInput, T extends Recipe<I>> Collection<RecipeHolder<T>> byType(RecipeType<T> type) {
|
|
return (Collection<RecipeHolder<T>>)this.byType.get(type);
|
|
}
|
|
|
|
public Collection<RecipeHolder<?>> values() {
|
|
return this.byKey.values();
|
|
}
|
|
|
|
@Nullable
|
|
public RecipeHolder<?> byKey(ResourceKey<Recipe<?>> key) {
|
|
return (RecipeHolder<?>)this.byKey.get(key);
|
|
}
|
|
|
|
public <I extends RecipeInput, T extends Recipe<I>> Stream<RecipeHolder<T>> getRecipesFor(RecipeType<T> type, I input, Level level) {
|
|
return input.isEmpty() ? Stream.empty() : this.byType(type).stream().filter(recipeHolder -> recipeHolder.value().matches(input, level));
|
|
}
|
|
}
|