package com.mingliqiye.minecraft.enchantment.conflict.mixin;

import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.InfinityEnchantment;
import net.minecraft.enchantment.MendingEnchantment;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(InfinityEnchantment.class)
public abstract class InfinityEnchantmentMixin {

    @Inject(method = "canAccept", at = @At("HEAD"), cancellable = true)
    private void canAccept(
            @NotNull Enchantment enchantment,
            @NotNull CallbackInfoReturnable<Boolean> cir
    ) {
        if (enchantment instanceof MendingEnchantment) {
            cir.setReturnValue(ModConfig.getInstance().isAllowInfinityEnchantment());
        }
    }
}

