| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.mojang.datafixers.DSL;
|
| 5 | import com.mojang.datafixers.DataFix;
|
| 6 | import com.mojang.datafixers.DataFixUtils;
|
| 7 | import com.mojang.datafixers.FieldFinder;
|
| 8 | import com.mojang.datafixers.OpticFinder;
|
| 9 | import com.mojang.datafixers.TypeRewriteRule;
|
| 10 | import com.mojang.datafixers.schemas.Schema;
|
| 11 | import com.mojang.datafixers.types.Type;
|
| 12 | import com.mojang.datafixers.types.templates.CompoundList.CompoundListType;
|
| 13 | import com.mojang.datafixers.util.Either;
|
| 14 | import com.mojang.datafixers.util.Pair;
|
| 15 | import com.mojang.datafixers.util.Unit;
|
| 16 | import com.mojang.serialization.Dynamic;
|
| 17 | import java.util.List;
|
| 18 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 19 |
|
| 20 | public class MissingDimensionFix extends DataFix {
|
| 21 | public MissingDimensionFix(final Schema schema, final boolean changesType) {
|
| 22 | super(schema, changesType);
|
| 23 | }
|
| 24 |
|
| 25 | protected static <A> Type<Pair<A, Dynamic<?>>> fields(final String name, final Type<A> type) {
|
| 26 | return DSL.and(DSL.field(name, type), DSL.remainderType());
|
| 27 | }
|
| 28 |
|
| 29 | protected static <A> Type<Pair<Either<A, Unit>, Dynamic<?>>> optionalFields(final String name, final Type<A> type) {
|
| 30 | return DSL.and(DSL.optional(DSL.field(name, type)), DSL.remainderType());
|
| 31 | }
|
| 32 |
|
| 33 | protected static <A1, A2> Type<Pair<Either<A1, Unit>, Pair<Either<A2, Unit>, Dynamic<?>>>> optionalFields(
|
| 34 | final String name1, final Type<A1> type1, final String name2, final Type<A2> type2
|
| 35 | ) {
|
| 36 | return DSL.and(DSL.optional(DSL.field(name1, type1)), DSL.optional(DSL.field(name2, type2)), DSL.remainderType());
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected TypeRewriteRule makeRule() {
|
| 41 | Schema schema = this.getInputSchema();
|
| 42 | Type<?> generatorType = DSL.taggedChoiceType(
|
| 43 | "type",
|
| 44 | DSL.string(),
|
| 45 | ImmutableMap.of(
|
| 46 | "minecraft:debug",
|
| 47 | DSL.remainderType(),
|
| 48 | "minecraft:flat",
|
| 49 | flatType(schema),
|
| 50 | "minecraft:noise",
|
| 51 | optionalFields(
|
| 52 | "biome_source",
|
| 53 | DSL.taggedChoiceType(
|
| 54 | "type",
|
| 55 | DSL.string(),
|
| 56 | ImmutableMap.of(
|
| 57 | "minecraft:fixed",
|
| 58 | fields("biome", schema.getType(References.BIOME)),
|
| 59 | "minecraft:multi_noise",
|
| 60 | DSL.list(fields("biome", schema.getType(References.BIOME))),
|
| 61 | "minecraft:checkerboard",
|
| 62 | fields("biomes", DSL.list(schema.getType(References.BIOME))),
|
| 63 | "minecraft:vanilla_layered",
|
| 64 | DSL.remainderType(),
|
| 65 | "minecraft:the_end",
|
| 66 | DSL.remainderType()
|
| 67 | )
|
| 68 | ),
|
| 69 | "settings",
|
| 70 | DSL.or(DSL.string(), optionalFields("default_block", schema.getType(References.BLOCK_NAME), "default_fluid", schema.getType(References.BLOCK_NAME)))
|
| 71 | )
|
| 72 | )
|
| 73 | );
|
| 74 | CompoundListType<String, ?> dimensionsType = DSL.compoundList(NamespacedSchema.namespacedString(), fields("generator", generatorType));
|
| 75 | Type<?> expectedDimensionsType = DSL.and(dimensionsType, DSL.remainderType());
|
| 76 | Type<?> settings = schema.getType(References.WORLD_GEN_SETTINGS);
|
| 77 | FieldFinder<?> dimensionsFinder = new FieldFinder<>("dimensions", expectedDimensionsType);
|
| 78 | if (!settings.findFieldType("dimensions").equals(expectedDimensionsType)) {
|
| 79 | throw new IllegalStateException();
|
| 80 | }
|
| 81 |
|
| 82 | OpticFinder<? extends List<? extends Pair<String, ?>>> dimensionListFinder = dimensionsType.finder();
|
| 83 | return this.fixTypeEverywhereTyped(
|
| 84 | "MissingDimensionFix", settings, input -> input.updateTyped(dimensionsFinder, dimensions -> dimensions.updateTyped(dimensionListFinder, generators -> {
|
| 85 | if (!(generators.getValue() instanceof List)) {
|
| 86 | throw new IllegalStateException("List exptected");
|
| 87 | } else if (((List)generators.getValue()).isEmpty()) {
|
| 88 | Dynamic<?> tag = input.get(DSL.remainderFinder());
|
| 89 | Dynamic<?> newDimensions = this.recreateSettings(tag);
|
| 90 | return DataFixUtils.orElse(dimensionsType.readTyped(newDimensions).result().map(Pair::getFirst), generators);
|
| 91 | } else {
|
| 92 | return generators;
|
| 93 | }
|
| 94 | }))
|
| 95 | );
|
| 96 | }
|
| 97 |
|
| 98 | protected static Type<? extends Pair<? extends Either<? extends Pair<? extends Either<?, Unit>, ? extends Pair<? extends Either<? extends List<? extends Pair<? extends Either<?, Unit>, Dynamic<?>>>, Unit>, Dynamic<?>>>, Unit>, Dynamic<?>>> flatType(
|
| 99 | final Schema schema
|
| 100 | ) {
|
| 101 | return optionalFields(
|
| 102 | "settings", optionalFields("biome", schema.getType(References.BIOME), "layers", DSL.list(optionalFields("block", schema.getType(References.BLOCK_NAME))))
|
| 103 | );
|
| 104 | }
|
| 105 |
|
| 106 | private <T> Dynamic<T> recreateSettings(final Dynamic<T> tag) {
|
| 107 | long seed = tag.get("seed").asLong(0L);
|
| 108 | return new Dynamic<>(tag.getOps(), WorldGenSettingsFix.vanillaLevels(tag, seed, WorldGenSettingsFix.defaultOverworld(tag, seed), false));
|
| 109 | }
|
| 110 | }
|
| 111 |
|