| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.core.registries.Registries;
|
| 8 | import net.minecraft.data.worldgen.placement.VegetationPlacements;
|
| 9 | import net.minecraft.references.BlockItemIds;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.util.Util;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.LevelReader;
|
| 15 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.levelgen.feature.Feature;
|
| 18 | import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
| 19 |
|
| 20 | public class GrassBlock extends SpreadingSnowyBlock implements BonemealableBlock {
|
| 21 | private static final int ATTEMPT_COUNT = 128;
|
| 22 | private static final float GROW_TALL_GRASS_CHANCE = 0.1F;
|
| 23 | private static final float PLACE_FLOWER_CHANCE = 0.125F;
|
| 24 |
|
| 25 | public GrassBlock(final BlockBehaviour.Properties properties) {
|
| 26 | super(properties, BlockItemIds.DIRT.block());
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 31 | return level.getBlockState(pos.above()).isAir() && level.isInsideBuildHeight(pos.above());
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 36 | return true;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 41 | BlockPos above = pos.above();
|
| 42 |
|
| 43 | label24:
|
| 44 | for (int attempt = 0; attempt < 128; attempt++) {
|
| 45 | BlockPos testPos = above;
|
| 46 | int randomizeCount = attempt / 16;
|
| 47 |
|
| 48 | for (int i = 0; i < randomizeCount; i++) {
|
| 49 | int dx = random.nextIntBetweenInclusive(-1, 1);
|
| 50 | int dy = random.nextIntBetweenInclusive(-1, 1) * random.nextInt(3) / 2;
|
| 51 | int dz = random.nextIntBetweenInclusive(-1, 1);
|
| 52 | testPos = testPos.offset(dx, dy, dz);
|
| 53 | if (this.stopBonemealSpread(level, testPos)) {
|
| 54 | continue label24;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | placeBonemealEffect(level, random, testPos, source);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | private static void placeBonemealEffect(final ServerLevel level, final RandomSource random, final BlockPos testPos, final BonemealSource source) {
|
| 63 | BlockState grass = Blocks.SHORT_GRASS.defaultBlockState();
|
| 64 | Optional<Holder.Reference<PlacedFeature>> grassFeature = level.registryAccess()
|
| 65 | .lookupOrThrow(Registries.PLACED_FEATURE)
|
| 66 | .get(VegetationPlacements.GRASS_BONEMEAL);
|
| 67 | BlockState testState = level.getBlockState(testPos);
|
| 68 | if (testState.is(grass.getBlock()) && random.nextFloat() < 0.1F) {
|
| 69 | BonemealableBlock bonemealableBlock = (BonemealableBlock)grass.getBlock();
|
| 70 | if (bonemealableBlock.isValidBonemealTarget(level, testPos, testState, source)) {
|
| 71 | bonemealableBlock.performBonemeal(level, random, testPos, testState, source);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | if (testState.isAir() && !level.isOutsideBuildHeight(testPos)) {
|
| 76 | if (random.nextFloat() < 0.125F) {
|
| 77 | List<Feature> features = level.getBiome(testPos).value().getGenerationSettings().getBoneMealFeatures();
|
| 78 | if (features.isEmpty()) {
|
| 79 | return;
|
| 80 | }
|
| 81 |
|
| 82 | Feature placementFeature = Util.getRandom(features, random);
|
| 83 | placementFeature.place(level, level.getChunkSource().getGenerator(), random, testPos);
|
| 84 | } else if (grassFeature.isPresent()) {
|
| 85 | grassFeature.get().value().place(level, level.getChunkSource().getGenerator(), random, testPos);
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | private boolean stopBonemealSpread(final ServerLevel level, final BlockPos testPos) {
|
| 91 | return !level.getBlockState(testPos.below()).is(this) || level.getBlockState(testPos).isCollisionShapeFullBlock(level, testPos);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public BonemealableBlock.Type getType() {
|
| 96 | return BonemealableBlock.Type.NEIGHBOR_SPREADER;
|
| 97 | }
|
| 98 | }
|
| 99 |
|