package net.minecraft.world.level.block;

import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.BlockParticleOption;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.phys.Vec3;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.entity.vehicle.Boat;
import net.minecraft.world.entity.item.PrimedTnt;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.shapes.VoxelShape;

public class HoneyBlock extends HalfTransparentBlock {
    protected static final VoxelShape SHAPE;
    
    public HoneyBlock(final Properties c) {
        super(c);
    }
    
    private static boolean doesEntityDoHoneyBlockSlideEffects(final Entity akq) {
        return akq instanceof LivingEntity || akq instanceof AbstractMinecart || akq instanceof PrimedTnt || akq instanceof Boat;
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return HoneyBlock.SHAPE;
    }
    
    @Override
    public void fallOn(final Level bjw, final BlockPos fk, final Entity akq, final float float4) {
        akq.playSound(SoundEvents.HONEY_BLOCK_SLIDE, 1.0f, 1.0f);
        if (!bjw.isClientSide) {
            bjw.broadcastEntityEvent(akq, (byte)54);
        }
        if (akq.causeFallDamage(float4, 0.2f)) {
            akq.playSound(this.soundType.getFallSound(), this.soundType.getVolume() * 0.5f, this.soundType.getPitch() * 0.75f);
        }
    }
    
    @Override
    public void entityInside(final BlockState byj, final Level bjw, final BlockPos fk, final Entity akq) {
        if (this.isSlidingDown(fk, akq)) {
            this.maybeDoSlideAchievement(akq, fk);
            this.doSlideMovement(akq);
            this.maybeDoSlideEffects(bjw, akq);
        }
        super.entityInside(byj, bjw, fk, akq);
    }
    
    private boolean isSlidingDown(final BlockPos fk, final Entity akq) {
        if (akq.onGround) {
            return false;
        }
        if (akq.getY() > fk.getY() + 0.9375 - 1.0E-7) {
            return false;
        }
        if (akq.getDeltaMovement().y >= -0.08) {
            return false;
        }
        final double double4 = Math.abs(fk.getX() + 0.5 - akq.getX());
        final double double5 = Math.abs(fk.getZ() + 0.5 - akq.getZ());
        final double double6 = 0.4375 + akq.getBbWidth() / 2.0f;
        return double4 + 1.0E-7 > double6 || double5 + 1.0E-7 > double6;
    }
    
    private void maybeDoSlideAchievement(final Entity akq, final BlockPos fk) {
        if (akq instanceof ServerPlayer && akq.level.getGameTime() % 20L == 0L) {
            CriteriaTriggers.HONEY_BLOCK_SLIDE.trigger((ServerPlayer)akq, akq.level.getBlockState(fk));
        }
    }
    
    private void doSlideMovement(final Entity akq) {
        final Vec3 cvl3 = akq.getDeltaMovement();
        if (cvl3.y < -0.13) {
            final double double4 = -0.05 / cvl3.y;
            akq.setDeltaMovement(new Vec3(cvl3.x * double4, -0.05, cvl3.z * double4));
        }
        else {
            akq.setDeltaMovement(new Vec3(cvl3.x, -0.05, cvl3.z));
        }
        akq.fallDistance = 0.0f;
    }
    
    private void maybeDoSlideEffects(final Level bjw, final Entity akq) {
        if (doesEntityDoHoneyBlockSlideEffects(akq)) {
            if (bjw.random.nextInt(5) == 0) {
                akq.playSound(SoundEvents.HONEY_BLOCK_SLIDE, 1.0f, 1.0f);
            }
            if (!bjw.isClientSide && bjw.random.nextInt(5) == 0) {
                bjw.broadcastEntityEvent(akq, (byte)53);
            }
        }
    }
    
    public static void showSlideParticles(final Entity akq) {
        showParticles(akq, 5);
    }
    
    public static void showJumpParticles(final Entity akq) {
        showParticles(akq, 10);
    }
    
    private static void showParticles(final Entity akq, final int integer) {
        if (!akq.level.isClientSide) {
            return;
        }
        final BlockState byj3 = Blocks.HONEY_BLOCK.defaultBlockState();
        for (int integer2 = 0; integer2 < integer; ++integer2) {
            akq.level.addParticle(new BlockParticleOption(ParticleTypes.BLOCK, byj3), akq.getX(), akq.getY(), akq.getZ(), 0.0, 0.0, 0.0);
        }
    }
    
    static {
        SHAPE = Block.box(1.0, 0.0, 1.0, 15.0, 15.0, 15.0);
    }
}
