package com.acethewildfire.acesbs.mixin;

import com.acethewildfire.acesbs.item.ModItems;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.Objects;

@Mixin(PlayerEntity.class)
public abstract class PlayerEntityMixin{

    @Shadow @Final
    PlayerInventory inventory;

    @Shadow public abstract Text getName();

    @Inject(method = "damage", at = @At(value = "TAIL"), locals = LocalCapture.CAPTURE_FAILSOFT, cancellable = true)
    private void damageMixin(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) {
        Item chestplate = this.inventory.getArmorStack(2).getItem();
        if (chestplate == ModItems.BERSERKERS_PAULDRON && !Objects.equals(this.getName().getLiteralString(), "Tennocraft12")){
            PlayerEntity player = (PlayerEntity) (Object) this;
            World world = player.getWorld();
            if (!world.isClient()) {
                world.playSound(null, player.getX(),player.getY(), player.getZ(), SoundEvents.ENTITY_SKELETON_HURT, SoundCategory.PLAYERS, 2f, 1f);
            }
            cir.setReturnValue(true);
        }
    }
}
