91 lines
3.9 KiB
Java
91 lines
3.9 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.datafixers.util.Pair
|
|
* org.jspecify.annotations.Nullable
|
|
*/
|
|
package net.minecraft.world.item.crafting;
|
|
|
|
import com.mojang.datafixers.util.Pair;
|
|
import net.minecraft.core.Holder;
|
|
import net.minecraft.core.HolderLookup;
|
|
import net.minecraft.core.component.DataComponents;
|
|
import net.minecraft.core.registries.Registries;
|
|
import net.minecraft.tags.EnchantmentTags;
|
|
import net.minecraft.world.item.ItemStack;
|
|
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.item.enchantment.Enchantment;
|
|
import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
|
import net.minecraft.world.item.enchantment.ItemEnchantments;
|
|
import net.minecraft.world.level.Level;
|
|
import org.jspecify.annotations.Nullable;
|
|
|
|
public class RepairItemRecipe
|
|
extends CustomRecipe {
|
|
public RepairItemRecipe(CraftingBookCategory category) {
|
|
super(category);
|
|
}
|
|
|
|
private static @Nullable Pair<ItemStack, ItemStack> getItemsToCombine(CraftingInput input) {
|
|
if (input.ingredientCount() != 2) {
|
|
return null;
|
|
}
|
|
ItemStack first = null;
|
|
for (int i = 0; i < input.size(); ++i) {
|
|
ItemStack itemStack = input.getItem(i);
|
|
if (itemStack.isEmpty()) continue;
|
|
if (first == null) {
|
|
first = itemStack;
|
|
continue;
|
|
}
|
|
return RepairItemRecipe.canCombine(first, itemStack) ? Pair.of((Object)first, (Object)itemStack) : null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static boolean canCombine(ItemStack first, ItemStack second) {
|
|
return second.is(first.getItem()) && first.getCount() == 1 && second.getCount() == 1 && first.has(DataComponents.MAX_DAMAGE) && second.has(DataComponents.MAX_DAMAGE) && first.has(DataComponents.DAMAGE) && second.has(DataComponents.DAMAGE);
|
|
}
|
|
|
|
@Override
|
|
public boolean matches(CraftingInput input, Level level) {
|
|
return RepairItemRecipe.getItemsToCombine(input) != null;
|
|
}
|
|
|
|
@Override
|
|
public ItemStack assemble(CraftingInput input, HolderLookup.Provider registries) {
|
|
Pair<ItemStack, ItemStack> itemsToCombine = RepairItemRecipe.getItemsToCombine(input);
|
|
if (itemsToCombine == null) {
|
|
return ItemStack.EMPTY;
|
|
}
|
|
ItemStack first = (ItemStack)itemsToCombine.getFirst();
|
|
ItemStack second = (ItemStack)itemsToCombine.getSecond();
|
|
int durability = Math.max(first.getMaxDamage(), second.getMaxDamage());
|
|
int remaining1 = first.getMaxDamage() - first.getDamageValue();
|
|
int remaining2 = second.getMaxDamage() - second.getDamageValue();
|
|
int remaining = remaining1 + remaining2 + durability * 5 / 100;
|
|
ItemStack itemStack = new ItemStack(first.getItem());
|
|
itemStack.set(DataComponents.MAX_DAMAGE, durability);
|
|
itemStack.setDamageValue(Math.max(durability - remaining, 0));
|
|
ItemEnchantments firstEnchants = EnchantmentHelper.getEnchantmentsForCrafting(first);
|
|
ItemEnchantments secondEnchants = EnchantmentHelper.getEnchantmentsForCrafting(second);
|
|
EnchantmentHelper.updateEnchantments(itemStack, enchantments -> registries.lookupOrThrow(Registries.ENCHANTMENT).listElements().filter(h -> h.is(EnchantmentTags.CURSE)).forEach(enchant -> {
|
|
int enchantLevel = Math.max(firstEnchants.getLevel((Holder<Enchantment>)enchant), secondEnchants.getLevel((Holder<Enchantment>)enchant));
|
|
if (enchantLevel > 0) {
|
|
enchantments.upgrade((Holder<Enchantment>)enchant, enchantLevel);
|
|
}
|
|
}));
|
|
return itemStack;
|
|
}
|
|
|
|
@Override
|
|
public RecipeSerializer<RepairItemRecipe> getSerializer() {
|
|
return RecipeSerializer.REPAIR_ITEM;
|
|
}
|
|
}
|
|
|