| 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.Objects;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.level.WorldGenLevel;
|
| 11 | import net.minecraft.world.level.block.DoublePlantBlock;
|
| 12 | import net.minecraft.world.level.block.MossyCarpetBlock;
|
| 13 | import net.minecraft.world.level.block.state.BlockState;
|
| 14 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 15 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 16 |
|
| 17 | public record SimpleBlockFeature(Holder<BlockStateProvider> toPlace, boolean scheduleTick) implements Feature {
|
| 18 | public static final MapCodec<SimpleBlockFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 19 | i -> i.group(
|
| 20 | BlockStateProvider.CODEC.fieldOf("to_place").forGetter(SimpleBlockFeature::toPlace),
|
| 21 | Codec.BOOL.optionalFieldOf("schedule_tick", false).forGetter(SimpleBlockFeature::scheduleTick)
|
| 22 | )
|
| 23 | .apply(i, SimpleBlockFeature::new)
|
| 24 | );
|
| 25 |
|
| 26 | public SimpleBlockFeature(final Holder<BlockStateProvider> toPlace) {
|
| 27 | this(toPlace, false);
|
| 28 | }
|
| 29 |
|
| 30 | public SimpleBlockFeature(final BlockStateProvider toPlace) {
|
| 31 | this(Holder.direct(toPlace));
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public MapCodec<SimpleBlockFeature> 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 | BlockState stateToPlace = this.toPlace.value().getOptionalState(level, random, origin);
|
| 42 | if (stateToPlace == null) {
|
| 43 | return false;
|
| 44 | }
|
| 45 |
|
| 46 | if (!stateToPlace.canSurvive(level, origin)) {
|
| 47 | return false;
|
| 48 | }
|
| 49 |
|
| 50 | if (stateToPlace.getBlock() instanceof DoublePlantBlock) {
|
| 51 | BlockState aboveState = level.getBlockState(origin.above());
|
| 52 | if (!aboveState.isAir() && (!Objects.equals(stateToPlace.getFluidState(), aboveState.getFluidState()) || !aboveState.canBeReplaced())) {
|
| 53 | return false;
|
| 54 | }
|
| 55 |
|
| 56 | DoublePlantBlock.placeAt(level, stateToPlace, origin, 2);
|
| 57 | } else if (stateToPlace.getBlock() instanceof MossyCarpetBlock) {
|
| 58 | MossyCarpetBlock.placeAt(level, origin, level.getRandom(), 2);
|
| 59 | } else {
|
| 60 | level.setBlock(origin, stateToPlace, 2);
|
| 61 | }
|
| 62 |
|
| 63 | if (this.scheduleTick) {
|
| 64 | level.scheduleTick(origin, level.getBlockState(origin).getBlock(), 1);
|
| 65 | }
|
| 66 |
|
| 67 | return true;
|
| 68 | }
|
| 69 | }
|
| 70 |
|