package net.minecraft.world.item.crafting; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import java.util.List; import java.util.Optional; import net.minecraft.core.HolderLookup; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.StreamCodec; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.item.crafting.display.RecipeDisplay; import net.minecraft.world.item.crafting.display.SlotDisplay; import net.minecraft.world.item.crafting.display.SmithingRecipeDisplay; import org.jetbrains.annotations.Nullable; public class SmithingTransformRecipe implements SmithingRecipe { final Optional template; final Optional base; final Optional addition; final ItemStack result; @Nullable private PlacementInfo placementInfo; public SmithingTransformRecipe(Optional template, Optional base, Optional addition, ItemStack result) { this.template = template; this.base = base; this.addition = addition; this.result = result; } public ItemStack assemble(SmithingRecipeInput input, HolderLookup.Provider registries) { ItemStack itemStack = input.base().transmuteCopy(this.result.getItem(), this.result.getCount()); itemStack.applyComponents(this.result.getComponentsPatch()); return itemStack; } @Override public Optional templateIngredient() { return this.template; } @Override public Optional baseIngredient() { return this.base; } @Override public Optional additionIngredient() { return this.addition; } @Override public RecipeSerializer getSerializer() { return RecipeSerializer.SMITHING_TRANSFORM; } @Override public PlacementInfo placementInfo() { if (this.placementInfo == null) { this.placementInfo = PlacementInfo.createFromOptionals(List.of(this.template, this.base, this.addition)); } return this.placementInfo; } @Override public List display() { return List.of( new SmithingRecipeDisplay( Ingredient.optionalIngredientToDisplay(this.template), Ingredient.optionalIngredientToDisplay(this.base), Ingredient.optionalIngredientToDisplay(this.addition), new SlotDisplay.ItemStackSlotDisplay(this.result), new SlotDisplay.ItemSlotDisplay(Items.SMITHING_TABLE) ) ); } public static class Serializer implements RecipeSerializer { private static final MapCodec CODEC = RecordCodecBuilder.mapCodec( instance -> instance.group( Ingredient.CODEC.optionalFieldOf("template").forGetter(smithingTransformRecipe -> smithingTransformRecipe.template), Ingredient.CODEC.optionalFieldOf("base").forGetter(smithingTransformRecipe -> smithingTransformRecipe.base), Ingredient.CODEC.optionalFieldOf("addition").forGetter(smithingTransformRecipe -> smithingTransformRecipe.addition), ItemStack.STRICT_CODEC.fieldOf("result").forGetter(smithingTransformRecipe -> smithingTransformRecipe.result) ) .apply(instance, SmithingTransformRecipe::new) ); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( Ingredient.OPTIONAL_CONTENTS_STREAM_CODEC, smithingTransformRecipe -> smithingTransformRecipe.template, Ingredient.OPTIONAL_CONTENTS_STREAM_CODEC, smithingTransformRecipe -> smithingTransformRecipe.base, Ingredient.OPTIONAL_CONTENTS_STREAM_CODEC, smithingTransformRecipe -> smithingTransformRecipe.addition, ItemStack.STREAM_CODEC, smithingTransformRecipe -> smithingTransformRecipe.result, SmithingTransformRecipe::new ); @Override public MapCodec codec() { return CODEC; } @Override public StreamCodec streamCodec() { return STREAM_CODEC; } } }