| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.util.RandomSource;
|
| 6 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 7 | import net.minecraft.world.level.BlockGetter;
|
| 8 | import net.minecraft.world.level.LevelReader;
|
| 9 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 10 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 13 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 14 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 15 | import net.minecraft.world.level.material.FluidState;
|
| 16 | import net.minecraft.world.level.material.Fluids;
|
| 17 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 18 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 19 | import net.minecraft.world.phys.shapes.Shapes;
|
| 20 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public class LanternBlock extends Block implements SimpleWaterloggedBlock {
|
| 24 | public static final BooleanProperty HANGING = BlockStateProperties.HANGING;
|
| 25 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 26 | private static final VoxelShape SHAPE_STANDING = Shapes.or(Block.column(4.0, 7.0, 9.0), Block.column(6.0, 0.0, 7.0));
|
| 27 | private static final VoxelShape SHAPE_HANGING = SHAPE_STANDING.move(0.0, 0.0625, 0.0).optimize();
|
| 28 |
|
| 29 | public LanternBlock(final BlockBehaviour.Properties properties) {
|
| 30 | super(properties);
|
| 31 | this.registerDefaultState(this.stateDefinition.any().setValue(HANGING, false).setValue(WATERLOGGED, false));
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 36 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 37 |
|
| 38 | for (Direction direction : context.getNearestLookingDirections()) {
|
| 39 | if (direction.getAxis() == Direction.Axis.Y) {
|
| 40 | BlockState state = this.defaultBlockState().setValue(HANGING, direction == Direction.UP);
|
| 41 | if (state.canSurvive(context.getLevel(), context.getClickedPos())) {
|
| 42 | return state.setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 43 | }
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | return null;
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 52 | return state.getValue(HANGING) ? SHAPE_HANGING : SHAPE_STANDING;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 57 | builder.add(HANGING, WATERLOGGED);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 62 | Direction direction = getConnectedDirection(state).getOpposite();
|
| 63 | return Block.canSupportCenter(level, pos.relative(direction), direction.getOpposite());
|
| 64 | }
|
| 65 |
|
| 66 | protected static Direction getConnectedDirection(final BlockState state) {
|
| 67 | return state.getValue(HANGING) ? Direction.DOWN : Direction.UP;
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected BlockState updateShape(
|
| 72 | final BlockState state,
|
| 73 | final LevelReader level,
|
| 74 | final ScheduledTickAccess ticks,
|
| 75 | final BlockPos pos,
|
| 76 | final Direction directionToNeighbour,
|
| 77 | final BlockPos neighbourPos,
|
| 78 | final BlockState neighbourState,
|
| 79 | final RandomSource random
|
| 80 | ) {
|
| 81 | if (state.getValue(WATERLOGGED)) {
|
| 82 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 83 | }
|
| 84 |
|
| 85 | return getConnectedDirection(state).getOpposite() == directionToNeighbour && !state.canSurvive(level, pos)
|
| 86 | ? Blocks.AIR.defaultBlockState()
|
| 87 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | protected FluidState getFluidState(final BlockState state) {
|
| 92 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 97 | return false;
|
| 98 | }
|
| 99 | }
|
| 100 |
|