package net.minecraft.world.item;

import java.util.function.Predicate;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
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.level.ItemLike;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;

public class BowItem extends ProjectileWeaponItem implements Vanishable {
    public BowItem(final Properties a) {
        super(a);
    }
    
    @Override
    public void releaseUsing(final ItemStack bki, final Level bqb, final LivingEntity aoy, final int integer) {
        if (!(aoy instanceof Player)) {
            return;
        }
        final Player bec3 = (Player)aoy;
        final boolean boolean7 = bec3.abilities.instabuild || EnchantmentHelper.getItemEnchantmentLevel(Enchantments.INFINITY_ARROWS, bki) > 0;
        ItemStack bki2 = bec3.getProjectile(bki);
        if (bki2.isEmpty() && !boolean7) {
            return;
        }
        if (bki2.isEmpty()) {
            bki2 = new ItemStack(Items.ARROW);
        }
        final int integer2 = this.getUseDuration(bki) - integer;
        final float float10 = getPowerForTime(integer2);
        if (float10 < 0.1) {
            return;
        }
        final boolean boolean8 = boolean7 && bki2.getItem() == Items.ARROW;
        if (!bqb.isClientSide) {
            final ArrowItem bih12 = (ArrowItem)((bki2.getItem() instanceof ArrowItem) ? bki2.getItem() : Items.ARROW);
            final AbstractArrow beg13 = bih12.createArrow(bqb, bki2, bec3);
            beg13.shootFromRotation(bec3, bec3.xRot, bec3.yRot, 0.0f, float10 * 3.0f, 1.0f);
            if (float10 == 1.0f) {
                beg13.setCritArrow(true);
            }
            final int integer3 = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.POWER_ARROWS, bki);
            if (integer3 > 0) {
                beg13.setBaseDamage(beg13.getBaseDamage() + integer3 * 0.5 + 0.5);
            }
            final int integer4 = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.PUNCH_ARROWS, bki);
            if (integer4 > 0) {
                beg13.setKnockback(integer4);
            }
            if (EnchantmentHelper.getItemEnchantmentLevel(Enchantments.FLAMING_ARROWS, bki) > 0) {
                beg13.setSecondsOnFire(100);
            }
            bki.<Player>hurtAndBreak(1, bec3, bec2 -> bec2.broadcastBreakEvent(bec3.getUsedItemHand()));
            if (boolean8 || (bec3.abilities.instabuild && (bki2.getItem() == Items.SPECTRAL_ARROW || bki2.getItem() == Items.TIPPED_ARROW))) {
                beg13.pickup = AbstractArrow.Pickup.CREATIVE_ONLY;
            }
            bqb.addFreshEntity(beg13);
        }
        bqb.playSound(null, bec3.getX(), bec3.getY(), bec3.getZ(), SoundEvents.ARROW_SHOOT, SoundSource.PLAYERS, 1.0f, 1.0f / (BowItem.random.nextFloat() * 0.4f + 1.2f) + float10 * 0.5f);
        if (!boolean8 && !bec3.abilities.instabuild) {
            bki2.shrink(1);
            if (bki2.isEmpty()) {
                bec3.inventory.removeItem(bki2);
            }
        }
        bec3.awardStat(Stats.ITEM_USED.get(this));
    }
    
    public static float getPowerForTime(final int integer) {
        float float2 = integer / 20.0f;
        float2 = (float2 * float2 + float2 * 2.0f) / 3.0f;
        if (float2 > 1.0f) {
            float2 = 1.0f;
        }
        return float2;
    }
    
    @Override
    public int getUseDuration(final ItemStack bki) {
        return 72000;
    }
    
    @Override
    public UseAnim getUseAnimation(final ItemStack bki) {
        return UseAnim.BOW;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bqb, final Player bec, final InteractionHand anf) {
        final ItemStack bki5 = bec.getItemInHand(anf);
        final boolean boolean6 = !bec.getProjectile(bki5).isEmpty();
        if (bec.abilities.instabuild || boolean6) {
            bec.startUsingItem(anf);
            return InteractionResultHolder.<ItemStack>consume(bki5);
        }
        return InteractionResultHolder.<ItemStack>fail(bki5);
    }
    
    @Override
    public Predicate<ItemStack> getAllSupportedProjectiles() {
        return BowItem.ARROW_ONLY;
    }
    
    @Override
    public int getDefaultProjectileRange() {
        return 15;
    }
}
