| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.OptionalInt;
|
| 8 | import java.util.function.Predicate;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.core.Vec3i;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.level.LevelAccessor;
|
| 14 | import net.minecraft.world.level.WorldGenLevel;
|
| 15 | import net.minecraft.world.level.block.Block;
|
| 16 | import net.minecraft.world.level.block.Blocks;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 18 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 19 | import net.minecraft.world.level.levelgen.Column;
|
| 20 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 21 | import net.minecraft.world.phys.shapes.Shapes;
|
| 22 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 23 |
|
| 24 | public record UnderwaterMagmaFeature(int floorSearchRange, int placementRadiusAroundFloor, float placementProbabilityPerValidPosition) implements Feature {
|
| 25 | public static final MapCodec<UnderwaterMagmaFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 26 | i -> i.group(
|
| 27 | Codec.intRange(0, 512).fieldOf("floor_search_range").forGetter(UnderwaterMagmaFeature::floorSearchRange),
|
| 28 | Codec.intRange(0, 64).fieldOf("placement_radius_around_floor").forGetter(UnderwaterMagmaFeature::placementRadiusAroundFloor),
|
| 29 | Codec.floatRange(0.0F, 1.0F).fieldOf("placement_probability_per_valid_position").forGetter(UnderwaterMagmaFeature::placementProbabilityPerValidPosition)
|
| 30 | )
|
| 31 | .apply(i, UnderwaterMagmaFeature::new)
|
| 32 | );
|
| 33 |
|
| 34 | @Override
|
| 35 | public MapCodec<UnderwaterMagmaFeature> codec() {
|
| 36 | return CODEC;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 41 | OptionalInt floorY = this.getFloorY(level, origin);
|
| 42 | if (floorY.isEmpty()) {
|
| 43 | return false;
|
| 44 | }
|
| 45 |
|
| 46 | BlockPos floorPos = origin.atY(floorY.getAsInt());
|
| 47 | Vec3i radius = new Vec3i(this.placementRadiusAroundFloor, this.placementRadiusAroundFloor, this.placementRadiusAroundFloor);
|
| 48 | BoundingBox bounds = BoundingBox.fromCorners(floorPos.subtract(radius), floorPos.offset(radius));
|
| 49 | return BlockPos.betweenClosedStream(bounds)
|
| 50 | .filter(pos -> random.nextFloat() < this.placementProbabilityPerValidPosition)
|
| 51 | .filter(pos -> this.isValidPlacement(level, pos))
|
| 52 | .mapToInt(pos -> {
|
| 53 | level.setBlock(pos, Blocks.MAGMA_BLOCK.defaultBlockState(), 2);
|
| 54 | return 1;
|
| 55 | })
|
| 56 | .sum()
|
| 57 | > 0;
|
| 58 | }
|
| 59 |
|
| 60 | private OptionalInt getFloorY(final WorldGenLevel level, final BlockPos origin) {
|
| 61 | Predicate<BlockState> insideColumn = state -> state.is(Blocks.WATER);
|
| 62 | Predicate<BlockState> validEdge = state -> !state.is(Blocks.WATER);
|
| 63 | Optional<Column> waterColumn = Column.scan(level, origin, this.floorSearchRange, insideColumn, validEdge);
|
| 64 | return waterColumn.map(Column::getFloor).orElseGet(OptionalInt::empty);
|
| 65 | }
|
| 66 |
|
| 67 | private boolean isValidPlacement(final WorldGenLevel level, final BlockPos pos) {
|
| 68 | if (!isWaterOrAir(level.getBlockState(pos)) && !this.isVisibleFromOutside(level, pos.below(), Direction.UP)) {
|
| 69 | for (Direction neighbourDir : Direction.Plane.HORIZONTAL) {
|
| 70 | if (this.isVisibleFromOutside(level, pos.relative(neighbourDir), neighbourDir.getOpposite())) {
|
| 71 | return false;
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | return true;
|
| 76 | } else {
|
| 77 | return false;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | private static boolean isWaterOrAir(final BlockState state) {
|
| 82 | return state.is(Blocks.WATER) || state.isAir();
|
| 83 | }
|
| 84 |
|
| 85 | private boolean isVisibleFromOutside(final LevelAccessor level, final BlockPos pos, final Direction coveredDirection) {
|
| 86 | BlockState state = level.getBlockState(pos);
|
| 87 | VoxelShape faceOcclusionShape = state.getFaceOcclusionShape(coveredDirection);
|
| 88 | return faceOcclusionShape == Shapes.empty() || !Block.isShapeFullBlock(faceOcclusionShape);
|
| 89 | }
|
| 90 | }
|
| 91 |
|