| 1 | package net.minecraft.world.level.levelgen.feature.stateproviders;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.util.ExtraCodecs;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.util.Util;
|
| 11 | import net.minecraft.world.level.LevelAccessor;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.levelgen.synth.NormalNoise;
|
| 14 |
|
| 15 | public class NoiseThresholdProvider extends NoiseBasedStateProvider {
|
| 16 | public static final MapCodec<NoiseThresholdProvider> CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> noiseCodec(i)
|
| 18 | .and(
|
| 19 | i.group(
|
| 20 | Codec.floatRange(-1.0F, 1.0F).fieldOf("threshold").forGetter(p -> p.threshold),
|
| 21 | Codec.floatRange(0.0F, 1.0F).fieldOf("high_chance").forGetter(p -> p.highChance),
|
| 22 | BlockState.CODEC.fieldOf("default_state").forGetter(p -> p.defaultState),
|
| 23 | ExtraCodecs.nonEmptyList(BlockState.CODEC.listOf()).fieldOf("low_states").forGetter(p -> p.lowStates),
|
| 24 | ExtraCodecs.nonEmptyList(BlockState.CODEC.listOf()).fieldOf("high_states").forGetter(p -> p.highStates)
|
| 25 | )
|
| 26 | )
|
| 27 | .apply(i, NoiseThresholdProvider::new)
|
| 28 | );
|
| 29 | private final float threshold;
|
| 30 | private final float highChance;
|
| 31 | private final BlockState defaultState;
|
| 32 | private final List<BlockState> lowStates;
|
| 33 | private final List<BlockState> highStates;
|
| 34 |
|
| 35 | public NoiseThresholdProvider(
|
| 36 | final long seed,
|
| 37 | final NormalNoise parameters,
|
| 38 | final float scale,
|
| 39 | final float threshold,
|
| 40 | final float highChance,
|
| 41 | final BlockState defaultState,
|
| 42 | final List<BlockState> lowStates,
|
| 43 | final List<BlockState> highStates
|
| 44 | ) {
|
| 45 | super(seed, parameters, scale);
|
| 46 | this.threshold = threshold;
|
| 47 | this.highChance = highChance;
|
| 48 | this.defaultState = defaultState;
|
| 49 | this.lowStates = lowStates;
|
| 50 | this.highStates = highStates;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public MapCodec<NoiseThresholdProvider> codec() {
|
| 55 | return CODEC;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public BlockState getState(final LevelAccessor level, final RandomSource random, final BlockPos pos) {
|
| 60 | double localValue = this.getNoiseValue(pos, this.scale);
|
| 61 | if (localValue < this.threshold) {
|
| 62 | return Util.getRandom(this.lowStates, random);
|
| 63 | } else {
|
| 64 | return random.nextFloat() < this.highChance ? Util.getRandom(this.highStates, random) : this.defaultState;
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|