| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.util.BlockUtil;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 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.material.Fluids;
|
| 18 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 19 |
|
| 20 | public abstract class GrowingPlantBodyBlock extends GrowingPlantBlock implements BonemealableBlock {
|
| 21 | protected GrowingPlantBodyBlock(
|
| 22 | final BlockBehaviour.Properties properties, final Direction growthDirection, final VoxelShape shape, final boolean scheduleFluidTicks
|
| 23 | ) {
|
| 24 | super(properties, growthDirection, shape, scheduleFluidTicks);
|
| 25 | }
|
| 26 |
|
| 27 | protected BlockState updateHeadAfterConvertedFromBody(final BlockState bodyState, final BlockState headState) {
|
| 28 | return headState;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | protected BlockState updateShape(
|
| 33 | final BlockState state,
|
| 34 | final LevelReader level,
|
| 35 | final ScheduledTickAccess ticks,
|
| 36 | final BlockPos pos,
|
| 37 | final Direction directionToNeighbour,
|
| 38 | final BlockPos neighbourPos,
|
| 39 | final BlockState neighbourState,
|
| 40 | final RandomSource random
|
| 41 | ) {
|
| 42 | if (directionToNeighbour == this.growthDirection.getOpposite() && !state.canSurvive(level, pos)) {
|
| 43 | ticks.scheduleTick(pos, this, 1);
|
| 44 | }
|
| 45 |
|
| 46 | GrowingPlantHeadBlock headBlock = this.getHeadBlock();
|
| 47 | if (directionToNeighbour == this.growthDirection && !neighbourState.is(this) && !neighbourState.is(headBlock)) {
|
| 48 | return this.updateHeadAfterConvertedFromBody(state, headBlock.getStateForPlacement(random));
|
| 49 | }
|
| 50 |
|
| 51 | if (this.scheduleFluidTicks) {
|
| 52 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 53 | }
|
| 54 |
|
| 55 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 60 | return new ItemStack(this.getHeadBlock());
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 65 | Optional<BlockPos> headPos = this.getHeadPos(level, pos, state.getBlock());
|
| 66 | if (headPos.isEmpty()) {
|
| 67 | return false;
|
| 68 | }
|
| 69 |
|
| 70 | BlockPos growthPos = headPos.get().relative(this.growthDirection);
|
| 71 | return this.getHeadBlock().canGrowInto(level.getBlockState(growthPos)) && level.isInsideBuildHeight(growthPos);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 76 | return true;
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 81 | Optional<BlockPos> headPos = this.getHeadPos(level, pos, state.getBlock());
|
| 82 | if (headPos.isPresent()) {
|
| 83 | BlockState forwardState = level.getBlockState(headPos.get());
|
| 84 | ((GrowingPlantHeadBlock)forwardState.getBlock()).performBonemeal(level, random, headPos.get(), forwardState, source);
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | private Optional<BlockPos> getHeadPos(final BlockGetter level, final BlockPos pos, final Block bodyBlock) {
|
| 89 | return BlockUtil.getTopConnectedBlock(level, pos, bodyBlock, this.growthDirection, this.getHeadBlock());
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | protected boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
|
| 94 | boolean result = super.canBeReplaced(state, context);
|
| 95 | return result && context.getItemInHand().is(this.getHeadBlock().asItem()) ? false : result;
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | protected Block getBodyBlock() {
|
| 100 | return this;
|
| 101 | }
|
| 102 | }
|
| 103 |
|