/* * Decompiled with CFR 0.152. */ package net.minecraft.world.item.crafting; import net.minecraft.core.HolderLookup; import net.minecraft.core.NonNullList; import net.minecraft.core.component.DataComponents; import net.minecraft.tags.ItemTags; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.component.WrittenBookContent; import net.minecraft.world.item.crafting.CraftingBookCategory; import net.minecraft.world.item.crafting.CraftingInput; import net.minecraft.world.item.crafting.CustomRecipe; import net.minecraft.world.item.crafting.RecipeSerializer; import net.minecraft.world.level.Level; public class BookCloningRecipe extends CustomRecipe { public BookCloningRecipe(CraftingBookCategory category) { super(category); } @Override public boolean matches(CraftingInput input, Level level) { if (input.ingredientCount() < 2) { return false; } boolean hasEmptyBooks = false; boolean hasSourceBook = false; for (int slot = 0; slot < input.size(); ++slot) { ItemStack itemStack = input.getItem(slot); if (itemStack.isEmpty()) continue; if (itemStack.has(DataComponents.WRITTEN_BOOK_CONTENT)) { if (hasSourceBook) { return false; } hasSourceBook = true; continue; } if (itemStack.is(ItemTags.BOOK_CLONING_TARGET)) { hasEmptyBooks = true; continue; } return false; } return hasSourceBook && hasEmptyBooks; } @Override public ItemStack assemble(CraftingInput input, HolderLookup.Provider registries) { int count = 0; ItemStack source = ItemStack.EMPTY; for (int slot = 0; slot < input.size(); ++slot) { ItemStack itemStack = input.getItem(slot); if (itemStack.isEmpty()) continue; if (itemStack.has(DataComponents.WRITTEN_BOOK_CONTENT)) { if (!source.isEmpty()) { return ItemStack.EMPTY; } source = itemStack; continue; } if (itemStack.is(ItemTags.BOOK_CLONING_TARGET)) { ++count; continue; } return ItemStack.EMPTY; } WrittenBookContent sourceContent = source.get(DataComponents.WRITTEN_BOOK_CONTENT); if (source.isEmpty() || count < 1 || sourceContent == null) { return ItemStack.EMPTY; } WrittenBookContent copiedContent = sourceContent.tryCraftCopy(); if (copiedContent == null) { return ItemStack.EMPTY; } ItemStack result = source.copyWithCount(count); result.set(DataComponents.WRITTEN_BOOK_CONTENT, copiedContent); return result; } @Override public NonNullList getRemainingItems(CraftingInput input) { NonNullList result = NonNullList.withSize(input.size(), ItemStack.EMPTY); for (int slot = 0; slot < result.size(); ++slot) { ItemStack itemStack = input.getItem(slot); ItemStack remainder = itemStack.getItem().getCraftingRemainder(); if (!remainder.isEmpty()) { result.set(slot, remainder); continue; } if (!itemStack.has(DataComponents.WRITTEN_BOOK_CONTENT)) continue; result.set(slot, itemStack.copyWithCount(1)); break; } return result; } @Override public RecipeSerializer getSerializer() { return RecipeSerializer.BOOK_CLONING; } }