| 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.DSL.TypeReference;
|
| 9 | import com.mojang.datafixers.schemas.Schema;
|
| 10 | import com.mojang.datafixers.types.Type;
|
| 11 | import com.mojang.serialization.Dynamic;
|
| 12 | import net.minecraft.util.Util;
|
| 13 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 14 |
|
| 15 | public abstract class NamedEntityWriteReadFix extends DataFix {
|
| 16 | private final String name;
|
| 17 | private final String entityName;
|
| 18 | private final TypeReference type;
|
| 19 |
|
| 20 | public NamedEntityWriteReadFix(final Schema outputSchema, final boolean changesType, final String name, final TypeReference type, final String entityName) {
|
| 21 | super(outputSchema, changesType);
|
| 22 | this.name = name;
|
| 23 | this.type = type;
|
| 24 | this.entityName = entityName;
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public TypeRewriteRule makeRule() {
|
| 29 | Type<?> inputEntityType = this.getInputSchema().getType(this.type);
|
| 30 | Type<?> inputEntityChoiceType = this.getInputSchema().getChoiceType(this.type, this.entityName);
|
| 31 | Type<?> outputEntityType = this.getOutputSchema().getType(this.type);
|
| 32 | OpticFinder<?> entityF = DSL.namedChoice(this.entityName, inputEntityChoiceType);
|
| 33 | Type<?> patchedEntityType = ExtraDataFixUtils.patchSubType(inputEntityType, inputEntityType, outputEntityType);
|
| 34 | return this.fix(inputEntityType, outputEntityType, patchedEntityType, entityF);
|
| 35 | }
|
| 36 |
|
| 37 | private <S, T, A> TypeRewriteRule fix(
|
| 38 | final Type<S> inputEntityType, final Type<T> outputEntityType, final Type<?> patchedEntityType, final OpticFinder<A> choiceFinder
|
| 39 | ) {
|
| 40 | return this.fixTypeEverywhereTyped(this.name, inputEntityType, outputEntityType, typed -> {
|
| 41 | if (typed.getOptional(choiceFinder).isEmpty()) {
|
| 42 | return ExtraDataFixUtils.cast(outputEntityType, typed);
|
| 43 | }
|
| 44 |
|
| 45 | Typed<?> fakeTyped = ExtraDataFixUtils.cast(patchedEntityType, typed);
|
| 46 | return Util.writeAndReadTypedOrThrow(fakeTyped, outputEntityType, this::fix);
|
| 47 | });
|
| 48 | }
|
| 49 |
|
| 50 | protected abstract <T> Dynamic<T> fix(final Dynamic<T> input);
|
| 51 | }
|
| 52 |
|