| 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.schemas.Schema;
|
| 8 | import com.mojang.datafixers.types.Type;
|
| 9 | import com.mojang.datafixers.types.templates.TaggedChoice.TaggedChoiceType;
|
| 10 | import java.util.Map;
|
| 11 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 12 |
|
| 13 | public class StatsRenameFix extends DataFix {
|
| 14 | private final String name;
|
| 15 | private final Map<String, String> renames;
|
| 16 |
|
| 17 | public StatsRenameFix(final Schema outputSchema, final String name, final Map<String, String> renames) {
|
| 18 | super(outputSchema, false);
|
| 19 | this.name = name;
|
| 20 | this.renames = renames;
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | protected TypeRewriteRule makeRule() {
|
| 25 | return TypeRewriteRule.seq(this.createStatRule(), this.createCriteriaRule());
|
| 26 | }
|
| 27 |
|
| 28 | private TypeRewriteRule createCriteriaRule() {
|
| 29 | Type<?> outputType = this.getOutputSchema().getType(References.OBJECTIVE);
|
| 30 | Type<?> inputType = this.getInputSchema().getType(References.OBJECTIVE);
|
| 31 | OpticFinder<?> criteriaTypeFinder = inputType.findField("CriteriaType");
|
| 32 | TaggedChoiceType<?> choiceType = criteriaTypeFinder.type()
|
| 33 | .findChoiceType("type", -1)
|
| 34 | .orElseThrow(() -> new IllegalStateException("Can't find choice type for criteria"));
|
| 35 | Type<?> customFieldType = choiceType.types().get("minecraft:custom");
|
| 36 | if (customFieldType == null) {
|
| 37 | throw new IllegalStateException("Failed to find custom criterion type variant");
|
| 38 | }
|
| 39 |
|
| 40 | OpticFinder<?> customTypeFinder = DSL.namedChoice("minecraft:custom", customFieldType);
|
| 41 | OpticFinder<String> idFinder = DSL.fieldFinder("id", NamespacedSchema.namespacedString());
|
| 42 | return this.fixTypeEverywhereTyped(
|
| 43 | this.name,
|
| 44 | inputType,
|
| 45 | outputType,
|
| 46 | input -> input.updateTyped(
|
| 47 | criteriaTypeFinder, stats -> stats.updateTyped(customTypeFinder, custom -> custom.update(idFinder, value -> this.renames.getOrDefault(value, value)))
|
| 48 | )
|
| 49 | );
|
| 50 | }
|
| 51 |
|
| 52 | private TypeRewriteRule createStatRule() {
|
| 53 | Type<?> outputType = this.getOutputSchema().getType(References.STATS);
|
| 54 | Type<?> inputType = this.getInputSchema().getType(References.STATS);
|
| 55 | OpticFinder<?> statsFinder = inputType.findField("stats");
|
| 56 | OpticFinder<?> customFinder = statsFinder.type().findField("minecraft:custom");
|
| 57 | OpticFinder<String> nameFinder = NamespacedSchema.namespacedString().finder();
|
| 58 | return this.fixTypeEverywhereTyped(
|
| 59 | this.name,
|
| 60 | inputType,
|
| 61 | outputType,
|
| 62 | input -> input.updateTyped(
|
| 63 | statsFinder, stats -> stats.updateTyped(customFinder, custom -> custom.update(nameFinder, value -> this.renames.getOrDefault(value, value)))
|
| 64 | )
|
| 65 | );
|
| 66 | }
|
| 67 | }
|
| 68 |
|