| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.advancements.predicates.MinMaxBounds;
|
| 7 | import net.minecraft.core.NonNullList;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 10 | import net.minecraft.network.codec.StreamCodec;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.item.ItemStackTemplate;
|
| 13 | import net.minecraft.world.item.component.WrittenBookContent;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 |
|
| 16 | public class BookCloningRecipe extends CustomRecipe {
|
| 17 | public static final MinMaxBounds.Ints ALLOWED_BOOK_GENERATION_RANGES = MinMaxBounds.Ints.between(0, 2);
|
| 18 | public static final MinMaxBounds.Ints DEFAULT_BOOK_GENERATION_RANGES = MinMaxBounds.Ints.between(0, 1);
|
| 19 | private static final Codec<MinMaxBounds.Ints> ALLOWED_GENERATION_CODEC = MinMaxBounds.Ints.CODEC
|
| 20 | .validate(MinMaxBounds.validateContainedInRange(ALLOWED_BOOK_GENERATION_RANGES));
|
| 21 | public static final MapCodec<BookCloningRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 22 | i -> i.group(
|
| 23 | Ingredient.CODEC.fieldOf("source").forGetter(o -> o.source),
|
| 24 | Ingredient.CODEC.fieldOf("material").forGetter(o -> o.material),
|
| 25 | ALLOWED_GENERATION_CODEC.optionalFieldOf("allowed_generations", DEFAULT_BOOK_GENERATION_RANGES).forGetter(o -> o.allowedGenerations),
|
| 26 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result)
|
| 27 | )
|
| 28 | .apply(i, BookCloningRecipe::new)
|
| 29 | );
|
| 30 | public static final StreamCodec<RegistryFriendlyByteBuf, BookCloningRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 31 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 32 | o -> o.source,
|
| 33 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 34 | o -> o.material,
|
| 35 | MinMaxBounds.Ints.STREAM_CODEC,
|
| 36 | o -> o.allowedGenerations,
|
| 37 | ItemStackTemplate.STREAM_CODEC,
|
| 38 | o -> o.result,
|
| 39 | BookCloningRecipe::new
|
| 40 | );
|
| 41 | public static final RecipeSerializer<BookCloningRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 42 | private final Ingredient source;
|
| 43 | private final Ingredient material;
|
| 44 | private final MinMaxBounds.Ints allowedGenerations;
|
| 45 | private final ItemStackTemplate result;
|
| 46 |
|
| 47 | public BookCloningRecipe(final Ingredient source, final Ingredient material, final MinMaxBounds.Ints allowedGenerations, final ItemStackTemplate result) {
|
| 48 | this.source = source;
|
| 49 | this.material = material;
|
| 50 | this.allowedGenerations = allowedGenerations;
|
| 51 | this.result = result;
|
| 52 | }
|
| 53 |
|
| 54 | private boolean canCraftCopy(final WrittenBookContent writtenBookContent) {
|
| 55 | return this.allowedGenerations.matches(writtenBookContent.generation());
|
| 56 | }
|
| 57 |
|
| 58 | public boolean matches(final CraftingInput input, final Level level) {
|
| 59 | if (input.ingredientCount() < 2) {
|
| 60 | return false;
|
| 61 | }
|
| 62 |
|
| 63 | boolean hasMaterial = false;
|
| 64 | boolean hasSource = false;
|
| 65 |
|
| 66 | for (int slot = 0; slot < input.size(); slot++) {
|
| 67 | ItemStack itemStack = input.getItem(slot);
|
| 68 | if (!itemStack.isEmpty()) {
|
| 69 | if (this.source.test(itemStack)) {
|
| 70 | WrittenBookContent writtenBookContent = itemStack.get(DataComponents.WRITTEN_BOOK_CONTENT);
|
| 71 | if (writtenBookContent == null || !this.canCraftCopy(writtenBookContent)) {
|
| 72 | return false;
|
| 73 | }
|
| 74 |
|
| 75 | if (hasSource) {
|
| 76 | return false;
|
| 77 | }
|
| 78 |
|
| 79 | hasSource = true;
|
| 80 | } else {
|
| 81 | if (!this.material.test(itemStack)) {
|
| 82 | return false;
|
| 83 | }
|
| 84 |
|
| 85 | hasMaterial = true;
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | return hasSource && hasMaterial;
|
| 91 | }
|
| 92 |
|
| 93 | public ItemStack assemble(final CraftingInput input) {
|
| 94 | int count = 0;
|
| 95 | ItemStack source = ItemStack.EMPTY;
|
| 96 |
|
| 97 | for (int slot = 0; slot < input.size(); slot++) {
|
| 98 | ItemStack itemStack = input.getItem(slot);
|
| 99 | if (!itemStack.isEmpty()) {
|
| 100 | if (this.source.test(itemStack) && itemStack.has(DataComponents.WRITTEN_BOOK_CONTENT)) {
|
| 101 | if (!source.isEmpty()) {
|
| 102 | return ItemStack.EMPTY;
|
| 103 | }
|
| 104 |
|
| 105 | source = itemStack;
|
| 106 | } else {
|
| 107 | if (!this.material.test(itemStack)) {
|
| 108 | return ItemStack.EMPTY;
|
| 109 | }
|
| 110 |
|
| 111 | count++;
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | WrittenBookContent sourceContent = source.get(DataComponents.WRITTEN_BOOK_CONTENT);
|
| 117 | if (sourceContent == null) {
|
| 118 | return ItemStack.EMPTY;
|
| 119 | }
|
| 120 |
|
| 121 | WrittenBookContent copiedContent = sourceContent.craftCopy();
|
| 122 | ItemStack result = TransmuteRecipe.createWithOriginalComponents(this.result, source, count - 1);
|
| 123 | result.set(DataComponents.WRITTEN_BOOK_CONTENT, copiedContent);
|
| 124 | return result;
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public NonNullList<ItemStack> getRemainingItems(final CraftingInput input) {
|
| 129 | NonNullList<ItemStack> result = NonNullList.withSize(input.size(), ItemStack.EMPTY);
|
| 130 |
|
| 131 | for (int slot = 0; slot < result.size(); slot++) {
|
| 132 | ItemStack itemStack = input.getItem(slot);
|
| 133 | ItemStackTemplate remainder = itemStack.getItem().getCraftingRemainder();
|
| 134 | if (remainder != null) {
|
| 135 | result.set(slot, remainder.create());
|
| 136 | } else if (itemStack.has(DataComponents.WRITTEN_BOOK_CONTENT)) {
|
| 137 | result.set(slot, itemStack.copyWithCount(1));
|
| 138 | break;
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | return result;
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public RecipeSerializer<BookCloningRecipe> getSerializer() {
|
| 147 | return SERIALIZER;
|
| 148 | }
|
| 149 | }
|
| 150 |
|