| 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.OpticFinder;
|
| 7 | import com.mojang.datafixers.TypeRewriteRule;
|
| 8 | import com.mojang.datafixers.Typed;
|
| 9 | import com.mojang.datafixers.schemas.Schema;
|
| 10 | import com.mojang.datafixers.types.Type;
|
| 11 | import com.mojang.serialization.Dynamic;
|
| 12 | import com.mojang.serialization.OptionalDynamic;
|
| 13 | import java.util.HashSet;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Map;
|
| 16 | import java.util.Set;
|
| 17 | import java.util.function.UnaryOperator;
|
| 18 | import net.minecraft.util.Util;
|
| 19 |
|
| 20 | public class TooltipDisplayComponentFix extends DataFix {
|
| 21 | private static final List<String> CONVERTED_ADDITIONAL_TOOLTIP_TYPES = List.of(
|
| 22 | "minecraft:banner_patterns",
|
| 23 | "minecraft:bees",
|
| 24 | "minecraft:block_entity_data",
|
| 25 | "minecraft:block_state",
|
| 26 | "minecraft:bundle_contents",
|
| 27 | "minecraft:charged_projectiles",
|
| 28 | "minecraft:container",
|
| 29 | "minecraft:container_loot",
|
| 30 | "minecraft:firework_explosion",
|
| 31 | "minecraft:fireworks",
|
| 32 | "minecraft:instrument",
|
| 33 | "minecraft:map_id",
|
| 34 | "minecraft:painting/variant",
|
| 35 | "minecraft:pot_decorations",
|
| 36 | "minecraft:potion_contents",
|
| 37 | "minecraft:tropical_fish/pattern",
|
| 38 | "minecraft:written_book_content"
|
| 39 | );
|
| 40 |
|
| 41 | public TooltipDisplayComponentFix(final Schema outputSchema) {
|
| 42 | super(outputSchema, true);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | protected TypeRewriteRule makeRule() {
|
| 47 | Type<?> componentsType = this.getInputSchema().getType(References.DATA_COMPONENTS);
|
| 48 | Type<?> newComponentsType = this.getOutputSchema().getType(References.DATA_COMPONENTS);
|
| 49 | OpticFinder<?> canPlaceOnFinder = componentsType.findField("minecraft:can_place_on");
|
| 50 | OpticFinder<?> canBreakFinder = componentsType.findField("minecraft:can_break");
|
| 51 | Type<?> newCanPlaceOnType = newComponentsType.findFieldType("minecraft:can_place_on");
|
| 52 | Type<?> newCanBreakType = newComponentsType.findFieldType("minecraft:can_break");
|
| 53 | return this.fixTypeEverywhereTyped(
|
| 54 | "TooltipDisplayComponentFix", componentsType, newComponentsType, typed -> fix(typed, canPlaceOnFinder, canBreakFinder, newCanPlaceOnType, newCanBreakType)
|
| 55 | );
|
| 56 | }
|
| 57 |
|
| 58 | private static Typed<?> fix(
|
| 59 | Typed<?> typed, final OpticFinder<?> canPlaceOnFinder, final OpticFinder<?> canBreakFinder, final Type<?> newCanPlaceOnType, final Type<?> newCanBreakType
|
| 60 | ) {
|
| 61 | Set<String> hiddenTooltips = new HashSet<>();
|
| 62 | typed = fixAdventureModePredicate(typed, canPlaceOnFinder, newCanPlaceOnType, "minecraft:can_place_on", hiddenTooltips);
|
| 63 | typed = fixAdventureModePredicate(typed, canBreakFinder, newCanBreakType, "minecraft:can_break", hiddenTooltips);
|
| 64 | return typed.update(
|
| 65 | DSL.remainderFinder(),
|
| 66 | remainder -> {
|
| 67 | remainder = fixSimpleComponent(remainder, "minecraft:trim", hiddenTooltips);
|
| 68 | remainder = fixSimpleComponent(remainder, "minecraft:unbreakable", hiddenTooltips);
|
| 69 | remainder = fixComponentAndUnwrap(remainder, "minecraft:dyed_color", "rgb", hiddenTooltips);
|
| 70 | remainder = fixComponentAndUnwrap(remainder, "minecraft:attribute_modifiers", "modifiers", hiddenTooltips);
|
| 71 | remainder = fixComponentAndUnwrap(remainder, "minecraft:enchantments", "levels", hiddenTooltips);
|
| 72 | remainder = fixComponentAndUnwrap(remainder, "minecraft:stored_enchantments", "levels", hiddenTooltips);
|
| 73 | remainder = fixComponentAndUnwrap(remainder, "minecraft:jukebox_playable", "song", hiddenTooltips);
|
| 74 | boolean hideTooltip = remainder.get("minecraft:hide_tooltip").result().isPresent();
|
| 75 | remainder = remainder.remove("minecraft:hide_tooltip");
|
| 76 | boolean hideAdditionalTooltip = remainder.get("minecraft:hide_additional_tooltip").result().isPresent();
|
| 77 | remainder = remainder.remove("minecraft:hide_additional_tooltip");
|
| 78 | if (hideAdditionalTooltip) {
|
| 79 | for (String componentId : CONVERTED_ADDITIONAL_TOOLTIP_TYPES) {
|
| 80 | if (remainder.get(componentId).result().isPresent()) {
|
| 81 | hiddenTooltips.add(componentId);
|
| 82 | }
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | return hiddenTooltips.isEmpty() && !hideTooltip
|
| 87 | ? remainder
|
| 88 | : remainder.set(
|
| 89 | "minecraft:tooltip_display",
|
| 90 | remainder.createMap(
|
| 91 | Map.of(
|
| 92 | remainder.createString("hide_tooltip"),
|
| 93 | remainder.createBoolean(hideTooltip),
|
| 94 | remainder.createString("hidden_components"),
|
| 95 | remainder.createList(hiddenTooltips.stream().map(remainder::createString))
|
| 96 | )
|
| 97 | )
|
| 98 | );
|
| 99 | }
|
| 100 | );
|
| 101 | }
|
| 102 |
|
| 103 | private static Dynamic<?> fixSimpleComponent(final Dynamic<?> remainder, final String componentId, final Set<String> hiddenTooltips) {
|
| 104 | return fixRemainderComponent(remainder, componentId, hiddenTooltips, UnaryOperator.identity());
|
| 105 | }
|
| 106 |
|
| 107 | private static Dynamic<?> fixComponentAndUnwrap(final Dynamic<?> remainder, final String componentId, final String fieldName, final Set<String> hiddenTooltips) {
|
| 108 | return fixRemainderComponent(remainder, componentId, hiddenTooltips, component -> DataFixUtils.orElse(component.get(fieldName).result(), component));
|
| 109 | }
|
| 110 |
|
| 111 | private static Dynamic<?> fixRemainderComponent(
|
| 112 | final Dynamic<?> remainder, final String componentId, final Set<String> hiddenTooltips, final UnaryOperator<Dynamic<?>> fixer
|
| 113 | ) {
|
| 114 | return remainder.update(componentId, component -> {
|
| 115 | boolean showInTooltip = component.get("show_in_tooltip").asBoolean(true);
|
| 116 | if (!showInTooltip) {
|
| 117 | hiddenTooltips.add(componentId);
|
| 118 | }
|
| 119 |
|
| 120 | return fixer.apply(component.remove("show_in_tooltip"));
|
| 121 | });
|
| 122 | }
|
| 123 |
|
| 124 | private static Typed<?> fixAdventureModePredicate(
|
| 125 | final Typed<?> typedComponents, final OpticFinder<?> componentFinder, final Type<?> newType, final String componentId, final Set<String> hiddenTooltips
|
| 126 | ) {
|
| 127 | return typedComponents.updateTyped(componentFinder, newType, typedComponent -> Util.writeAndReadTypedOrThrow(typedComponent, newType, component -> {
|
| 128 | OptionalDynamic<?> predicates = component.get("predicates");
|
| 129 | if (predicates.result().isEmpty()) {
|
| 130 | return component;
|
| 131 | }
|
| 132 |
|
| 133 | boolean showInTooltip = component.get("show_in_tooltip").asBoolean(true);
|
| 134 | if (!showInTooltip) {
|
| 135 | hiddenTooltips.add(componentId);
|
| 136 | }
|
| 137 |
|
| 138 | return predicates.result().get();
|
| 139 | }));
|
| 140 | }
|
| 141 | }
|
| 142 |
|