package net.minecraft.world.level.block; import com.mojang.serialization.MapCodec; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.util.RandomSource; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.ScheduledTickAccess; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition.Builder; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; import net.minecraft.world.level.pathfinder.PathComputationType; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; import org.jetbrains.annotations.Nullable; public class LanternBlock extends Block implements SimpleWaterloggedBlock { public static final MapCodec CODEC = simpleCodec(LanternBlock::new); public static final BooleanProperty HANGING = BlockStateProperties.HANGING; public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; private static final VoxelShape SHAPE_STANDING = Shapes.or(Block.column(4.0, 7.0, 9.0), Block.column(6.0, 0.0, 7.0)); private static final VoxelShape SHAPE_HANGING = SHAPE_STANDING.move(0.0, 0.0625, 0.0).optimize(); @Override public MapCodec codec() { return CODEC; } public LanternBlock(BlockBehaviour.Properties properties) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(HANGING, false).setValue(WATERLOGGED, false)); } @Nullable @Override public BlockState getStateForPlacement(BlockPlaceContext context) { FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos()); for (Direction direction : context.getNearestLookingDirections()) { if (direction.getAxis() == Direction.Axis.Y) { BlockState blockState = this.defaultBlockState().setValue(HANGING, direction == Direction.UP); if (blockState.canSurvive(context.getLevel(), context.getClickedPos())) { return blockState.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER); } } } return null; } @Override protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { return state.getValue(HANGING) ? SHAPE_HANGING : SHAPE_STANDING; } @Override protected void createBlockStateDefinition(Builder builder) { builder.add(HANGING, WATERLOGGED); } @Override protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) { Direction direction = getConnectedDirection(state).getOpposite(); return Block.canSupportCenter(level, pos.relative(direction), direction.getOpposite()); } protected static Direction getConnectedDirection(BlockState state) { return state.getValue(HANGING) ? Direction.DOWN : Direction.UP; } @Override protected BlockState updateShape( BlockState state, LevelReader level, ScheduledTickAccess scheduledTickAccess, BlockPos pos, Direction direction, BlockPos neighborPos, BlockState neighborState, RandomSource random ) { if ((Boolean)state.getValue(WATERLOGGED)) { scheduledTickAccess.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level)); } return getConnectedDirection(state).getOpposite() == direction && !state.canSurvive(level, pos) ? Blocks.AIR.defaultBlockState() : super.updateShape(state, level, scheduledTickAccess, pos, direction, neighborPos, neighborState, random); } @Override protected FluidState getFluidState(BlockState state) { return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state); } @Override protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) { return false; } }