package net.minecraft.world.entity.projectile;

import javax.annotation.Nullable;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.monster.Endermite;
import net.minecraft.world.level.GameRules;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.Item;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;

public class ThrownEnderpearl extends ThrowableItemProjectile {
    public ThrownEnderpearl(final EntityType<? extends ThrownEnderpearl> aoq, final Level bqb) {
        super(aoq, bqb);
    }
    
    public ThrownEnderpearl(final Level bqb, final LivingEntity aoy) {
        super(EntityType.ENDER_PEARL, aoy, bqb);
    }
    
    public ThrownEnderpearl(final Level bqb, final double double2, final double double3, final double double4) {
        super(EntityType.ENDER_PEARL, double2, double3, double4, bqb);
    }
    
    @Override
    protected Item getDefaultItem() {
        return Items.ENDER_PEARL;
    }
    
    @Override
    protected void onHitEntity(final EntityHitResult dei) {
        super.onHitEntity(dei);
        dei.getEntity().hurt(DamageSource.thrown(this, this.getOwner()), 0.0f);
    }
    
    @Override
    protected void onHit(final HitResult dej) {
        super.onHit(dej);
        final Entity aom3 = this.getOwner();
        for (int integer4 = 0; integer4 < 32; ++integer4) {
            this.level.addParticle(ParticleTypes.PORTAL, this.getX(), this.getY() + this.random.nextDouble() * 2.0, this.getZ(), this.random.nextGaussian(), 0.0, this.random.nextGaussian());
        }
        if (!this.level.isClientSide && !this.removed) {
            if (aom3 instanceof ServerPlayer) {
                final ServerPlayer ze4 = (ServerPlayer)aom3;
                if (ze4.connection.getConnection().isConnected() && ze4.level == this.level && !ze4.isSleeping()) {
                    if (this.random.nextFloat() < 0.05f && this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING)) {
                        final Endermite bbs5 = EntityType.ENDERMITE.create(this.level);
                        bbs5.setPlayerSpawned(true);
                        bbs5.moveTo(aom3.getX(), aom3.getY(), aom3.getZ(), aom3.yRot, aom3.xRot);
                        this.level.addFreshEntity(bbs5);
                    }
                    if (aom3.isPassenger()) {
                        aom3.stopRiding();
                    }
                    aom3.teleportTo(this.getX(), this.getY(), this.getZ());
                    aom3.fallDistance = 0.0f;
                    aom3.hurt(DamageSource.FALL, 5.0f);
                }
            }
            else if (aom3 != null) {
                aom3.teleportTo(this.getX(), this.getY(), this.getZ());
                aom3.fallDistance = 0.0f;
            }
            this.remove();
        }
    }
    
    @Override
    public void tick() {
        final Entity aom2 = this.getOwner();
        if (aom2 instanceof Player && !aom2.isAlive()) {
            this.remove();
        }
        else {
            super.tick();
        }
    }
    
    @Nullable
    @Override
    public Entity changeDimension(final ServerLevel zd) {
        final Entity aom3 = this.getOwner();
        if (aom3 != null && aom3.level.dimension() != zd.dimension()) {
            this.setOwner(null);
        }
        return super.changeDimension(zd);
    }
}
