| 1 | package net.minecraft.world.level.levelgen.flat;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.function.Function;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.core.HolderGetter;
|
| 13 | import net.minecraft.core.HolderSet;
|
| 14 | import net.minecraft.core.registries.Registries;
|
| 15 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 16 | import net.minecraft.data.worldgen.placement.MiscOverworldPlacements;
|
| 17 | import net.minecraft.data.worldgen.placement.PlacementUtils;
|
| 18 | import net.minecraft.resources.RegistryOps;
|
| 19 | import net.minecraft.util.ExtraCodecs;
|
| 20 | import net.minecraft.world.level.biome.Biome;
|
| 21 | import net.minecraft.world.level.biome.BiomeGenerationSettings;
|
| 22 | import net.minecraft.world.level.biome.Biomes;
|
| 23 | import net.minecraft.world.level.block.Blocks;
|
| 24 | import net.minecraft.world.level.block.state.BlockState;
|
| 25 | import net.minecraft.world.level.dimension.DimensionType;
|
| 26 | import net.minecraft.world.level.levelgen.GenerationStep;
|
| 27 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 28 | import net.minecraft.world.level.levelgen.feature.FillLayerFeature;
|
| 29 | import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
| 30 | import net.minecraft.world.level.levelgen.structure.BuiltinStructureSets;
|
| 31 | import net.minecraft.world.level.levelgen.structure.StructureSet;
|
| 32 | import org.slf4j.Logger;
|
| 33 |
|
| 34 | public class FlatLevelGeneratorSettings {
|
| 35 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 36 | public static final Codec<FlatLevelGeneratorSettings> CODEC = RecordCodecBuilder.<FlatLevelGeneratorSettings>create(
|
| 37 | i -> i.group(
|
| 38 | RegistryCodecs.holderSet(Registries.STRUCTURE_SET).lenientOptionalFieldOf("structure_overrides").forGetter(c -> c.structureOverrides),
|
| 39 | FlatLayerInfo.CODEC.listOf().fieldOf("layers").forGetter(FlatLevelGeneratorSettings::getLayersInfo),
|
| 40 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.BOOL, "lakes", false).forGetter(s -> s.addLakes),
|
| 41 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.BOOL, "features", false).forGetter(s -> s.decoration),
|
| 42 | Biome.CODEC.lenientOptionalFieldOf("biome").orElseGet(Optional::empty).forGetter(s -> Optional.of(s.biome)),
|
| 43 | RegistryOps.retrieveElement(Biomes.PLAINS),
|
| 44 | RegistryOps.retrieveElement(MiscOverworldPlacements.LAKE_LAVA_UNDERGROUND),
|
| 45 | RegistryOps.retrieveElement(MiscOverworldPlacements.LAKE_LAVA_SURFACE)
|
| 46 | )
|
| 47 | .apply(i, FlatLevelGeneratorSettings::new)
|
| 48 | )
|
| 49 | .comapFlatMap(FlatLevelGeneratorSettings::validateHeight, Function.identity())
|
| 50 | .stable();
|
| 51 | private final Optional<HolderSet<StructureSet>> structureOverrides;
|
| 52 | private final List<FlatLayerInfo> layersInfo = Lists.newArrayList();
|
| 53 | private final Holder<Biome> biome;
|
| 54 | private final List<BlockState> layers;
|
| 55 | private boolean voidGen;
|
| 56 | private boolean decoration;
|
| 57 | private boolean addLakes;
|
| 58 | private final List<Holder<PlacedFeature>> lakes;
|
| 59 |
|
| 60 | private static DataResult<FlatLevelGeneratorSettings> validateHeight(final FlatLevelGeneratorSettings settings) {
|
| 61 | int totalHeight = settings.layersInfo.stream().mapToInt(FlatLayerInfo::getHeight).sum();
|
| 62 | return totalHeight > DimensionType.Y_SIZE
|
| 63 | ? DataResult.error(() -> "Sum of layer heights is > " + DimensionType.Y_SIZE, settings)
|
| 64 | : DataResult.success(settings);
|
| 65 | }
|
| 66 |
|
| 67 | private FlatLevelGeneratorSettings(
|
| 68 | final Optional<HolderSet<StructureSet>> structureOverrides,
|
| 69 | final List<FlatLayerInfo> layers,
|
| 70 | final boolean lakes,
|
| 71 | final boolean features,
|
| 72 | final Optional<Holder<Biome>> biome,
|
| 73 | final Holder.Reference<Biome> fallbackBiome,
|
| 74 | final Holder<PlacedFeature> lavaUnderground,
|
| 75 | final Holder<PlacedFeature> lavaSurface
|
| 76 | ) {
|
| 77 | this(structureOverrides, getBiome(biome, fallbackBiome), List.of(lavaUnderground, lavaSurface));
|
| 78 | if (lakes) {
|
| 79 | this.setAddLakes();
|
| 80 | }
|
| 81 |
|
| 82 | if (features) {
|
| 83 | this.setDecoration();
|
| 84 | }
|
| 85 |
|
| 86 | this.layersInfo.addAll(layers);
|
| 87 | this.updateLayers();
|
| 88 | }
|
| 89 |
|
| 90 | private static Holder<Biome> getBiome(final Optional<? extends Holder<Biome>> biome, final Holder<Biome> fallbackBiome) {
|
| 91 | if (biome.isEmpty()) {
|
| 92 | LOGGER.error("Unknown biome, defaulting to plains");
|
| 93 | return fallbackBiome;
|
| 94 | } else {
|
| 95 | return (Holder<Biome>)biome.get();
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | public FlatLevelGeneratorSettings(
|
| 100 | final Optional<HolderSet<StructureSet>> structureOverrides, final Holder<Biome> biome, final List<Holder<PlacedFeature>> lakes
|
| 101 | ) {
|
| 102 | this.structureOverrides = structureOverrides;
|
| 103 | this.biome = biome;
|
| 104 | this.layers = Lists.newArrayList();
|
| 105 | this.lakes = lakes;
|
| 106 | }
|
| 107 |
|
| 108 | public FlatLevelGeneratorSettings withBiomeAndLayers(
|
| 109 | final List<FlatLayerInfo> layers, final Optional<HolderSet<StructureSet>> structureOverrides, final Holder<Biome> biome
|
| 110 | ) {
|
| 111 | FlatLevelGeneratorSettings settings = new FlatLevelGeneratorSettings(structureOverrides, biome, this.lakes);
|
| 112 |
|
| 113 | for (FlatLayerInfo layerInfo : layers) {
|
| 114 | settings.layersInfo.add(layerInfo);
|
| 115 | settings.updateLayers();
|
| 116 | }
|
| 117 |
|
| 118 | if (this.decoration) {
|
| 119 | settings.setDecoration();
|
| 120 | }
|
| 121 |
|
| 122 | if (this.addLakes) {
|
| 123 | settings.setAddLakes();
|
| 124 | }
|
| 125 |
|
| 126 | return settings;
|
| 127 | }
|
| 128 |
|
| 129 | public void setDecoration() {
|
| 130 | this.decoration = true;
|
| 131 | }
|
| 132 |
|
| 133 | public void setAddLakes() {
|
| 134 | this.addLakes = true;
|
| 135 | }
|
| 136 |
|
| 137 | public BiomeGenerationSettings adjustGenerationSettings(final Holder<Biome> sourceBiome) {
|
| 138 | if (!sourceBiome.equals(this.biome)) {
|
| 139 | return sourceBiome.value().getGenerationSettings();
|
| 140 | }
|
| 141 |
|
| 142 | BiomeGenerationSettings biomeGenerationSettings = this.getBiome().value().getGenerationSettings();
|
| 143 | BiomeGenerationSettings.PlainBuilder newGenerationSettings = new BiomeGenerationSettings.PlainBuilder();
|
| 144 | if (this.addLakes) {
|
| 145 | for (Holder<PlacedFeature> lake : this.lakes) {
|
| 146 | newGenerationSettings.addFeature(GenerationStep.Decoration.LAKES, lake);
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | boolean biomeDecoration = (!this.voidGen || sourceBiome.is(Biomes.THE_VOID)) && this.decoration;
|
| 151 | if (biomeDecoration) {
|
| 152 | List<HolderSet<PlacedFeature>> features = biomeGenerationSettings.features();
|
| 153 |
|
| 154 | for (int stepIndex = 0; stepIndex < features.size(); stepIndex++) {
|
| 155 | if (stepIndex != GenerationStep.Decoration.UNDERGROUND_STRUCTURES.ordinal()
|
| 156 | && stepIndex != GenerationStep.Decoration.SURFACE_STRUCTURES.ordinal()
|
| 157 | && (!this.addLakes || stepIndex != GenerationStep.Decoration.LAKES.ordinal())) {
|
| 158 | for (Holder<PlacedFeature> feature : features.get(stepIndex)) {
|
| 159 | newGenerationSettings.addFeature(stepIndex, feature);
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | List<BlockState> layers = this.getLayers();
|
| 166 |
|
| 167 | for (int i = 0; i < layers.size(); i++) {
|
| 168 | BlockState layer = layers.get(i);
|
| 169 | if (!Heightmap.Types.MOTION_BLOCKING.isOpaque().test(layer)) {
|
| 170 | layers.set(i, null);
|
| 171 | newGenerationSettings.addFeature(GenerationStep.Decoration.TOP_LAYER_MODIFICATION, PlacementUtils.inlinePlaced(new FillLayerFeature(i, layer)));
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | return newGenerationSettings.build();
|
| 176 | }
|
| 177 |
|
| 178 | public Optional<HolderSet<StructureSet>> structureOverrides() {
|
| 179 | return this.structureOverrides;
|
| 180 | }
|
| 181 |
|
| 182 | public Holder<Biome> getBiome() {
|
| 183 | return this.biome;
|
| 184 | }
|
| 185 |
|
| 186 | public List<FlatLayerInfo> getLayersInfo() {
|
| 187 | return this.layersInfo;
|
| 188 | }
|
| 189 |
|
| 190 | public List<BlockState> getLayers() {
|
| 191 | return this.layers;
|
| 192 | }
|
| 193 |
|
| 194 | public void updateLayers() {
|
| 195 | this.layers.clear();
|
| 196 |
|
| 197 | for (FlatLayerInfo layer : this.layersInfo) {
|
| 198 | for (int y = 0; y < layer.getHeight(); y++) {
|
| 199 | this.layers.add(layer.getBlockState());
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | this.voidGen = this.layers.stream().allMatch(s -> s.is(Blocks.AIR));
|
| 204 | }
|
| 205 |
|
| 206 | public static FlatLevelGeneratorSettings getDefault(
|
| 207 | final HolderGetter<Biome> biomes, final HolderGetter<StructureSet> structureSets, final HolderGetter<PlacedFeature> placedFeatures
|
| 208 | ) {
|
| 209 | HolderSet<StructureSet> structureSettings = HolderSet.direct(
|
| 210 | structureSets.getOrThrow(BuiltinStructureSets.STRONGHOLDS), structureSets.getOrThrow(BuiltinStructureSets.VILLAGES)
|
| 211 | );
|
| 212 | FlatLevelGeneratorSettings result = new FlatLevelGeneratorSettings(Optional.of(structureSettings), getDefaultBiome(biomes), createLakesList(placedFeatures));
|
| 213 | result.getLayersInfo().add(new FlatLayerInfo(1, Blocks.BEDROCK));
|
| 214 | result.getLayersInfo().add(new FlatLayerInfo(2, Blocks.DIRT));
|
| 215 | result.getLayersInfo().add(new FlatLayerInfo(1, Blocks.GRASS_BLOCK));
|
| 216 | result.updateLayers();
|
| 217 | return result;
|
| 218 | }
|
| 219 |
|
| 220 | public static Holder<Biome> getDefaultBiome(final HolderGetter<Biome> biomes) {
|
| 221 | return biomes.getOrThrow(Biomes.PLAINS);
|
| 222 | }
|
| 223 |
|
| 224 | public static List<Holder<PlacedFeature>> createLakesList(final HolderGetter<PlacedFeature> placedFeatures) {
|
| 225 | return List.of(placedFeatures.getOrThrow(MiscOverworldPlacements.LAKE_LAVA_UNDERGROUND), placedFeatures.getOrThrow(MiscOverworldPlacements.LAKE_LAVA_SURFACE));
|
| 226 | }
|
| 227 | }
|
| 228 |
|