| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.BiFunction;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.HolderSet;
|
| 10 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 11 | import net.minecraft.core.registries.Registries;
|
| 12 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 13 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 14 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 15 | import net.minecraft.network.codec.StreamCodec;
|
| 16 | import net.minecraft.resources.ResourceKey;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 |
|
| 21 | public interface Recipe<T extends RecipeInput> {
|
| 22 | Codec<Recipe<?>> DIRECT_CODEC = BuiltInRegistries.RECIPE_SERIALIZER.byNameCodec().dispatch(Recipe::getSerializer, RecipeSerializer::codec);
|
| 23 | Codec<ResourceKey<Recipe<?>>> KEY_CODEC = ResourceKey.codec(Registries.RECIPE);
|
| 24 | StreamCodec<RegistryFriendlyByteBuf, Recipe<?>> STREAM_CODEC = ByteBufCodecs.registry(Registries.RECIPE_SERIALIZER)
|
| 25 | .dispatch(Recipe::getSerializer, RecipeSerializer::streamCodec);
|
| 26 | Codec<Holder<Recipe<?>>> CODEC = RegistryCodecs.holder(Registries.RECIPE, DIRECT_CODEC);
|
| 27 | Codec<HolderSet<Recipe<?>>> LIST_CODEC = RegistryCodecs.holderSet(Registries.RECIPE, DIRECT_CODEC);
|
| 28 |
|
| 29 | boolean matches(T input, Level level);
|
| 30 |
|
| 31 | ItemStack assemble(T input);
|
| 32 |
|
| 33 | default boolean isSpecial() {
|
| 34 | return false;
|
| 35 | }
|
| 36 |
|
| 37 | boolean showNotification();
|
| 38 |
|
| 39 | String group();
|
| 40 |
|
| 41 | RecipeSerializer<? extends Recipe<T>> getSerializer();
|
| 42 |
|
| 43 | RecipeType<? extends Recipe<T>> getType();
|
| 44 |
|
| 45 | PlacementInfo placementInfo();
|
| 46 |
|
| 47 | default List<RecipeDisplay> display() {
|
| 48 | return List.of();
|
| 49 | }
|
| 50 |
|
| 51 | RecipeBookCategory recipeBookCategory();
|
| 52 |
|
| 53 | interface BookInfo<CategoryType> {
|
| 54 | CategoryType category();
|
| 55 |
|
| 56 | String group();
|
| 57 |
|
| 58 | static <CategoryType, SelfType extends Recipe.BookInfo<CategoryType>> MapCodec<SelfType> mapCodec(
|
| 59 | final Codec<CategoryType> categoryCodec, final CategoryType defaultCategory, final Recipe.BookInfo.Constructor<CategoryType, SelfType> constructor
|
| 60 | ) {
|
| 61 | return RecordCodecBuilder.mapCodec(
|
| 62 | i -> i.group(
|
| 63 | categoryCodec.optionalFieldOf("category", defaultCategory).forGetter(Recipe.BookInfo::category),
|
| 64 | Codec.STRING.optionalFieldOf("group", "").forGetter(Recipe.BookInfo::group)
|
| 65 | )
|
| 66 | .apply(i, constructor)
|
| 67 | );
|
| 68 | }
|
| 69 |
|
| 70 | static <CategoryType, SelfType extends Recipe.BookInfo<CategoryType>> StreamCodec<RegistryFriendlyByteBuf, SelfType> streamCodec(
|
| 71 | final StreamCodec<? super RegistryFriendlyByteBuf, CategoryType> categoryCodec, final Recipe.BookInfo.Constructor<CategoryType, SelfType> constructor
|
| 72 | ) {
|
| 73 | return StreamCodec.composite(categoryCodec, Recipe.BookInfo::category, ByteBufCodecs.STRING_UTF8, Recipe.BookInfo::group, constructor);
|
| 74 | }
|
| 75 |
|
| 76 | @FunctionalInterface
|
| 77 | interface Constructor<CategoryType, SelfType extends Recipe.BookInfo<CategoryType>> extends BiFunction<CategoryType, String, SelfType> {
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | record CommonInfo(boolean showNotification) {
|
| 82 | public static final MapCodec<Recipe.CommonInfo> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 83 | i -> i.group(Codec.BOOL.optionalFieldOf("show_notification", true).forGetter(Recipe.CommonInfo::showNotification)).apply(i, Recipe.CommonInfo::new)
|
| 84 | );
|
| 85 | public static final StreamCodec<RegistryFriendlyByteBuf, Recipe.CommonInfo> STREAM_CODEC = StreamCodec.composite(
|
| 86 | ByteBufCodecs.BOOL, Recipe.CommonInfo::showNotification, Recipe.CommonInfo::new
|
| 87 | );
|
| 88 | }
|
| 89 | }
|
| 90 |
|