package net.minecraft.util.datafix.fixes; import com.mojang.datafixers.DSL; import com.mojang.datafixers.DataFix; import com.mojang.datafixers.OpticFinder; import com.mojang.datafixers.TypeRewriteRule; import com.mojang.datafixers.Typed; import com.mojang.datafixers.schemas.Schema; import com.mojang.datafixers.types.Type; import com.mojang.serialization.Dynamic; import com.mojang.serialization.DynamicOps; import net.minecraft.Util; import net.minecraft.util.datafix.LegacyComponentDataFixUtils; import net.minecraft.util.datafix.schemas.NamespacedSchema; public class EntityCustomNameToComponentFix extends DataFix { public EntityCustomNameToComponentFix(Schema outputSchema) { super(outputSchema, true); } @Override public TypeRewriteRule makeRule() { Type type = this.getInputSchema().getType(References.ENTITY); Type type2 = this.getOutputSchema().getType(References.ENTITY); OpticFinder opticFinder = DSL.fieldFinder("id", NamespacedSchema.namespacedString()); OpticFinder opticFinder2 = (OpticFinder)type.findField("CustomName"); Type type3 = type2.findFieldType("CustomName"); return this.fixTypeEverywhereTyped("EntityCustomNameToComponentFix", type, type2, typed -> fixEntity(typed, opticFinder, opticFinder2, type3)); } private static Typed fixEntity(Typed data, OpticFinder customNameOptic, OpticFinder idOptic, Type newType) { return data.update(idOptic, newType, string -> { String string2 = (String)data.getOptional(customNameOptic).orElse(""); Dynamic dynamic = fixCustomName(data.getOps(), string, string2); return Util.readTypedOrThrow(newType, dynamic).getValue(); }); } private static Dynamic fixCustomName(DynamicOps ops, String customName, String id) { return "minecraft:commandblock_minecart".equals(id) ? new Dynamic<>(ops, ops.createString(customName)) : LegacyComponentDataFixUtils.createPlainTextComponent(ops, customName); } }