| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.particles.BlockParticleOption;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.server.level.ServerPlayer;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 12 | import net.minecraft.world.entity.LivingEntity;
|
| 13 | import net.minecraft.world.entity.item.PrimedTnt;
|
| 14 | import net.minecraft.world.entity.vehicle.boat.AbstractBoat;
|
| 15 | import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
|
| 16 | import net.minecraft.world.level.BlockGetter;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.phys.Vec3;
|
| 21 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 22 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 23 |
|
| 24 | public class HoneyBlock extends HalfTransparentBlock {
|
| 25 | private static final double SLIDE_STARTS_WHEN_VERTICAL_SPEED_IS_AT_LEAST = 0.13;
|
| 26 | private static final double MIN_FALL_SPEED_TO_BE_CONSIDERED_SLIDING = 0.08;
|
| 27 | private static final double THROTTLE_SLIDE_SPEED_TO = 0.05;
|
| 28 | private static final int SLIDE_ADVANCEMENT_CHECK_INTERVAL = 20;
|
| 29 | private static final VoxelShape SHAPE = Block.column(14.0, 0.0, 15.0);
|
| 30 |
|
| 31 | public HoneyBlock(final BlockBehaviour.Properties properties) {
|
| 32 | super(properties);
|
| 33 | }
|
| 34 |
|
| 35 | private static boolean doesEntityDoHoneyBlockSlideEffects(final Entity entity) {
|
| 36 | return entity instanceof LivingEntity || entity instanceof AbstractMinecart || entity instanceof PrimedTnt || entity instanceof AbstractBoat;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 41 | return SHAPE;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void fallOn(final Level level, final BlockState state, final BlockPos pos, final Entity entity, final double fallDistance) {
|
| 46 | entity.playSound(SoundEvents.HONEY_BLOCK_SLIDE, 1.0F, 1.0F);
|
| 47 | if (!level.isClientSide()) {
|
| 48 | level.broadcastEntityEvent(entity, (byte)54);
|
| 49 | }
|
| 50 |
|
| 51 | if (entity.causeFallDamage(fallDistance, 0.2F, level.damageSources().fall())) {
|
| 52 | entity.playSound(this.soundType.getFallSound(), this.soundType.getVolume() * 0.5F, this.soundType.getPitch() * 0.75F);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected void entityInside(
|
| 58 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 59 | ) {
|
| 60 | if (this.isSlidingDown(pos, entity)) {
|
| 61 | this.maybeDoSlideAchievement(entity, pos);
|
| 62 | this.doSlideMovement(entity);
|
| 63 | this.maybeDoSlideEffects(level, entity);
|
| 64 | }
|
| 65 |
|
| 66 | super.entityInside(state, level, pos, entity, effectApplier, isPrecise);
|
| 67 | }
|
| 68 |
|
| 69 | private static double getOldDeltaY(final double deltaY) {
|
| 70 | return deltaY / 0.98F + 0.08;
|
| 71 | }
|
| 72 |
|
| 73 | private static double getNewDeltaY(final double deltaY) {
|
| 74 | return (deltaY - 0.08) * 0.98F;
|
| 75 | }
|
| 76 |
|
| 77 | private boolean isSlidingDown(final BlockPos pos, final Entity entity) {
|
| 78 | if (entity.onGround()) {
|
| 79 | return false;
|
| 80 | }
|
| 81 |
|
| 82 | if (entity.getY() > pos.getY() + 0.9375 - 1.0E-7) {
|
| 83 | return false;
|
| 84 | }
|
| 85 |
|
| 86 | if (getOldDeltaY(entity.getDeltaMovement().y) >= -0.08) {
|
| 87 | return false;
|
| 88 | }
|
| 89 |
|
| 90 | double dx = Math.abs(pos.getX() + 0.5 - entity.getX());
|
| 91 | double dz = Math.abs(pos.getZ() + 0.5 - entity.getZ());
|
| 92 | double overlapDistance = 0.4375 + entity.getBbWidth() / 2.0F;
|
| 93 | return dx + 1.0E-7 > overlapDistance || dz + 1.0E-7 > overlapDistance;
|
| 94 | }
|
| 95 |
|
| 96 | private void maybeDoSlideAchievement(final Entity entity, final BlockPos pos) {
|
| 97 | if (entity instanceof ServerPlayer serverPlayer && entity.level().getGameTime() % 20L == 0L) {
|
| 98 | CriteriaTriggers.HONEY_BLOCK_SLIDE.trigger(serverPlayer, entity.level().getBlockState(pos));
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | private void doSlideMovement(final Entity entity) {
|
| 103 | Vec3 deltaMovement = entity.getDeltaMovement();
|
| 104 | if (getOldDeltaY(entity.getDeltaMovement().y) < -0.13) {
|
| 105 | double horizontalReductionFactor = -0.05 / getOldDeltaY(entity.getDeltaMovement().y);
|
| 106 | entity.setDeltaMovement(new Vec3(deltaMovement.x * horizontalReductionFactor, getNewDeltaY(-0.05), deltaMovement.z * horizontalReductionFactor));
|
| 107 | } else {
|
| 108 | entity.setDeltaMovement(new Vec3(deltaMovement.x, getNewDeltaY(-0.05), deltaMovement.z));
|
| 109 | }
|
| 110 |
|
| 111 | entity.resetFallDistance();
|
| 112 | }
|
| 113 |
|
| 114 | private void maybeDoSlideEffects(final Level level, final Entity entity) {
|
| 115 | if (doesEntityDoHoneyBlockSlideEffects(entity)) {
|
| 116 | RandomSource random = level.getRandom();
|
| 117 | if (random.nextInt(5) == 0) {
|
| 118 | entity.playSound(SoundEvents.HONEY_BLOCK_SLIDE, 1.0F, 1.0F);
|
| 119 | }
|
| 120 |
|
| 121 | if (!level.isClientSide() && random.nextInt(5) == 0) {
|
| 122 | level.broadcastEntityEvent(entity, (byte)53);
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | public static void showSlideParticles(final Entity entity) {
|
| 128 | showParticles(entity, 5);
|
| 129 | }
|
| 130 |
|
| 131 | public static void showJumpParticles(final Entity entity) {
|
| 132 | showParticles(entity, 10);
|
| 133 | }
|
| 134 |
|
| 135 | private static void showParticles(final Entity entity, final int count) {
|
| 136 | if (entity.level().isClientSide()) {
|
| 137 | BlockState blockState = Blocks.HONEY_BLOCK.defaultBlockState();
|
| 138 |
|
| 139 | for (int i = 0; i < count; i++) {
|
| 140 | entity.level().addParticle(new BlockParticleOption(ParticleTypes.BLOCK, blockState), entity.getX(), entity.getY(), entity.getZ(), 0.0, 0.0, 0.0);
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 |
|