| 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 net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.level.WorldGenLevel;
|
| 12 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 13 | import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
|
| 14 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 15 | import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
| 16 |
|
| 17 | public record SingleBlockPillarFeature(
|
| 18 | Holder<BlockStateProvider> block, BlockPredicate canReplace, Direction direction, float chanceToContinue, Optional<Holder<PlacedFeature>> capFeature
|
| 19 | ) implements Feature {
|
| 20 | public static final MapCodec<SingleBlockPillarFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 21 | i -> i.group(
|
| 22 | BlockStateProvider.CODEC.fieldOf("block").forGetter(SingleBlockPillarFeature::block),
|
| 23 | BlockPredicate.CODEC.optionalFieldOf("can_replace", BlockPredicate.alwaysTrue()).forGetter(SingleBlockPillarFeature::canReplace),
|
| 24 | Direction.VERTICAL_CODEC.fieldOf("direction").forGetter(SingleBlockPillarFeature::direction),
|
| 25 | Codec.floatRange(0.0F, 1.0F).optionalFieldOf("chance_to_continue", 1.0F).forGetter(SingleBlockPillarFeature::chanceToContinue),
|
| 26 | PlacedFeature.CODEC.optionalFieldOf("cap_feature").forGetter(SingleBlockPillarFeature::capFeature)
|
| 27 | )
|
| 28 | .apply(i, SingleBlockPillarFeature::new)
|
| 29 | );
|
| 30 |
|
| 31 | public SingleBlockPillarFeature(
|
| 32 | final Holder<BlockStateProvider> block, final BlockPredicate mayReplace, final Direction direction, final float chanceToContinue
|
| 33 | ) {
|
| 34 | this(block, mayReplace, direction, chanceToContinue, Optional.empty());
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public MapCodec<SingleBlockPillarFeature> codec() {
|
| 39 | return CODEC;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 44 | BlockPos.MutableBlockPos pos = origin.mutable();
|
| 45 |
|
| 46 | while (this.canReplace.test(level, pos) && random.nextFloat() < this.chanceToContinue && !level.isOutsideBuildHeight(pos)) {
|
| 47 | level.setBlock(pos, this.block.value().getState(level, random, pos), 2);
|
| 48 | pos.move(this.direction);
|
| 49 | }
|
| 50 |
|
| 51 | pos.move(this.direction.getOpposite());
|
| 52 | this.capFeature.ifPresent(feature -> feature.value().place(level, chunkGenerator, random, pos));
|
| 53 | return true;
|
| 54 | }
|
| 55 | }
|
| 56 |
|