44 lines
1.9 KiB
Java
44 lines
1.9 KiB
Java
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<String> opticFinder = DSL.fieldFinder("id", NamespacedSchema.namespacedString());
|
|
OpticFinder<String> opticFinder2 = (OpticFinder<String>)type.findField("CustomName");
|
|
Type<?> type3 = type2.findFieldType("CustomName");
|
|
return this.fixTypeEverywhereTyped("EntityCustomNameToComponentFix", type, type2, typed -> fixEntity(typed, opticFinder, opticFinder2, type3));
|
|
}
|
|
|
|
private static <T> Typed<?> fixEntity(Typed<?> data, OpticFinder<String> customNameOptic, OpticFinder<String> idOptic, Type<T> 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 <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);
|
|
}
|
|
}
|