| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.function.Function;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.tags.BlockTags;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.entity.Entity;
|
| 10 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 11 | import net.minecraft.world.entity.LivingEntity;
|
| 12 | import net.minecraft.world.entity.monster.Ravager;
|
| 13 | import net.minecraft.world.item.ItemStack;
|
| 14 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 15 | import net.minecraft.world.level.BlockGetter;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.LevelReader;
|
| 18 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 19 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 22 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 23 | import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
|
| 24 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 25 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 26 | import net.minecraft.world.level.gamerules.GameRules;
|
| 27 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 28 | import net.minecraft.world.phys.shapes.Shapes;
|
| 29 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 |
|
| 32 | public class PitcherCropBlock extends DoublePlantBlock implements BonemealableBlock {
|
| 33 | public static final int MAX_AGE = 4;
|
| 34 | public static final IntegerProperty AGE = BlockStateProperties.AGE_4;
|
| 35 | public static final EnumProperty<DoubleBlockHalf> HALF = DoublePlantBlock.HALF;
|
| 36 | private static final int DOUBLE_PLANT_AGE_INTERSECTION = 3;
|
| 37 | private static final int BONEMEAL_INCREASE = 1;
|
| 38 | private static final VoxelShape SHAPE_BULB = Block.column(6.0, -1.0, 3.0);
|
| 39 | private static final VoxelShape SHAPE_CROP = Block.column(10.0, -1.0, 5.0);
|
| 40 | private final Function<BlockState, VoxelShape> shapes = this.makeShapes();
|
| 41 |
|
| 42 | public PitcherCropBlock(final BlockBehaviour.Properties properties) {
|
| 43 | super(properties);
|
| 44 | }
|
| 45 |
|
| 46 | private Function<BlockState, VoxelShape> makeShapes() {
|
| 47 | int[] plantHeights = new int[]{0, 9, 11, 22, 26};
|
| 48 | return this.getShapeForEachState(state -> {
|
| 49 | int height = (state.getValue(AGE) == 0 ? 4 : 6) + plantHeights[state.getValue(AGE)];
|
| 50 | int width = state.getValue(AGE) == 0 ? 6 : 10;
|
| 51 |
|
| 52 | return switch ((DoubleBlockHalf)state.getValue(HALF)) {
|
| 53 | case LOWER -> Block.column(width, -1.0, Math.min(16, -1 + height));
|
| 54 | case UPPER -> Block.column(width, 0.0, Math.max(0, -1 + height - 16));
|
| 55 | };
|
| 56 | });
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 61 | return this.defaultBlockState();
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 66 | return this.shapes.apply(state);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 71 | if (state.getValue(HALF) == DoubleBlockHalf.LOWER) {
|
| 72 | return state.getValue(AGE) == 0 ? SHAPE_BULB : SHAPE_CROP;
|
| 73 | } else {
|
| 74 | return Shapes.empty();
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public BlockState updateShape(
|
| 80 | final BlockState state,
|
| 81 | final LevelReader level,
|
| 82 | final ScheduledTickAccess ticks,
|
| 83 | final BlockPos pos,
|
| 84 | final Direction directionToNeighbour,
|
| 85 | final BlockPos neighbourPos,
|
| 86 | final BlockState neighbourState,
|
| 87 | final RandomSource random
|
| 88 | ) {
|
| 89 | if (isDouble(state.getValue(AGE))) {
|
| 90 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 91 | } else {
|
| 92 | return state.canSurvive(level, pos) ? state : Blocks.AIR.defaultBlockState();
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 98 | return isLower(state) && !sufficientLight(level, pos) ? false : super.canSurvive(state, level, pos);
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | protected boolean mayPlaceOn(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 103 | return state.is(BlockTags.SUPPORTS_CROPS);
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 108 | builder.add(AGE);
|
| 109 | super.createBlockStateDefinition(builder);
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public void entityInside(
|
| 114 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 115 | ) {
|
| 116 | if (level instanceof ServerLevel serverLevel && entity instanceof Ravager && serverLevel.getGameRules().get(GameRules.MOB_GRIEFING)) {
|
| 117 | serverLevel.destroyBlock(pos, true, entity);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
|
| 123 | return false;
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public boolean isRandomlyTicking(final BlockState state) {
|
| 132 | return state.getValue(HALF) == DoubleBlockHalf.LOWER && !this.isMaxAge(state);
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 137 | float growthSpeed = CropBlock.getGrowthSpeed(this, level, pos);
|
| 138 | boolean shouldProgressGrowth = random.nextInt((int)(25.0F / growthSpeed) + 1) == 0;
|
| 139 | if (shouldProgressGrowth) {
|
| 140 | this.grow(level, state, pos, 1);
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | private void grow(final ServerLevel level, final BlockState lowerState, final BlockPos lowerPos, final int increase) {
|
| 145 | int updatedAge = Math.min(lowerState.getValue(AGE) + increase, 4);
|
| 146 | if (this.canGrow(level, lowerPos, lowerState, updatedAge)) {
|
| 147 | BlockState newLowerState = lowerState.setValue(AGE, updatedAge);
|
| 148 | level.setBlock(lowerPos, newLowerState, 2);
|
| 149 | if (isDouble(updatedAge)) {
|
| 150 | level.setBlockAndUpdate(lowerPos.above(), newLowerState.setValue(HALF, DoubleBlockHalf.UPPER));
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | private static boolean canGrowInto(final LevelReader level, final BlockPos pos) {
|
| 156 | BlockState state = level.getBlockState(pos);
|
| 157 | return state.isAir() || state.is(Blocks.PITCHER_CROP);
|
| 158 | }
|
| 159 |
|
| 160 | private static boolean sufficientLight(final LevelReader level, final BlockPos pos) {
|
| 161 | return CropBlock.hasSufficientLight(level, pos);
|
| 162 | }
|
| 163 |
|
| 164 | private static boolean isLower(final BlockState state) {
|
| 165 | return state.is(Blocks.PITCHER_CROP) && state.getValue(HALF) == DoubleBlockHalf.LOWER;
|
| 166 | }
|
| 167 |
|
| 168 | private static boolean isDouble(final int age) {
|
| 169 | return age >= 3;
|
| 170 | }
|
| 171 |
|
| 172 | private boolean canGrow(final LevelReader level, final BlockPos lowerPos, final BlockState lowerState, final int newAge) {
|
| 173 | return !this.isMaxAge(lowerState)
|
| 174 | && sufficientLight(level, lowerPos)
|
| 175 | && level.isInsideBuildHeight(lowerPos.above())
|
| 176 | && (!isDouble(newAge) || canGrowInto(level, lowerPos.above()));
|
| 177 | }
|
| 178 |
|
| 179 | private boolean isMaxAge(final BlockState state) {
|
| 180 | return state.getValue(AGE) >= 4;
|
| 181 | }
|
| 182 |
|
| 183 | private PitcherCropBlock.@Nullable PosAndState getLowerHalf(final LevelReader level, final BlockPos pos, final BlockState state) {
|
| 184 | if (isLower(state)) {
|
| 185 | return new PitcherCropBlock.PosAndState(pos, state);
|
| 186 | }
|
| 187 |
|
| 188 | BlockPos lowerPos = pos.below();
|
| 189 | BlockState lowerState = level.getBlockState(lowerPos);
|
| 190 | return isLower(lowerState) ? new PitcherCropBlock.PosAndState(lowerPos, lowerState) : null;
|
| 191 | }
|
| 192 |
|
| 193 | @Override
|
| 194 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 195 | PitcherCropBlock.PosAndState lowerHalf = this.getLowerHalf(level, pos, state);
|
| 196 | return lowerHalf == null ? false : this.canGrow(level, lowerHalf.pos, lowerHalf.state, lowerHalf.state.getValue(AGE) + 1);
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 201 | return true;
|
| 202 | }
|
| 203 |
|
| 204 | @Override
|
| 205 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 206 | PitcherCropBlock.PosAndState lowerHalf = this.getLowerHalf(level, pos, state);
|
| 207 | if (lowerHalf != null) {
|
| 208 | this.grow(level, lowerHalf.state, lowerHalf.pos, 1);
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | private record PosAndState(BlockPos pos, BlockState state) {
|
| 213 | }
|
| 214 | }
|
| 215 |
|