| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import java.util.Collections;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.tags.BlockTags;
|
| 13 | import net.minecraft.tags.FluidTags;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.world.entity.LivingEntity;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 | import net.minecraft.world.level.BlockGetter;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.LevelAccessor;
|
| 20 | import net.minecraft.world.level.LevelReader;
|
| 21 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 22 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 25 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 26 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 27 | import net.minecraft.world.level.material.FlowingFluid;
|
| 28 | import net.minecraft.world.level.material.FluidState;
|
| 29 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 30 | import net.minecraft.world.level.redstone.Orientation;
|
| 31 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 32 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 33 | import net.minecraft.world.phys.shapes.EntityCollisionContext;
|
| 34 | import net.minecraft.world.phys.shapes.Shapes;
|
| 35 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 36 | import org.jspecify.annotations.Nullable;
|
| 37 |
|
| 38 | /**
|
| 39 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 40 | */
|
| 41 | public class LiquidBlock extends Block implements BucketPickup {
|
| 42 | public static final IntegerProperty LEVEL = BlockStateProperties.LEVEL;
|
| 43 | protected final FlowingFluid fluid;
|
| 44 | private final List<FluidState> stateCache;
|
| 45 | public static final ImmutableList<Direction> POSSIBLE_FLOW_DIRECTIONS = ImmutableList.of(
|
| 46 | Direction.DOWN, Direction.SOUTH, Direction.NORTH, Direction.EAST, Direction.WEST
|
| 47 | );
|
| 48 | private static final int BUBBLE_COLUMN_CHECK_DELAY = 20;
|
| 49 |
|
| 50 | /**
|
| 51 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 52 | */
|
| 53 | public LiquidBlock(final FlowingFluid fluid, final BlockBehaviour.Properties properties) {
|
| 54 | super(properties);
|
| 55 | this.fluid = fluid;
|
| 56 | this.stateCache = Lists.newArrayList();
|
| 57 | this.stateCache.add(fluid.getSource(false));
|
| 58 |
|
| 59 | for (int level = 1; level < 8; level++) {
|
| 60 | this.stateCache.add(fluid.getFlowing(8 - level, false));
|
| 61 | }
|
| 62 |
|
| 63 | this.stateCache.add(fluid.getFlowing(8, true));
|
| 64 | this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, 0));
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 69 | if (context.alwaysCollideWithFluid()) {
|
| 70 | return Shapes.block();
|
| 71 | } else {
|
| 72 | return state.getValue(LEVEL) != 0
|
| 73 | ? Shapes.empty()
|
| 74 | : this.ifMobIsColliding(context)
|
| 75 | .map(LivingEntity::getLiquidCollisionShape)
|
| 76 | .filter(
|
| 77 | liquidStableShape -> context.isAbove(liquidStableShape, pos, true) && context.canStandOnFluid(level.getFluidState(pos.above()), state.getFluidState())
|
| 78 | )
|
| 79 | .orElse(Shapes.empty());
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | private Optional<LivingEntity> ifMobIsColliding(final CollisionContext context) {
|
| 84 | return context instanceof EntityCollisionContext entityCollisionContext && entityCollisionContext.getEntity() instanceof LivingEntity mob
|
| 85 | ? Optional.of(mob)
|
| 86 | : Optional.empty();
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected boolean isRandomlyTicking(final BlockState state) {
|
| 91 | return state.getFluidState().isRandomlyTicking();
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 96 | state.getFluidState().randomTick(level, pos, random);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | protected boolean propagatesSkylightDown(final BlockState state) {
|
| 101 | return false;
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 106 | return !this.fluid.is(FluidTags.LAVA);
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | protected FluidState getFluidState(final BlockState state) {
|
| 111 | int level = state.getValue(LEVEL);
|
| 112 | return this.stateCache.get(Math.min(level, 8));
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | protected boolean skipRendering(final BlockState state, final BlockState neighborState, final Direction direction) {
|
| 117 | return neighborState.getFluidState().getType().isSame(this.fluid);
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | protected RenderShape getRenderShape(final BlockState state) {
|
| 122 | return RenderShape.INVISIBLE;
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | protected List<ItemStack> getDrops(final BlockState state, final LootParams.Builder params) {
|
| 127 | return Collections.emptyList();
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 132 | return Shapes.empty();
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 137 | if (this.shouldSpreadLiquid(level, pos, state)) {
|
| 138 | level.scheduleTick(pos, state.getFluidState().getType(), this.fluid.getTickDelay(level));
|
| 139 | }
|
| 140 |
|
| 141 | if (shouldBubbleColumnOccupy(state)) {
|
| 142 | BlockState stateBelow = level.getBlockState(pos.below());
|
| 143 | this.tryScheduleBubbleBlockColumn(level, pos, stateBelow);
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 149 | if (shouldBubbleColumnOccupy(state)) {
|
| 150 | BlockState stateBelow = level.getBlockState(pos.below());
|
| 151 | BubbleColumnBlock.updateColumn(Blocks.BUBBLE_COLUMN, level, pos, stateBelow);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | protected BlockState updateShape(
|
| 157 | final BlockState state,
|
| 158 | final LevelReader level,
|
| 159 | final ScheduledTickAccess ticks,
|
| 160 | final BlockPos pos,
|
| 161 | final Direction directionToNeighbour,
|
| 162 | final BlockPos neighbourPos,
|
| 163 | final BlockState neighbourState,
|
| 164 | final RandomSource random
|
| 165 | ) {
|
| 166 | if (state.getFluidState().isSource() || neighbourState.getFluidState().isSource()) {
|
| 167 | ticks.scheduleTick(pos, state.getFluidState().getType(), this.fluid.getTickDelay(level));
|
| 168 | }
|
| 169 |
|
| 170 | if (directionToNeighbour == Direction.DOWN && shouldBubbleColumnOccupy(state)) {
|
| 171 | this.tryScheduleBubbleBlockColumn(ticks, pos, neighbourState);
|
| 172 | }
|
| 173 |
|
| 174 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 175 | }
|
| 176 |
|
| 177 | private static boolean shouldBubbleColumnOccupy(final BlockState state) {
|
| 178 | return state.getFluidState().is(FluidTags.BUBBLE_COLUMN_CAN_OCCUPY) && state.getFluidState().isSource() && state.getFluidState().isFull();
|
| 179 | }
|
| 180 |
|
| 181 | @Override
|
| 182 | protected void neighborChanged(
|
| 183 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 184 | ) {
|
| 185 | if (this.shouldSpreadLiquid(level, pos, state)) {
|
| 186 | level.scheduleTick(pos, state.getFluidState().getType(), this.fluid.getTickDelay(level));
|
| 187 | }
|
| 188 |
|
| 189 | if (shouldBubbleColumnOccupy(state)) {
|
| 190 | BlockState stateBelow = level.getBlockState(pos.below());
|
| 191 | this.tryScheduleBubbleBlockColumn(level, pos, stateBelow);
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | private void tryScheduleBubbleBlockColumn(final ScheduledTickAccess ticks, final BlockPos pos, final BlockState stateBelow) {
|
| 196 | if (stateBelow.is(BlockTags.ENABLES_BUBBLE_COLUMN_DRAG_DOWN) || stateBelow.is(BlockTags.ENABLES_BUBBLE_COLUMN_PUSH_UP)) {
|
| 197 | ticks.scheduleTick(pos, this, 20);
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | private boolean shouldSpreadLiquid(final Level level, final BlockPos pos, final BlockState state) {
|
| 202 | if (this.fluid.is(FluidTags.LAVA)) {
|
| 203 | boolean isOverSoulSoil = level.getBlockState(pos.below()).is(Blocks.SOUL_SOIL);
|
| 204 |
|
| 205 | for (Direction direction : POSSIBLE_FLOW_DIRECTIONS) {
|
| 206 | BlockPos neighbourPos = pos.relative(direction.getOpposite());
|
| 207 | if (level.getFluidState(neighbourPos).is(FluidTags.WATER)) {
|
| 208 | Block convertToBlock = level.getFluidState(pos).isSource() ? Blocks.OBSIDIAN : Blocks.COBBLESTONE;
|
| 209 | level.setBlockAndUpdate(pos, convertToBlock.defaultBlockState());
|
| 210 | this.fizz(level, pos);
|
| 211 | return false;
|
| 212 | }
|
| 213 |
|
| 214 | if (isOverSoulSoil && level.getBlockState(neighbourPos).is(Blocks.BLUE_ICE)) {
|
| 215 | level.setBlockAndUpdate(pos, Blocks.BASALT.defaultBlockState());
|
| 216 | this.fizz(level, pos);
|
| 217 | return false;
|
| 218 | }
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | return true;
|
| 223 | }
|
| 224 |
|
| 225 | private void fizz(final LevelAccessor level, final BlockPos pos) {
|
| 226 | level.levelEvent(1501, pos, 0);
|
| 227 | }
|
| 228 |
|
| 229 | @Override
|
| 230 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 231 | builder.add(LEVEL);
|
| 232 | }
|
| 233 |
|
| 234 | @Override
|
| 235 | public ItemStack pickupBlock(final @Nullable LivingEntity user, final LevelAccessor level, final BlockPos pos, final BlockState state) {
|
| 236 | if (state.getValue(LEVEL) == 0) {
|
| 237 | level.setBlock(pos, Blocks.AIR.defaultBlockState(), 11);
|
| 238 | return new ItemStack(this.fluid.getBucket());
|
| 239 | } else {
|
| 240 | return ItemStack.EMPTY;
|
| 241 | }
|
| 242 | }
|
| 243 |
|
| 244 | @Override
|
| 245 | public Optional<SoundEvent> getPickupSound() {
|
| 246 | return this.fluid.getPickupSound();
|
| 247 | }
|
| 248 | }
|
| 249 |
|