| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.tags.BlockTags;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.entity.LivingEntity;
|
| 9 | import net.minecraft.world.item.ItemStack;
|
| 10 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 11 | import net.minecraft.world.level.BlockGetter;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.LevelReader;
|
| 14 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 15 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 18 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 19 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 20 | import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
|
| 21 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 22 | import net.minecraft.world.level.material.FluidState;
|
| 23 | import net.minecraft.world.level.material.Fluids;
|
| 24 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 25 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public class SmallDripleafBlock extends DoublePlantBlock implements SimpleWaterloggedBlock, BonemealableBlock {
|
| 29 | private static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 30 | public static final EnumProperty<Direction> FACING = BlockStateProperties.HORIZONTAL_FACING;
|
| 31 | private static final VoxelShape SHAPE = Block.column(12.0, 0.0, 13.0);
|
| 32 |
|
| 33 | public SmallDripleafBlock(final BlockBehaviour.Properties properties) {
|
| 34 | super(properties);
|
| 35 | this.registerDefaultState(this.stateDefinition.any().setValue(HALF, DoubleBlockHalf.LOWER).setValue(WATERLOGGED, false).setValue(FACING, Direction.NORTH));
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 40 | return SHAPE;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected boolean mayPlaceOn(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 45 | return state.is(BlockTags.SUPPORTS_SMALL_DRIPLEAF) || level.getFluidState(pos.above()).isSourceOfType(Fluids.WATER) && super.mayPlaceOn(state, level, pos);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 50 | BlockState state = super.getStateForPlacement(context);
|
| 51 | return state != null
|
| 52 | ? copyWaterloggedFrom(context.getLevel(), context.getClickedPos(), state.setValue(FACING, context.getHorizontalDirection().getOpposite()))
|
| 53 | : null;
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 58 | if (!level.isClientSide()) {
|
| 59 | BlockPos abovePos = pos.above();
|
| 60 | BlockState blockState = DoublePlantBlock.copyWaterloggedFrom(
|
| 61 | level, abovePos, this.defaultBlockState().setValue(HALF, DoubleBlockHalf.UPPER).setValue(FACING, state.getValue(FACING))
|
| 62 | );
|
| 63 | level.setBlockAndUpdate(abovePos, blockState);
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | protected FluidState getFluidState(final BlockState state) {
|
| 69 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 74 | if (state.getValue(HALF) == DoubleBlockHalf.UPPER) {
|
| 75 | return super.canSurvive(state, level, pos);
|
| 76 | }
|
| 77 |
|
| 78 | BlockPos belowPos = pos.below();
|
| 79 | BlockState belowState = level.getBlockState(belowPos);
|
| 80 | return this.mayPlaceOn(belowState, level, belowPos);
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | protected BlockState updateShape(
|
| 85 | final BlockState state,
|
| 86 | final LevelReader level,
|
| 87 | final ScheduledTickAccess ticks,
|
| 88 | final BlockPos pos,
|
| 89 | final Direction directionToNeighbour,
|
| 90 | final BlockPos neighbourPos,
|
| 91 | final BlockState neighbourState,
|
| 92 | final RandomSource random
|
| 93 | ) {
|
| 94 | if (state.getValue(WATERLOGGED)) {
|
| 95 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 96 | }
|
| 97 |
|
| 98 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 103 | builder.add(HALF, WATERLOGGED, FACING);
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 108 | return true;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 113 | return true;
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 118 | if (state.getValue(DoublePlantBlock.HALF) == DoubleBlockHalf.LOWER) {
|
| 119 | BlockPos above = pos.above();
|
| 120 | level.setBlock(above, level.getFluidState(above).createLegacyBlock(), 18);
|
| 121 | BigDripleafBlock.placeWithRandomHeight(level, random, pos, state.getValue(FACING));
|
| 122 | } else {
|
| 123 | BlockPos belowPos = pos.below();
|
| 124 | this.performBonemeal(level, random, belowPos, level.getBlockState(belowPos), source);
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 130 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 135 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected float getMaxVerticalOffset() {
|
| 140 | return 0.1F;
|
| 141 | }
|
| 142 | }
|
| 143 |
|