| 1 | package net.minecraft.world.level.levelgen.material.condition;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.resources.Identifier;
|
| 6 | import net.minecraft.util.Mth;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.level.levelgen.PositionalRandomFactory;
|
| 9 | import net.minecraft.world.level.levelgen.VerticalAnchor;
|
| 10 | import net.minecraft.world.level.levelgen.material.MaterialRuleContext;
|
| 11 |
|
| 12 | public record VerticalGradientCondition(Identifier randomName, VerticalAnchor trueAtAndBelow, VerticalAnchor falseAtAndAbove) implements MaterialCondition {
|
| 13 | public static final MapCodec<VerticalGradientCondition> CODEC = RecordCodecBuilder.mapCodec(
|
| 14 | i -> i.group(
|
| 15 | Identifier.CODEC.fieldOf("random_name").forGetter(VerticalGradientCondition::randomName),
|
| 16 | VerticalAnchor.CODEC.fieldOf("true_at_and_below").forGetter(VerticalGradientCondition::trueAtAndBelow),
|
| 17 | VerticalAnchor.CODEC.fieldOf("false_at_and_above").forGetter(VerticalGradientCondition::falseAtAndAbove)
|
| 18 | )
|
| 19 | .apply(i, VerticalGradientCondition::new)
|
| 20 | );
|
| 21 |
|
| 22 | @Override
|
| 23 | public MapCodec<VerticalGradientCondition> codec() {
|
| 24 | return CODEC;
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public ConditionEvaluator compile(final MaterialRuleContext ruleContext) {
|
| 29 | final int trueAtAndBelow = ruleContext.resolveAnchorY(this.trueAtAndBelow);
|
| 30 | final int falseAtAndAbove = ruleContext.resolveAnchorY(this.falseAtAndAbove);
|
| 31 | final PositionalRandomFactory randomFactory = ruleContext.getOrCreateRandomFactory(this.randomName);
|
| 32 | return new MaterialRuleContext.LazyYCondition(ruleContext) {
|
| 33 | @Override
|
| 34 | protected boolean compute() {
|
| 35 | int blockY = this.context.blockY();
|
| 36 | if (blockY <= trueAtAndBelow) {
|
| 37 | return true;
|
| 38 | }
|
| 39 |
|
| 40 | if (blockY >= falseAtAndAbove) {
|
| 41 | return false;
|
| 42 | }
|
| 43 |
|
| 44 | double probability = Mth.map(blockY, trueAtAndBelow, falseAtAndAbove, 1.0, 0.0);
|
| 45 | RandomSource random = randomFactory.at(this.context.blockX(), blockY, this.context.blockZ());
|
| 46 | return random.nextFloat() < probability;
|
| 47 | }
|
| 48 | };
|
| 49 | }
|
| 50 | }
|
| 51 |
|