| 1 | package net.minecraft.world.level.levelgen.material.rule;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.SharedConstants;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.level.block.Blocks;
|
| 10 | import net.minecraft.world.level.block.state.BlockState;
|
| 11 | import net.minecraft.world.level.levelgen.PositionalRandomFactory;
|
| 12 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 13 | import net.minecraft.world.level.levelgen.material.MaterialRuleContext;
|
| 14 | import net.minecraft.world.level.levelgen.material.MaterialRules;
|
| 15 |
|
| 16 | public record OreVeinRule(
|
| 17 | BlockState oreBlock,
|
| 18 | BlockState rawOreBlock,
|
| 19 | BlockState fillerBlock,
|
| 20 | float rawOreChance,
|
| 21 | DensityFunction density,
|
| 22 | DensityFunction richness,
|
| 23 | DensityFunction fillerGap
|
| 24 | ) implements MaterialRule {
|
| 25 | public static final float VEININESS_THRESHOLD = 0.4F;
|
| 26 | public static final int EDGE_ROUNDOFF_BEGIN = 20;
|
| 27 | public static final float MAX_EDGE_ROUNDOFF = 0.2F;
|
| 28 | public static final float VEIN_SOLIDNESS = 0.7F;
|
| 29 | public static final float MIN_RICHNESS = 0.1F;
|
| 30 | public static final float MAX_RICHNESS = 0.3F;
|
| 31 | public static final float MAX_RICHNESS_THRESHOLD = 0.6F;
|
| 32 | private static final float CHANCE_OF_RAW_ORE_BLOCK = 0.02F;
|
| 33 | public static final float SKIP_ORE_IF_GAP_NOISE_IS_BELOW = -0.3F;
|
| 34 | public static final MapCodec<OreVeinRule> CODEC = RecordCodecBuilder.mapCodec(
|
| 35 | i -> i.group(
|
| 36 | BlockState.CODEC.fieldOf("ore_block").forGetter(OreVeinRule::oreBlock),
|
| 37 | BlockState.CODEC.fieldOf("raw_ore_block").forGetter(OreVeinRule::rawOreBlock),
|
| 38 | BlockState.CODEC.fieldOf("filler_block").forGetter(OreVeinRule::fillerBlock),
|
| 39 | Codec.floatRange(0.0F, 1.0F).fieldOf("raw_ore_chance").forGetter(OreVeinRule::rawOreChance),
|
| 40 | DensityFunction.CODEC.fieldOf("density").forGetter(OreVeinRule::density),
|
| 41 | DensityFunction.CODEC.fieldOf("richness").forGetter(OreVeinRule::richness),
|
| 42 | DensityFunction.CODEC.fieldOf("filler_gap").forGetter(OreVeinRule::fillerGap)
|
| 43 | )
|
| 44 | .apply(i, OreVeinRule::new)
|
| 45 | );
|
| 46 |
|
| 47 | @Override
|
| 48 | public RuleEvaluator compile(final MaterialRuleContext context) {
|
| 49 | if (SharedConstants.DEBUG_DISABLE_ORE_VEINS) {
|
| 50 | return (var0, var1, var2x) -> null;
|
| 51 | }
|
| 52 |
|
| 53 | BlockState defaultState = SharedConstants.DEBUG_ORE_VEINS ? Blocks.AIR.defaultBlockState() : null;
|
| 54 | MaterialRules.DensityGetter densitySampler = context.getDensitiesInChunk(this.density, true);
|
| 55 | MaterialRules.DensityGetter richnessSampler = context.getDensitiesInChunk(this.richness, true);
|
| 56 | MaterialRules.DensityGetter fillerGapSampler = context.getDensitiesInChunk(this.fillerGap, false);
|
| 57 | PositionalRandomFactory randomFactory = context.getOrCreateRandomFactory(Identifier.withDefaultNamespace("ore"));
|
| 58 | BlockState fillerBlock = SharedConstants.DEBUG_ORE_VEINS ? Blocks.OAK_BUTTON.defaultBlockState() : this.fillerBlock;
|
| 59 | return (blockX, blockY, blockZ) -> {
|
| 60 | float density = densitySampler.get();
|
| 61 | if (density <= 0.0F) {
|
| 62 | return defaultState;
|
| 63 | } else {
|
| 64 | RandomSource random = randomFactory.at(blockX, blockY, blockZ);
|
| 65 | if (random.nextFloat() > density) {
|
| 66 | return defaultState;
|
| 67 | } else {
|
| 68 | float richness = richnessSampler.get();
|
| 69 | if (random.nextFloat() < richness && fillerGapSampler.get() < 0.0F) {
|
| 70 | return random.nextFloat() < this.rawOreChance ? this.rawOreBlock : this.oreBlock;
|
| 71 | } else {
|
| 72 | return fillerBlock;
|
| 73 | }
|
| 74 | }
|
| 75 | }
|
| 76 | };
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public MapCodec<OreVeinRule> codec() {
|
| 81 | return CODEC;
|
| 82 | }
|
| 83 |
|
| 84 | public enum VeinType {
|
| 85 | COPPER(Blocks.COPPER_ORE.defaultBlockState(), Blocks.RAW_COPPER_BLOCK.defaultBlockState(), Blocks.GRANITE.defaultBlockState(), 0, 50),
|
| 86 | IRON(Blocks.DEEPSLATE_IRON_ORE.defaultBlockState(), Blocks.RAW_IRON_BLOCK.defaultBlockState(), Blocks.TUFF.defaultBlockState(), -60, -8);
|
| 87 |
|
| 88 | private final BlockState oreBlock;
|
| 89 | private final BlockState rawOreBlock;
|
| 90 | private final BlockState fillerBlock;
|
| 91 | public final int minY;
|
| 92 | public final int maxY;
|
| 93 |
|
| 94 | VeinType(final BlockState oreBlock, final BlockState rawOreBlock, final BlockState fillerBlock, final int minY, final int maxY) {
|
| 95 | this.oreBlock = oreBlock;
|
| 96 | this.rawOreBlock = rawOreBlock;
|
| 97 | this.fillerBlock = fillerBlock;
|
| 98 | this.minY = minY;
|
| 99 | this.maxY = maxY;
|
| 100 | }
|
| 101 |
|
| 102 | public OreVeinRule create(final DensityFunction density, final DensityFunction richness, final DensityFunction fillerGap) {
|
| 103 | return new OreVeinRule(this.oreBlock, this.rawOreBlock, this.fillerBlock, 0.02F, density, richness, fillerGap);
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|