| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.sounds.SoundEvents;
|
| 6 | import net.minecraft.sounds.SoundSource;
|
| 7 | import net.minecraft.tags.FluidTags;
|
| 8 | import net.minecraft.world.level.Level;
|
| 9 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 10 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.material.FluidState;
|
| 13 | import net.minecraft.world.level.redstone.Orientation;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | /**
|
| 17 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 18 | */
|
| 19 | public class SpongeBlock extends Block {
|
| 20 | public static final int MAX_DEPTH = 6;
|
| 21 | public static final int MAX_COUNT = 64;
|
| 22 | private static final Direction[] ALL_DIRECTIONS = Direction.values();
|
| 23 |
|
| 24 | /**
|
| 25 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 26 | */
|
| 27 | public SpongeBlock(final BlockBehaviour.Properties properties) {
|
| 28 | super(properties);
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 33 | if (!oldState.is(state.getBlock())) {
|
| 34 | this.tryAbsorbWater(level, pos);
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | protected void neighborChanged(
|
| 40 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 41 | ) {
|
| 42 | this.tryAbsorbWater(level, pos);
|
| 43 | super.neighborChanged(state, level, pos, block, orientation, movedByPiston);
|
| 44 | }
|
| 45 |
|
| 46 | protected void tryAbsorbWater(final Level level, final BlockPos pos) {
|
| 47 | if (this.removeWaterBreadthFirstSearch(level, pos)) {
|
| 48 | level.setBlock(pos, Blocks.WET_SPONGE.defaultBlockState(), 2);
|
| 49 | level.playSound(null, pos, SoundEvents.SPONGE_ABSORB, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | private boolean removeWaterBreadthFirstSearch(final Level level, final BlockPos startPos) {
|
| 54 | return BlockPos.breadthFirstTraversal(startPos, 6, 65, (pos, consumer) -> {
|
| 55 | for (Direction direction : ALL_DIRECTIONS) {
|
| 56 | consumer.accept(pos.relative(direction));
|
| 57 | }
|
| 58 | }, pos -> {
|
| 59 | if (pos.equals(startPos)) {
|
| 60 | return BlockPos.TraversalNodeStatus.ACCEPT;
|
| 61 | } else {
|
| 62 | BlockState state = level.getBlockState(pos);
|
| 63 | FluidState fluidState = level.getFluidState(pos);
|
| 64 | if (!fluidState.is(FluidTags.WATER)) {
|
| 65 | return BlockPos.TraversalNodeStatus.SKIP;
|
| 66 | } else if (state.getBlock() instanceof BucketPickup bucketPickup && !bucketPickup.pickupBlock(null, level, pos, state).isEmpty()) {
|
| 67 | return BlockPos.TraversalNodeStatus.ACCEPT;
|
| 68 | } else {
|
| 69 | if (state.getBlock() instanceof LiquidBlock) {
|
| 70 | level.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
|
| 71 | } else {
|
| 72 | if (!state.is(Blocks.KELP) && !state.is(Blocks.KELP_PLANT) && !state.is(Blocks.SEAGRASS) && !state.is(Blocks.TALL_SEAGRASS)) {
|
| 73 | return BlockPos.TraversalNodeStatus.SKIP;
|
| 74 | }
|
| 75 |
|
| 76 | BlockEntity blockEntity = state.hasBlockEntity() ? level.getBlockEntity(pos) : null;
|
| 77 | dropResources(state, level, pos, blockEntity);
|
| 78 | level.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
|
| 79 | }
|
| 80 |
|
| 81 | return BlockPos.TraversalNodeStatus.ACCEPT;
|
| 82 | }
|
| 83 | }
|
| 84 | }) > 1;
|
| 85 | }
|
| 86 | }
|
| 87 |
|