| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 8 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 9 | import net.minecraft.network.codec.StreamCodec;
|
| 10 | import net.minecraft.tags.EnchantmentTags;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 13 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 14 | import net.minecraft.world.item.enchantment.ItemEnchantments;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public class RepairItemRecipe extends CustomRecipe {
|
| 19 | public static final RepairItemRecipe INSTANCE = new RepairItemRecipe();
|
| 20 | public static final MapCodec<RepairItemRecipe> MAP_CODEC = MapCodec.unit(INSTANCE);
|
| 21 | public static final StreamCodec<RegistryFriendlyByteBuf, RepairItemRecipe> STREAM_CODEC = StreamCodec.unit(INSTANCE);
|
| 22 | public static final RecipeSerializer<RepairItemRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 23 |
|
| 24 | private static @Nullable Pair<ItemStack, ItemStack> getItemsToCombine(final CraftingInput input) {
|
| 25 | if (input.ingredientCount() != 2) {
|
| 26 | return null;
|
| 27 | }
|
| 28 |
|
| 29 | ItemStack first = null;
|
| 30 |
|
| 31 | for (int i = 0; i < input.size(); i++) {
|
| 32 | ItemStack itemStack = input.getItem(i);
|
| 33 | if (!itemStack.isEmpty()) {
|
| 34 | if (first != null) {
|
| 35 | return canCombine(first, itemStack) ? Pair.of(first, itemStack) : null;
|
| 36 | }
|
| 37 |
|
| 38 | first = itemStack;
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | return null;
|
| 43 | }
|
| 44 |
|
| 45 | private static boolean canCombine(final ItemStack first, final ItemStack second) {
|
| 46 | return second.is(first.getItem())
|
| 47 | && first.getCount() == 1
|
| 48 | && second.getCount() == 1
|
| 49 | && first.has(DataComponents.MAX_DAMAGE)
|
| 50 | && second.has(DataComponents.MAX_DAMAGE)
|
| 51 | && first.has(DataComponents.DAMAGE)
|
| 52 | && second.has(DataComponents.DAMAGE);
|
| 53 | }
|
| 54 |
|
| 55 | public boolean matches(final CraftingInput input, final Level level) {
|
| 56 | return getItemsToCombine(input) != null;
|
| 57 | }
|
| 58 |
|
| 59 | public ItemStack assemble(final CraftingInput input) {
|
| 60 | Pair<ItemStack, ItemStack> itemsToCombine = getItemsToCombine(input);
|
| 61 | if (itemsToCombine == null) {
|
| 62 | return ItemStack.EMPTY;
|
| 63 | }
|
| 64 |
|
| 65 | ItemStack first = itemsToCombine.getFirst();
|
| 66 | ItemStack second = itemsToCombine.getSecond();
|
| 67 | int durability = Math.max(first.getMaxDamage(), second.getMaxDamage());
|
| 68 | int remaining1 = first.getMaxDamage() - first.getDamageValue();
|
| 69 | int remaining2 = second.getMaxDamage() - second.getDamageValue();
|
| 70 | int remaining = remaining1 + remaining2 + durability * 5 / 100;
|
| 71 | ItemStack itemStack = new ItemStack(first.getItem());
|
| 72 | itemStack.set(DataComponents.MAX_DAMAGE, durability);
|
| 73 | itemStack.setDamageValue(Math.max(durability - remaining, 0));
|
| 74 | ItemEnchantments firstEnchants = EnchantmentHelper.getEnchantmentsForCrafting(first);
|
| 75 | ItemEnchantments secondEnchants = EnchantmentHelper.getEnchantmentsForCrafting(second);
|
| 76 | EnchantmentHelper.updateEnchantments(itemStack, newEnchantments -> {
|
| 77 | for (Holder<Enchantment> enchantment : Sets.union(firstEnchants.keySet(), secondEnchants.keySet())) {
|
| 78 | if (enchantment.is(EnchantmentTags.CURSE)) {
|
| 79 | int enchantLevel = Math.max(firstEnchants.getLevel(enchantment), secondEnchants.getLevel(enchantment));
|
| 80 | newEnchantments.set(enchantment, enchantLevel);
|
| 81 | }
|
| 82 | }
|
| 83 | });
|
| 84 | return itemStack;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public RecipeSerializer<RepairItemRecipe> getSerializer() {
|
| 89 | return SERIALIZER;
|
| 90 | }
|
| 91 | }
|
| 92 |
|