| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 6 | import net.minecraft.network.codec.StreamCodec;
|
| 7 | import net.minecraft.world.item.ItemStack;
|
| 8 | import net.minecraft.world.item.ItemStackTemplate;
|
| 9 | import net.minecraft.world.level.Level;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public abstract class SingleItemRecipe implements Recipe<SingleRecipeInput> {
|
| 13 | protected final Recipe.CommonInfo commonInfo;
|
| 14 | private final Ingredient input;
|
| 15 | private final ItemStackTemplate result;
|
| 16 | private @Nullable PlacementInfo placementInfo;
|
| 17 |
|
| 18 | public SingleItemRecipe(final Recipe.CommonInfo commonInfo, final Ingredient input, final ItemStackTemplate result) {
|
| 19 | this.commonInfo = commonInfo;
|
| 20 | this.input = input;
|
| 21 | this.result = result;
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public abstract RecipeSerializer<? extends SingleItemRecipe> getSerializer();
|
| 26 |
|
| 27 | @Override
|
| 28 | public abstract RecipeType<? extends SingleItemRecipe> getType();
|
| 29 |
|
| 30 | public boolean matches(final SingleRecipeInput input, final Level level) {
|
| 31 | return this.input.test(input.item());
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public boolean showNotification() {
|
| 36 | return this.commonInfo.showNotification();
|
| 37 | }
|
| 38 |
|
| 39 | public Ingredient input() {
|
| 40 | return this.input;
|
| 41 | }
|
| 42 |
|
| 43 | protected ItemStackTemplate result() {
|
| 44 | return this.result;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public PlacementInfo placementInfo() {
|
| 49 | if (this.placementInfo == null) {
|
| 50 | this.placementInfo = PlacementInfo.create(this.input);
|
| 51 | }
|
| 52 |
|
| 53 | return this.placementInfo;
|
| 54 | }
|
| 55 |
|
| 56 | public ItemStack assemble(final SingleRecipeInput input) {
|
| 57 | return this.result.create();
|
| 58 | }
|
| 59 |
|
| 60 | public static <T extends SingleItemRecipe> MapCodec<T> simpleMapCodec(final SingleItemRecipe.Factory<T> factory) {
|
| 61 | return RecordCodecBuilder.mapCodec(
|
| 62 | i -> i.group(
|
| 63 | Recipe.CommonInfo.MAP_CODEC.forGetter(o -> o.commonInfo),
|
| 64 | Ingredient.CODEC.fieldOf("ingredient").forGetter(SingleItemRecipe::input),
|
| 65 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(SingleItemRecipe::result)
|
| 66 | )
|
| 67 | .apply(i, factory::create)
|
| 68 | );
|
| 69 | }
|
| 70 |
|
| 71 | public static <T extends SingleItemRecipe> StreamCodec<RegistryFriendlyByteBuf, T> simpleStreamCodec(final SingleItemRecipe.Factory<T> factory) {
|
| 72 | return StreamCodec.composite(
|
| 73 | Recipe.CommonInfo.STREAM_CODEC,
|
| 74 | o -> o.commonInfo,
|
| 75 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 76 | SingleItemRecipe::input,
|
| 77 | ItemStackTemplate.STREAM_CODEC,
|
| 78 | SingleItemRecipe::result,
|
| 79 | factory::create
|
| 80 | );
|
| 81 | }
|
| 82 |
|
| 83 | @FunctionalInterface
|
| 84 | public interface Factory<T extends SingleItemRecipe> {
|
| 85 | T create(Recipe.CommonInfo commonInfo, Ingredient ingredient, ItemStackTemplate result);
|
| 86 | }
|
| 87 | }
|
| 88 |
|