package net.minecraft.world.level.block; import com.mojang.serialization.MapCodec; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; import net.minecraft.tags.BlockTags; import net.minecraft.util.RandomSource; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; 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.block.state.properties.DoubleBlockHalf; import net.minecraft.world.level.block.state.properties.EnumProperty; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; import org.jspecify.annotations.Nullable; public class SmallDripleafBlock extends DoublePlantBlock implements SimpleWaterloggedBlock, BonemealableBlock { public static final MapCodec CODEC = simpleCodec(SmallDripleafBlock::new); private static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; public static final EnumProperty FACING = BlockStateProperties.HORIZONTAL_FACING; private static final VoxelShape SHAPE = Block.column(12.0, 0.0, 13.0); @Override public MapCodec codec() { return CODEC; } public SmallDripleafBlock(final BlockBehaviour.Properties properties) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(HALF, DoubleBlockHalf.LOWER).setValue(WATERLOGGED, false).setValue(FACING, Direction.NORTH)); } @Override protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) { return SHAPE; } @Override protected boolean mayPlaceOn(final BlockState state, final BlockGetter level, final BlockPos pos) { return state.is(BlockTags.SUPPORTS_SMALL_DRIPLEAF) || level.getFluidState(pos.above()).isSourceOfType(Fluids.WATER) && super.mayPlaceOn(state, level, pos); } @Nullable @Override public BlockState getStateForPlacement(final BlockPlaceContext context) { BlockState state = super.getStateForPlacement(context); return state != null ? copyWaterloggedFrom(context.getLevel(), context.getClickedPos(), state.setValue(FACING, context.getHorizontalDirection().getOpposite())) : null; } @Override public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, @Nullable final LivingEntity by, final ItemStack itemStack) { if (!level.isClientSide()) { BlockPos abovePos = pos.above(); BlockState blockState = DoublePlantBlock.copyWaterloggedFrom( level, abovePos, this.defaultBlockState().setValue(HALF, DoubleBlockHalf.UPPER).setValue(FACING, (Direction)state.getValue(FACING)) ); level.setBlock(abovePos, blockState, 3); } } @Override protected FluidState getFluidState(final BlockState state) { return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state); } @Override protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) { if (state.getValue(HALF) == DoubleBlockHalf.UPPER) { return super.canSurvive(state, level, pos); } else { BlockPos belowPos = pos.below(); BlockState belowState = level.getBlockState(belowPos); return this.mayPlaceOn(belowState, level, belowPos); } } @Override protected BlockState updateShape( final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random ) { if ((Boolean)state.getValue(WATERLOGGED)) { ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level)); } return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random); } @Override protected void createBlockStateDefinition(final Builder builder) { builder.add(HALF, WATERLOGGED, FACING); } @Override public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state) { return true; } @Override public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state) { return true; } @Override public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state) { if (state.getValue(DoublePlantBlock.HALF) == DoubleBlockHalf.LOWER) { BlockPos above = pos.above(); level.setBlock(above, level.getFluidState(above).createLegacyBlock(), 18); BigDripleafBlock.placeWithRandomHeight(level, random, pos, state.getValue(FACING)); } else { BlockPos belowPos = pos.below(); this.performBonemeal(level, random, belowPos, level.getBlockState(belowPos)); } } @Override protected BlockState rotate(final BlockState state, final Rotation rotation) { return state.setValue(FACING, rotation.rotate(state.getValue(FACING))); } @Override protected BlockState mirror(final BlockState state, final Mirror mirror) { return state.rotate(mirror.getRotation(state.getValue(FACING))); } @Override protected float getMaxVerticalOffset() { return 0.1F; } }