| 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.util.Either;
|
| 9 | import com.mojang.datafixers.util.Pair;
|
| 10 | import java.util.Objects;
|
| 11 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 12 |
|
| 13 | public class BlockNameFlatteningFix extends DataFix {
|
| 14 | public BlockNameFlatteningFix(final Schema outputSchema, final boolean changesType) {
|
| 15 | super(outputSchema, changesType);
|
| 16 | }
|
| 17 |
|
| 18 | @Override
|
| 19 | public TypeRewriteRule makeRule() {
|
| 20 | Type<?> blockType = this.getInputSchema().getType(References.BLOCK_NAME);
|
| 21 | Type<?> newBlockType = this.getOutputSchema().getType(References.BLOCK_NAME);
|
| 22 | Type<Pair<String, Either<Integer, String>>> expectedBlockType = DSL.named(
|
| 23 | References.BLOCK_NAME.typeName(), DSL.or(DSL.intType(), NamespacedSchema.namespacedString())
|
| 24 | );
|
| 25 | Type<Pair<String, String>> expectedNewBlockType = DSL.named(References.BLOCK_NAME.typeName(), NamespacedSchema.namespacedString());
|
| 26 | if (Objects.equals(blockType, expectedBlockType) && Objects.equals(newBlockType, expectedNewBlockType)) {
|
| 27 | return this.fixTypeEverywhere(
|
| 28 | "BlockNameFlatteningFix",
|
| 29 | expectedBlockType,
|
| 30 | expectedNewBlockType,
|
| 31 | ops -> block -> block.mapSecond(
|
| 32 | choice -> choice.map(BlockStateData::upgradeBlock, name -> BlockStateData.upgradeBlock(NamespacedSchema.ensureNamespaced(name)))
|
| 33 | )
|
| 34 | );
|
| 35 | } else {
|
| 36 | throw new IllegalStateException("Expected and actual types don't match.");
|
| 37 | }
|
| 38 | }
|
| 39 | }
|
| 40 |
|