| 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.OpticFinder;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.Typed;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.datafixers.types.Type;
|
| 10 | import com.mojang.datafixers.util.Either;
|
| 11 | import com.mojang.datafixers.util.Pair;
|
| 12 | import com.mojang.datafixers.util.Unit;
|
| 13 | import com.mojang.serialization.DataResult;
|
| 14 | import com.mojang.serialization.Dynamic;
|
| 15 | import com.mojang.serialization.DynamicOps;
|
| 16 | import com.mojang.serialization.MapLike;
|
| 17 | import java.util.function.Function;
|
| 18 |
|
| 19 | public class ChunkRenamesFix extends DataFix {
|
| 20 | public ChunkRenamesFix(final Schema outputSchema) {
|
| 21 | super(outputSchema, true);
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | protected TypeRewriteRule makeRule() {
|
| 26 | Type<?> chunkType = this.getInputSchema().getType(References.CHUNK);
|
| 27 | OpticFinder<?> levelFinder = chunkType.findField("Level");
|
| 28 | OpticFinder<?> structureFinder = levelFinder.type().findField("Structures");
|
| 29 | Type<?> newChunkType = this.getOutputSchema().getType(References.CHUNK);
|
| 30 | Type<?> newStructuresType = newChunkType.findFieldType("structures");
|
| 31 | return this.fixTypeEverywhereTyped("Chunk Renames; purge Level-tag", chunkType, newChunkType, chunk -> {
|
| 32 | Typed<?> level = chunk.getTyped(levelFinder);
|
| 33 | Typed<?> chunkTyped = appendChunkName(level);
|
| 34 | chunkTyped = chunkTyped.set(DSL.remainderFinder(), mergeRemainders(chunk, level.get(DSL.remainderFinder())));
|
| 35 | chunkTyped = renameField(chunkTyped, "TileEntities", "block_entities");
|
| 36 | chunkTyped = renameField(chunkTyped, "TileTicks", "block_ticks");
|
| 37 | chunkTyped = renameField(chunkTyped, "Entities", "entities");
|
| 38 | chunkTyped = renameField(chunkTyped, "Sections", "sections");
|
| 39 | chunkTyped = chunkTyped.updateTyped(structureFinder, newStructuresType, structure -> renameField(structure, "Starts", "starts"));
|
| 40 | chunkTyped = renameField(chunkTyped, "Structures", "structures");
|
| 41 | return chunkTyped.update(DSL.remainderFinder(), remainder -> remainder.remove("Level"));
|
| 42 | });
|
| 43 | }
|
| 44 |
|
| 45 | private static Typed<?> renameField(final Typed<?> input, final String oldName, final String newName) {
|
| 46 | return renameFieldHelper(input, oldName, newName, input.getType().findFieldType(oldName)).update(DSL.remainderFinder(), tag -> tag.remove(oldName));
|
| 47 | }
|
| 48 |
|
| 49 | private static <A> Typed<?> renameFieldHelper(final Typed<?> input, final String oldName, final String newName, final Type<A> fieldType) {
|
| 50 | Type<Either<A, Unit>> oldType = DSL.optional(DSL.field(oldName, fieldType));
|
| 51 | Type<Either<A, Unit>> newType = DSL.optional(DSL.field(newName, fieldType));
|
| 52 | return input.update(oldType.finder(), newType, Function.identity());
|
| 53 | }
|
| 54 |
|
| 55 | private static <A> Typed<Pair<String, A>> appendChunkName(final Typed<A> input) {
|
| 56 | return new Typed<>(DSL.named("chunk", input.getType()), input.getOps(), Pair.of("chunk", input.getValue()));
|
| 57 | }
|
| 58 |
|
| 59 | private static <T> Dynamic<T> mergeRemainders(final Typed<?> chunk, final Dynamic<T> levelRemainder) {
|
| 60 | DynamicOps<T> ops = levelRemainder.getOps();
|
| 61 | Dynamic<T> chunkRemainder = chunk.get(DSL.remainderFinder()).convert(ops);
|
| 62 | DataResult<T> toMap = ops.getMap(levelRemainder.getValue()).flatMap(map -> ops.mergeToMap(chunkRemainder.getValue(), (MapLike<T>)map));
|
| 63 | return toMap.result().map(v -> new Dynamic<>(ops, (T)v)).orElse(levelRemainder);
|
| 64 | }
|
| 65 | }
|
| 66 |
|