| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Function3;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 | import java.util.function.Function;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Direction;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 |
|
| 16 | public abstract class AbstractOreFeature implements Feature {
|
| 17 | protected final List<BlockReplacement> targetStates;
|
| 18 | protected final int size;
|
| 19 | protected final float discardChanceOnAirExposure;
|
| 20 |
|
| 21 | public AbstractOreFeature(final List<BlockReplacement> targetStates, final int size, final float discardChanceOnAirExposure) {
|
| 22 | this.targetStates = targetStates;
|
| 23 | this.size = size;
|
| 24 | this.discardChanceOnAirExposure = discardChanceOnAirExposure;
|
| 25 | }
|
| 26 |
|
| 27 | protected static <T extends AbstractOreFeature> MapCodec<T> makeCodec(final Function3<List<BlockReplacement>, Integer, Float, T> constructor) {
|
| 28 | return RecordCodecBuilder.mapCodec(
|
| 29 | i -> i.group(
|
| 30 | Codec.list(BlockReplacement.CODEC).fieldOf("targets").forGetter(AbstractOreFeature::targetStates),
|
| 31 | Codec.intRange(0, 64).fieldOf("size").forGetter(AbstractOreFeature::size),
|
| 32 | Codec.floatRange(0.0F, 1.0F).fieldOf("discard_chance_on_air_exposure").forGetter(AbstractOreFeature::discardChanceOnAirExposure)
|
| 33 | )
|
| 34 | .apply(i, constructor)
|
| 35 | );
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public abstract MapCodec<? extends AbstractOreFeature> codec();
|
| 40 |
|
| 41 | public final List<BlockReplacement> targetStates() {
|
| 42 | return this.targetStates;
|
| 43 | }
|
| 44 |
|
| 45 | public final int size() {
|
| 46 | return this.size;
|
| 47 | }
|
| 48 |
|
| 49 | public final float discardChanceOnAirExposure() {
|
| 50 | return this.discardChanceOnAirExposure;
|
| 51 | }
|
| 52 |
|
| 53 | public boolean canPlaceOre(
|
| 54 | final BlockState state,
|
| 55 | final Function<BlockPos, BlockState> blockGetter,
|
| 56 | final RandomSource random,
|
| 57 | final BlockReplacement targetState,
|
| 58 | final BlockPos.MutableBlockPos orePos
|
| 59 | ) {
|
| 60 | if (!targetState.target().test(state, orePos, random)) {
|
| 61 | return false;
|
| 62 | } else {
|
| 63 | return shouldSkipAirCheck(random, this.discardChanceOnAirExposure) ? true : !isAdjacentToAir(blockGetter, orePos);
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | public static boolean isAdjacentToAir(final Function<BlockPos, BlockState> blockGetter, final BlockPos pos) {
|
| 68 | return checkNeighbors(blockGetter, pos, BlockBehaviour.BlockStateBase::isAir);
|
| 69 | }
|
| 70 |
|
| 71 | public static boolean checkNeighbors(final Function<BlockPos, BlockState> blockGetter, final BlockPos pos, final Predicate<BlockState> predicate) {
|
| 72 | BlockPos.MutableBlockPos neighborPos = new BlockPos.MutableBlockPos();
|
| 73 |
|
| 74 | for (Direction direction : Direction.values()) {
|
| 75 | neighborPos.setWithOffset(pos, direction);
|
| 76 | if (predicate.test(blockGetter.apply(neighborPos))) {
|
| 77 | return true;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | return false;
|
| 82 | }
|
| 83 |
|
| 84 | private static boolean shouldSkipAirCheck(final RandomSource random, final float discardChanceOnAirExposure) {
|
| 85 | if (discardChanceOnAirExposure <= 0.0F) {
|
| 86 | return true;
|
| 87 | } else {
|
| 88 | return discardChanceOnAirExposure >= 1.0F ? false : random.nextFloat() >= discardChanceOnAirExposure;
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|