| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import net.minecraft.core.NonNullList;
|
| 5 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 6 | import net.minecraft.network.codec.StreamCodec;
|
| 7 | import net.minecraft.world.item.Item;
|
| 8 | import net.minecraft.world.item.ItemStack;
|
| 9 | import net.minecraft.world.item.ItemStackTemplate;
|
| 10 |
|
| 11 | public interface CraftingRecipe extends Recipe<CraftingInput> {
|
| 12 | @Override
|
| 13 | default RecipeType<CraftingRecipe> getType() {
|
| 14 | return RecipeType.CRAFTING;
|
| 15 | }
|
| 16 |
|
| 17 | @Override
|
| 18 | RecipeSerializer<? extends CraftingRecipe> getSerializer();
|
| 19 |
|
| 20 | CraftingBookCategory category();
|
| 21 |
|
| 22 | default NonNullList<ItemStack> getRemainingItems(final CraftingInput input) {
|
| 23 | return defaultCraftingReminder(input);
|
| 24 | }
|
| 25 |
|
| 26 | static NonNullList<ItemStack> defaultCraftingReminder(final CraftingInput input) {
|
| 27 | NonNullList<ItemStack> result = NonNullList.withSize(input.size(), ItemStack.EMPTY);
|
| 28 |
|
| 29 | for (int slot = 0; slot < result.size(); slot++) {
|
| 30 | Item item = input.getItem(slot).getItem();
|
| 31 | ItemStackTemplate remainder = item.getCraftingRemainder();
|
| 32 | result.set(slot, remainder != null ? remainder.create() : ItemStack.EMPTY);
|
| 33 | }
|
| 34 |
|
| 35 | return result;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | default RecipeBookCategory recipeBookCategory() {
|
| 40 | return switch (this.category()) {
|
| 41 | case BUILDING -> RecipeBookCategories.CRAFTING_BUILDING_BLOCKS;
|
| 42 | case EQUIPMENT -> RecipeBookCategories.CRAFTING_EQUIPMENT;
|
| 43 | case REDSTONE -> RecipeBookCategories.CRAFTING_REDSTONE;
|
| 44 | case MISC -> RecipeBookCategories.CRAFTING_MISC;
|
| 45 | };
|
| 46 | }
|
| 47 |
|
| 48 | record CraftingBookInfo(CraftingBookCategory category, String group) implements Recipe.BookInfo<CraftingBookCategory> {
|
| 49 | public static final MapCodec<CraftingRecipe.CraftingBookInfo> MAP_CODEC = Recipe.BookInfo.mapCodec(
|
| 50 | CraftingBookCategory.CODEC, CraftingBookCategory.MISC, CraftingRecipe.CraftingBookInfo::new
|
| 51 | );
|
| 52 | public static final StreamCodec<RegistryFriendlyByteBuf, CraftingRecipe.CraftingBookInfo> STREAM_CODEC = Recipe.BookInfo.streamCodec(
|
| 53 | CraftingBookCategory.STREAM_CODEC, CraftingRecipe.CraftingBookInfo::new
|
| 54 | );
|
| 55 | }
|
| 56 | }
|
| 57 |
|