| 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.TypeRewriteRule;
|
| 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 java.util.Locale;
|
| 11 | import java.util.Objects;
|
| 12 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 13 |
|
| 14 | public abstract class SimplestEntityRenameFix extends DataFix {
|
| 15 | private final String name;
|
| 16 |
|
| 17 | public SimplestEntityRenameFix(final String name, final Schema outputSchema, final boolean changesType) {
|
| 18 | super(outputSchema, changesType);
|
| 19 | this.name = name;
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | public TypeRewriteRule makeRule() {
|
| 24 | TaggedChoiceType<String> oldType = (TaggedChoiceType<String>)this.getInputSchema().findChoiceType(References.ENTITY);
|
| 25 | TaggedChoiceType<String> newType = (TaggedChoiceType<String>)this.getOutputSchema().findChoiceType(References.ENTITY);
|
| 26 | Type<Pair<String, String>> entityNameType = DSL.named(References.ENTITY_NAME.typeName(), NamespacedSchema.namespacedString());
|
| 27 | if (!Objects.equals(this.getOutputSchema().getType(References.ENTITY_NAME), entityNameType)) {
|
| 28 | throw new IllegalStateException("Entity name type is not what was expected.");
|
| 29 | } else {
|
| 30 | return TypeRewriteRule.seq(this.fixTypeEverywhere(this.name, oldType, newType, ops -> input -> input.mapFirst(oldName -> {
|
| 31 | String newName = this.rename(oldName);
|
| 32 | Type<?> oldEntityType = oldType.types().get(oldName);
|
| 33 | Type<?> newEntityType = newType.types().get(newName);
|
| 34 | if (!newEntityType.equals(oldEntityType, true, true)) {
|
| 35 | throw new IllegalStateException(String.format(Locale.ROOT, "Dynamic type check failed: %s not equal to %s", newEntityType, oldEntityType));
|
| 36 | } else {
|
| 37 | return newName;
|
| 38 | }
|
| 39 | })), this.fixTypeEverywhere(this.name + " for entity name", entityNameType, ops -> input -> input.mapSecond(this::rename)));
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | protected abstract String rename(final String name);
|
| 44 | }
|
| 45 |
|