| 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 com.mojang.serialization.OptionalDynamic;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.Set;
|
| 14 | import java.util.function.Predicate;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import net.minecraft.util.datafix.LegacyComponentDataFixUtils;
|
| 17 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 18 |
|
| 19 | public class ItemStackCustomNameToOverrideComponentFix extends DataFix {
|
| 20 | private static final Set<String> MAP_NAMES = Set.of(
|
| 21 | "filled_map.buried_treasure",
|
| 22 | "filled_map.explorer_jungle",
|
| 23 | "filled_map.explorer_swamp",
|
| 24 | "filled_map.mansion",
|
| 25 | "filled_map.monument",
|
| 26 | "filled_map.trial_chambers",
|
| 27 | "filled_map.village_desert",
|
| 28 | "filled_map.village_plains",
|
| 29 | "filled_map.village_savanna",
|
| 30 | "filled_map.village_snowy",
|
| 31 | "filled_map.village_taiga"
|
| 32 | );
|
| 33 |
|
| 34 | public ItemStackCustomNameToOverrideComponentFix(final Schema outputSchema) {
|
| 35 | super(outputSchema, false);
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public final TypeRewriteRule makeRule() {
|
| 40 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 41 | OpticFinder<Pair<String, String>> idFinder = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
|
| 42 | OpticFinder<?> componentsFinder = itemStackType.findField("components");
|
| 43 | return this.fixTypeEverywhereTyped(
|
| 44 | "ItemStack custom_name to item_name component fix",
|
| 45 | itemStackType,
|
| 46 | input -> {
|
| 47 | Optional<Pair<String, String>> id = input.getOptional(idFinder);
|
| 48 | Optional<String> maybeId = id.map(Pair::getSecond);
|
| 49 | if (maybeId.filter(s -> s.equals("minecraft:white_banner")).isPresent()) {
|
| 50 | return input.updateTyped(componentsFinder, ItemStackCustomNameToOverrideComponentFix::fixBanner);
|
| 51 | } else {
|
| 52 | return maybeId.filter(s -> s.equals("minecraft:filled_map")).isPresent()
|
| 53 | ? input.updateTyped(componentsFinder, ItemStackCustomNameToOverrideComponentFix::fixMap)
|
| 54 | : input;
|
| 55 | }
|
| 56 | }
|
| 57 | );
|
| 58 | }
|
| 59 |
|
| 60 | private static <T> Typed<T> fixMap(final Typed<T> value) {
|
| 61 | return fixCustomName(value, MAP_NAMES::contains);
|
| 62 | }
|
| 63 |
|
| 64 | private static <T> Typed<T> fixBanner(final Typed<T> value) {
|
| 65 | return fixCustomName(value, e -> e.equals("block.minecraft.ominous_banner"));
|
| 66 | }
|
| 67 |
|
| 68 | private static <T> Typed<T> fixCustomName(final Typed<T> typed, final Predicate<String> expectedTranslationKey) {
|
| 69 | return Util.writeAndReadTypedOrThrow(
|
| 70 | typed,
|
| 71 | typed.getType(),
|
| 72 | value -> {
|
| 73 | OptionalDynamic<?> customNameTag = value.get("minecraft:custom_name");
|
| 74 | Optional<String> hasCorrectTranslationKey = customNameTag.asString()
|
| 75 | .result()
|
| 76 | .flatMap(LegacyComponentDataFixUtils::extractTranslationString)
|
| 77 | .filter(expectedTranslationKey);
|
| 78 | return hasCorrectTranslationKey.isPresent() ? value.renameField("minecraft:custom_name", "minecraft:item_name") : value;
|
| 79 | }
|
| 80 | );
|
| 81 | }
|
| 82 | }
|
| 83 |
|