| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.google.common.collect.ImmutableMap.Builder;
|
| 6 | import com.mojang.datafixers.DataFix;
|
| 7 | import com.mojang.datafixers.TypeRewriteRule;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.datafixers.types.Type;
|
| 10 | import com.mojang.logging.LogUtils;
|
| 11 | import com.mojang.serialization.Dynamic;
|
| 12 | import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
|
| 13 | import java.util.Comparator;
|
| 14 | import java.util.HashMap;
|
| 15 | import java.util.List;
|
| 16 | import java.util.Locale;
|
| 17 | import java.util.Map;
|
| 18 | import java.util.Optional;
|
| 19 | import java.util.Map.Entry;
|
| 20 | import java.util.function.Function;
|
| 21 | import java.util.stream.LongStream;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 | import org.slf4j.Logger;
|
| 24 |
|
| 25 | public class StructuresBecomeConfiguredFix extends DataFix {
|
| 26 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 27 | private static final Map<String, StructuresBecomeConfiguredFix.Conversion> CONVERSION_MAP = ImmutableMap.<String, StructuresBecomeConfiguredFix.Conversion>builder()
|
| 28 | .put(
|
| 29 | "mineshaft",
|
| 30 | StructuresBecomeConfiguredFix.Conversion.biomeMapped(
|
| 31 | Map.of(List.of("minecraft:badlands", "minecraft:eroded_badlands", "minecraft:wooded_badlands"), "minecraft:mineshaft_mesa"), "minecraft:mineshaft"
|
| 32 | )
|
| 33 | )
|
| 34 | .put(
|
| 35 | "shipwreck",
|
| 36 | StructuresBecomeConfiguredFix.Conversion.biomeMapped(
|
| 37 | Map.of(List.of("minecraft:beach", "minecraft:snowy_beach"), "minecraft:shipwreck_beached"), "minecraft:shipwreck"
|
| 38 | )
|
| 39 | )
|
| 40 | .put(
|
| 41 | "ocean_ruin",
|
| 42 | StructuresBecomeConfiguredFix.Conversion.biomeMapped(
|
| 43 | Map.of(List.of("minecraft:warm_ocean", "minecraft:lukewarm_ocean", "minecraft:deep_lukewarm_ocean"), "minecraft:ocean_ruin_warm"),
|
| 44 | "minecraft:ocean_ruin_cold"
|
| 45 | )
|
| 46 | )
|
| 47 | .put(
|
| 48 | "village",
|
| 49 | StructuresBecomeConfiguredFix.Conversion.biomeMapped(
|
| 50 | Map.of(
|
| 51 | List.of("minecraft:desert"),
|
| 52 | "minecraft:village_desert",
|
| 53 | List.of("minecraft:savanna"),
|
| 54 | "minecraft:village_savanna",
|
| 55 | List.of("minecraft:snowy_plains"),
|
| 56 | "minecraft:village_snowy",
|
| 57 | List.of("minecraft:taiga"),
|
| 58 | "minecraft:village_taiga"
|
| 59 | ),
|
| 60 | "minecraft:village_plains"
|
| 61 | )
|
| 62 | )
|
| 63 | .put(
|
| 64 | "ruined_portal",
|
| 65 | StructuresBecomeConfiguredFix.Conversion.biomeMapped(
|
| 66 | Map.of(
|
| 67 | List.of("minecraft:desert"),
|
| 68 | "minecraft:ruined_portal_desert",
|
| 69 | List.of(
|
| 70 | "minecraft:badlands",
|
| 71 | "minecraft:eroded_badlands",
|
| 72 | "minecraft:wooded_badlands",
|
| 73 | "minecraft:windswept_hills",
|
| 74 | "minecraft:windswept_forest",
|
| 75 | "minecraft:windswept_gravelly_hills",
|
| 76 | "minecraft:savanna_plateau",
|
| 77 | "minecraft:windswept_savanna",
|
| 78 | "minecraft:stony_shore",
|
| 79 | "minecraft:meadow",
|
| 80 | "minecraft:frozen_peaks",
|
| 81 | "minecraft:jagged_peaks",
|
| 82 | "minecraft:stony_peaks",
|
| 83 | "minecraft:snowy_slopes"
|
| 84 | ),
|
| 85 | "minecraft:ruined_portal_mountain",
|
| 86 | List.of("minecraft:bamboo_jungle", "minecraft:jungle", "minecraft:sparse_jungle"),
|
| 87 | "minecraft:ruined_portal_jungle",
|
| 88 | List.of(
|
| 89 | "minecraft:deep_frozen_ocean",
|
| 90 | "minecraft:deep_cold_ocean",
|
| 91 | "minecraft:deep_ocean",
|
| 92 | "minecraft:deep_lukewarm_ocean",
|
| 93 | "minecraft:frozen_ocean",
|
| 94 | "minecraft:ocean",
|
| 95 | "minecraft:cold_ocean",
|
| 96 | "minecraft:lukewarm_ocean",
|
| 97 | "minecraft:warm_ocean"
|
| 98 | ),
|
| 99 | "minecraft:ruined_portal_ocean"
|
| 100 | ),
|
| 101 | "minecraft:ruined_portal"
|
| 102 | )
|
| 103 | )
|
| 104 | .put("pillager_outpost", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:pillager_outpost"))
|
| 105 | .put("mansion", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:mansion"))
|
| 106 | .put("jungle_pyramid", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:jungle_pyramid"))
|
| 107 | .put("desert_pyramid", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:desert_pyramid"))
|
| 108 | .put("igloo", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:igloo"))
|
| 109 | .put("swamp_hut", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:swamp_hut"))
|
| 110 | .put("stronghold", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:stronghold"))
|
| 111 | .put("monument", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:monument"))
|
| 112 | .put("fortress", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:fortress"))
|
| 113 | .put("endcity", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:end_city"))
|
| 114 | .put("buried_treasure", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:buried_treasure"))
|
| 115 | .put("nether_fossil", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:nether_fossil"))
|
| 116 | .put("bastion_remnant", StructuresBecomeConfiguredFix.Conversion.trivial("minecraft:bastion_remnant"))
|
| 117 | .build();
|
| 118 |
|
| 119 | public StructuresBecomeConfiguredFix(final Schema outputSchema) {
|
| 120 | super(outputSchema, false);
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | protected TypeRewriteRule makeRule() {
|
| 125 | Type<?> chunkType = this.getInputSchema().getType(References.CHUNK);
|
| 126 | Type<?> newChunkType = this.getInputSchema().getType(References.CHUNK);
|
| 127 | return this.writeFixAndRead("StucturesToConfiguredStructures", chunkType, newChunkType, this::fix);
|
| 128 | }
|
| 129 |
|
| 130 | private Dynamic<?> fix(final Dynamic<?> chunk) {
|
| 131 | return chunk.update(
|
| 132 | "structures", structures -> structures.update("starts", s -> this.updateStarts(s, chunk)).update("References", r -> this.updateReferences(r, chunk))
|
| 133 | );
|
| 134 | }
|
| 135 |
|
| 136 | private Dynamic<?> updateStarts(final Dynamic<?> starts, final Dynamic<?> chunk) {
|
| 137 | Map<? extends Dynamic<?>, ? extends Dynamic<?>> values = starts.getMapValues().result().orElse(Map.of());
|
| 138 | HashMap<Dynamic<?>, Dynamic<?>> newMap = Maps.newHashMap();
|
| 139 | values.forEach((key, start) -> {
|
| 140 | if (!start.get("id").asString("INVALID").equals("INVALID")) {
|
| 141 | Dynamic<?> newKey = this.findUpdatedStructureType((Dynamic<?>)key, chunk);
|
| 142 | if (newKey == null) {
|
| 143 | LOGGER.warn("Encountered unknown structure in datafixer: {}", key.asString("<missing key>"));
|
| 144 | } else {
|
| 145 | newMap.computeIfAbsent(newKey, k -> start.set("id", newKey));
|
| 146 | }
|
| 147 | }
|
| 148 | });
|
| 149 | return chunk.createMap(newMap);
|
| 150 | }
|
| 151 |
|
| 152 | private Dynamic<?> updateReferences(final Dynamic<?> references, final Dynamic<?> chunk) {
|
| 153 | Map<? extends Dynamic<?>, ? extends Dynamic<?>> values = references.getMapValues().result().orElse(Map.of());
|
| 154 | HashMap<Dynamic<?>, Dynamic<?>> newMap = Maps.newHashMap();
|
| 155 | values.forEach(
|
| 156 | (key, refList) -> {
|
| 157 | if (refList.asLongStream().count() != 0L) {
|
| 158 | Dynamic<?> newKey = this.findUpdatedStructureType((Dynamic<?>)key, chunk);
|
| 159 | if (newKey == null) {
|
| 160 | LOGGER.warn("Encountered unknown structure in datafixer: {}", key.asString("<missing key>"));
|
| 161 | } else {
|
| 162 | newMap.compute(
|
| 163 | newKey, (k, oldRefList) -> oldRefList == null ? refList : refList.createLongList(LongStream.concat(oldRefList.asLongStream(), refList.asLongStream()))
|
| 164 | );
|
| 165 | }
|
| 166 | }
|
| 167 | }
|
| 168 | );
|
| 169 | return chunk.createMap(newMap);
|
| 170 | }
|
| 171 |
|
| 172 | private @Nullable Dynamic<?> findUpdatedStructureType(final Dynamic<?> dynamicKey, final Dynamic<?> chunk) {
|
| 173 | String key = dynamicKey.asString("UNKNOWN").toLowerCase(Locale.ROOT);
|
| 174 | StructuresBecomeConfiguredFix.Conversion conversion = CONVERSION_MAP.get(key);
|
| 175 | if (conversion == null) {
|
| 176 | return null;
|
| 177 | }
|
| 178 |
|
| 179 | String resultingId = conversion.fallback;
|
| 180 | if (!conversion.biomeMapping().isEmpty()) {
|
| 181 | Optional<String> result = this.guessConfiguration(chunk, conversion);
|
| 182 | if (result.isPresent()) {
|
| 183 | resultingId = result.get();
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | return chunk.createString(resultingId);
|
| 188 | }
|
| 189 |
|
| 190 | private Optional<String> guessConfiguration(final Dynamic<?> chunk, final StructuresBecomeConfiguredFix.Conversion conversion) {
|
| 191 | Object2IntArrayMap<String> matches = new Object2IntArrayMap<>();
|
| 192 | chunk.get("sections").asList(Function.identity()).forEach(s -> s.get("biomes").get("palette").asList(Function.identity()).forEach(biome -> {
|
| 193 | String mapping = conversion.biomeMapping().get(biome.asString(""));
|
| 194 | if (mapping != null) {
|
| 195 | matches.mergeInt(mapping, 1, Integer::sum);
|
| 196 | }
|
| 197 | }));
|
| 198 | return matches.object2IntEntrySet().stream().max(Comparator.comparingInt(it.unimi.dsi.fastutil.objects.Object2IntMap.Entry::getIntValue)).map(Entry::getKey);
|
| 199 | }
|
| 200 |
|
| 201 | private record Conversion(Map<String, String> biomeMapping, String fallback) {
|
| 202 | public static StructuresBecomeConfiguredFix.Conversion trivial(final String result) {
|
| 203 | return new StructuresBecomeConfiguredFix.Conversion(Map.of(), result);
|
| 204 | }
|
| 205 |
|
| 206 | public static StructuresBecomeConfiguredFix.Conversion biomeMapped(final Map<List<String>, String> mapping, final String fallback) {
|
| 207 | return new StructuresBecomeConfiguredFix.Conversion(unpack(mapping), fallback);
|
| 208 | }
|
| 209 |
|
| 210 | private static Map<String, String> unpack(final Map<List<String>, String> packed) {
|
| 211 | Builder<String, String> builder = ImmutableMap.builder();
|
| 212 |
|
| 213 | for (Entry<List<String>, String> entry : packed.entrySet()) {
|
| 214 | entry.getKey().forEach(k -> builder.put(k, entry.getValue()));
|
| 215 | }
|
| 216 |
|
| 217 | return builder.build();
|
| 218 | }
|
| 219 | }
|
| 220 | }
|
| 221 |
|