| 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 net.minecraft.core.component.DataComponents;
|
| 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.ShapedCraftingRecipeDisplay;
|
| 14 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 |
|
| 17 | public class ImbueRecipe extends NormalCraftingRecipe {
|
| 18 | public static final MapCodec<ImbueRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 19 | i -> i.group(
|
| 20 | Recipe.CommonInfo.MAP_CODEC.forGetter(o -> o.commonInfo),
|
| 21 | CraftingRecipe.CraftingBookInfo.MAP_CODEC.forGetter(o -> o.bookInfo),
|
| 22 | Ingredient.CODEC.fieldOf("source").forGetter(o -> o.source),
|
| 23 | Ingredient.CODEC.fieldOf("material").forGetter(o -> o.material),
|
| 24 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result)
|
| 25 | )
|
| 26 | .apply(i, ImbueRecipe::new)
|
| 27 | );
|
| 28 | public static final StreamCodec<RegistryFriendlyByteBuf, ImbueRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 29 | Recipe.CommonInfo.STREAM_CODEC,
|
| 30 | o -> o.commonInfo,
|
| 31 | CraftingRecipe.CraftingBookInfo.STREAM_CODEC,
|
| 32 | o -> o.bookInfo,
|
| 33 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 34 | o -> o.source,
|
| 35 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 36 | o -> o.material,
|
| 37 | ItemStackTemplate.STREAM_CODEC,
|
| 38 | o -> o.result,
|
| 39 | ImbueRecipe::new
|
| 40 | );
|
| 41 | public static final RecipeSerializer<ImbueRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 42 | private final Ingredient source;
|
| 43 | private final Ingredient material;
|
| 44 | private final ItemStackTemplate result;
|
| 45 |
|
| 46 | public ImbueRecipe(
|
| 47 | final Recipe.CommonInfo commonInfo,
|
| 48 | final CraftingRecipe.CraftingBookInfo bookInfo,
|
| 49 | final Ingredient source,
|
| 50 | final Ingredient material,
|
| 51 | final ItemStackTemplate result
|
| 52 | ) {
|
| 53 | super(commonInfo, bookInfo);
|
| 54 | this.source = source;
|
| 55 | this.material = material;
|
| 56 | this.result = result;
|
| 57 | }
|
| 58 |
|
| 59 | public boolean matches(final CraftingInput input, final Level level) {
|
| 60 | if (input.width() == 3 && input.height() == 3 && input.ingredientCount() == 9) {
|
| 61 | for (int y = 0; y < input.height(); y++) {
|
| 62 | for (int x = 0; x < input.width(); x++) {
|
| 63 | ItemStack itemStack = input.getItem(x, y);
|
| 64 | if (itemStack.isEmpty()) {
|
| 65 | return false;
|
| 66 | }
|
| 67 |
|
| 68 | Ingredient ingredient = x == 1 && y == 1 ? this.source : this.material;
|
| 69 | if (!ingredient.test(itemStack)) {
|
| 70 | return false;
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | return true;
|
| 76 | } else {
|
| 77 | return false;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | public ItemStack assemble(final CraftingInput input) {
|
| 82 | ItemStack source = input.getItem(1, 1);
|
| 83 | ItemStack result = this.result.create();
|
| 84 | result.set(DataComponents.POTION_CONTENTS, source.get(DataComponents.POTION_CONTENTS));
|
| 85 | return result;
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public RecipeSerializer<ImbueRecipe> getSerializer() {
|
| 90 | return SERIALIZER;
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected PlacementInfo createPlacementInfo() {
|
| 95 | return PlacementInfo.create(
|
| 96 | List.of(this.material, this.material, this.material, this.material, this.source, this.material, this.material, this.material, this.material)
|
| 97 | );
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public List<RecipeDisplay> display() {
|
| 102 | SlotDisplay material = this.material.display();
|
| 103 | SlotDisplay.WithAnyPotion source = new SlotDisplay.WithAnyPotion(this.source.display());
|
| 104 | return List.of(
|
| 105 | new ShapedCraftingRecipeDisplay(
|
| 106 | 3,
|
| 107 | 3,
|
| 108 | List.of(material, material, material, material, source, material, material, material, material),
|
| 109 | new SlotDisplay.WithAnyPotion(new SlotDisplay.ItemStackSlotDisplay(this.result)),
|
| 110 | new SlotDisplay.ItemSlotDisplay(Items.CRAFTING_TABLE)
|
| 111 | )
|
| 112 | );
|
| 113 | }
|
| 114 | }
|
| 115 |
|