| 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.DSL.TypeReference;
|
| 6 | import com.mojang.datafixers.schemas.Schema;
|
| 7 | import com.mojang.datafixers.types.templates.TaggedChoice.TaggedChoiceType;
|
| 8 | import java.util.Locale;
|
| 9 |
|
| 10 | public class AddNewChoices extends DataFix {
|
| 11 | private final String name;
|
| 12 | private final TypeReference type;
|
| 13 |
|
| 14 | public AddNewChoices(final Schema outputSchema, final String name, final TypeReference type) {
|
| 15 | super(outputSchema, true);
|
| 16 | this.name = name;
|
| 17 | this.type = type;
|
| 18 | }
|
| 19 |
|
| 20 | @Override
|
| 21 | public TypeRewriteRule makeRule() {
|
| 22 | TaggedChoiceType<?> inputType = this.getInputSchema().findChoiceType(this.type);
|
| 23 | TaggedChoiceType<?> outputType = this.getOutputSchema().findChoiceType(this.type);
|
| 24 | return this.cap(inputType, outputType);
|
| 25 | }
|
| 26 |
|
| 27 | private <K> TypeRewriteRule cap(final TaggedChoiceType<K> inputType, final TaggedChoiceType<?> outputType) {
|
| 28 | if (inputType.getKeyType() != outputType.getKeyType()) {
|
| 29 | throw new IllegalStateException("Could not inject: key type is not the same");
|
| 30 | }
|
| 31 |
|
| 32 | TaggedChoiceType<K> outputChoiceType = (TaggedChoiceType<K>)outputType;
|
| 33 | return this.fixTypeEverywhere(this.name, inputType, outputChoiceType, ops -> input -> {
|
| 34 | if (!outputChoiceType.hasType(input.getFirst())) {
|
| 35 | throw new IllegalArgumentException(String.format(Locale.ROOT, "%s: Unknown type %s in '%s'", this.name, input.getFirst(), this.type.typeName()));
|
| 36 | } else {
|
| 37 | return input;
|
| 38 | }
|
| 39 | });
|
| 40 | }
|
| 41 | }
|
| 42 |
|