| 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.network.RegistryFriendlyByteBuf;
|
| 7 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 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.ShapelessCraftingRecipeDisplay;
|
| 14 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 |
|
| 17 | public class ShapelessRecipe extends NormalCraftingRecipe {
|
| 18 | public static final MapCodec<ShapelessRecipe> 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 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result),
|
| 23 | Ingredient.CODEC.listOf(1, 9).fieldOf("ingredients").forGetter(o -> o.ingredients)
|
| 24 | )
|
| 25 | .apply(i, ShapelessRecipe::new)
|
| 26 | );
|
| 27 | public static final StreamCodec<RegistryFriendlyByteBuf, ShapelessRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 28 | Recipe.CommonInfo.STREAM_CODEC,
|
| 29 | o -> o.commonInfo,
|
| 30 | CraftingRecipe.CraftingBookInfo.STREAM_CODEC,
|
| 31 | o -> o.bookInfo,
|
| 32 | ItemStackTemplate.STREAM_CODEC,
|
| 33 | o -> o.result,
|
| 34 | Ingredient.CONTENTS_STREAM_CODEC.apply(ByteBufCodecs.list()),
|
| 35 | o -> o.ingredients,
|
| 36 | ShapelessRecipe::new
|
| 37 | );
|
| 38 | public static final RecipeSerializer<ShapelessRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 39 | private final ItemStackTemplate result;
|
| 40 | private final List<Ingredient> ingredients;
|
| 41 |
|
| 42 | public ShapelessRecipe(
|
| 43 | final Recipe.CommonInfo commonInfo, final CraftingRecipe.CraftingBookInfo bookInfo, final ItemStackTemplate result, final List<Ingredient> ingredients
|
| 44 | ) {
|
| 45 | super(commonInfo, bookInfo);
|
| 46 | this.result = result;
|
| 47 | this.ingredients = ingredients;
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public RecipeSerializer<ShapelessRecipe> getSerializer() {
|
| 52 | return SERIALIZER;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected PlacementInfo createPlacementInfo() {
|
| 57 | return PlacementInfo.create(this.ingredients);
|
| 58 | }
|
| 59 |
|
| 60 | public boolean matches(final CraftingInput input, final Level level) {
|
| 61 | if (input.ingredientCount() != this.ingredients.size()) {
|
| 62 | return false;
|
| 63 | } else {
|
| 64 | return input.size() == 1 && this.ingredients.size() == 1 ? this.ingredients.getFirst().test(input.getItem(0)) : input.stackedContents().canCraft(this, null);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | public ItemStack assemble(final CraftingInput input) {
|
| 69 | return this.result.create();
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public List<RecipeDisplay> display() {
|
| 74 | return List.of(
|
| 75 | new ShapelessCraftingRecipeDisplay(
|
| 76 | this.ingredients.stream().map(Ingredient::display).toList(),
|
| 77 | new SlotDisplay.ItemStackSlotDisplay(this.result),
|
| 78 | new SlotDisplay.ItemSlotDisplay(Items.CRAFTING_TABLE)
|
| 79 | )
|
| 80 | );
|
| 81 | }
|
| 82 | }
|
| 83 |
|