| 1 | package net.minecraft.world.level.biome;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.MapCodec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import java.util.EnumMap;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.Map.Entry;
|
| 13 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 14 | import net.minecraft.util.StringRepresentable;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import net.minecraft.util.random.WeightedList;
|
| 17 | import net.minecraft.util.valueproviders.ConstantInt;
|
| 18 | import net.minecraft.util.valueproviders.IntProvider;
|
| 19 | import net.minecraft.util.valueproviders.IntProviders;
|
| 20 | import net.minecraft.util.valueproviders.UniformInt;
|
| 21 | import net.minecraft.world.entity.EntityType;
|
| 22 | import net.minecraft.world.entity.EntityTypes;
|
| 23 | import net.minecraft.world.entity.MobCategory;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 | import org.slf4j.Logger;
|
| 26 |
|
| 27 | public class MobSpawnSettings {
|
| 28 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 29 | public static final float DEFAULT_CREATURE_WORLD_GEN_SPAWN_PROBABILITY = 0.1F;
|
| 30 | public static final WeightedList<MobSpawnSettings.SpawnerData> EMPTY_MOB_LIST = WeightedList.of();
|
| 31 | public static final MobSpawnSettings EMPTY = new MobSpawnSettings.Builder().build();
|
| 32 | public static final MobSpawnSettings NO_SPAWNS = Util.make(() -> {
|
| 33 | MobSpawnSettings.Builder builder = new MobSpawnSettings.Builder();
|
| 34 |
|
| 35 | for (MobCategory category : MobCategory.values()) {
|
| 36 | builder.noSpawns(category);
|
| 37 | }
|
| 38 |
|
| 39 | return builder.build();
|
| 40 | });
|
| 41 | public static final Codec<MobSpawnSettings> CODEC = RecordCodecBuilder.create(
|
| 42 | i -> i.group(
|
| 43 | Codec.simpleMap(
|
| 44 | MobCategory.CODEC,
|
| 45 | WeightedList.codec(MobSpawnSettings.SpawnerData.CODEC).promotePartial(Util.prefix("Spawn data: ", LOGGER::error)),
|
| 46 | StringRepresentable.keys(MobCategory.values())
|
| 47 | )
|
| 48 | .fieldOf("spawns_by_category")
|
| 49 | .forGetter(b -> b.spawnsByCategory),
|
| 50 | Codec.simpleMap(BuiltInRegistries.ENTITY_TYPE.byNameCodec(), MobSpawnSettings.MobSpawnCost.CODEC, BuiltInRegistries.ENTITY_TYPE)
|
| 51 | .fieldOf("spawn_costs")
|
| 52 | .forGetter(b -> b.mobSpawnCosts)
|
| 53 | )
|
| 54 | .apply(i, MobSpawnSettings::new)
|
| 55 | );
|
| 56 | private final Map<MobCategory, WeightedList<MobSpawnSettings.SpawnerData>> spawnsByCategory;
|
| 57 | private final Map<EntityType<?>, MobSpawnSettings.MobSpawnCost> mobSpawnCosts;
|
| 58 |
|
| 59 | private MobSpawnSettings(
|
| 60 | final Map<MobCategory, WeightedList<MobSpawnSettings.SpawnerData>> spawnsByCategory, final Map<EntityType<?>, MobSpawnSettings.MobSpawnCost> mobSpawnCosts
|
| 61 | ) {
|
| 62 | this.spawnsByCategory = ImmutableMap.copyOf(spawnsByCategory);
|
| 63 | this.mobSpawnCosts = ImmutableMap.copyOf(mobSpawnCosts);
|
| 64 | }
|
| 65 |
|
| 66 | public WeightedList<MobSpawnSettings.SpawnerData> getMobsToSpawn(final MobCategory category) {
|
| 67 | return this.spawnsByCategory.getOrDefault(category, EMPTY_MOB_LIST);
|
| 68 | }
|
| 69 |
|
| 70 | public @Nullable WeightedList<MobSpawnSettings.SpawnerData> getMobsInCategory(final MobCategory category) {
|
| 71 | return this.spawnsByCategory.get(category);
|
| 72 | }
|
| 73 |
|
| 74 | public Set<MobCategory> definedCategories() {
|
| 75 | return this.spawnsByCategory.keySet();
|
| 76 | }
|
| 77 |
|
| 78 | public MobSpawnSettings.@Nullable MobSpawnCost getMobSpawnCost(final EntityType<?> type) {
|
| 79 | return this.mobSpawnCosts.get(type);
|
| 80 | }
|
| 81 |
|
| 82 | public Map<EntityType<?>, MobSpawnSettings.MobSpawnCost> allSpawnCosts() {
|
| 83 | return this.mobSpawnCosts;
|
| 84 | }
|
| 85 |
|
| 86 | public static class Builder {
|
| 87 | private final Map<MobCategory, WeightedList.Builder<MobSpawnSettings.SpawnerData>> spawnsByCategory = new EnumMap<>(MobCategory.class);
|
| 88 | private final Map<EntityType<?>, MobSpawnSettings.MobSpawnCost> mobSpawnCosts = Maps.newLinkedHashMap();
|
| 89 |
|
| 90 | public MobSpawnSettings.Builder addSpawn(final EntityType<?> type, final int weight, final int minCount, final int maxCount) {
|
| 91 | IntProvider count;
|
| 92 | if (minCount == maxCount) {
|
| 93 | count = new ConstantInt(minCount);
|
| 94 | } else {
|
| 95 | count = new UniformInt(minCount, maxCount);
|
| 96 | }
|
| 97 |
|
| 98 | this.addSpawn(type, type.getCategory(), weight, count);
|
| 99 | return this;
|
| 100 | }
|
| 101 |
|
| 102 | public MobSpawnSettings.Builder addSpawn(final EntityType<?> type, final int weight, final IntProvider count) {
|
| 103 | this.addSpawn(type, type.getCategory(), weight, count);
|
| 104 | return this;
|
| 105 | }
|
| 106 |
|
| 107 | @Deprecated
|
| 108 | public MobSpawnSettings.Builder addSpawn(final EntityType<?> type, final MobCategory category, final int weight, final IntProvider count) {
|
| 109 | this.forCategory(category).add(new MobSpawnSettings.SpawnerData(type, count), weight);
|
| 110 | return this;
|
| 111 | }
|
| 112 |
|
| 113 | public MobSpawnSettings.Builder addAllSpawns(final MobCategory category, final WeightedList<MobSpawnSettings.SpawnerData> spawns) {
|
| 114 | this.forCategory(category).addAll(spawns);
|
| 115 | return this;
|
| 116 | }
|
| 117 |
|
| 118 | public MobSpawnSettings.Builder noSpawns(final MobCategory category) {
|
| 119 | this.spawnsByCategory.put(category, WeightedList.builder());
|
| 120 | return this;
|
| 121 | }
|
| 122 |
|
| 123 | public MobSpawnSettings.Builder dontOverride(final MobCategory category) {
|
| 124 | this.spawnsByCategory.remove(category);
|
| 125 | return this;
|
| 126 | }
|
| 127 |
|
| 128 | public MobSpawnSettings.Builder addMobSpawnCost(final EntityType<?> type, final double charge, final double energyBudget) {
|
| 129 | this.mobSpawnCosts.put(type, new MobSpawnSettings.MobSpawnCost(energyBudget, charge));
|
| 130 | return this;
|
| 131 | }
|
| 132 |
|
| 133 | public MobSpawnSettings.Builder addAllCosts(final Map<EntityType<?>, MobSpawnSettings.MobSpawnCost> costs) {
|
| 134 | this.mobSpawnCosts.putAll(costs);
|
| 135 | return this;
|
| 136 | }
|
| 137 |
|
| 138 | public MobSpawnSettings build() {
|
| 139 | return new MobSpawnSettings(
|
| 140 | this.spawnsByCategory.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, e -> e.getValue().build())),
|
| 141 | ImmutableMap.copyOf(this.mobSpawnCosts)
|
| 142 | );
|
| 143 | }
|
| 144 |
|
| 145 | private WeightedList.Builder<MobSpawnSettings.SpawnerData> forCategory(final MobCategory category) {
|
| 146 | return this.spawnsByCategory.computeIfAbsent(category, var0 -> WeightedList.builder());
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | public record MobSpawnCost(double energyBudget, double charge) {
|
| 151 | public static final Codec<MobSpawnSettings.MobSpawnCost> CODEC = RecordCodecBuilder.create(
|
| 152 | i -> i.group(Codec.DOUBLE.fieldOf("energy_budget").forGetter(e -> e.energyBudget), Codec.DOUBLE.fieldOf("charge").forGetter(e -> e.charge))
|
| 153 | .apply(i, MobSpawnSettings.MobSpawnCost::new)
|
| 154 | );
|
| 155 | }
|
| 156 |
|
| 157 | public record SpawnerData(EntityType<?> type, IntProvider count) {
|
| 158 | public static final MapCodec<MobSpawnSettings.SpawnerData> CODEC = RecordCodecBuilder.mapCodec(
|
| 159 | i -> i.group(
|
| 160 | BuiltInRegistries.ENTITY_TYPE.byNameCodec().fieldOf("type").forGetter(MobSpawnSettings.SpawnerData::type),
|
| 161 | IntProviders.CODEC.fieldOf("count").forGetter(MobSpawnSettings.SpawnerData::count)
|
| 162 | )
|
| 163 | .apply(i, MobSpawnSettings.SpawnerData::new)
|
| 164 | );
|
| 165 |
|
| 166 | public SpawnerData {
|
| 167 | type = type.getCategory() == MobCategory.MISC ? EntityTypes.PIG : type;
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public String toString() {
|
| 172 | return EntityType.getKey(this.type)
|
| 173 | + "*("
|
| 174 | + this.count.minInclusive()
|
| 175 | + "-"
|
| 176 | + this.count.maxInclusive()
|
| 177 | + ") ("
|
| 178 | + BuiltInRegistries.INT_PROVIDER_TYPE.getKey(this.count.codec())
|
| 179 | + ")";
|
| 180 | }
|
| 181 | }
|
| 182 | }
|
| 183 |
|