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