TooltipDisplayComponentFix.java

net.minecraft.util.datafix.fixes.TooltipDisplayComponentFix

信息

  • 全限定名:net.minecraft.util.datafix.fixes.TooltipDisplayComponentFix
  • 类型:public class
  • 包:net.minecraft.util.datafix.fixes
  • 源码路径:src/main/java/net/minecraft/util/datafix/fixes/TooltipDisplayComponentFix.java
  • 起始行号:L20
  • 继承:DataFix
  • 职责:

    TODO

字段/常量

  • CONVERTED_ADDITIONAL_TOOLTIP_TYPES
    • 类型: List<String>
    • 修饰符: private static final
    • 源码定位: L21
    • 说明:

      TODO

内部类/嵌套类型

构造器

public TooltipDisplayComponentFix(Schema outputSchema) @ L41

  • 构造器名:TooltipDisplayComponentFix
  • 源码定位:L41
  • 修饰符:public

参数:

  • outputSchema: Schema

说明:

TODO

方法

下面的方法块按源码顺序生成。

protected TypeRewriteRule makeRule() @ L45

  • 方法名:makeRule
  • 源码定位:L45
  • 返回类型:TypeRewriteRule
  • 修饰符:protected

参数:

说明:

TODO

private static Typed<?> fix(Typed<?> typed, OpticFinder<?> canPlaceOnFinder, OpticFinder<?> canBreakFinder, Type<?> newCanPlaceOnType, Type<?> newCanBreakType) @ L61

  • 方法名:fix
  • 源码定位:L61
  • 返回类型:Typed<?>
  • 修饰符:private static

参数:

  • typed: Typed<?>
  • canPlaceOnFinder: OpticFinder<?>
  • canBreakFinder: OpticFinder<?>
  • newCanPlaceOnType: Type<?>
  • newCanBreakType: Type<?>

说明:

TODO

private static Dynamic<?> fixSimpleComponent(Dynamic<?> remainder, String componentId, Set<String> hiddenTooltips) @ L106

  • 方法名:fixSimpleComponent
  • 源码定位:L106
  • 返回类型:Dynamic<?>
  • 修饰符:private static

参数:

  • remainder: Dynamic<?>
  • componentId: String
  • hiddenTooltips: Set

说明:

TODO

private static Dynamic<?> fixComponentAndUnwrap(Dynamic<?> remainder, String componentId, String fieldName, Set<String> hiddenTooltips) @ L110

  • 方法名:fixComponentAndUnwrap
  • 源码定位:L110
  • 返回类型:Dynamic<?>
  • 修饰符:private static

参数:

  • remainder: Dynamic<?>
  • componentId: String
  • fieldName: String
  • hiddenTooltips: Set

说明:

TODO

private static Dynamic<?> fixRemainderComponent(Dynamic<?> remainder, String componentId, Set<String> hiddenTooltips, UnaryOperator<Dynamic<?>> fixer) @ L114

  • 方法名:fixRemainderComponent
  • 源码定位:L114
  • 返回类型:Dynamic<?>
  • 修饰符:private static

参数:

  • remainder: Dynamic<?>
  • componentId: String
  • hiddenTooltips: Set
  • fixer: UnaryOperator<Dynamic<?>>

说明:

TODO

private static Typed<?> fixAdventureModePredicate(Typed<?> typedComponents, OpticFinder<?> componentFinder, Type<?> newType, String componentId, Set<String> hiddenTooltips) @ L125

  • 方法名:fixAdventureModePredicate
  • 源码定位:L125
  • 返回类型:Typed<?>
  • 修饰符:private static

参数:

  • typedComponents: Typed<?>
  • componentFinder: OpticFinder<?>
  • newType: Type<?>
  • componentId: String
  • hiddenTooltips: Set

说明:

TODO

代码

public class TooltipDisplayComponentFix extends DataFix {
    private static final List<String> CONVERTED_ADDITIONAL_TOOLTIP_TYPES = List.of(
        "minecraft:banner_patterns",
        "minecraft:bees",
        "minecraft:block_entity_data",
        "minecraft:block_state",
        "minecraft:bundle_contents",
        "minecraft:charged_projectiles",
        "minecraft:container",
        "minecraft:container_loot",
        "minecraft:firework_explosion",
        "minecraft:fireworks",
        "minecraft:instrument",
        "minecraft:map_id",
        "minecraft:painting/variant",
        "minecraft:pot_decorations",
        "minecraft:potion_contents",
        "minecraft:tropical_fish/pattern",
        "minecraft:written_book_content"
    );
 
    public TooltipDisplayComponentFix(Schema outputSchema) {
        super(outputSchema, true);
    }
 
