| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.tags.BlockTags;
|
| 12 | import net.minecraft.tags.FluidTags;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 16 | import net.minecraft.world.entity.LivingEntity;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 | import net.minecraft.world.item.Items;
|
| 19 | import net.minecraft.world.level.BlockGetter;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 | import net.minecraft.world.level.LevelAccessor;
|
| 22 | import net.minecraft.world.level.LevelReader;
|
| 23 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 24 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 25 | import net.minecraft.world.level.block.state.BlockState;
|
| 26 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 27 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 28 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 29 | import net.minecraft.world.level.material.FluidState;
|
| 30 | import net.minecraft.world.level.material.Fluids;
|
| 31 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 32 | import net.minecraft.world.phys.shapes.Shapes;
|
| 33 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 34 | import org.jspecify.annotations.Nullable;
|
| 35 |
|
| 36 | public class BubbleColumnBlock extends Block implements BucketPickup {
|
| 37 | public static final BooleanProperty DRAG_DOWN = BlockStateProperties.DRAG;
|
| 38 | private static final int CHECK_PERIOD = 5;
|
| 39 |
|
| 40 | public BubbleColumnBlock(final BlockBehaviour.Properties properties) {
|
| 41 | super(properties);
|
| 42 | this.registerDefaultState(this.stateDefinition.any().setValue(DRAG_DOWN, true));
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | protected void entityInside(
|
| 47 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 48 | ) {
|
| 49 | if (isPrecise) {
|
| 50 | BlockState stateAbove = level.getBlockState(pos.above());
|
| 51 | boolean nothingAbove = stateAbove.getCollisionShape(level, pos).isEmpty() && stateAbove.getFluidState().isEmpty();
|
| 52 | if (nothingAbove) {
|
| 53 | entity.onAboveBubbleColumn(state.getValue(DRAG_DOWN), pos);
|
| 54 | } else {
|
| 55 | entity.onInsideBubbleColumn(state.getValue(DRAG_DOWN));
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 62 | updateColumn(this, level, pos, state, level.getBlockState(pos.below()));
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected FluidState getFluidState(final BlockState state) {
|
| 67 | return Fluids.WATER.getSource(false);
|
| 68 | }
|
| 69 |
|
| 70 | public static void updateColumn(final Block bubbleColumn, final LevelAccessor level, final BlockPos occupyAt, final BlockState belowState) {
|
| 71 | updateColumn(bubbleColumn, level, occupyAt, level.getBlockState(occupyAt), belowState);
|
| 72 | }
|
| 73 |
|
| 74 | public static void updateColumn(
|
| 75 | final Block bubbleColumn, final LevelAccessor level, final BlockPos occupyAt, final BlockState occupyState, final BlockState belowState
|
| 76 | ) {
|
| 77 | if (canOccupy(bubbleColumn, occupyState)) {
|
| 78 | BlockState columnState = getColumnState(bubbleColumn, belowState, occupyState);
|
| 79 | level.setBlock(occupyAt, columnState, 2);
|
| 80 | BlockPos.MutableBlockPos pos = occupyAt.mutable().move(Direction.UP);
|
| 81 |
|
| 82 | while (canOccupy(bubbleColumn, level.getBlockState(pos))) {
|
| 83 | if (!level.setBlock(pos, columnState, 2)) {
|
| 84 | return;
|
| 85 | }
|
| 86 |
|
| 87 | pos.move(Direction.UP);
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | private static boolean canOccupy(final Block bubbleColumn, final BlockState occupyState) {
|
| 93 | if (occupyState.is(bubbleColumn)) {
|
| 94 | return true;
|
| 95 | }
|
| 96 |
|
| 97 | FluidState occupyFluid = occupyState.getFluidState();
|
| 98 | return occupyFluid.is(FluidTags.BUBBLE_COLUMN_CAN_OCCUPY)
|
| 99 | && occupyState.getBlock() instanceof LiquidBlock
|
| 100 | && occupyFluid.isSource()
|
| 101 | && occupyFluid.getAmount() >= 8;
|
| 102 | }
|
| 103 |
|
| 104 | private static BlockState getColumnState(final Block bubbleColumn, final BlockState belowState, final BlockState occupyState) {
|
| 105 | if (belowState.is(bubbleColumn)) {
|
| 106 | return belowState;
|
| 107 | } else if (belowState.is(BlockTags.ENABLES_BUBBLE_COLUMN_PUSH_UP)) {
|
| 108 | return bubbleColumn.defaultBlockState().setValue(DRAG_DOWN, false);
|
| 109 | } else if (belowState.is(BlockTags.ENABLES_BUBBLE_COLUMN_DRAG_DOWN)) {
|
| 110 | return bubbleColumn.defaultBlockState().setValue(DRAG_DOWN, true);
|
| 111 | } else {
|
| 112 | return occupyState.is(bubbleColumn) ? Blocks.WATER.defaultBlockState() : occupyState;
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 118 | double x = pos.getX();
|
| 119 | double y = pos.getY();
|
| 120 | double z = pos.getZ();
|
| 121 | if (state.getValue(DRAG_DOWN)) {
|
| 122 | level.addAlwaysVisibleParticle(ParticleTypes.CURRENT_DOWN, x + 0.5, y + 0.8, z, 0.0, 0.0, 0.0);
|
| 123 | if (random.nextInt(200) == 0) {
|
| 124 | level.playLocalSound(
|
| 125 | x, y, z, SoundEvents.BUBBLE_COLUMN_WHIRLPOOL_AMBIENT, SoundSource.BLOCKS, 0.2F + random.nextFloat() * 0.2F, 0.9F + random.nextFloat() * 0.15F, false
|
| 126 | );
|
| 127 | }
|
| 128 | } else {
|
| 129 | level.addAlwaysVisibleParticle(ParticleTypes.BUBBLE_COLUMN_UP, x + 0.5, y, z + 0.5, 0.0, 0.04, 0.0);
|
| 130 | level.addAlwaysVisibleParticle(ParticleTypes.BUBBLE_COLUMN_UP, x + random.nextFloat(), y + random.nextFloat(), z + random.nextFloat(), 0.0, 0.04, 0.0);
|
| 131 | if (random.nextInt(200) == 0) {
|
| 132 | level.playLocalSound(
|
| 133 | x, y, z, SoundEvents.BUBBLE_COLUMN_UPWARDS_AMBIENT, SoundSource.BLOCKS, 0.2F + random.nextFloat() * 0.2F, 0.9F + random.nextFloat() * 0.15F, false
|
| 134 | );
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | protected BlockState updateShape(
|
| 141 | final BlockState state,
|
| 142 | final LevelReader level,
|
| 143 | final ScheduledTickAccess ticks,
|
| 144 | final BlockPos pos,
|
| 145 | final Direction directionToNeighbour,
|
| 146 | final BlockPos neighbourPos,
|
| 147 | final BlockState neighbourState,
|
| 148 | final RandomSource random
|
| 149 | ) {
|
| 150 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 151 | if (!state.canSurvive(level, pos)
|
| 152 | || directionToNeighbour == Direction.DOWN
|
| 153 | || directionToNeighbour == Direction.UP && !neighbourState.is(this) && canOccupy(this, neighbourState)) {
|
| 154 | ticks.scheduleTick(pos, this, 5);
|
| 155 | }
|
| 156 |
|
| 157 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 162 | BlockState belowState = level.getBlockState(pos.below());
|
| 163 | return belowState.is(this) || belowState.is(BlockTags.ENABLES_BUBBLE_COLUMN_PUSH_UP) || belowState.is(BlockTags.ENABLES_BUBBLE_COLUMN_DRAG_DOWN);
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 168 | return Shapes.empty();
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | protected RenderShape getRenderShape(final BlockState state) {
|
| 173 | return RenderShape.INVISIBLE;
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 178 | builder.add(DRAG_DOWN);
|
| 179 | }
|
| 180 |
|
| 181 | @Override
|
| 182 | public ItemStack pickupBlock(final @Nullable LivingEntity user, final LevelAccessor level, final BlockPos pos, final BlockState state) {
|
| 183 | level.setBlock(pos, Blocks.AIR.defaultBlockState(), 11);
|
| 184 | return new ItemStack(Items.WATER_BUCKET);
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public Optional<SoundEvent> getPickupSound() {
|
| 189 | return Fluids.WATER.getPickupSound();
|
| 190 | }
|
| 191 | }
|
| 192 |
|