| 1 | package net.minecraft.world.attribute.modifier;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.Set;
|
| 6 | import net.minecraft.util.random.WeightedList;
|
| 7 | import net.minecraft.world.attribute.EnvironmentAttribute;
|
| 8 | import net.minecraft.world.attribute.LerpFunction;
|
| 9 | import net.minecraft.world.entity.EntityType;
|
| 10 | import net.minecraft.world.entity.MobCategory;
|
| 11 | import net.minecraft.world.level.biome.MobSpawnSettings;
|
| 12 |
|
| 13 | public interface MobSpawnSettingsModifier extends AttributeModifier<MobSpawnSettings, MobSpawnSettings> {
|
| 14 | static MobSpawnSettingsModifier overlay() {
|
| 15 | return MobSpawnSettingsModifier.Overlay.INSTANCE;
|
| 16 | }
|
| 17 |
|
| 18 | @Override
|
| 19 | default Codec<MobSpawnSettings> argumentCodec(final EnvironmentAttribute<MobSpawnSettings> attribute) {
|
| 20 | return attribute.valueCodec();
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | default LerpFunction<MobSpawnSettings> argumentKeyframeLerp(final EnvironmentAttribute<MobSpawnSettings> attribute) {
|
| 25 | return attribute.type().keyframeLerp();
|
| 26 | }
|
| 27 |
|
| 28 | record Overlay() implements MobSpawnSettingsModifier {
|
| 29 | private static final MobSpawnSettingsModifier.Overlay INSTANCE = new MobSpawnSettingsModifier.Overlay();
|
| 30 | private static final MobCategory[] ALL_CATEGORIES = MobCategory.values();
|
| 31 |
|
| 32 | public MobSpawnSettings apply(final MobSpawnSettings first, final MobSpawnSettings second) {
|
| 33 | Set<MobCategory> firstCategories = first.definedCategories();
|
| 34 | Set<MobCategory> secondCategories = second.definedCategories();
|
| 35 | Map<EntityType<?>, MobSpawnSettings.MobSpawnCost> firstSpawnCosts = first.allSpawnCosts();
|
| 36 | Map<EntityType<?>, MobSpawnSettings.MobSpawnCost> secondSpawnCosts = second.allSpawnCosts();
|
| 37 | if (firstCategories.isEmpty() && firstSpawnCosts.isEmpty()) {
|
| 38 | return second;
|
| 39 | }
|
| 40 |
|
| 41 | if (secondCategories.isEmpty() && secondSpawnCosts.isEmpty()) {
|
| 42 | return first;
|
| 43 | }
|
| 44 |
|
| 45 | if (secondCategories.containsAll(firstCategories) && secondSpawnCosts.keySet().containsAll(firstSpawnCosts.keySet())) {
|
| 46 | return second;
|
| 47 | }
|
| 48 |
|
| 49 | MobSpawnSettings.Builder builder = new MobSpawnSettings.Builder();
|
| 50 |
|
| 51 | for (MobCategory category : ALL_CATEGORIES) {
|
| 52 | WeightedList<MobSpawnSettings.SpawnerData> secondSpawns = second.getMobsInCategory(category);
|
| 53 | if (secondSpawns != null) {
|
| 54 | builder.addAllSpawns(category, secondSpawns);
|
| 55 | } else {
|
| 56 | WeightedList<MobSpawnSettings.SpawnerData> firstSpawns = first.getMobsInCategory(category);
|
| 57 | if (firstSpawns != null) {
|
| 58 | builder.addAllSpawns(category, firstSpawns);
|
| 59 | }
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | return builder.addAllCosts(firstSpawnCosts).addAllCosts(secondSpawnCosts).build();
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|