package net.minecraft.world.entity.projectile;

import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.network.protocol.Packet;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.level.block.entity.TheEndGatewayBlockEntity;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;

public abstract class ThrowableProjectile extends Projectile {
    protected ThrowableProjectile(final EntityType<? extends ThrowableProjectile> aoq, final Level bqb) {
        super(aoq, bqb);
    }
    
    protected ThrowableProjectile(final EntityType<? extends ThrowableProjectile> aoq, final double double2, final double double3, final double double4, final Level bqb) {
        this(aoq, bqb);
        this.setPos(double2, double3, double4);
    }
    
    protected ThrowableProjectile(final EntityType<? extends ThrowableProjectile> aoq, final LivingEntity aoy, final Level bqb) {
        this(aoq, aoy.getX(), aoy.getEyeY() - 0.10000000149011612, aoy.getZ(), bqb);
        this.setOwner(aoy);
    }
    
    @Override
    public boolean shouldRenderAtSqrDistance(final double double1) {
        double double2 = this.getBoundingBox().getSize() * 4.0;
        if (Double.isNaN(double2)) {
            double2 = 4.0;
        }
        double2 *= 64.0;
        return double1 < double2 * double2;
    }
    
    @Override
    public void tick() {
        super.tick();
        final HitResult dej2 = ProjectileUtil.getHitResult(this, this::canHitEntity, ClipContext.Block.OUTLINE);
        boolean boolean3 = false;
        if (dej2.getType() == HitResult.Type.BLOCK) {
            final BlockPos fu4 = ((BlockHitResult)dej2).getBlockPos();
            final BlockState cfj5 = this.level.getBlockState(fu4);
            if (cfj5.is(Blocks.NETHER_PORTAL)) {
                this.handleInsidePortal(fu4);
                boolean3 = true;
            }
            else if (cfj5.is(Blocks.END_GATEWAY)) {
                final BlockEntity cdl6 = this.level.getBlockEntity(fu4);
                if (cdl6 instanceof TheEndGatewayBlockEntity) {
                    ((TheEndGatewayBlockEntity)cdl6).teleportEntity(this);
                }
                boolean3 = true;
            }
        }
        if (dej2.getType() != HitResult.Type.MISS && !boolean3) {
            this.onHit(dej2);
        }
        final Vec3 dem4 = this.getDeltaMovement();
        final double double5 = this.getX() + dem4.x;
        final double double6 = this.getY() + dem4.y;
        final double double7 = this.getZ() + dem4.z;
        this.updateRotation();
        float float14;
        if (this.isInWater()) {
            for (int integer12 = 0; integer12 < 4; ++integer12) {
                final float float13 = 0.25f;
                this.level.addParticle(ParticleTypes.BUBBLE, double5 - dem4.x * 0.25, double6 - dem4.y * 0.25, double7 - dem4.z * 0.25, dem4.x, dem4.y, dem4.z);
            }
            float14 = 0.8f;
        }
        else {
            float14 = 0.99f;
        }
        this.setDeltaMovement(dem4.scale(float14));
        if (!this.isNoGravity()) {
            final Vec3 dem5 = this.getDeltaMovement();
            this.setDeltaMovement(dem5.x, dem5.y - this.getGravity(), dem5.z);
        }
        this.setPos(double5, double6, double7);
    }
    
    protected float getGravity() {
        return 0.03f;
    }
    
    @Override
    public Packet<?> getAddEntityPacket() {
        return new ClientboundAddEntityPacket(this);
    }
}
