| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.Optional;
|
| 5 | import java.util.function.BiPredicate;
|
| 6 | import java.util.function.Predicate;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.core.particles.ParticleOptions;
|
| 10 | import net.minecraft.core.particles.ParticleTypes;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.tags.FluidTags;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 15 | import net.minecraft.world.entity.Entity;
|
| 16 | import net.minecraft.world.level.BlockGetter;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.LevelReader;
|
| 19 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.block.state.properties.SpeleothemThickness;
|
| 22 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 23 | import net.minecraft.world.level.material.Fluid;
|
| 24 | import net.minecraft.world.level.material.FluidState;
|
| 25 | import net.minecraft.world.level.material.Fluids;
|
| 26 | import net.minecraft.world.phys.Vec3;
|
| 27 | import net.minecraft.world.phys.shapes.BooleanOp;
|
| 28 | import net.minecraft.world.phys.shapes.Shapes;
|
| 29 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 |
|
| 32 | public class PointedDripstoneBlock extends SpeleothemBlock {
|
| 33 | private static final int MAX_SEARCH_LENGTH_WHEN_CHECKING_DRIP_TYPE = 11;
|
| 34 | private static final float DRIP_PROBABILITY_PER_ANIMATE_TICK = 0.02F;
|
| 35 | private static final float DRIP_PROBABILITY_PER_ANIMATE_TICK_IF_UNDER_LIQUID_SOURCE = 0.12F;
|
| 36 | private static final int MAX_SEARCH_LENGTH_BETWEEN_STALACTITE_TIP_AND_CAULDRON = 11;
|
| 37 | private static final float WATER_TRANSFER_PROBABILITY_PER_RANDOM_TICK = 0.17578125F;
|
| 38 | private static final float LAVA_TRANSFER_PROBABILITY_PER_RANDOM_TICK = 0.05859375F;
|
| 39 | private static final float STALAGMITE_FALL_DISTANCE_OFFSET = 2.5F;
|
| 40 | private static final int STALAGMITE_FALL_DAMAGE_MODIFIER = 2;
|
| 41 | private static final double STALACTITE_DRIP_START_PIXEL = SHAPE_TIP_DOWN.min(Direction.Axis.Y);
|
| 42 | private static final VoxelShape REQUIRED_SPACE_TO_DRIP_THROUGH_NON_SOLID_BLOCK = Block.column(4.0, 0.0, 16.0);
|
| 43 |
|
| 44 | public PointedDripstoneBlock(final BlockState blockToGrowOn, final BlockBehaviour.Properties properties) {
|
| 45 | super(blockToGrowOn, properties);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected @LevelEvent.Value int getStalactiteLandingSound() {
|
| 50 | return 1045;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void fallOn(final Level level, final BlockState state, final BlockPos pos, final Entity entity, final double fallDistance) {
|
| 55 | if (state.getValue(TIP_DIRECTION) == Direction.UP && state.getValue(THICKNESS) == SpeleothemThickness.TIP) {
|
| 56 | entity.causeFallDamage(fallDistance + 2.5, 2.0F, level.damageSources().stalagmite());
|
| 57 | } else {
|
| 58 | super.fallOn(level, state, pos, entity, fallDistance);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 64 | if (isFreeHangingStalactite(state)) {
|
| 65 | float randomValue = random.nextFloat();
|
| 66 | if (!(randomValue > 0.12F)) {
|
| 67 | getFluidAboveStalactite(level, pos, state)
|
| 68 | .filter(fluidAbove -> randomValue < 0.02F || canFillCauldron(fluidAbove.fluid))
|
| 69 | .ifPresent(fluidAbove -> spawnDripParticle(level, pos, state, fluidAbove.fluid, fluidAbove.pos));
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 76 | maybeTransferFluid(state, level, pos, random.nextFloat());
|
| 77 | super.randomTick(state, level, pos, random);
|
| 78 | }
|
| 79 |
|
| 80 | @VisibleForTesting
|
| 81 | public static void maybeTransferFluid(final BlockState state, final ServerLevel level, final BlockPos pos, final float randomValue) {
|
| 82 | if (!(randomValue > 0.17578125F) || !(randomValue > 0.05859375F)) {
|
| 83 | if (isStalactiteStartPos(state, level, pos)) {
|
| 84 | Optional<PointedDripstoneBlock.FluidInfo> fluidInfo = getFluidAboveStalactite(level, pos, state);
|
| 85 | if (!fluidInfo.isEmpty()) {
|
| 86 | Fluid fluid = fluidInfo.get().fluid;
|
| 87 | float transferProbability;
|
| 88 | if (fluid == Fluids.WATER) {
|
| 89 | transferProbability = 0.17578125F;
|
| 90 | } else {
|
| 91 | if (fluid != Fluids.LAVA) {
|
| 92 | return;
|
| 93 | }
|
| 94 |
|
| 95 | transferProbability = 0.05859375F;
|
| 96 | }
|
| 97 |
|
| 98 | if (!(randomValue >= transferProbability)) {
|
| 99 | BlockPos stalactiteTipPos = findTip(state, level, pos, 11, false);
|
| 100 | if (stalactiteTipPos != null) {
|
| 101 | if (fluidInfo.get().sourceState.is(Blocks.MUD) && fluid == Fluids.WATER) {
|
| 102 | BlockState newState = Blocks.CLAY.defaultBlockState();
|
| 103 | level.setBlockAndUpdate(fluidInfo.get().pos, newState);
|
| 104 | Block.pushEntitiesUp(fluidInfo.get().sourceState, newState, level, fluidInfo.get().pos);
|
| 105 | level.gameEvent(GameEvent.BLOCK_CHANGE, fluidInfo.get().pos, GameEvent.Context.of(newState));
|
| 106 | level.levelEvent(1504, stalactiteTipPos, 0);
|
| 107 | } else {
|
| 108 | BlockPos cauldronPos = findFillableCauldronBelowStalactiteTip(level, stalactiteTipPos, fluid);
|
| 109 | if (cauldronPos != null) {
|
| 110 | level.levelEvent(1504, stalactiteTipPos, 0);
|
| 111 | int fallDistance = stalactiteTipPos.getY() - cauldronPos.getY();
|
| 112 | int delay = 50 + fallDistance;
|
| 113 | BlockState cauldronState = level.getBlockState(cauldronPos);
|
| 114 | level.scheduleTick(cauldronPos, cauldronState.getBlock(), delay);
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | public static void spawnDripParticle(final Level level, final BlockPos stalactiteTipPos, final BlockState stalactiteTipState) {
|
| 125 | getFluidAboveStalactite(level, stalactiteTipPos, stalactiteTipState)
|
| 126 | .ifPresent(fluidAbove -> spawnDripParticle(level, stalactiteTipPos, stalactiteTipState, fluidAbove.fluid, fluidAbove.pos));
|
| 127 | }
|
| 128 |
|
| 129 | private static void spawnDripParticle(
|
| 130 | final Level level, final BlockPos stalactiteTipPos, final BlockState stalactiteTipState, final Fluid fluidAbove, final BlockPos posAbove
|
| 131 | ) {
|
| 132 | Vec3 offset = stalactiteTipState.getOffset(stalactiteTipPos);
|
| 133 | double PIXEL_SIZE = 0.0625;
|
| 134 | double x = stalactiteTipPos.getX() + 0.5 + offset.x;
|
| 135 | double y = stalactiteTipPos.getY() + STALACTITE_DRIP_START_PIXEL - 0.0625;
|
| 136 | double z = stalactiteTipPos.getZ() + 0.5 + offset.z;
|
| 137 | ParticleOptions dripParticle = getDripParticle(level, fluidAbove, posAbove);
|
| 138 | level.addParticle(dripParticle, x, y, z, 0.0, 0.0, 0.0);
|
| 139 | }
|
| 140 |
|
| 141 | private static Optional<BlockPos> findRootBlock(final Level level, final BlockPos pos, final BlockState dripStoneState, final int maxSearchLength) {
|
| 142 | Direction tipDirection = dripStoneState.getValue(TIP_DIRECTION);
|
| 143 | BiPredicate<BlockPos, BlockState> pathPredicate = (pathPos, state) -> state.is(dripStoneState.getBlock()) && state.getValue(TIP_DIRECTION) == tipDirection;
|
| 144 | return findBlockVertical(
|
| 145 | level, pos, tipDirection.getOpposite().getAxisDirection(), pathPredicate, state -> !state.is(dripStoneState.getBlock()), maxSearchLength
|
| 146 | );
|
| 147 | }
|
| 148 |
|
| 149 | private static @Nullable BlockPos findFillableCauldronBelowStalactiteTip(final Level level, final BlockPos stalactiteTipPos, final Fluid fluid) {
|
| 150 | Predicate<BlockState> cauldronPredicate = state -> state.getBlock() instanceof AbstractCauldronBlock
|
| 151 | && ((AbstractCauldronBlock)state.getBlock()).canReceiveStalactiteDrip(fluid);
|
| 152 | BiPredicate<BlockPos, BlockState> pathPredicate = (pos, state) -> canDripThrough(level, pos, state);
|
| 153 | return findBlockVertical(level, stalactiteTipPos, Direction.DOWN.getAxisDirection(), pathPredicate, cauldronPredicate, 11).orElse(null);
|
| 154 | }
|
| 155 |
|
| 156 | public static @Nullable BlockPos findStalactiteTipAboveCauldron(final Level level, final BlockPos cauldronPos) {
|
| 157 | BiPredicate<BlockPos, BlockState> pathPredicate = (pos, state) -> canDripThrough(level, pos, state);
|
| 158 | return findBlockVertical(level, cauldronPos, Direction.UP.getAxisDirection(), pathPredicate, SpeleothemBlock::isFreeHangingStalactite, 11).orElse(null);
|
| 159 | }
|
| 160 |
|
| 161 | public static Fluid getCauldronFillFluidType(final ServerLevel level, final BlockPos stalactitePos) {
|
| 162 | return getFluidAboveStalactite(level, stalactitePos, level.getBlockState(stalactitePos))
|
| 163 | .map(fluidSource -> fluidSource.fluid)
|
| 164 | .filter(PointedDripstoneBlock::canFillCauldron)
|
| 165 | .orElse(Fluids.EMPTY);
|
| 166 | }
|
| 167 |
|
| 168 | private static Optional<PointedDripstoneBlock.FluidInfo> getFluidAboveStalactite(
|
| 169 | final Level level, final BlockPos stalactitePos, final BlockState stalactiteState
|
| 170 | ) {
|
| 171 | return !isStalactite(stalactiteState) ? Optional.empty() : findRootBlock(level, stalactitePos, stalactiteState, 11).map(rootPos -> {
|
| 172 | BlockPos abovePos = rootPos.above();
|
| 173 | BlockState aboveState = level.getBlockState(abovePos);
|
| 174 | Fluid fluid;
|
| 175 | if (aboveState.is(Blocks.MUD) && !level.environmentAttributes().getValue(EnvironmentAttributes.WATER_EVAPORATES, abovePos)) {
|
| 176 | fluid = Fluids.WATER;
|
| 177 | } else {
|
| 178 | fluid = level.getFluidState(abovePos).getType();
|
| 179 | }
|
| 180 |
|
| 181 | return new PointedDripstoneBlock.FluidInfo(abovePos, fluid, aboveState);
|
| 182 | });
|
| 183 | }
|
| 184 |
|
| 185 | private static boolean canFillCauldron(final Fluid fluidAbove) {
|
| 186 | return fluidAbove == Fluids.LAVA || fluidAbove == Fluids.WATER;
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | protected boolean canGrow(final LevelReader level, final BlockPos pos) {
|
| 191 | FluidState fluidState = level.getBlockState(pos.above(2)).getFluidState();
|
| 192 | return super.canGrow(level, pos) && fluidState.is(Fluids.WATER) && fluidState.isSource();
|
| 193 | }
|
| 194 |
|
| 195 | private static ParticleOptions getDripParticle(final Level level, final Fluid fluidAbove, final BlockPos posAbove) {
|
| 196 | if (fluidAbove.isSame(Fluids.EMPTY)) {
|
| 197 | return level.environmentAttributes().getValue(EnvironmentAttributes.DEFAULT_DRIPSTONE_PARTICLE, posAbove);
|
| 198 | } else {
|
| 199 | return fluidAbove.is(FluidTags.LAVA) ? ParticleTypes.DRIPPING_DRIPSTONE_LAVA : ParticleTypes.DRIPPING_DRIPSTONE_WATER;
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | protected boolean blocksStalagmiteScan(final LevelReader level, final BlockPos pos, final BlockState state) {
|
| 205 | return !canDripThrough(level, pos, state);
|
| 206 | }
|
| 207 |
|
| 208 | private static boolean canDripThrough(final BlockGetter level, final BlockPos pos, final BlockState state) {
|
| 209 | if (state.isAir()) {
|
| 210 | return true;
|
| 211 | }
|
| 212 |
|
| 213 | if (state.isSolidRender()) {
|
| 214 | return false;
|
| 215 | }
|
| 216 |
|
| 217 | if (!state.getFluidState().isEmpty()) {
|
| 218 | return false;
|
| 219 | }
|
| 220 |
|
| 221 | VoxelShape collisionShape = state.getCollisionShape(level, pos);
|
| 222 | return !Shapes.joinIsNotEmpty(REQUIRED_SPACE_TO_DRIP_THROUGH_NON_SOLID_BLOCK, collisionShape, BooleanOp.AND);
|
| 223 | }
|
| 224 |
|
| 225 | private record FluidInfo(BlockPos pos, Fluid fluid, BlockState sourceState) {
|
| 226 | }
|
| 227 | }
|
| 228 |
|