| 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.Objects;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.component.DataComponents;
|
| 10 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 11 | import net.minecraft.network.codec.StreamCodec;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.item.Items;
|
| 14 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 15 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 16 | import net.minecraft.world.item.crafting.display.SmithingRecipeDisplay;
|
| 17 | import net.minecraft.world.item.equipment.trim.ArmorTrim;
|
| 18 | import net.minecraft.world.item.equipment.trim.TrimMaterial;
|
| 19 | import net.minecraft.world.item.equipment.trim.TrimPattern;
|
| 20 |
|
| 21 | public class SmithingTrimRecipe extends SimpleSmithingRecipe {
|
| 22 | public static final MapCodec<SmithingTrimRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 23 | i -> i.group(
|
| 24 | Recipe.CommonInfo.MAP_CODEC.forGetter(o -> o.commonInfo),
|
| 25 | Ingredient.CODEC.fieldOf("template").forGetter(o -> o.template),
|
| 26 | Ingredient.CODEC.fieldOf("base").forGetter(o -> o.base),
|
| 27 | Ingredient.CODEC.fieldOf("addition").forGetter(o -> o.addition),
|
| 28 | TrimPattern.CODEC.fieldOf("pattern").forGetter(o -> o.pattern)
|
| 29 | )
|
| 30 | .apply(i, SmithingTrimRecipe::new)
|
| 31 | );
|
| 32 | public static final StreamCodec<RegistryFriendlyByteBuf, SmithingTrimRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 33 | Recipe.CommonInfo.STREAM_CODEC,
|
| 34 | o -> o.commonInfo,
|
| 35 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 36 | o -> o.template,
|
| 37 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 38 | o -> o.base,
|
| 39 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 40 | o -> o.addition,
|
| 41 | TrimPattern.STREAM_CODEC,
|
| 42 | o -> o.pattern,
|
| 43 | SmithingTrimRecipe::new
|
| 44 | );
|
| 45 | public static final RecipeSerializer<SmithingTrimRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 46 | private final Ingredient template;
|
| 47 | private final Ingredient base;
|
| 48 | private final Ingredient addition;
|
| 49 | private final Holder<TrimPattern> pattern;
|
| 50 |
|
| 51 | public SmithingTrimRecipe(
|
| 52 | final Recipe.CommonInfo commonInfo, final Ingredient template, final Ingredient base, final Ingredient addition, final Holder<TrimPattern> pattern
|
| 53 | ) {
|
| 54 | super(commonInfo);
|
| 55 | this.template = template;
|
| 56 | this.base = base;
|
| 57 | this.addition = addition;
|
| 58 | this.pattern = pattern;
|
| 59 | }
|
| 60 |
|
| 61 | public ItemStack assemble(final SmithingRecipeInput input) {
|
| 62 | return applyTrim(input.base(), input.addition(), this.pattern);
|
| 63 | }
|
| 64 |
|
| 65 | public static ItemStack applyTrim(final ItemStack baseItem, final ItemStack materialItem, final Holder<TrimPattern> pattern) {
|
| 66 | Holder<TrimMaterial> material = materialItem.get(DataComponents.PROVIDES_TRIM_MATERIAL);
|
| 67 | if (material != null) {
|
| 68 | ArmorTrim existingTrim = baseItem.get(DataComponents.TRIM);
|
| 69 | ArmorTrim newTrim = new ArmorTrim(material, pattern);
|
| 70 | if (Objects.equals(existingTrim, newTrim)) {
|
| 71 | return ItemStack.EMPTY;
|
| 72 | }
|
| 73 |
|
| 74 | ItemStack trimmedItem = baseItem.copyWithCount(1);
|
| 75 | trimmedItem.set(DataComponents.TRIM, newTrim);
|
| 76 | return trimmedItem;
|
| 77 | } else {
|
| 78 | return ItemStack.EMPTY;
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public Optional<Ingredient> templateIngredient() {
|
| 84 | return Optional.of(this.template);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public Ingredient baseIngredient() {
|
| 89 | return this.base;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public Optional<Ingredient> additionIngredient() {
|
| 94 | return Optional.of(this.addition);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public RecipeSerializer<SmithingTrimRecipe> getSerializer() {
|
| 99 | return SERIALIZER;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected PlacementInfo createPlacementInfo() {
|
| 104 | return PlacementInfo.create(List.of(this.template, this.base, this.addition));
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public List<RecipeDisplay> display() {
|
| 109 | SlotDisplay base = this.base.display();
|
| 110 | SlotDisplay material = this.addition.display();
|
| 111 | SlotDisplay template = this.template.display();
|
| 112 | return List.of(
|
| 113 | new SmithingRecipeDisplay(
|
| 114 | template, base, material, new SlotDisplay.SmithingTrimDemoSlotDisplay(base, material, this.pattern), new SlotDisplay.ItemSlotDisplay(Items.SMITHING_TABLE)
|
| 115 | )
|
| 116 | );
|
| 117 | }
|
| 118 | }
|
| 119 |
|