| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.level.LevelAccessor;
|
| 10 | import net.minecraft.world.level.WorldGenLevel;
|
| 11 | import net.minecraft.world.level.block.Blocks;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 14 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 15 |
|
| 16 | public record BlockPileFeature(Holder<BlockStateProvider> stateProvider) implements Feature {
|
| 17 | public static final MapCodec<BlockPileFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 18 | i -> i.group(BlockStateProvider.CODEC.fieldOf("state_provider").forGetter(BlockPileFeature::stateProvider)).apply(i, BlockPileFeature::new)
|
| 19 | );
|
| 20 |
|
| 21 | @Override
|
| 22 | public MapCodec<BlockPileFeature> codec() {
|
| 23 | return CODEC;
|
| 24 | }
|
| 25 |
|
| 26 | @Override
|
| 27 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 28 | if (origin.getY() < level.getMinY() + 5) {
|
| 29 | return false;
|
| 30 | }
|
| 31 |
|
| 32 | int xr = 2 + random.nextInt(2);
|
| 33 | int zr = 2 + random.nextInt(2);
|
| 34 |
|
| 35 | for (BlockPos blockPos : BlockPos.betweenClosed(origin.offset(-xr, 0, -zr), origin.offset(xr, 1, zr))) {
|
| 36 | int xd = origin.getX() - blockPos.getX();
|
| 37 | int zd = origin.getZ() - blockPos.getZ();
|
| 38 | if (xd * xd + zd * zd <= random.nextFloat() * 10.0F - random.nextFloat() * 6.0F) {
|
| 39 | this.tryPlaceBlock(level, blockPos, random);
|
| 40 | } else if (random.nextFloat() < 0.031) {
|
| 41 | this.tryPlaceBlock(level, blockPos, random);
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | return true;
|
| 46 | }
|
| 47 |
|
| 48 | private boolean mayPlaceOn(final LevelAccessor level, final BlockPos blockPos, final RandomSource random) {
|
| 49 | BlockPos below = blockPos.below();
|
| 50 | BlockState belowState = level.getBlockState(below);
|
| 51 | return belowState.is(Blocks.DIRT_PATH) ? random.nextBoolean() : belowState.isFaceSturdy(level, below, Direction.UP);
|
| 52 | }
|
| 53 |
|
| 54 | private void tryPlaceBlock(final WorldGenLevel level, final BlockPos blockPos, final RandomSource random) {
|
| 55 | if (level.isEmptyBlock(blockPos) && this.mayPlaceOn(level, blockPos, random)) {
|
| 56 | level.setBlock(blockPos, this.stateProvider.value().getState(level, random, blockPos), 260);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 |
|