| 1 | package net.minecraft.world.level.biome;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import com.google.common.collect.ImmutableList;
|
| 5 | import com.google.common.collect.Lists;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import com.mojang.serialization.MapCodec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.function.Supplier;
|
| 13 | import java.util.stream.Collectors;
|
| 14 | import net.minecraft.core.Holder;
|
| 15 | import net.minecraft.core.HolderGetter;
|
| 16 | import net.minecraft.core.HolderSet;
|
| 17 | import net.minecraft.resources.ResourceKey;
|
| 18 | import net.minecraft.tags.FeatureTags;
|
| 19 | import net.minecraft.util.Util;
|
| 20 | import net.minecraft.world.level.levelgen.GenerationStep;
|
| 21 | import net.minecraft.world.level.levelgen.carver.WorldCarver;
|
| 22 | import net.minecraft.world.level.levelgen.feature.Feature;
|
| 23 | import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
| 24 | import org.slf4j.Logger;
|
| 25 |
|
| 26 | public class BiomeGenerationSettings {
|
| 27 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 28 | public static final BiomeGenerationSettings EMPTY = new BiomeGenerationSettings(HolderSet.empty(), List.of());
|
| 29 | public static final MapCodec<BiomeGenerationSettings> CODEC = RecordCodecBuilder.mapCodec(
|
| 30 | i -> i.group(
|
| 31 | WorldCarver.LIST_CODEC.promotePartial(Util.prefix("Carver: ", LOGGER::error)).fieldOf("carvers").forGetter(b -> b.carvers),
|
| 32 | PlacedFeature.LIST_OF_LISTS_CODEC.promotePartial(Util.prefix("Features: ", LOGGER::error)).fieldOf("features").forGetter(b -> b.features)
|
| 33 | )
|
| 34 | .apply(i, BiomeGenerationSettings::new)
|
| 35 | );
|
| 36 | private final HolderSet<WorldCarver> carvers;
|
| 37 | private final List<HolderSet<PlacedFeature>> features;
|
| 38 | private final Supplier<List<Feature>> boneMealFeatures;
|
| 39 | private final Supplier<Set<PlacedFeature>> featureSet;
|
| 40 |
|
| 41 | private BiomeGenerationSettings(final HolderSet<WorldCarver> carvers, final List<HolderSet<PlacedFeature>> features) {
|
| 42 | this.carvers = carvers;
|
| 43 | this.features = features;
|
| 44 | this.boneMealFeatures = Suppliers.memoize(
|
| 45 | () -> features.stream()
|
| 46 | .flatMap(HolderSet::stream)
|
| 47 | .flatMap(feature -> ((PlacedFeature)feature.value()).getFeatures())
|
| 48 | .filter(feature -> feature.is(FeatureTags.CAN_SPAWN_FROM_BONE_MEAL))
|
| 49 | .map(Holder::value)
|
| 50 | .collect(ImmutableList.toImmutableList())
|
| 51 | );
|
| 52 | this.featureSet = Suppliers.memoize(() -> features.stream().flatMap(HolderSet::stream).map(Holder::value).collect(Collectors.toSet()));
|
| 53 | }
|
| 54 |
|
| 55 | public Iterable<Holder<WorldCarver>> getCarvers() {
|
| 56 | return this.carvers;
|
| 57 | }
|
| 58 |
|
| 59 | public List<Feature> getBoneMealFeatures() {
|
| 60 | return this.boneMealFeatures.get();
|
| 61 | }
|
| 62 |
|
| 63 | public List<HolderSet<PlacedFeature>> features() {
|
| 64 | return this.features;
|
| 65 | }
|
| 66 |
|
| 67 | public boolean hasFeature(final PlacedFeature feature) {
|
| 68 | return this.featureSet.get().contains(feature);
|
| 69 | }
|
| 70 |
|
| 71 | public static class Builder extends BiomeGenerationSettings.PlainBuilder {
|
| 72 | private final HolderGetter<PlacedFeature> placedFeatures;
|
| 73 | private final HolderGetter<WorldCarver> worldCarvers;
|
| 74 |
|
| 75 | public Builder(final HolderGetter<PlacedFeature> placedFeatures, final HolderGetter<WorldCarver> worldCarvers) {
|
| 76 | this.placedFeatures = placedFeatures;
|
| 77 | this.worldCarvers = worldCarvers;
|
| 78 | }
|
| 79 |
|
| 80 | public BiomeGenerationSettings.Builder addFeature(final GenerationStep.Decoration step, final ResourceKey<PlacedFeature> feature) {
|
| 81 | this.addFeature(step.ordinal(), this.placedFeatures.getOrThrow(feature));
|
| 82 | return this;
|
| 83 | }
|
| 84 |
|
| 85 | public BiomeGenerationSettings.Builder addCarver(final ResourceKey<WorldCarver> carver) {
|
| 86 | this.addCarver(this.worldCarvers.getOrThrow(carver));
|
| 87 | return this;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | public static class PlainBuilder {
|
| 92 | private final List<Holder<WorldCarver>> carvers = new ArrayList<>();
|
| 93 | private final List<List<Holder<PlacedFeature>>> features = new ArrayList<>();
|
| 94 |
|
| 95 | public BiomeGenerationSettings.PlainBuilder addFeature(final GenerationStep.Decoration step, final Holder<PlacedFeature> feature) {
|
| 96 | return this.addFeature(step.ordinal(), feature);
|
| 97 | }
|
| 98 |
|
| 99 | public BiomeGenerationSettings.PlainBuilder addFeature(final int index, final Holder<PlacedFeature> feature) {
|
| 100 | this.addFeatureStepsUpTo(index);
|
| 101 | this.features.get(index).add(feature);
|
| 102 | return this;
|
| 103 | }
|
| 104 |
|
| 105 | public BiomeGenerationSettings.PlainBuilder addCarver(final Holder<WorldCarver> carver) {
|
| 106 | this.carvers.add(carver);
|
| 107 | return this;
|
| 108 | }
|
| 109 |
|
| 110 | private void addFeatureStepsUpTo(final int index) {
|
| 111 | while (this.features.size() <= index) {
|
| 112 | this.features.add(Lists.newArrayList());
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | public BiomeGenerationSettings build() {
|
| 117 | return new BiomeGenerationSettings(HolderSet.direct(this.carvers), this.features.stream().map(HolderSet::direct).collect(ImmutableList.toImmutableList()));
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|