| 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.types.templates.List.ListType;
|
| 11 | import com.mojang.datafixers.util.Either;
|
| 12 | import com.mojang.datafixers.util.Pair;
|
| 13 | import com.mojang.datafixers.util.Unit;
|
| 14 | import com.mojang.serialization.Dynamic;
|
| 15 | import com.mojang.serialization.OptionalDynamic;
|
| 16 | import java.util.Objects;
|
| 17 | import java.util.Optional;
|
| 18 | import java.util.Set;
|
| 19 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | public class MergeTerrainChunkStatusFix extends DataFix {
|
| 23 | private static final String BEFORE_TERRAIN_STATUS = "minecraft:biomes";
|
| 24 | private static final String OLD_FINAL_TERRAIN_STATUS = "minecraft:carvers";
|
| 25 | private static final Set<String> OLD_INTERMEDIATE_TERRAIN_STATUSES = Set.of("minecraft:noise", "minecraft:surface");
|
| 26 |
|
| 27 | public MergeTerrainChunkStatusFix(final Schema schema) {
|
| 28 | super(schema, false);
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | protected TypeRewriteRule makeRule() {
|
| 33 | Type<?> chunkType = this.getInputSchema().getType(References.CHUNK);
|
| 34 | OpticFinder<?> sectionsF = chunkType.findField("sections");
|
| 35 | Type<?> sectionType = ((ListType)sectionsF.type()).getElement();
|
| 36 | Type<?> biomesType = sectionType.findFieldType("biomes");
|
| 37 | Type<?> blockStatesType = sectionType.findFieldType("block_states");
|
| 38 | return this.makeRule(chunkType, sectionsF, sectionType, biomesType, blockStatesType);
|
| 39 | }
|
| 40 |
|
| 41 | private <Biomes, BlockStates> TypeRewriteRule makeRule(
|
| 42 | final Type<?> chunkType, final OpticFinder<?> sectionsF, final Type<?> sectionType, final Type<Biomes> biomesType, final Type<BlockStates> blockStatesType
|
| 43 | ) {
|
| 44 | Type<Pair<Either<Biomes, Unit>, Pair<Either<BlockStates, Unit>, Dynamic<?>>>> expectedSectionType = DSL.and(
|
| 45 | DSL.optional(DSL.field("biomes", biomesType)), DSL.optional(DSL.field("block_states", blockStatesType)), DSL.remainderType()
|
| 46 | );
|
| 47 | if (!Objects.equals(sectionType, expectedSectionType)) {
|
| 48 | throw new IllegalStateException(sectionType + " did not match " + expectedSectionType);
|
| 49 | }
|
| 50 |
|
| 51 | OpticFinder<Pair<Either<Biomes, Unit>, Pair<Either<BlockStates, Unit>, Dynamic<?>>>> sectionF = DSL.typeFinder(expectedSectionType);
|
| 52 | return this.fixTypeEverywhereTyped("MergeTerrainChunkStatusFix", chunkType, chunk -> {
|
| 53 | MergeTerrainChunkStatusFix.FixedAndStatuses statuses = this.fixStatuses(chunk);
|
| 54 | chunk = statuses.fixed;
|
| 55 | boolean statusIntermediate = OLD_INTERMEDIATE_TERRAIN_STATUSES.contains(statuses.oldStatus);
|
| 56 | boolean targetStatusIntermediate = statuses.oldTargetStatus != null && OLD_INTERMEDIATE_TERRAIN_STATUSES.contains(statuses.oldTargetStatus);
|
| 57 | if (statusIntermediate || targetStatusIntermediate) {
|
| 58 | boolean onlyRemoveBlocksBelowZero = statuses.oldTargetStatus != null && !targetStatusIntermediate;
|
| 59 | chunk = removeBlockStates(chunk, sectionsF, sectionF, onlyRemoveBlocksBelowZero);
|
| 60 | }
|
| 61 |
|
| 62 | return chunk;
|
| 63 | });
|
| 64 | }
|
| 65 |
|
| 66 | private static <Biomes, BlockStates> Typed<?> removeBlockStates(
|
| 67 | final Typed<?> chunk,
|
| 68 | final OpticFinder<?> sectionsF,
|
| 69 | final OpticFinder<Pair<Either<Biomes, Unit>, Pair<Either<BlockStates, Unit>, Dynamic<?>>>> sectionF,
|
| 70 | final boolean onlyBelowZero
|
| 71 | ) {
|
| 72 | return chunk.update(DSL.remainderFinder(), remainder -> remainder.remove("Heightmaps").remove("blending_data"))
|
| 73 | .updateTyped(
|
| 74 | sectionsF,
|
| 75 | sections -> sections.update(
|
| 76 | sectionF,
|
| 77 | section -> {
|
| 78 | byte y = section.getSecond().getSecond().get("Y").asByte((byte)0);
|
| 79 | return y >= 0 && onlyBelowZero
|
| 80 | ? section
|
| 81 | : section.mapSecond(
|
| 82 | blockStatesAndRemainder -> blockStatesAndRemainder.<Either<BlockStates, Unit>>mapFirst(var0x -> Either.right(Unit.INSTANCE))
|
| 83 | .mapSecond(sectionRemainder -> sectionRemainder.remove("block_states"))
|
| 84 | );
|
| 85 | }
|
| 86 | )
|
| 87 | );
|
| 88 | }
|
| 89 |
|
| 90 | private MergeTerrainChunkStatusFix.FixedAndStatuses fixStatuses(final Typed<?> chunk) {
|
| 91 | Dynamic<?> remainder = chunk.getOrCreate(DSL.remainderFinder());
|
| 92 | String oldStatus = unwrapStatus(remainder.get("Status"));
|
| 93 | remainder = remainder.set("Status", remainder.createString(fixStatus(oldStatus)));
|
| 94 | Optional<? extends Dynamic<?>> maybeBelowZeroRetrogen = remainder.get("below_zero_retrogen").result();
|
| 95 | String oldTargetStatus;
|
| 96 | if (maybeBelowZeroRetrogen.isPresent()) {
|
| 97 | Dynamic<?> belowZeroRetrogen = (Dynamic<?>)maybeBelowZeroRetrogen.get();
|
| 98 | oldTargetStatus = unwrapStatus(belowZeroRetrogen.get("target_status"));
|
| 99 | belowZeroRetrogen = belowZeroRetrogen.set("target_status", remainder.createString(fixStatus(oldTargetStatus)));
|
| 100 | remainder = remainder.set("below_zero_retrogen", belowZeroRetrogen);
|
| 101 | } else {
|
| 102 | oldTargetStatus = null;
|
| 103 | }
|
| 104 |
|
| 105 | return new MergeTerrainChunkStatusFix.FixedAndStatuses(chunk.set(DSL.remainderFinder(), remainder), oldStatus, oldTargetStatus);
|
| 106 | }
|
| 107 |
|
| 108 | private static String unwrapStatus(final OptionalDynamic<?> status) {
|
| 109 | return NamespacedSchema.ensureNamespaced(status.asString("minecraft:empty"));
|
| 110 | }
|
| 111 |
|
| 112 | private static String fixStatus(final String status) {
|
| 113 | if ("minecraft:carvers".equals(status)) {
|
| 114 | return "minecraft:terrain";
|
| 115 | } else {
|
| 116 | return OLD_INTERMEDIATE_TERRAIN_STATUSES.contains(status) ? "minecraft:biomes" : status;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | private record FixedAndStatuses(Typed<?> fixed, String oldStatus, @Nullable String oldTargetStatus) {
|
| 121 | }
|
| 122 | }
|
| 123 |
|