| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.MapCodec;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import java.util.List;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Direction;
|
| 12 | import net.minecraft.core.HolderSet;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import net.minecraft.util.valueproviders.IntProvider;
|
| 17 | import net.minecraft.util.valueproviders.IntProviders;
|
| 18 | import net.minecraft.util.valueproviders.UniformInt;
|
| 19 | import net.minecraft.world.level.WorldGenLevel;
|
| 20 | import net.minecraft.world.level.block.Block;
|
| 21 | import net.minecraft.world.level.block.Blocks;
|
| 22 | import net.minecraft.world.level.block.BuddingAmethystBlock;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 25 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 26 | import net.minecraft.world.level.levelgen.GeodeBlockSettings;
|
| 27 | import net.minecraft.world.level.levelgen.GeodeCrackSettings;
|
| 28 | import net.minecraft.world.level.levelgen.GeodeLayerSettings;
|
| 29 | import net.minecraft.world.level.levelgen.LegacyRandomSource;
|
| 30 | import net.minecraft.world.level.levelgen.WorldgenRandom;
|
| 31 | import net.minecraft.world.level.levelgen.synth.Noise;
|
| 32 | import net.minecraft.world.level.levelgen.synth.NormalNoise;
|
| 33 | import net.minecraft.world.level.material.FluidState;
|
| 34 |
|
| 35 | public record GeodeFeature(
|
| 36 | GeodeBlockSettings blockSettings,
|
| 37 | GeodeLayerSettings layerSettings,
|
| 38 | GeodeCrackSettings crackSettings,
|
| 39 | double usePotentialPlacementsChance,
|
| 40 | double useAlternateLayer0Chance,
|
| 41 | boolean placementsRequireLayer0Alternate,
|
| 42 | IntProvider outerWallDistance,
|
| 43 | IntProvider distributionPoints,
|
| 44 | IntProvider pointOffset,
|
| 45 | int minGenOffset,
|
| 46 | int maxGenOffset,
|
| 47 | double noiseMultiplier,
|
| 48 | int invalidBlocksThreshold
|
| 49 | ) implements Feature {
|
| 50 | private static final NormalNoise NOISE_PARAMETERS = NormalNoise.createParity(-4, 1.0);
|
| 51 | public static final Codec<Double> CHANCE_RANGE = Codec.doubleRange(0.0, 1.0);
|
| 52 | public static final MapCodec<GeodeFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 53 | i -> i.group(
|
| 54 | GeodeBlockSettings.CODEC.fieldOf("blocks").forGetter(GeodeFeature::blockSettings),
|
| 55 | GeodeLayerSettings.CODEC.fieldOf("layers").forGetter(GeodeFeature::layerSettings),
|
| 56 | GeodeCrackSettings.CODEC.fieldOf("crack").forGetter(GeodeFeature::crackSettings),
|
| 57 | CHANCE_RANGE.optionalFieldOf("use_potential_placements_chance", 0.35).forGetter(GeodeFeature::usePotentialPlacementsChance),
|
| 58 | CHANCE_RANGE.optionalFieldOf("use_alternate_layer0_chance", 0.0).forGetter(GeodeFeature::useAlternateLayer0Chance),
|
| 59 | Codec.BOOL.optionalFieldOf("placements_require_layer0_alternate", true).forGetter(GeodeFeature::placementsRequireLayer0Alternate),
|
| 60 | IntProviders.codec(1, 20).optionalFieldOf("outer_wall_distance", UniformInt.of(4, 5)).forGetter(GeodeFeature::outerWallDistance),
|
| 61 | IntProviders.codec(1, 20).optionalFieldOf("distribution_points", UniformInt.of(3, 4)).forGetter(GeodeFeature::distributionPoints),
|
| 62 | IntProviders.codec(0, 10).optionalFieldOf("point_offset", UniformInt.of(1, 2)).forGetter(GeodeFeature::pointOffset),
|
| 63 | Codec.INT.optionalFieldOf("min_gen_offset", -16).forGetter(GeodeFeature::minGenOffset),
|
| 64 | Codec.INT.optionalFieldOf("max_gen_offset", 16).forGetter(GeodeFeature::maxGenOffset),
|
| 65 | CHANCE_RANGE.optionalFieldOf("noise_multiplier", 0.05).forGetter(GeodeFeature::noiseMultiplier),
|
| 66 | Codec.INT.fieldOf("invalid_blocks_threshold").forGetter(GeodeFeature::invalidBlocksThreshold)
|
| 67 | )
|
| 68 | .apply(i, GeodeFeature::new)
|
| 69 | );
|
| 70 | private static final Direction[] DIRECTIONS = Direction.values();
|
| 71 |
|
| 72 | @Override
|
| 73 | public MapCodec<GeodeFeature> codec() {
|
| 74 | return CODEC;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 79 | List<Pair<BlockPos, Integer>> points = Lists.newLinkedList();
|
| 80 | int numPoints = this.distributionPoints.sample(random);
|
| 81 | Noise noise = NOISE_PARAMETERS.create(new WorldgenRandom(new LegacyRandomSource(level.getSeed())));
|
| 82 | List<BlockPos> crackPoints = Lists.newLinkedList();
|
| 83 | double crackSizeAdjustment = (double)numPoints / this.outerWallDistance.maxInclusive();
|
| 84 | double innerAir = 1.0 / Math.sqrt(this.layerSettings.filling);
|
| 85 | double innermostBlockLayer = 1.0 / Math.sqrt(this.layerSettings.innerLayer + crackSizeAdjustment);
|
| 86 | double innerCrust = 1.0 / Math.sqrt(this.layerSettings.middleLayer + crackSizeAdjustment);
|
| 87 | double outerCrust = 1.0 / Math.sqrt(this.layerSettings.outerLayer + crackSizeAdjustment);
|
| 88 | double crackSize = 1.0 / Math.sqrt(this.crackSettings.baseCrackSize + random.nextDouble() / 2.0 + (numPoints > 3 ? crackSizeAdjustment : 0.0));
|
| 89 | boolean shouldGenerateCrack = random.nextFloat() < this.crackSettings.generateCrackChance;
|
| 90 | int numInvalidPoints = 0;
|
| 91 |
|
| 92 | for (int i = 0; i < numPoints; i++) {
|
| 93 | int x = this.outerWallDistance.sample(random);
|
| 94 | int y = this.outerWallDistance.sample(random);
|
| 95 | int z = this.outerWallDistance.sample(random);
|
| 96 | BlockPos pos = origin.offset(x, y, z);
|
| 97 | BlockState state = level.getBlockState(pos);
|
| 98 | if (state.isAir() || state.is(this.blockSettings.invalidBlocks())) {
|
| 99 | if (++numInvalidPoints > this.invalidBlocksThreshold) {
|
| 100 | return false;
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | points.add(Pair.of(pos, this.pointOffset.sample(random)));
|
| 105 | }
|
| 106 |
|
| 107 | if (shouldGenerateCrack) {
|
| 108 | int offsetIndex = random.nextInt(4);
|
| 109 | int crackOffset = numPoints * 2 + 1;
|
| 110 | if (offsetIndex == 0) {
|
| 111 | crackPoints.add(origin.offset(crackOffset, 7, 0));
|
| 112 | crackPoints.add(origin.offset(crackOffset, 5, 0));
|
| 113 | crackPoints.add(origin.offset(crackOffset, 1, 0));
|
| 114 | } else if (offsetIndex == 1) {
|
| 115 | crackPoints.add(origin.offset(0, 7, crackOffset));
|
| 116 | crackPoints.add(origin.offset(0, 5, crackOffset));
|
| 117 | crackPoints.add(origin.offset(0, 1, crackOffset));
|
| 118 | } else if (offsetIndex == 2) {
|
| 119 | crackPoints.add(origin.offset(crackOffset, 7, crackOffset));
|
| 120 | crackPoints.add(origin.offset(crackOffset, 5, crackOffset));
|
| 121 | crackPoints.add(origin.offset(crackOffset, 1, crackOffset));
|
| 122 | } else {
|
| 123 | crackPoints.add(origin.offset(0, 7, 0));
|
| 124 | crackPoints.add(origin.offset(0, 5, 0));
|
| 125 | crackPoints.add(origin.offset(0, 1, 0));
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | List<BlockPos> potentialCrystalPlacements = Lists.newArrayList();
|
| 130 | HolderSet<Block> cantReplace = this.blockSettings.cannotReplace();
|
| 131 | Predicate<BlockState> canReplace = s -> !s.is(cantReplace);
|
| 132 |
|
| 133 | for (BlockPos pointInside : BlockPos.betweenClosed(
|
| 134 | origin.offset(this.minGenOffset, this.minGenOffset, this.minGenOffset), origin.offset(this.maxGenOffset, this.maxGenOffset, this.maxGenOffset)
|
| 135 | )) {
|
| 136 | double noiseOffset = noise.get(pointInside.getX(), pointInside.getY(), pointInside.getZ()) * this.noiseMultiplier;
|
| 137 | double distSumShell = 0.0;
|
| 138 |
|
| 139 | for (Pair<BlockPos, Integer> point : points) {
|
| 140 | distSumShell += Mth.invSqrt(pointInside.distSqr(point.getFirst()) + point.getSecond().intValue()) + noiseOffset;
|
| 141 | }
|
| 142 |
|
| 143 | if (!(distSumShell < outerCrust)) {
|
| 144 | if (distSumShell >= innerAir) {
|
| 145 | this.safeSetBlock(level, pointInside, this.blockSettings.fillingProvider().value().getState(level, random, pointInside), canReplace);
|
| 146 | } else {
|
| 147 | double distSumCrack = 0.0;
|
| 148 |
|
| 149 | for (BlockPos point : crackPoints) {
|
| 150 | distSumCrack += Mth.invSqrt(pointInside.distSqr(point) + this.crackSettings.crackPointOffset) + noiseOffset;
|
| 151 | }
|
| 152 |
|
| 153 | if (shouldGenerateCrack && distSumCrack >= crackSize) {
|
| 154 | this.safeSetBlock(level, pointInside, Blocks.AIR.defaultBlockState(), canReplace);
|
| 155 |
|
| 156 | for (Direction direction : DIRECTIONS) {
|
| 157 | BlockPos adjacentPos = pointInside.relative(direction);
|
| 158 | FluidState adjacentFluidState = level.getFluidState(adjacentPos);
|
| 159 | if (!adjacentFluidState.isEmpty()) {
|
| 160 | level.scheduleTick(adjacentPos, adjacentFluidState.getType(), 0);
|
| 161 | }
|
| 162 | }
|
| 163 | } else if (distSumShell >= innermostBlockLayer) {
|
| 164 | boolean useAlternateLayer = random.nextFloat() < this.useAlternateLayer0Chance;
|
| 165 | if (useAlternateLayer) {
|
| 166 | this.safeSetBlock(level, pointInside, this.blockSettings.alternateInnerLayerProvider().value().getState(level, random, pointInside), canReplace);
|
| 167 | } else {
|
| 168 | this.safeSetBlock(level, pointInside, this.blockSettings.innerLayerProvider().value().getState(level, random, pointInside), canReplace);
|
| 169 | }
|
| 170 |
|
| 171 | if ((!this.placementsRequireLayer0Alternate || useAlternateLayer) && random.nextFloat() < this.usePotentialPlacementsChance) {
|
| 172 | potentialCrystalPlacements.add(pointInside.immutable());
|
| 173 | }
|
| 174 | } else if (distSumShell >= innerCrust) {
|
| 175 | this.safeSetBlock(level, pointInside, this.blockSettings.middleLayerProvider().value().getState(level, random, pointInside), canReplace);
|
| 176 | } else if (distSumShell >= outerCrust) {
|
| 177 | this.safeSetBlock(level, pointInside, this.blockSettings.outerLayerProvider().value().getState(level, random, pointInside), canReplace);
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | List<BlockState> innerPlacements = this.blockSettings.innerPlacements();
|
| 184 |
|
| 185 | for (BlockPos crystalPos : potentialCrystalPlacements) {
|
| 186 | BlockState blockState = Util.getRandom(innerPlacements, random);
|
| 187 |
|
| 188 | for (Direction direction : DIRECTIONS) {
|
| 189 | if (blockState.hasProperty(BlockStateProperties.FACING)) {
|
| 190 | blockState = blockState.setValue(BlockStateProperties.FACING, direction);
|
| 191 | }
|
| 192 |
|
| 193 | BlockPos placePos = crystalPos.relative(direction);
|
| 194 | BlockState placeState = level.getBlockState(placePos);
|
| 195 | if (blockState.hasProperty(BlockStateProperties.WATERLOGGED)) {
|
| 196 | blockState = blockState.setValue(BlockStateProperties.WATERLOGGED, placeState.getFluidState().isSource());
|
| 197 | }
|
| 198 |
|
| 199 | if (BuddingAmethystBlock.canClusterGrowAtState(placeState)) {
|
| 200 | this.safeSetBlock(level, placePos, blockState, canReplace);
|
| 201 | break;
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | return true;
|
| 207 | }
|
| 208 | }
|
| 209 |
|