package net.minecraft.world.entity.projectile;

import javax.annotation.Nullable;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.monster.Endermite;
import net.minecraft.world.level.GameRules;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.block.entity.TheEndGatewayBlockEntity;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;

public class ThrownEnderpearl extends ThrowableItemProjectile {
    private LivingEntity originalOwner;
    
    public ThrownEnderpearl(final EntityType<? extends ThrownEnderpearl> aku, final Level bjw) {
        super(aku, bjw);
    }
    
    public ThrownEnderpearl(final Level bjw, final LivingEntity akz) {
        super(EntityType.ENDER_PEARL, akz, bjw);
        this.originalOwner = akz;
    }
    
    public ThrownEnderpearl(final Level bjw, final double double2, final double double3, final double double4) {
        super(EntityType.ENDER_PEARL, double2, double3, double4, bjw);
    }
    
    @Override
    protected Item getDefaultItem() {
        return Items.ENDER_PEARL;
    }
    
    @Override
    protected void onHit(final HitResult cvi) {
        final LivingEntity akz3 = this.getOwner();
        if (cvi.getType() == HitResult.Type.ENTITY) {
            final Entity akq4 = ((EntityHitResult)cvi).getEntity();
            if (akq4 == this.originalOwner) {
                return;
            }
            akq4.hurt(DamageSource.thrown(this, akz3), 0.0f);
        }
        if (cvi.getType() == HitResult.Type.BLOCK) {
            final BlockPos fk4 = ((BlockHitResult)cvi).getBlockPos();
            final BlockEntity bwl5 = this.level.getBlockEntity(fk4);
            if (bwl5 instanceof TheEndGatewayBlockEntity) {
                final TheEndGatewayBlockEntity bxm6 = (TheEndGatewayBlockEntity)bwl5;
                if (akz3 != null) {
                    if (akz3 instanceof ServerPlayer) {
                        CriteriaTriggers.ENTER_BLOCK.trigger((ServerPlayer)akz3, this.level.getBlockState(fk4));
                    }
                    bxm6.teleportEntity(akz3);
                    this.remove();
                    return;
                }
                bxm6.teleportEntity(this);
                return;
            }
        }
        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) {
            if (akz3 instanceof ServerPlayer) {
                final ServerPlayer xe4 = (ServerPlayer)akz3;
                if (xe4.connection.getConnection().isConnected() && xe4.level == this.level && !xe4.isSleeping()) {
                    if (this.random.nextFloat() < 0.05f && this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING)) {
                        final Endermite awn5 = EntityType.ENDERMITE.create(this.level);
                        awn5.setPlayerSpawned(true);
                        awn5.moveTo(akz3.getX(), akz3.getY(), akz3.getZ(), akz3.yRot, akz3.xRot);
                        this.level.addFreshEntity(awn5);
                    }
                    if (akz3.isPassenger()) {
                        akz3.stopRiding();
                    }
                    akz3.teleportTo(this.getX(), this.getY(), this.getZ());
                    akz3.fallDistance = 0.0f;
                    akz3.hurt(DamageSource.FALL, 5.0f);
                }
            }
            else if (akz3 != null) {
                akz3.teleportTo(this.getX(), this.getY(), this.getZ());
                akz3.fallDistance = 0.0f;
            }
            this.remove();
        }
    }
    
    @Override
    public void tick() {
        final LivingEntity akz2 = this.getOwner();
        if (akz2 != null && akz2 instanceof Player && !akz2.isAlive()) {
            this.remove();
        }
        else {
            super.tick();
        }
    }
    
    @Nullable
    @Override
    public Entity changeDimension(final DimensionType cbi) {
        if (this.owner.dimension != cbi) {
            this.owner = null;
        }
        return super.changeDimension(cbi);
    }
}
