| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 8 | import net.minecraft.network.codec.StreamCodec;
|
| 9 | import net.minecraft.world.item.ItemStack;
|
| 10 | import net.minecraft.world.item.ItemStackTemplate;
|
| 11 | import net.minecraft.world.item.Items;
|
| 12 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 13 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 14 | import net.minecraft.world.item.crafting.display.SmithingRecipeDisplay;
|
| 15 |
|
| 16 | public class SmithingTransformRecipe extends SimpleSmithingRecipe {
|
| 17 | public static final MapCodec<SmithingTransformRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 18 | i -> i.group(
|
| 19 | Recipe.CommonInfo.MAP_CODEC.forGetter(o -> o.commonInfo),
|
| 20 | Ingredient.CODEC.optionalFieldOf("template").forGetter(o -> o.template),
|
| 21 | Ingredient.CODEC.fieldOf("base").forGetter(o -> o.base),
|
| 22 | Ingredient.CODEC.optionalFieldOf("addition").forGetter(o -> o.addition),
|
| 23 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result)
|
| 24 | )
|
| 25 | .apply(i, SmithingTransformRecipe::new)
|
| 26 | );
|
| 27 | public static final StreamCodec<RegistryFriendlyByteBuf, SmithingTransformRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 28 | Recipe.CommonInfo.STREAM_CODEC,
|
| 29 | o -> o.commonInfo,
|
| 30 | Ingredient.OPTIONAL_CONTENTS_STREAM_CODEC,
|
| 31 | o -> o.template,
|
| 32 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 33 | o -> o.base,
|
| 34 | Ingredient.OPTIONAL_CONTENTS_STREAM_CODEC,
|
| 35 | o -> o.addition,
|
| 36 | ItemStackTemplate.STREAM_CODEC,
|
| 37 | o -> o.result,
|
| 38 | SmithingTransformRecipe::new
|
| 39 | );
|
| 40 | public static final RecipeSerializer<SmithingTransformRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 41 | private final Optional<Ingredient> template;
|
| 42 | private final Ingredient base;
|
| 43 | private final Optional<Ingredient> addition;
|
| 44 | private final ItemStackTemplate result;
|
| 45 |
|
| 46 | public SmithingTransformRecipe(
|
| 47 | final Recipe.CommonInfo commonInfo,
|
| 48 | final Optional<Ingredient> template,
|
| 49 | final Ingredient base,
|
| 50 | final Optional<Ingredient> addition,
|
| 51 | final ItemStackTemplate result
|
| 52 | ) {
|
| 53 | super(commonInfo);
|
| 54 | this.template = template;
|
| 55 | this.base = base;
|
| 56 | this.addition = addition;
|
| 57 | this.result = result;
|
| 58 | }
|
| 59 |
|
| 60 | public ItemStack assemble(final SmithingRecipeInput input) {
|
| 61 | return TransmuteRecipe.createWithOriginalComponents(this.result, input.base());
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public Optional<Ingredient> templateIngredient() {
|
| 66 | return this.template;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public Ingredient baseIngredient() {
|
| 71 | return this.base;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public Optional<Ingredient> additionIngredient() {
|
| 76 | return this.addition;
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public RecipeSerializer<SmithingTransformRecipe> getSerializer() {
|
| 81 | return SERIALIZER;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | protected PlacementInfo createPlacementInfo() {
|
| 86 | return PlacementInfo.createFromOptionals(List.of(this.template, Optional.of(this.base), this.addition));
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public List<RecipeDisplay> display() {
|
| 91 | return List.of(
|
| 92 | new SmithingRecipeDisplay(
|
| 93 | Ingredient.optionalIngredientToDisplay(this.template),
|
| 94 | this.base.display(),
|
| 95 | Ingredient.optionalIngredientToDisplay(this.addition),
|
| 96 | new SlotDisplay.ItemStackSlotDisplay(this.result),
|
| 97 | new SlotDisplay.ItemSlotDisplay(Items.SMITHING_TABLE)
|
| 98 | )
|
| 99 | );
|
| 100 | }
|
| 101 | }
|
| 102 |
|