| 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.Typed;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.datafixers.types.Type;
|
| 10 | import com.mojang.datafixers.util.Pair;
|
| 11 | import java.util.Optional;
|
| 12 | import net.minecraft.util.Util;
|
| 13 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 14 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 15 |
|
| 16 | public class UnfilledBuriedTreasureMapFix extends DataFix {
|
| 17 | private static final String NEW_TRANSLATION = "item.minecraft.buried_treasure_map";
|
| 18 | private static final String OLD_TRANSLATION = "filled_map.buried_treasure";
|
| 19 |
|
| 20 | public UnfilledBuriedTreasureMapFix(final Schema outputSchema) {
|
| 21 | super(outputSchema, false);
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | protected TypeRewriteRule makeRule() {
|
| 26 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 27 | OpticFinder<Pair<String, String>> idFinder = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
|
| 28 | OpticFinder<?> componentsFinder = itemStackType.findField("components");
|
| 29 | return this.fixTypeEverywhereTyped(
|
| 30 | "UnfilledBuriedTreasureMapFix",
|
| 31 | itemStackType,
|
| 32 | itemStack -> {
|
| 33 | Optional<String> id = itemStack.getOptional(idFinder).map(Pair::getSecond);
|
| 34 | return id.filter(itemId -> itemId.equals("minecraft:map")).isPresent()
|
| 35 | ? itemStack.updateTyped(componentsFinder, UnfilledBuriedTreasureMapFix::fix)
|
| 36 | : itemStack;
|
| 37 | }
|
| 38 | );
|
| 39 | }
|
| 40 |
|
| 41 | private static <T> Typed<T> fix(final Typed<T> typed) {
|
| 42 | return Util.writeAndReadTypedOrThrow(
|
| 43 | typed,
|
| 44 | typed.getType(),
|
| 45 | value -> {
|
| 46 | boolean holdsLegacyName = value.get("minecraft:item_name")
|
| 47 | .result()
|
| 48 | .flatMap(ExtraDataFixUtils::getPlainTranslationKey)
|
| 49 | .filter(nameKey -> nameKey.equals("filled_map.buried_treasure"))
|
| 50 | .isPresent();
|
| 51 | return holdsLegacyName
|
| 52 | ? value.update("minecraft:item_name", itemName -> itemName.set("translate", itemName.createString("item.minecraft.buried_treasure_map")))
|
| 53 | : value;
|
| 54 | }
|
| 55 | );
|
| 56 | }
|
| 57 | }
|
| 58 |
|