package com.acethewildfire.acesbs.mixin;

import com.acethewildfire.acesbs.item.custom.SovsPlateCarrierArmorItem;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin {

    @Inject(method = "damage", at = @At("HEAD"), cancellable = true)
    private void preventChestplateBreak(
            int amount,
            LivingEntity entity,
            EquipmentSlot slot,
            CallbackInfo cir
    ) {
        ItemStack stack = (ItemStack)(Object)this;

        if (stack.getItem() instanceof SovsPlateCarrierArmorItem) {

            int newDamage = stack.getDamage() + amount;

            if (newDamage >= stack.getMaxDamage()) {
                stack.setDamage(stack.getMaxDamage());
                cir.cancel(); // cancel breaking
            }
        }
    }
}
