| 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 net.minecraft.network.RegistryFriendlyByteBuf;
|
| 8 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 9 | import net.minecraft.network.codec.StreamCodec;
|
| 10 | import net.minecraft.world.item.Item;
|
| 11 | import net.minecraft.world.item.ItemStackTemplate;
|
| 12 | import net.minecraft.world.item.crafting.display.FurnaceRecipeDisplay;
|
| 13 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 14 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 15 |
|
| 16 | public abstract class AbstractCookingRecipe extends SingleItemRecipe {
|
| 17 | protected final AbstractCookingRecipe.CookingBookInfo bookInfo;
|
| 18 | private final float experience;
|
| 19 | private final int cookingTime;
|
| 20 |
|
| 21 | public AbstractCookingRecipe(
|
| 22 | final Recipe.CommonInfo commonInfo,
|
| 23 | final AbstractCookingRecipe.CookingBookInfo bookInfo,
|
| 24 | final Ingredient ingredient,
|
| 25 | final ItemStackTemplate result,
|
| 26 | final float experience,
|
| 27 | final int cookingTime
|
| 28 | ) {
|
| 29 | super(commonInfo, ingredient, result);
|
| 30 | this.bookInfo = bookInfo;
|
| 31 | this.experience = experience;
|
| 32 | this.cookingTime = cookingTime;
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public abstract RecipeSerializer<? extends AbstractCookingRecipe> getSerializer();
|
| 37 |
|
| 38 | @Override
|
| 39 | public abstract RecipeType<? extends AbstractCookingRecipe> getType();
|
| 40 |
|
| 41 | public float experience() {
|
| 42 | return this.experience;
|
| 43 | }
|
| 44 |
|
| 45 | public int cookingTime() {
|
| 46 | return this.cookingTime;
|
| 47 | }
|
| 48 |
|
| 49 | public CookingBookCategory category() {
|
| 50 | return this.bookInfo.category;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public String group() {
|
| 55 | return this.bookInfo.group;
|
| 56 | }
|
| 57 |
|
| 58 | protected abstract Item furnaceIcon();
|
| 59 |
|
| 60 | @Override
|
| 61 | public List<RecipeDisplay> display() {
|
| 62 | return List.of(
|
| 63 | new FurnaceRecipeDisplay(
|
| 64 | this.input().display(),
|
| 65 | SlotDisplay.AnyFuel.INSTANCE,
|
| 66 | new SlotDisplay.ItemStackSlotDisplay(this.result()),
|
| 67 | new SlotDisplay.ItemSlotDisplay(this.furnaceIcon()),
|
| 68 | this.cookingTime,
|
| 69 | this.experience
|
| 70 | )
|
| 71 | );
|
| 72 | }
|
| 73 |
|
| 74 | public static <T extends AbstractCookingRecipe> MapCodec<T> cookingMapCodec(final AbstractCookingRecipe.Factory<T> factory) {
|
| 75 | return RecordCodecBuilder.mapCodec(
|
| 76 | i -> i.group(
|
| 77 | Recipe.CommonInfo.MAP_CODEC.forGetter(o -> o.commonInfo),
|
| 78 | AbstractCookingRecipe.CookingBookInfo.MAP_CODEC.forGetter(o -> o.bookInfo),
|
| 79 | Ingredient.CODEC.fieldOf("ingredient").forGetter(SingleItemRecipe::input),
|
| 80 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(SingleItemRecipe::result),
|
| 81 | Codec.FLOAT.optionalFieldOf("experience", 0.0F).forGetter(AbstractCookingRecipe::experience),
|
| 82 | Codec.INT.fieldOf("cookingtime").forGetter(AbstractCookingRecipe::cookingTime)
|
| 83 | )
|
| 84 | .apply(i, factory::create)
|
| 85 | );
|
| 86 | }
|
| 87 |
|
| 88 | public static <T extends AbstractCookingRecipe> StreamCodec<RegistryFriendlyByteBuf, T> cookingStreamCodec(final AbstractCookingRecipe.Factory<T> factory) {
|
| 89 | return StreamCodec.composite(
|
| 90 | Recipe.CommonInfo.STREAM_CODEC,
|
| 91 | o -> o.commonInfo,
|
| 92 | AbstractCookingRecipe.CookingBookInfo.STREAM_CODEC,
|
| 93 | o -> o.bookInfo,
|
| 94 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 95 | SingleItemRecipe::input,
|
| 96 | ItemStackTemplate.STREAM_CODEC,
|
| 97 | SingleItemRecipe::result,
|
| 98 | ByteBufCodecs.FLOAT,
|
| 99 | AbstractCookingRecipe::experience,
|
| 100 | ByteBufCodecs.INT,
|
| 101 | AbstractCookingRecipe::cookingTime,
|
| 102 | factory::create
|
| 103 | );
|
| 104 | }
|
| 105 |
|
| 106 | public record CookingBookInfo(CookingBookCategory category, String group) implements Recipe.BookInfo<CookingBookCategory> {
|
| 107 | public static final MapCodec<AbstractCookingRecipe.CookingBookInfo> MAP_CODEC = Recipe.BookInfo.mapCodec(
|
| 108 | CookingBookCategory.CODEC, CookingBookCategory.MISC, AbstractCookingRecipe.CookingBookInfo::new
|
| 109 | );
|
| 110 | public static final StreamCodec<RegistryFriendlyByteBuf, AbstractCookingRecipe.CookingBookInfo> STREAM_CODEC = Recipe.BookInfo.streamCodec(
|
| 111 | CookingBookCategory.STREAM_CODEC, AbstractCookingRecipe.CookingBookInfo::new
|
| 112 | );
|
| 113 | }
|
| 114 |
|
| 115 | @FunctionalInterface
|
| 116 | public interface Factory<T extends AbstractCookingRecipe> {
|
| 117 | T create(
|
| 118 | Recipe.CommonInfo commonInfo,
|
| 119 | AbstractCookingRecipe.CookingBookInfo cbookInfotegory,
|
| 120 | Ingredient ingredient,
|
| 121 | ItemStackTemplate result,
|
| 122 | float experience,
|
| 123 | int cookingTime
|
| 124 | );
|
| 125 | }
|
| 126 | }
|
| 127 |
|