| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 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.item.Items;
|
| 13 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 14 | import net.minecraft.world.item.crafting.display.ShapedCraftingRecipeDisplay;
|
| 15 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 |
|
| 18 | public class ShapedRecipe extends NormalCraftingRecipe {
|
| 19 | public static final MapCodec<ShapedRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 20 | i -> i.group(
|
| 21 | Recipe.CommonInfo.MAP_CODEC.forGetter(o -> o.commonInfo),
|
| 22 | CraftingRecipe.CraftingBookInfo.MAP_CODEC.forGetter(o -> o.bookInfo),
|
| 23 | ShapedRecipePattern.MAP_CODEC.forGetter(o -> o.pattern),
|
| 24 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result)
|
| 25 | )
|
| 26 | .apply(i, ShapedRecipe::new)
|
| 27 | );
|
| 28 | public static final StreamCodec<RegistryFriendlyByteBuf, ShapedRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 29 | Recipe.CommonInfo.STREAM_CODEC,
|
| 30 | o -> o.commonInfo,
|
| 31 | CraftingRecipe.CraftingBookInfo.STREAM_CODEC,
|
| 32 | o -> o.bookInfo,
|
| 33 | ShapedRecipePattern.STREAM_CODEC,
|
| 34 | o -> o.pattern,
|
| 35 | ItemStackTemplate.STREAM_CODEC,
|
| 36 | o -> o.result,
|
| 37 | ShapedRecipe::new
|
| 38 | );
|
| 39 | public static final RecipeSerializer<ShapedRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 40 | private final ShapedRecipePattern pattern;
|
| 41 | private final ItemStackTemplate result;
|
| 42 |
|
| 43 | public ShapedRecipe(
|
| 44 | final Recipe.CommonInfo commonInfo, final CraftingRecipe.CraftingBookInfo bookInfo, final ShapedRecipePattern pattern, final ItemStackTemplate result
|
| 45 | ) {
|
| 46 | super(commonInfo, bookInfo);
|
| 47 | this.pattern = pattern;
|
| 48 | this.result = result;
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public RecipeSerializer<ShapedRecipe> getSerializer() {
|
| 53 | return SERIALIZER;
|
| 54 | }
|
| 55 |
|
| 56 | @VisibleForTesting
|
| 57 | public List<Optional<Ingredient>> getIngredients() {
|
| 58 | return this.pattern.ingredients();
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected PlacementInfo createPlacementInfo() {
|
| 63 | return PlacementInfo.createFromOptionals(this.pattern.ingredients());
|
| 64 | }
|
| 65 |
|
| 66 | public boolean matches(final CraftingInput input, final Level level) {
|
| 67 | return this.pattern.matches(input);
|
| 68 | }
|
| 69 |
|
| 70 | public ItemStack assemble(final CraftingInput input) {
|
| 71 | return this.result.create();
|
| 72 | }
|
| 73 |
|
| 74 | public int getWidth() {
|
| 75 | return this.pattern.width();
|
| 76 | }
|
| 77 |
|
| 78 | public int getHeight() {
|
| 79 | return this.pattern.height();
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public List<RecipeDisplay> display() {
|
| 84 | return List.of(
|
| 85 | new ShapedCraftingRecipeDisplay(
|
| 86 | this.pattern.width(),
|
| 87 | this.pattern.height(),
|
| 88 | this.pattern.ingredients().stream().map(e -> e.map(Ingredient::display).orElse(SlotDisplay.Empty.INSTANCE)).toList(),
|
| 89 | new SlotDisplay.ItemStackSlotDisplay(this.result),
|
| 90 | new SlotDisplay.ItemSlotDisplay(Items.CRAFTING_TABLE)
|
| 91 | )
|
| 92 | );
|
| 93 | }
|
| 94 | }
|
| 95 |
|