| 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.Optional;
|
| 6 | import net.minecraft.core.component.DataComponentPatch;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 8 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 9 | import net.minecraft.network.codec.StreamCodec;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.item.ItemStackTemplate;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.block.entity.PotDecorations;
|
| 14 |
|
| 15 | public class DecoratedPotRecipe extends CustomRecipe {
|
| 16 | public static final MapCodec<DecoratedPotRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> i.group(
|
| 18 | Ingredient.CODEC.fieldOf("back").forGetter(o -> o.backPattern),
|
| 19 | Ingredient.CODEC.fieldOf("left").forGetter(o -> o.leftPattern),
|
| 20 | Ingredient.CODEC.fieldOf("right").forGetter(o -> o.rightPattern),
|
| 21 | Ingredient.CODEC.fieldOf("front").forGetter(o -> o.frontPattern),
|
| 22 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result)
|
| 23 | )
|
| 24 | .apply(i, DecoratedPotRecipe::new)
|
| 25 | );
|
| 26 | public static final StreamCodec<RegistryFriendlyByteBuf, DecoratedPotRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 27 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 28 | o -> o.backPattern,
|
| 29 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 30 | o -> o.leftPattern,
|
| 31 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 32 | o -> o.rightPattern,
|
| 33 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 34 | o -> o.frontPattern,
|
| 35 | ItemStackTemplate.STREAM_CODEC,
|
| 36 | o -> o.result,
|
| 37 | DecoratedPotRecipe::new
|
| 38 | );
|
| 39 | public static final RecipeSerializer<DecoratedPotRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 40 | private final Ingredient backPattern;
|
| 41 | private final Ingredient leftPattern;
|
| 42 | private final Ingredient rightPattern;
|
| 43 | private final Ingredient frontPattern;
|
| 44 | private final ItemStackTemplate result;
|
| 45 |
|
| 46 | public DecoratedPotRecipe(final Ingredient wallPattern, final ItemStackTemplate result) {
|
| 47 | this(wallPattern, wallPattern, wallPattern, wallPattern, result);
|
| 48 | }
|
| 49 |
|
| 50 | public DecoratedPotRecipe(
|
| 51 | final Ingredient backPattern, final Ingredient leftPattern, final Ingredient rightPattern, final Ingredient frontPattern, final ItemStackTemplate result
|
| 52 | ) {
|
| 53 | this.backPattern = backPattern;
|
| 54 | this.leftPattern = leftPattern;
|
| 55 | this.rightPattern = rightPattern;
|
| 56 | this.frontPattern = frontPattern;
|
| 57 | this.result = result;
|
| 58 | }
|
| 59 |
|
| 60 | private static ItemStack back(final CraftingInput input) {
|
| 61 | return input.getItem(1, 0);
|
| 62 | }
|
| 63 |
|
| 64 | private static ItemStack left(final CraftingInput input) {
|
| 65 | return input.getItem(0, 1);
|
| 66 | }
|
| 67 |
|
| 68 | private static ItemStack right(final CraftingInput input) {
|
| 69 | return input.getItem(2, 1);
|
| 70 | }
|
| 71 |
|
| 72 | private static ItemStack front(final CraftingInput input) {
|
| 73 | return input.getItem(1, 2);
|
| 74 | }
|
| 75 |
|
| 76 | public boolean matches(final CraftingInput input, final Level level) {
|
| 77 | return input.width() == 3 && input.height() == 3 && input.ingredientCount() == 4
|
| 78 | ? this.backPattern.test(back(input)) && this.leftPattern.test(left(input)) && this.rightPattern.test(right(input)) && this.frontPattern.test(front(input))
|
| 79 | : false;
|
| 80 | }
|
| 81 |
|
| 82 | public ItemStack assemble(final CraftingInput input) {
|
| 83 | PotDecorations decorations = new PotDecorations(
|
| 84 | convertToOptional(back(input)), convertToOptional(left(input)), convertToOptional(right(input)), convertToOptional(front(input))
|
| 85 | );
|
| 86 | DataComponentPatch components = DataComponentPatch.builder().set(DataComponents.POT_DECORATIONS, decorations).build();
|
| 87 | return this.result.apply(components);
|
| 88 | }
|
| 89 |
|
| 90 | private static Optional<ItemStackTemplate> convertToOptional(final ItemStack input) {
|
| 91 | return input.isEmpty() ? Optional.empty() : Optional.of(ItemStackTemplate.fromNonEmptyStack(input, 1));
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public RecipeSerializer<DecoratedPotRecipe> getSerializer() {
|
| 96 | return SERIALIZER;
|
| 97 | }
|
| 98 | }
|
| 99 |
|