    @Override
    protected TypeRewriteRule makeRule() {
        Type<?> componentsType = this.getInputSchema().getType(References.DATA_COMPONENTS);
        Type<?> newComponentsType = this.getOutputSchema().getType(References.DATA_COMPONENTS);
        OpticFinder<?> canPlaceOnFinder = componentsType.findField("minecraft:can_place_on");
        OpticFinder<?> canBreakFinder = componentsType.findField("minecraft:can_break");
        Type<?> newCanPlaceOnType = newComponentsType.findFieldType("minecraft:can_place_on");
        Type<?> newCanBreakType = newComponentsType.findFieldType("minecraft:can_break");
        return this.fixTypeEverywhereTyped(
            "TooltipDisplayComponentFix",
            componentsType,
            newComponentsType,
            typed -> fix(typed, canPlaceOnFinder, canBreakFinder, newCanPlaceOnType, newCanBreakType)
        );
    }
 
    private static Typed<?> fix(
        Typed<?> typed, OpticFinder<?> canPlaceOnFinder, OpticFinder<?> canBreakFinder, Type<?> newCanPlaceOnType, Type<?> newCanBreakType
    ) {
        Set<String> hiddenTooltips = new HashSet<>();
        typed = fixAdventureModePredicate(typed, canPlaceOnFinder, newCanPlaceOnType, "minecraft:can_place_on", hiddenTooltips);
        typed = fixAdventureModePredicate(typed, canBreakFinder, newCanBreakType, "minecraft:can_break", hiddenTooltips);
        return typed.update(
            DSL.remainderFinder(),
            remainder -> {
                remainder = fixSimpleComponent(remainder, "minecraft:trim", hiddenTooltips);
                remainder = fixSimpleComponent(remainder, "minecraft:unbreakable", hiddenTooltips);
                remainder = fixComponentAndUnwrap(remainder, "minecraft:dyed_color", "rgb", hiddenTooltips);
                remainder = fixComponentAndUnwrap(remainder, "minecraft:attribute_modifiers", "modifiers", hiddenTooltips);
                remainder = fixComponentAndUnwrap(remainder, "minecraft:enchantments", "levels", hiddenTooltips);
                remainder = fixComponentAndUnwrap(remainder, "minecraft:stored_enchantments", "levels", hiddenTooltips);
                remainder = fixComponentAndUnwrap(remainder, "minecraft:jukebox_playable", "song", hiddenTooltips);
                boolean hideTooltip = remainder.get("minecraft:hide_tooltip").result().isPresent();
                remainder = remainder.remove("minecraft:hide_tooltip");
                boolean hideAdditionalTooltip = remainder.get("minecraft:hide_additional_tooltip").result().isPresent();
                remainder = remainder.remove("minecraft:hide_additional_tooltip");
                if (hideAdditionalTooltip) {
                    for (String componentId : CONVERTED_ADDITIONAL_TOOLTIP_TYPES) {
                        if (remainder.get(componentId).result().isPresent()) {
                            hiddenTooltips.add(componentId);
                        }
                    }
                }
 
                return hiddenTooltips.isEmpty() && !hideTooltip
                    ? remainder
                    : remainder.set(
                        "minecraft:tooltip_display",
                        remainder.createMap(
                            Map.of(
                                remainder.createString("hide_tooltip"),
                                remainder.createBoolean(hideTooltip),
                                remainder.createString("hidden_components"),
                                remainder.createList(hiddenTooltips.stream().map(remainder::createString))
                            )
                        )
                    );
            }
        );
    }
 
    private static Dynamic<?> fixSimpleComponent(Dynamic<?> remainder, String componentId, Set<String> hiddenTooltips) {
        return fixRemainderComponent(remainder, componentId, hiddenTooltips, UnaryOperator.identity());
    }
 
    private static Dynamic<?> fixComponentAndUnwrap(Dynamic<?> remainder, String componentId, String fieldName, Set<String> hiddenTooltips) {
        return fixRemainderComponent(remainder, componentId, hiddenTooltips, component -> DataFixUtils.orElse(component.get(fieldName).result(), component));
    }
 
    private static Dynamic<?> fixRemainderComponent(Dynamic<?> remainder, String componentId, Set<String> hiddenTooltips, UnaryOperator<Dynamic<?>> fixer) {
        return remainder.update(componentId, component -> {
            boolean showInTooltip = component.get("show_in_tooltip").asBoolean(true);
            if (!showInTooltip) {
                hiddenTooltips.add(componentId);
            }
 
            return fixer.apply(component.remove("show_in_tooltip"));
        });
    }
 
    private static Typed<?> fixAdventureModePredicate(
        Typed<?> typedComponents, OpticFinder<?> componentFinder, Type<?> newType, String componentId, Set<String> hiddenTooltips
    ) {
        return typedComponents.updateTyped(componentFinder, newType, typedComponent -> Util.writeAndReadTypedOrThrow(typedComponent, newType, component -> {
            OptionalDynamic<?> predicates = component.get("predicates");
            if (predicates.result().isEmpty()) {
                return component;
            } else {
                boolean showInTooltip = component.get("show_in_tooltip").asBoolean(true);
                if (!showInTooltip) {
                    hiddenTooltips.add(componentId);
                }
 
                return predicates.result().get();
            }
        }));
    }
}

引用的其他类

  • Schema

    • 引用位置: 参数
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.writeAndReadTypedOrThrow()
  • ResolvableProfile

    • 引用位置: 参数/返回值