| 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.DataFixUtils;
|
| 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.util.Pair;
|
| 10 | import com.mojang.serialization.Dynamic;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.function.Function;
|
| 14 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 15 |
|
| 16 | public abstract class LegacyBlockRenameFix extends DataFix {
|
| 17 | private final String name;
|
| 18 |
|
| 19 | public LegacyBlockRenameFix(final Schema outputSchema, final String name) {
|
| 20 | super(outputSchema, false);
|
| 21 | this.name = name;
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public TypeRewriteRule makeRule() {
|
| 26 | Type<?> blockType = this.getInputSchema().getType(References.BLOCK_NAME);
|
| 27 | Type<Pair<String, String>> expectedType = DSL.named(References.BLOCK_NAME.typeName(), NamespacedSchema.namespacedString());
|
| 28 | if (!Objects.equals(blockType, expectedType)) {
|
| 29 | throw new IllegalStateException("block type is not what was expected.");
|
| 30 | }
|
| 31 |
|
| 32 | TypeRewriteRule blockRule = this.fixTypeEverywhere(this.name + " for block", expectedType, ops -> input -> input.mapSecond(this::renameBlock));
|
| 33 | TypeRewriteRule blockStateRule = this.fixTypeEverywhereTyped(
|
| 34 | this.name + " for block_state", this.getInputSchema().getType(References.BLOCK_STATE), input -> input.update(DSL.remainderFinder(), this::fixBlockState)
|
| 35 | );
|
| 36 | TypeRewriteRule flatBlockStateRule = this.fixTypeEverywhereTyped(
|
| 37 | this.name + " for flat_block_state",
|
| 38 | this.getInputSchema().getType(References.FLAT_BLOCK_STATE),
|
| 39 | input -> input.update(DSL.remainderFinder(), tag -> DataFixUtils.orElse(tag.asString().result().map(this::fixFlatBlockState).map(tag::createString), tag))
|
| 40 | );
|
| 41 | return TypeRewriteRule.seq(blockRule, blockStateRule, flatBlockStateRule);
|
| 42 | }
|
| 43 |
|
| 44 | private Dynamic<?> fixBlockState(final Dynamic<?> tag) {
|
| 45 | Optional<String> name = tag.get("Name").asString().result();
|
| 46 | return name.isPresent() ? tag.set("Name", tag.createString(this.renameBlock(name.get()))) : tag;
|
| 47 | }
|
| 48 |
|
| 49 | private String fixFlatBlockState(final String string) {
|
| 50 | int startProperties = string.indexOf(91);
|
| 51 | int startNbt = string.indexOf(123);
|
| 52 | int end = string.length();
|
| 53 | if (startProperties > 0) {
|
| 54 | end = startProperties;
|
| 55 | }
|
| 56 |
|
| 57 | if (startNbt > 0) {
|
| 58 | end = Math.min(end, startNbt);
|
| 59 | }
|
| 60 |
|
| 61 | String name = string.substring(0, end);
|
| 62 | String newName = this.renameBlock(name);
|
| 63 | return newName + string.substring(end);
|
| 64 | }
|
| 65 |
|
| 66 | protected abstract String renameBlock(String block);
|
| 67 |
|
| 68 | public static DataFix create(final Schema outputSchema, final String name, final Function<String, String> renamer) {
|
| 69 | return new LegacyBlockRenameFix(outputSchema, name) {
|
| 70 | @Override
|
| 71 | protected String renameBlock(final String block) {
|
| 72 | return renamer.apply(block);
|
| 73 | }
|
| 74 | };
|
| 75 | }
|
| 76 | }
|
| 77 |
|