package com.zivilon.cinder_loe.mixins;

import lotr.common.enchant.LOTREnchantment;
import lotr.common.enchant.LOTREnchantmentHelper;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(LOTREnchantmentHelper.class)
public class MixinLOTREnchantmenHelper {

    /**
     * Prevent negative modifiers (weak or detrimental enchants) from being applied.
     */
    @Redirect(
            method = "applyRandomEnchantments(Lnet/minecraft/item/ItemStack;Ljava/util/Random;ZZ)V",
            at = @At(
                    value = "INVOKE",
                    target = "Llotr/common/enchant/LOTREnchantmentHelper;setHasEnchant(Lnet/minecraft/item/ItemStack;Llotr/common/enchant/LOTREnchantment;)V"
            ),
            remap = false
    )
    private static void skipNegativeEnchant(ItemStack itemstack, LOTREnchantment ench) {
        // Only apply if enchant is beneficial
        if (ench.isBeneficial()) {
            LOTREnchantmentHelper.setHasEnchant(itemstack, ench);
        }
    }
}
