| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DataFix;
|
| 4 | import com.mojang.datafixers.TypeRewriteRule;
|
| 5 | import com.mojang.datafixers.Typed;
|
| 6 | import com.mojang.datafixers.schemas.Schema;
|
| 7 | import com.mojang.datafixers.types.Type;
|
| 8 | import com.mojang.datafixers.types.templates.TaggedChoice.TaggedChoiceType;
|
| 9 | import com.mojang.datafixers.util.Pair;
|
| 10 | import com.mojang.serialization.DynamicOps;
|
| 11 | import java.util.Locale;
|
| 12 | import java.util.function.Function;
|
| 13 | import net.minecraft.util.Util;
|
| 14 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 15 |
|
| 16 | public abstract class EntityRenameFix extends DataFix {
|
| 17 | protected final String name;
|
| 18 |
|
| 19 | public EntityRenameFix(final String name, final Schema outputSchema, final boolean changesType) {
|
| 20 | super(outputSchema, changesType);
|
| 21 | this.name = name;
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public TypeRewriteRule makeRule() {
|
| 26 | TaggedChoiceType<String> oldType = (TaggedChoiceType<String>)this.getInputSchema().findChoiceType(References.ENTITY);
|
| 27 | TaggedChoiceType<String> newType = (TaggedChoiceType<String>)this.getOutputSchema().findChoiceType(References.ENTITY);
|
| 28 | Function<String, Type<?>> patchedInputTypes = Util.memoize(name -> {
|
| 29 | Type<?> type = oldType.types().get(name);
|
| 30 | return ExtraDataFixUtils.patchSubType(type, oldType, newType);
|
| 31 | });
|
| 32 | return this.fixTypeEverywhere(this.name, oldType, newType, ops -> input -> {
|
| 33 | String oldName = input.getFirst();
|
| 34 | Type<?> oldEntityType = patchedInputTypes.apply(oldName);
|
| 35 | Pair<String, Typed<?>> newEntity = this.fix(oldName, this.getEntity(input.getSecond(), ops, oldEntityType));
|
| 36 | Type<?> expectedType = newType.types().get(newEntity.getFirst());
|
| 37 | if (!expectedType.equals(newEntity.getSecond().getType(), true, true)) {
|
| 38 | throw new IllegalStateException(String.format(Locale.ROOT, "Dynamic type check failed: %s not equal to %s", expectedType, newEntity.getSecond().getType()));
|
| 39 | } else {
|
| 40 | return Pair.of(newEntity.getFirst(), newEntity.getSecond().getValue());
|
| 41 | }
|
| 42 | });
|
| 43 | }
|
| 44 |
|
| 45 | private <A> Typed<A> getEntity(final Object input, final DynamicOps<?> ops, final Type<A> oldEntityType) {
|
| 46 | return new Typed<>(oldEntityType, ops, (A)input);
|
| 47 | }
|
| 48 |
|
| 49 | protected abstract Pair<String, Typed<?>> fix(final String name, final Typed<?> entity);
|
| 50 | }
|
| 51 |
|