package net.minecraft.server;

public class ItemBow extends Item {

    public ItemBow() {
        this.maxStackSize = 1;
        this.setMaxDurability(384);
        this.b(CreativeModeTab.j);
        this.a(new MinecraftKey("pull"), new IDynamicTexture() {
        });
        this.a(new MinecraftKey("pulling"), new IDynamicTexture() {
        });
    }

    private ItemStack a(EntityHuman entityhuman) {
        if (this.d(entityhuman.b(EnumHand.OFF_HAND))) {
            return entityhuman.b(EnumHand.OFF_HAND);
        } else if (this.d(entityhuman.b(EnumHand.MAIN_HAND))) {
            return entityhuman.b(EnumHand.MAIN_HAND);
        } else {
            for (int i = 0; i < entityhuman.inventory.getSize(); ++i) {
                ItemStack itemstack = entityhuman.inventory.getItem(i);

                if (this.d(itemstack)) {
                    return itemstack;
                }
            }

            return ItemStack.a;
        }
    }

    protected boolean d(ItemStack itemstack) {
        return itemstack.getItem() instanceof ItemArrow;
    }

    public void a(ItemStack itemstack, World world, EntityLiving entityliving, int i) {
        if (entityliving instanceof EntityHuman) {
            EntityHuman entityhuman = (EntityHuman) entityliving;
            boolean flag = entityhuman.abilities.canInstantlyBuild || EnchantmentManager.getEnchantmentLevel(Enchantments.ARROW_INFINITE, itemstack) > 0;
            ItemStack itemstack1 = this.a(entityhuman);

            if (!itemstack1.isEmpty() || flag) {
                if (itemstack1.isEmpty()) {
                    itemstack1 = new ItemStack(Items.ARROW);
                }

                int j = this.e(itemstack) - i;
                float f = b(j);

                if ((double) f >= 0.1D) {
                    boolean flag1 = flag && itemstack1.getItem() == Items.ARROW;

                    if (!world.isClientSide) {
                        ItemArrow itemarrow = (ItemArrow) ((ItemArrow) (itemstack1.getItem() instanceof ItemArrow ? itemstack1.getItem() : Items.ARROW));
                        EntityArrow entityarrow = itemarrow.a(world, itemstack1, (EntityLiving) entityhuman);

                        entityarrow.a(entityhuman, entityhuman.pitch, entityhuman.yaw, 0.0F, f * 3.0F, 1.0F);
                        if (f == 1.0F) {
                            entityarrow.setCritical(true);
                        }

                        int k = EnchantmentManager.getEnchantmentLevel(Enchantments.ARROW_DAMAGE, itemstack);

                        if (k > 0) {
                            entityarrow.c(entityarrow.k() + (double) k * 0.5D + 0.5D);
                        }

                        int l = EnchantmentManager.getEnchantmentLevel(Enchantments.ARROW_KNOCKBACK, itemstack);

                        if (l > 0) {
                            entityarrow.setKnockbackStrength(l);
                        }

                        if (EnchantmentManager.getEnchantmentLevel(Enchantments.ARROW_FIRE, itemstack) > 0) {
                            entityarrow.setOnFire(100);
                        }

                        itemstack.damage(1, entityhuman);
                        if (flag1 || entityhuman.abilities.canInstantlyBuild && (itemstack1.getItem() == Items.SPECTRAL_ARROW || itemstack1.getItem() == Items.TIPPED_ARROW)) {
                            entityarrow.fromPlayer = EntityArrow.PickupStatus.CREATIVE_ONLY;
                        }

                        world.addEntity(entityarrow);
                    }

                    world.a((EntityHuman) null, entityhuman.locX, entityhuman.locY, entityhuman.locZ, SoundEffects.w, SoundCategory.PLAYERS, 1.0F, 1.0F / (ItemBow.j.nextFloat() * 0.4F + 1.2F) + f * 0.5F);
                    if (!flag1 && !entityhuman.abilities.canInstantlyBuild) {
                        itemstack1.subtract(1);
                        if (itemstack1.isEmpty()) {
                            entityhuman.inventory.f(itemstack1);
                        }
                    }

                    entityhuman.b(StatisticList.b((Item) this));
                }
            }
        }
    }

    public static float b(int i) {
        float f = (float) i / 20.0F;

        f = (f * f + f * 2.0F) / 3.0F;
        if (f > 1.0F) {
            f = 1.0F;
        }

        return f;
    }

    public int e(ItemStack itemstack) {
        return 72000;
    }

    public EnumAnimation f(ItemStack itemstack) {
        return EnumAnimation.BOW;
    }

    public InteractionResultWrapper<ItemStack> a(World world, EntityHuman entityhuman, EnumHand enumhand) {
        ItemStack itemstack = entityhuman.b(enumhand);
        boolean flag = !this.a(entityhuman).isEmpty();

        if (!entityhuman.abilities.canInstantlyBuild && !flag) {
            return flag ? new InteractionResultWrapper(EnumInteractionResult.PASS, itemstack) : new InteractionResultWrapper(EnumInteractionResult.FAIL, itemstack);
        } else {
            entityhuman.c(enumhand);
            return new InteractionResultWrapper(EnumInteractionResult.SUCCESS, itemstack);
        }
    }

    public int c() {
        return 1;
    }
}
