EntityCustomNameToComponentFix.java

net.minecraft.util.datafix.fixes.EntityCustomNameToComponentFix

信息

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

    TODO

字段/常量

内部类/嵌套类型

构造器

public EntityCustomNameToComponentFix(Schema outputSchema) @ L19

  • 构造器名:EntityCustomNameToComponentFix
  • 源码定位:L19
  • 修饰符:public

参数:

  • outputSchema: Schema

说明:

TODO

方法

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

public TypeRewriteRule makeRule() @ L23

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

参数:

说明:

TODO

private static <T> Typed<?> fixEntity(Typed<?> entity, Type<?> newEntityType, OpticFinder<String> idF, OpticFinder<String> customNameF, Type<T> newCustomNameType) @ L35

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

参数:

  • entity: Typed<?>
  • newEntityType: Type<?>
  • idF: OpticFinder
  • customNameF: OpticFinder
  • newCustomNameType: Type

说明:

TODO

private static <T> Dynamic<T> fixCustomName(DynamicOps<T> ops, String customName, String id) @ L50

  • 方法名:fixCustomName
  • 源码定位:L50
  • 返回类型: Dynamic
  • 修饰符:private static

参数:

  • ops: DynamicOps
  • customName: String
  • id: String

说明:

TODO

代码

public class EntityCustomNameToComponentFix extends DataFix {
    public EntityCustomNameToComponentFix(Schema outputSchema) {
        super(outputSchema, true);
    }
 
    @Override
    public TypeRewriteRule makeRule() {
        Type<?> entityType = this.getInputSchema().getType(References.ENTITY);
        Type<?> newEntityType = this.getOutputSchema().getType(References.ENTITY);
        OpticFinder<String> idF = DSL.fieldFinder("id", NamespacedSchema.namespacedString());
        OpticFinder<String> customNameF = (OpticFinder<String>)entityType.findField("CustomName");
        Type<?> newCustomNameType = newEntityType.findFieldType("CustomName");
        return this.fixTypeEverywhereTyped(
            "EntityCustomNameToComponentFix", entityType, newEntityType, entity -> fixEntity(entity, newEntityType, idF, customNameF, newCustomNameType)
        );
    }
 
    private static <T> Typed<?> fixEntity(
        Typed<?> entity, Type<?> newEntityType, OpticFinder<String> idF, OpticFinder<String> customNameF, Type<T> newCustomNameType
    ) {
        Optional<String> customName = entity.getOptional(customNameF);
        if (customName.isEmpty()) {
            return ExtraDataFixUtils.cast(newEntityType, entity);
        } else if (customName.get().isEmpty()) {
            return Util.writeAndReadTypedOrThrow(entity, newEntityType, dynamic -> dynamic.remove("CustomName"));
        } else {
            String id = entity.getOptional(idF).orElse("");
            Dynamic<?> component = fixCustomName(entity.getOps(), customName.get(), id);
            return entity.set(customNameF, Util.readTypedOrThrow(newCustomNameType, component));
        }
    }
 
    private static <T> Dynamic<T> fixCustomName(DynamicOps<T> ops, String customName, String id) {
        return "minecraft:commandblock_minecart".equals(id)
            ? new Dynamic<>(ops, ops.createString(customName))
            : LegacyComponentDataFixUtils.createPlainTextComponent(ops, customName);
    }
}

引用的其他类

  • Schema

    • 引用位置: 参数
  • Util

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

    • 引用位置: 方法调用
    • 关联成员: ExtraDataFixUtils.cast()
  • LegacyComponentDataFixUtils

    • 引用位置: 方法调用
    • 关联成员: LegacyComponentDataFixUtils.createPlainTextComponent()
  • NamespacedSchema

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

    • 引用位置: 返回值