| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DSL;
|
| 4 | import com.mojang.datafixers.DataFix;
|
| 5 | import com.mojang.datafixers.DataFixUtils;
|
| 6 | import com.mojang.datafixers.OpticFinder;
|
| 7 | import com.mojang.datafixers.TypeRewriteRule;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.datafixers.types.Type;
|
| 10 | import com.mojang.datafixers.types.templates.CompoundList.CompoundListType;
|
| 11 | import com.mojang.datafixers.util.Pair;
|
| 12 | import com.mojang.serialization.Dynamic;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Objects;
|
| 15 | import java.util.Optional;
|
| 16 | import java.util.stream.Collectors;
|
| 17 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 18 |
|
| 19 | public class NewVillageFix extends DataFix {
|
| 20 | public NewVillageFix(final Schema outputSchema, final boolean changesType) {
|
| 21 | super(outputSchema, changesType);
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | protected TypeRewriteRule makeRule() {
|
| 26 | CompoundListType<String, ?> startsType = DSL.compoundList(DSL.string(), this.getInputSchema().getType(References.STRUCTURE_FEATURE));
|
| 27 | OpticFinder<? extends List<? extends Pair<String, ?>>> finder = startsType.finder();
|
| 28 | return this.cap(startsType);
|
| 29 | }
|
| 30 |
|
| 31 | private <SF> TypeRewriteRule cap(final CompoundListType<String, SF> startsType) {
|
| 32 | Type<?> chunkType = this.getInputSchema().getType(References.CHUNK);
|
| 33 | Type<?> structureType = this.getInputSchema().getType(References.STRUCTURE_FEATURE);
|
| 34 | OpticFinder<?> levelFinder = chunkType.findField("Level");
|
| 35 | OpticFinder<?> structuresFinder = levelFinder.type().findField("Structures");
|
| 36 | OpticFinder<?> startsFinder = structuresFinder.type().findField("Starts");
|
| 37 | OpticFinder<List<Pair<String, SF>>> listFinder = startsType.finder();
|
| 38 | return TypeRewriteRule.seq(
|
| 39 | this.fixTypeEverywhereTyped(
|
| 40 | "NewVillageFix",
|
| 41 | chunkType,
|
| 42 | input -> input.updateTyped(
|
| 43 | levelFinder,
|
| 44 | level -> level.updateTyped(
|
| 45 | structuresFinder,
|
| 46 | structures -> structures.updateTyped(
|
| 47 | startsFinder,
|
| 48 | starts -> starts.update(
|
| 49 | listFinder,
|
| 50 | list -> list.stream()
|
| 51 | .filter(pair -> !Objects.equals(pair.getFirst(), "Village"))
|
| 52 | .map(pair -> pair.mapFirst(name -> name.equals("New_Village") ? "Village" : name))
|
| 53 | .collect(Collectors.toList())
|
| 54 | )
|
| 55 | )
|
| 56 | .update(DSL.remainderFinder(), tag -> tag.update("References", references -> {
|
| 57 | Optional<? extends Dynamic<?>> village = references.get("New_Village").result();
|
| 58 | return DataFixUtils.orElse(village.map(v -> references.remove("New_Village").set("Village", (Dynamic<?>)v)), references).remove("Village");
|
| 59 | }))
|
| 60 | )
|
| 61 | )
|
| 62 | ),
|
| 63 | this.fixTypeEverywhereTyped(
|
| 64 | "NewVillageStartFix",
|
| 65 | structureType,
|
| 66 | input -> input.update(
|
| 67 | DSL.remainderFinder(),
|
| 68 | tag -> tag.update(
|
| 69 | "id", id -> Objects.equals(NamespacedSchema.ensureNamespaced(id.asString("")), "minecraft:new_village") ? id.createString("minecraft:village") : id
|
| 70 | )
|
| 71 | )
|
| 72 | )
|
| 73 | );
|
| 74 | }
|
| 75 | }
|
| 76 |
|