| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.concurrent.CompletableFuture;
|
| 8 | import java.util.stream.Stream;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.core.HolderLookup;
|
| 12 | import net.minecraft.core.HolderSet;
|
| 13 | import net.minecraft.server.level.WorldGenRegion;
|
| 14 | import net.minecraft.util.Util;
|
| 15 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 16 | import net.minecraft.world.level.NoiseColumn;
|
| 17 | import net.minecraft.world.level.StructureManager;
|
| 18 | import net.minecraft.world.level.biome.Biome;
|
| 19 | import net.minecraft.world.level.biome.BiomeManager;
|
| 20 | import net.minecraft.world.level.biome.FixedBiomeSource;
|
| 21 | import net.minecraft.world.level.block.Blocks;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 24 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 25 | import net.minecraft.world.level.chunk.ChunkGeneratorStructureState;
|
| 26 | import net.minecraft.world.level.levelgen.blending.Blender;
|
| 27 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 28 | import net.minecraft.world.level.levelgen.flat.FlatLevelGeneratorSettings;
|
| 29 | import net.minecraft.world.level.levelgen.structure.StructureSet;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 |
|
| 32 | public class FlatLevelSource extends ChunkGenerator {
|
| 33 | public static final MapCodec<FlatLevelSource> CODEC = RecordCodecBuilder.mapCodec(
|
| 34 | i -> i.group(FlatLevelGeneratorSettings.CODEC.fieldOf("settings").forGetter(FlatLevelSource::settings)).apply(i, i.stable(FlatLevelSource::new))
|
| 35 | );
|
| 36 | private final FlatLevelGeneratorSettings settings;
|
| 37 |
|
| 38 | public FlatLevelSource(final FlatLevelGeneratorSettings generatorSettings) {
|
| 39 | super(new FixedBiomeSource(generatorSettings.getBiome()), Util.memoize(generatorSettings::adjustGenerationSettings));
|
| 40 | this.settings = generatorSettings;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public ChunkGeneratorStructureState createState(final HolderLookup<StructureSet> structureSets, final RandomState randomState, final long levelSeed) {
|
| 45 | Stream<Holder<StructureSet>> structures = this.settings
|
| 46 | .structureOverrides()
|
| 47 | .map(HolderSet::stream)
|
| 48 | .orElseGet(() -> structureSets.listElements().map(e -> (Holder<StructureSet>)e));
|
| 49 | return ChunkGeneratorStructureState.createForFlat(randomState, levelSeed, this.getOrigin(randomState), this.biomeSource, structures);
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected MapCodec<? extends ChunkGenerator> codec() {
|
| 54 | return CODEC;
|
| 55 | }
|
| 56 |
|
| 57 | public FlatLevelGeneratorSettings settings() {
|
| 58 | return this.settings;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public int getSpawnHeight(final LevelHeightAccessor heightAccessor) {
|
| 63 | return heightAccessor.getMinY() + Math.min(heightAccessor.getHeight(), this.settings.getLayers().size());
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public CompletableFuture<ChunkAccess> buildTerrain(
|
| 68 | final ChunkAccess chunk,
|
| 69 | final Blender blender,
|
| 70 | final RandomState randomState,
|
| 71 | final StructureManager structureManager,
|
| 72 | final BiomeManager biomeManager,
|
| 73 | final @Nullable WorldGenRegion carverBiomeRegion,
|
| 74 | final Set<Holder<Biome>> possibleBiomes
|
| 75 | ) {
|
| 76 | List<BlockState> layers = this.settings.getLayers();
|
| 77 | BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
| 78 | Heightmap oceanFloor = chunk.getOrCreateHeightmapUnprimed(Heightmap.Types.OCEAN_FLOOR_WG);
|
| 79 | Heightmap worldSurface = chunk.getOrCreateHeightmapUnprimed(Heightmap.Types.WORLD_SURFACE_WG);
|
| 80 |
|
| 81 | for (int layerIndex = 0; layerIndex < Math.min(chunk.getHeight(), layers.size()); layerIndex++) {
|
| 82 | BlockState blockState = layers.get(layerIndex);
|
| 83 | if (blockState != null) {
|
| 84 | int y = chunk.getMinY() + layerIndex;
|
| 85 |
|
| 86 | for (int x = 0; x < 16; x++) {
|
| 87 | for (int z = 0; z < 16; z++) {
|
| 88 | chunk.setBlockState(blockPos.set(x, y, z), blockState);
|
| 89 | oceanFloor.update(x, y, z, blockState);
|
| 90 | worldSurface.update(x, y, z, blockState);
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | return CompletableFuture.completedFuture(chunk);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public int getBaseHeight(final int x, final int z, final Heightmap.Types type, final LevelHeightAccessor heightAccessor, final RandomState randomState) {
|
| 101 | List<BlockState> layers = this.settings.getLayers();
|
| 102 |
|
| 103 | for (int layerIndex = Math.min(layers.size() - 1, heightAccessor.getMaxY()); layerIndex >= 0; layerIndex--) {
|
| 104 | BlockState state = layers.get(layerIndex);
|
| 105 | if (state != null && type.isOpaque().test(state)) {
|
| 106 | return heightAccessor.getMinY() + layerIndex + 1;
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | return heightAccessor.getMinY();
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public NoiseColumn getBaseColumn(final int x, final int z, final LevelHeightAccessor heightAccessor, final RandomState randomState) {
|
| 115 | return new NoiseColumn(
|
| 116 | heightAccessor.getMinY(),
|
| 117 | this.settings
|
| 118 | .getLayers()
|
| 119 | .stream()
|
| 120 | .limit(heightAccessor.getHeight())
|
| 121 | .map(state -> state == null ? Blocks.AIR.defaultBlockState() : state)
|
| 122 | .toArray(BlockState[]::new)
|
| 123 | );
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public void addDebugScreenInfo(final List<String> result, final RandomState randomState, final BlockPos feetPos, final SamplerContext samplerContext) {
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public void spawnOriginalMobs(final WorldGenRegion worldGenRegion) {
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public int getMinY() {
|
| 136 | return 0;
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public int getGenDepth() {
|
| 141 | return 384;
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public int getSeaLevel() {
|
| 146 | return -63;
|
| 147 | }
|
| 148 | }
|
| 149 |
|