package net.minecraft.world.level.block; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.block.state.properties.RotationSegment; import net.minecraft.world.level.block.state.properties.WoodType; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; public class StandingSignBlock extends SignBlock { public static final MapCodec CODEC = RecordCodecBuilder.mapCodec( instance -> instance.group(WoodType.CODEC.fieldOf("wood_type").forGetter(SignBlock::type), propertiesCodec()).apply(instance, StandingSignBlock::new) ); public static final IntegerProperty ROTATION = BlockStateProperties.ROTATION_16; @Override public MapCodec codec() { return CODEC; } public StandingSignBlock(WoodType type, BlockBehaviour.Properties properties) { super(type, properties.sound(type.soundType())); this.registerDefaultState(this.stateDefinition.any().setValue(ROTATION, 0).setValue(WATERLOGGED, false)); } @Override protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) { return level.getBlockState(pos.below()).isSolid(); } @Override public BlockState getStateForPlacement(BlockPlaceContext context) { FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos()); return this.defaultBlockState() .setValue(ROTATION, RotationSegment.convertToSegment(context.getRotation() + 180.0F)) .setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER); } @Override protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) { return direction == Direction.DOWN && !this.canSurvive(state, level, pos) ? Blocks.AIR.defaultBlockState() : super.updateShape(state, direction, neighborState, level, pos, neighborPos); } @Override public float getYRotationDegrees(BlockState state) { return RotationSegment.convertToDegrees((Integer)state.getValue(ROTATION)); } @Override protected BlockState rotate(BlockState state, Rotation rotation) { return state.setValue(ROTATION, rotation.rotate((Integer)state.getValue(ROTATION), 16)); } @Override protected BlockState mirror(BlockState state, Mirror mirror) { return state.setValue(ROTATION, mirror.mirror((Integer)state.getValue(ROTATION), 16)); } @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { builder.add(ROTATION, WATERLOGGED); } }