package net.minecraft.world.item;

import net.minecraft.world.entity.monster.SharedMonsterAttributes;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import com.google.common.collect.Multimap;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.MoverType;
import net.minecraft.util.Mth;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.projectile.ThrownTrident;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.resources.ResourceLocation;

public class TridentItem extends Item {
    public TridentItem(final Properties a) {
        super(a);
        this.addProperty(new ResourceLocation("throwing"), (ben, bjw, akz) -> (akz != null && akz.isUsingItem() && akz.getUseItem() == ben) ? 1.0f : 0.0f);
    }
    
    @Override
    public boolean canAttackBlock(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj) {
        return !ayj.isCreative();
    }
    
    @Override
    public UseAnim getUseAnimation(final ItemStack ben) {
        return UseAnim.SPEAR;
    }
    
    @Override
    public int getUseDuration(final ItemStack ben) {
        return 72000;
    }
    
    @Override
    public void releaseUsing(final ItemStack ben, final Level bjw, final LivingEntity akz, final int integer) {
        if (!(akz instanceof Player)) {
            return;
        }
        final Player ayj2 = (Player)akz;
        final int integer2 = this.getUseDuration(ben) - integer;
        if (integer2 < 10) {
            return;
        }
        final int integer3 = EnchantmentHelper.getRiptide(ben);
        if (integer3 > 0 && !ayj2.isInWaterOrRain()) {
            return;
        }
        if (!bjw.isClientSide) {
            ben.<Player>hurtAndBreak(1, ayj2, ayj -> ayj.broadcastBreakEvent(akz.getUsedItemHand()));
            if (integer3 == 0) {
                final ThrownTrident azk9 = new ThrownTrident(bjw, ayj2, ben);
                azk9.shootFromRotation(ayj2, ayj2.xRot, ayj2.yRot, 0.0f, 2.5f + integer3 * 0.5f, 1.0f);
                if (ayj2.abilities.instabuild) {
                    azk9.pickup = AbstractArrow.Pickup.CREATIVE_ONLY;
                }
                bjw.addFreshEntity(azk9);
                bjw.playSound(null, azk9, SoundEvents.TRIDENT_THROW, SoundSource.PLAYERS, 1.0f, 1.0f);
                if (!ayj2.abilities.instabuild) {
                    ayj2.inventory.removeItem(ben);
                }
            }
        }
        ayj2.awardStat(Stats.ITEM_USED.get(this));
        if (integer3 > 0) {
            final float float9 = ayj2.yRot;
            final float float10 = ayj2.xRot;
            float float11 = -Mth.sin(float9 * 0.017453292f) * Mth.cos(float10 * 0.017453292f);
            float float12 = -Mth.sin(float10 * 0.017453292f);
            float float13 = Mth.cos(float9 * 0.017453292f) * Mth.cos(float10 * 0.017453292f);
            final float float14 = Mth.sqrt(float11 * float11 + float12 * float12 + float13 * float13);
            final float float15 = 3.0f * ((1.0f + integer3) / 4.0f);
            float11 *= float15 / float14;
            float12 *= float15 / float14;
            float13 *= float15 / float14;
            ayj2.push(float11, float12, float13);
            ayj2.startAutoSpinAttack(20);
            if (ayj2.onGround) {
                final float float16 = 1.1999999f;
                ayj2.move(MoverType.SELF, new Vec3(0.0, 1.1999999284744263, 0.0));
            }
            SoundEvent aah16;
            if (integer3 >= 3) {
                aah16 = SoundEvents.TRIDENT_RIPTIDE_3;
            }
            else if (integer3 == 2) {
                aah16 = SoundEvents.TRIDENT_RIPTIDE_2;
            }
            else {
                aah16 = SoundEvents.TRIDENT_RIPTIDE_1;
            }
            bjw.playSound(null, ayj2, aah16, SoundSource.PLAYERS, 1.0f, 1.0f);
        }
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bjw, final Player ayj, final InteractionHand ajk) {
        final ItemStack ben5 = ayj.getItemInHand(ajk);
        if (ben5.getDamageValue() >= ben5.getMaxDamage() - 1) {
            return InteractionResultHolder.<ItemStack>fail(ben5);
        }
        if (EnchantmentHelper.getRiptide(ben5) > 0 && !ayj.isInWaterOrRain()) {
            return InteractionResultHolder.<ItemStack>fail(ben5);
        }
        ayj.startUsingItem(ajk);
        return InteractionResultHolder.<ItemStack>consume(ben5);
    }
    
    @Override
    public boolean hurtEnemy(final ItemStack ben, final LivingEntity akz2, final LivingEntity akz3) {
        ben.<LivingEntity>hurtAndBreak(1, akz3, akz -> akz.broadcastBreakEvent(EquipmentSlot.MAINHAND));
        return true;
    }
    
    @Override
    public boolean mineBlock(final ItemStack ben, final Level bjw, final BlockState byj, final BlockPos fk, final LivingEntity akz) {
        if (byj.getDestroySpeed(bjw, fk) != 0.0) {
            ben.<LivingEntity>hurtAndBreak(2, akz, akz -> akz.broadcastBreakEvent(EquipmentSlot.MAINHAND));
        }
        return true;
    }
    
    @Override
    public Multimap<String, AttributeModifier> getDefaultAttributeModifiers(final EquipmentSlot akv) {
        final Multimap<String, AttributeModifier> multimap3 = super.getDefaultAttributeModifiers(akv);
        if (akv == EquipmentSlot.MAINHAND) {
            multimap3.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(TridentItem.BASE_ATTACK_DAMAGE_UUID, "Tool modifier", 8.0, AttributeModifier.Operation.ADDITION));
            multimap3.put(SharedMonsterAttributes.ATTACK_SPEED.getName(), new AttributeModifier(TridentItem.BASE_ATTACK_SPEED_UUID, "Tool modifier", -2.9000000953674316, AttributeModifier.Operation.ADDITION));
        }
        return multimap3;
    }
    
    @Override
    public int getEnchantmentValue() {
        return 1;
    }
}
