| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DataFixUtils;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.Registry;
|
| 8 | import net.minecraft.core.registries.Registries;
|
| 9 | import net.minecraft.resources.ResourceKey;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.tags.TagKey;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.world.item.Item;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.level.BlockGetter;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.LevelReader;
|
| 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.IntegerProperty;
|
| 24 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 25 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 26 |
|
| 27 | /**
|
| 28 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 29 | */
|
| 30 | public class StemBlock extends VegetationBlock implements BonemealableBlock {
|
| 31 | public static final int MAX_AGE = 7;
|
| 32 | public static final IntegerProperty AGE = BlockStateProperties.AGE_7;
|
| 33 | private static final VoxelShape[] SHAPES = Block.boxes(7, age -> Block.column(2.0, 0.0, 2 + age * 2));
|
| 34 | private final ResourceKey<Block> fruit;
|
| 35 | private final ResourceKey<Block> attachedStem;
|
| 36 | private final ResourceKey<Item> seed;
|
| 37 | private final TagKey<Block> stemSupportBlocks;
|
| 38 | private final TagKey<Block> fruitSupportBlocks;
|
| 39 |
|
| 40 | /**
|
| 41 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 42 | */
|
| 43 | public StemBlock(
|
| 44 | final ResourceKey<Block> fruit,
|
| 45 | final ResourceKey<Block> attachedStem,
|
| 46 | final ResourceKey<Item> seed,
|
| 47 | final TagKey<Block> stemSupportBlocks,
|
| 48 | final TagKey<Block> fruitSupportBlocks,
|
| 49 | final BlockBehaviour.Properties properties
|
| 50 | ) {
|
| 51 | super(properties);
|
| 52 | this.fruit = fruit;
|
| 53 | this.attachedStem = attachedStem;
|
| 54 | this.seed = seed;
|
| 55 | this.stemSupportBlocks = stemSupportBlocks;
|
| 56 | this.fruitSupportBlocks = fruitSupportBlocks;
|
| 57 | this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0));
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 62 | return SHAPES[state.getValue(AGE)];
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected boolean mayPlaceOn(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 67 | return state.is(this.stemSupportBlocks);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected void randomTick(BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 72 | if (level.getRawBrightness(pos, 0) >= 9) {
|
| 73 | float growthSpeed = CropBlock.getGrowthSpeed(this, level, pos);
|
| 74 | if (random.nextInt((int)(25.0F / growthSpeed) + 1) == 0) {
|
| 75 | int age = state.getValue(AGE);
|
| 76 | if (age < 7) {
|
| 77 | state = state.setValue(AGE, age + 1);
|
| 78 | level.setBlock(pos, state, 2);
|
| 79 | } else {
|
| 80 | Direction direction = Direction.Plane.HORIZONTAL.getRandomDirection(random);
|
| 81 | BlockPos relative = pos.relative(direction);
|
| 82 | BlockState stateBelow = level.getBlockState(relative.below());
|
| 83 | if (level.getBlockState(relative).isAir() && stateBelow.is(this.fruitSupportBlocks)) {
|
| 84 | Registry<Block> blocks = level.registryAccess().lookupOrThrow(Registries.BLOCK);
|
| 85 | Optional<Block> fruit = blocks.getOptional(this.fruit);
|
| 86 | Optional<Block> stem = blocks.getOptional(this.attachedStem);
|
| 87 | if (fruit.isPresent() && stem.isPresent()) {
|
| 88 | level.setBlockAndUpdate(relative, fruit.get().defaultBlockState());
|
| 89 | level.setBlockAndUpdate(pos, stem.get().defaultBlockState().setValue(HorizontalDirectionalBlock.FACING, direction));
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 99 | return new ItemStack(DataFixUtils.orElse(level.registryAccess().lookupOrThrow(Registries.ITEM).getOptional(this.seed), this));
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 104 | return state.getValue(AGE) != 7;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 109 | return true;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 114 | int age = Math.min(7, state.getValue(AGE) + Mth.nextInt(random, 2, 5));
|
| 115 | BlockState newState = state.setValue(AGE, age);
|
| 116 | level.setBlock(pos, newState, 2);
|
| 117 | if (age == 7) {
|
| 118 | newState.randomTick(level, pos, random);
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 124 | builder.add(AGE);
|
| 125 | }
|
| 126 | }
|
| 127 |
|