| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import java.util.HashSet;
|
| 5 | import java.util.Set;
|
| 6 | import java.util.function.Predicate;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.core.HolderSet;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.util.valueproviders.IntProvider;
|
| 13 | import net.minecraft.world.level.WorldGenLevel;
|
| 14 | import net.minecraft.world.level.block.Block;
|
| 15 | import net.minecraft.world.level.block.Blocks;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 18 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 19 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 20 | import net.minecraft.world.level.levelgen.placement.CaveSurface;
|
| 21 | import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
| 22 |
|
| 23 | public class WaterloggedVegetationPatchFeature extends VegetationPatchFeature {
|
| 24 | public static final MapCodec<WaterloggedVegetationPatchFeature> CODEC = makeCodec(WaterloggedVegetationPatchFeature::new);
|
| 25 |
|
| 26 | public WaterloggedVegetationPatchFeature(
|
| 27 | final HolderSet<Block> replaceable,
|
| 28 | final Holder<BlockStateProvider> groundState,
|
| 29 | final Holder<PlacedFeature> vegetationFeature,
|
| 30 | final CaveSurface surface,
|
| 31 | final IntProvider depth,
|
| 32 | final float extraBottomBlockChance,
|
| 33 | final int verticalRange,
|
| 34 | final float vegetationChance,
|
| 35 | final IntProvider xzRadius,
|
| 36 | final float extraEdgeColumnChance
|
| 37 | ) {
|
| 38 | super(replaceable, groundState, vegetationFeature, surface, depth, extraBottomBlockChance, verticalRange, vegetationChance, xzRadius, extraEdgeColumnChance);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public MapCodec<WaterloggedVegetationPatchFeature> codec() {
|
| 43 | return CODEC;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public Set<BlockPos> placeGroundPatch(
|
| 48 | final WorldGenLevel level, final RandomSource random, final BlockPos origin, final Predicate<BlockState> replaceable, final int xRadius, final int zRadius
|
| 49 | ) {
|
| 50 | Set<BlockPos> surface = super.placeGroundPatch(level, random, origin, replaceable, xRadius, zRadius);
|
| 51 | Set<BlockPos> waterSurface = new HashSet<>();
|
| 52 | BlockPos.MutableBlockPos testPos = new BlockPos.MutableBlockPos();
|
| 53 |
|
| 54 | for (BlockPos surfacePos : surface) {
|
| 55 | if (!isExposed(level, surface, surfacePos, testPos)) {
|
| 56 | waterSurface.add(surfacePos);
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | for (BlockPos surfacePos : waterSurface) {
|
| 61 | level.setBlock(surfacePos, Blocks.WATER.defaultBlockState(), 2);
|
| 62 | }
|
| 63 |
|
| 64 | return waterSurface;
|
| 65 | }
|
| 66 |
|
| 67 | private static boolean isExposed(final WorldGenLevel level, final Set<BlockPos> surface, final BlockPos pos, final BlockPos.MutableBlockPos testPos) {
|
| 68 | return isExposedDirection(level, pos, testPos, Direction.NORTH)
|
| 69 | || isExposedDirection(level, pos, testPos, Direction.EAST)
|
| 70 | || isExposedDirection(level, pos, testPos, Direction.SOUTH)
|
| 71 | || isExposedDirection(level, pos, testPos, Direction.WEST)
|
| 72 | || isExposedDirection(level, pos, testPos, Direction.DOWN);
|
| 73 | }
|
| 74 |
|
| 75 | private static boolean isExposedDirection(final WorldGenLevel level, final BlockPos pos, final BlockPos.MutableBlockPos testPos, final Direction direction) {
|
| 76 | testPos.setWithOffset(pos, direction);
|
| 77 | return !level.getBlockState(testPos).isFaceSturdy(level, testPos, direction.getOpposite());
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | protected boolean placeVegetation(final WorldGenLevel level, final ChunkGenerator generator, final RandomSource random, final BlockPos placementPos) {
|
| 82 | if (super.placeVegetation(level, generator, random, placementPos.below())) {
|
| 83 | BlockState placed = level.getBlockState(placementPos);
|
| 84 | if (placed.hasProperty(BlockStateProperties.WATERLOGGED) && !placed.getValue(BlockStateProperties.WATERLOGGED)) {
|
| 85 | level.setBlock(placementPos, placed.setValue(BlockStateProperties.WATERLOGGED, true), 2);
|
| 86 | }
|
| 87 |
|
| 88 | return true;
|
| 89 | } else {
|
| 90 | return false;
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|