| 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.DynamicOps;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Objects;
|
| 16 | import java.util.Optional;
|
| 17 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 18 |
|
| 19 | public class EntityRidingToPassengersFix extends DataFix {
|
| 20 | public EntityRidingToPassengersFix(final Schema outputSchema, final boolean changesType) {
|
| 21 | super(outputSchema, changesType);
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public TypeRewriteRule makeRule() {
|
| 26 | Schema inputSchema = this.getInputSchema();
|
| 27 | Schema outputSchema = this.getOutputSchema();
|
| 28 | Type<?> oldEntityTreeType = inputSchema.getTypeRaw(References.ENTITY_TREE);
|
| 29 | Type<?> newEntityTreeType = outputSchema.getTypeRaw(References.ENTITY_TREE);
|
| 30 | Type<?> entityType = inputSchema.getTypeRaw(References.ENTITY);
|
| 31 | return this.cap(inputSchema, outputSchema, oldEntityTreeType, newEntityTreeType, entityType);
|
| 32 | }
|
| 33 |
|
| 34 | private <OldEntityTree, NewEntityTree, Entity> TypeRewriteRule cap(
|
| 35 | final Schema inputSchema,
|
| 36 | final Schema outputType,
|
| 37 | final Type<OldEntityTree> oldEntityTreeType,
|
| 38 | final Type<NewEntityTree> newEntityTreeType,
|
| 39 | final Type<Entity> entityType
|
| 40 | ) {
|
| 41 | Type<Pair<String, Pair<Either<OldEntityTree, Unit>, Entity>>> oldType = DSL.named(
|
| 42 | References.ENTITY_TREE.typeName(), DSL.and(DSL.optional(DSL.field("Riding", oldEntityTreeType)), entityType)
|
| 43 | );
|
| 44 | Type<Pair<String, Pair<Either<List<NewEntityTree>, Unit>, Entity>>> newType = DSL.named(
|
| 45 | References.ENTITY_TREE.typeName(), DSL.and(DSL.optional(DSL.field("Passengers", DSL.list(newEntityTreeType))), entityType)
|
| 46 | );
|
| 47 | Type<?> oldEntityType = inputSchema.getType(References.ENTITY_TREE);
|
| 48 | Type<?> newEntityType = outputType.getType(References.ENTITY_TREE);
|
| 49 | if (!Objects.equals(oldEntityType, oldType)) {
|
| 50 | throw new IllegalStateException("Old entity type is not what was expected.");
|
| 51 | }
|
| 52 |
|
| 53 | if (!newEntityType.equals(newType, true, true)) {
|
| 54 | throw new IllegalStateException("New entity type is not what was expected.");
|
| 55 | }
|
| 56 |
|
| 57 | Type<?> patchedEntityTreeType = ExtraDataFixUtils.patchSubType(oldType, oldType, newType);
|
| 58 | OpticFinder<Entity> entityFinder = DSL.typeFinder(entityType);
|
| 59 | OpticFinder<Pair<String, Pair<Either<List<NewEntityTree>, Unit>, Entity>>> newEntityTreeValueFinder = DSL.typeFinder(newType);
|
| 60 | OpticFinder<NewEntityTree> ridingFinder = DSL.fieldFinder("Riding", newEntityTreeType);
|
| 61 | Type<?> oldPlayerType = inputSchema.getType(References.PLAYER);
|
| 62 | Type<?> newPlayerType = outputType.getType(References.PLAYER);
|
| 63 | return TypeRewriteRule.seq(
|
| 64 | this.fixTypeEverywhere(
|
| 65 | "EntityRidingToPassengerFix",
|
| 66 | oldType,
|
| 67 | newType,
|
| 68 | ops -> badlyTypedInput -> {
|
| 69 | Typed<?> input = ExtraDataFixUtils.cast(patchedEntityTreeType, badlyTypedInput, ops);
|
| 70 | Optional<Pair<String, Pair<Either<List<NewEntityTree>, Unit>, Entity>>> maybeRiding = input.getOptionalTyped(ridingFinder)
|
| 71 | .flatMap(t -> t.getOptional(newEntityTreeValueFinder));
|
| 72 | Entity entity = input.getOptional(entityFinder).orElseThrow();
|
| 73 | if (maybeRiding.isEmpty()) {
|
| 74 | Either<List<NewEntityTree>, Unit> passengers = Either.right(Unit.INSTANCE);
|
| 75 | return Pair.of(References.ENTITY_TREE.typeName(), Pair.of(passengers, entity));
|
| 76 | } else {
|
| 77 | return addPassengerToTop(maybeRiding.get(), entity, ops, newEntityTreeType, newEntityTreeValueFinder);
|
| 78 | }
|
| 79 | }
|
| 80 | ),
|
| 81 | this.writeAndRead("player RootVehicle injecter", oldPlayerType, newPlayerType)
|
| 82 | );
|
| 83 | }
|
| 84 |
|
| 85 | private static <Entity, EntityTree> Pair<String, Pair<Either<List<EntityTree>, Unit>, Entity>> addPassengerToTop(
|
| 86 | final Pair<String, Pair<Either<List<EntityTree>, Unit>, Entity>> root,
|
| 87 | final Entity passengerEntity,
|
| 88 | final DynamicOps<?> ops,
|
| 89 | final Type<EntityTree> rawEntityTreeType,
|
| 90 | final OpticFinder<Pair<String, Pair<Either<List<EntityTree>, Unit>, Entity>>> entityTreeFinder
|
| 91 | ) {
|
| 92 | Entity rootEntity = root.getSecond().getSecond();
|
| 93 | Optional<List<EntityTree>> passengers = root.getSecond().getFirst().left();
|
| 94 | Pair<String, Pair<Either<List<EntityTree>, Unit>, Entity>> newPassenger;
|
| 95 | if (passengers.isPresent() && !passengers.get().isEmpty()) {
|
| 96 | Pair<String, Pair<Either<List<EntityTree>, Unit>, Entity>> unwrappedPassenger = unwrapRecursiveValue(
|
| 97 | passengers.get().getFirst(), ops, rawEntityTreeType, entityTreeFinder
|
| 98 | );
|
| 99 | newPassenger = addPassengerToTop(unwrappedPassenger, passengerEntity, ops, rawEntityTreeType, entityTreeFinder);
|
| 100 | } else {
|
| 101 | newPassenger = Pair.of(References.ENTITY_TREE.typeName(), Pair.of(Either.right(Unit.INSTANCE), passengerEntity));
|
| 102 | }
|
| 103 |
|
| 104 | List<EntityTree> newPassengers = List.of(wrapRecursiveValue(newPassenger, ops, rawEntityTreeType, entityTreeFinder));
|
| 105 | return Pair.of(References.ENTITY_TREE.typeName(), Pair.of(Either.left(newPassengers), rootEntity));
|
| 106 | }
|
| 107 |
|
| 108 | private static <Raw, Value> Value unwrapRecursiveValue(final Raw raw, final DynamicOps<?> ops, final Type<Raw> rawType, final OpticFinder<Value> valueFinder) {
|
| 109 | return new Typed<>(rawType, ops, raw).getOptional(valueFinder).orElseThrow();
|
| 110 | }
|
| 111 |
|
| 112 | private static <Raw, Value> Raw wrapRecursiveValue(final Value value, final DynamicOps<?> ops, final Type<Raw> rawType, final OpticFinder<Value> valueFinder) {
|
| 113 | return (Raw)rawType.pointTyped(ops).orElseThrow().set(valueFinder, value).getValue();
|
| 114 | }
|
| 115 | }
|
| 116 |
|