package net.minecraft.world.item;

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.world.entity.ai.attributes.Attributes;
import com.google.common.collect.ImmutableMultimap;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attribute;
import com.google.common.collect.Multimap;

public class TridentItem extends Item implements Vanishable {
    private final Multimap<Attribute, AttributeModifier> defaultModifiers;
    
    public TridentItem(final Properties a) {
        super(a);
        final ImmutableMultimap.Builder<Attribute, AttributeModifier> builder3 = ImmutableMultimap.<Attribute, AttributeModifier>builder();
        builder3.put(Attributes.ATTACK_DAMAGE, new AttributeModifier(TridentItem.BASE_ATTACK_DAMAGE_UUID, "Tool modifier", 8.0, AttributeModifier.Operation.ADDITION));
        builder3.put(Attributes.ATTACK_SPEED, new AttributeModifier(TridentItem.BASE_ATTACK_SPEED_UUID, "Tool modifier", -2.9000000953674316, AttributeModifier.Operation.ADDITION));
        this.defaultModifiers = builder3.build();
    }
    
    @Override
    public boolean canAttackBlock(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec) {
        return !bec.isCreative();
    }
    
    @Override
    public UseAnim getUseAnimation(final ItemStack bki) {
        return UseAnim.SPEAR;
    }
    
    @Override
    public int getUseDuration(final ItemStack bki) {
        return 72000;
    }
    
    @Override
    public void releaseUsing(final ItemStack bki, final Level bqb, final LivingEntity aoy, final int integer) {
        if (!(aoy instanceof Player)) {
            return;
        }
        final Player bec2 = (Player)aoy;
        final int integer2 = this.getUseDuration(bki) - integer;
        if (integer2 < 10) {
            return;
        }
        final int integer3 = EnchantmentHelper.getRiptide(bki);
        if (integer3 > 0 && !bec2.isInWaterOrRain()) {
            return;
        }
        if (!bqb.isClientSide) {
            bki.<Player>hurtAndBreak(1, bec2, bec -> bec.broadcastBreakEvent(aoy.getUsedItemHand()));
            if (integer3 == 0) {
                final ThrownTrident bfe9 = new ThrownTrident(bqb, bec2, bki);
                bfe9.shootFromRotation(bec2, bec2.xRot, bec2.yRot, 0.0f, 2.5f + integer3 * 0.5f, 1.0f);
                if (bec2.abilities.instabuild) {
                    bfe9.pickup = AbstractArrow.Pickup.CREATIVE_ONLY;
                }
                bqb.addFreshEntity(bfe9);
                bqb.playSound(null, bfe9, SoundEvents.TRIDENT_THROW, SoundSource.PLAYERS, 1.0f, 1.0f);
                if (!bec2.abilities.instabuild) {
                    bec2.inventory.removeItem(bki);
                }
            }
        }
        bec2.awardStat(Stats.ITEM_USED.get(this));
        if (integer3 > 0) {
            final float float9 = bec2.yRot;
            final float float10 = bec2.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;
            bec2.push(float11, float12, float13);
            bec2.startAutoSpinAttack(20);
            if (bec2.isOnGround()) {
                final float float16 = 1.1999999f;
                bec2.move(MoverType.SELF, new Vec3(0.0, 1.1999999284744263, 0.0));
            }
            SoundEvent ack16;
            if (integer3 >= 3) {
                ack16 = SoundEvents.TRIDENT_RIPTIDE_3;
            }
            else if (integer3 == 2) {
                ack16 = SoundEvents.TRIDENT_RIPTIDE_2;
            }
            else {
                ack16 = SoundEvents.TRIDENT_RIPTIDE_1;
            }
            bqb.playSound(null, bec2, ack16, SoundSource.PLAYERS, 1.0f, 1.0f);
        }
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bqb, final Player bec, final InteractionHand anf) {
        final ItemStack bki5 = bec.getItemInHand(anf);
        if (bki5.getDamageValue() >= bki5.getMaxDamage() - 1) {
            return InteractionResultHolder.<ItemStack>fail(bki5);
        }
        if (EnchantmentHelper.getRiptide(bki5) > 0 && !bec.isInWaterOrRain()) {
            return InteractionResultHolder.<ItemStack>fail(bki5);
        }
        bec.startUsingItem(anf);
        return InteractionResultHolder.<ItemStack>consume(bki5);
    }
    
    @Override
    public boolean hurtEnemy(final ItemStack bki, final LivingEntity aoy2, final LivingEntity aoy3) {
        bki.<LivingEntity>hurtAndBreak(1, aoy3, aoy -> aoy.broadcastBreakEvent(EquipmentSlot.MAINHAND));
        return true;
    }
    
    @Override
    public boolean mineBlock(final ItemStack bki, final Level bqb, final BlockState cfj, final BlockPos fu, final LivingEntity aoy) {
        if (cfj.getDestroySpeed(bqb, fu) != 0.0) {
            bki.<LivingEntity>hurtAndBreak(2, aoy, aoy -> aoy.broadcastBreakEvent(EquipmentSlot.MAINHAND));
        }
        return true;
    }
    
    @Override
    public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(final EquipmentSlot aor) {
        if (aor == EquipmentSlot.MAINHAND) {
            return this.defaultModifiers;
        }
        return super.getDefaultAttributeModifiers(aor);
    }
    
    @Override
    public int getEnchantmentValue() {
        return 1;
    }
}
