package net.minecraft.util.datafix.fixes; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import com.mojang.datafixers.DataFix; import com.mojang.datafixers.TypeRewriteRule; import com.mojang.datafixers.schemas.Schema; import com.mojang.datafixers.types.Type; import com.mojang.datafixers.util.Pair; import com.mojang.logging.LogUtils; import com.mojang.serialization.Dynamic; import com.mojang.serialization.DynamicOps; import com.mojang.serialization.JsonOps; import net.minecraft.Util; import org.slf4j.Logger; public class UnflattenTextComponentFix extends DataFix { private static final Logger LOGGER = LogUtils.getLogger(); public UnflattenTextComponentFix(Schema outputSchema) { super(outputSchema, true); } @Override protected TypeRewriteRule makeRule() { Type> type = (Type>)this.getInputSchema().getType(References.TEXT_COMPONENT); Type type2 = this.getOutputSchema().getType(References.TEXT_COMPONENT); return this.createFixer(type, type2); } private TypeRewriteRule createFixer(Type> inputType, Type outputType) { return this.fixTypeEverywhere( "UnflattenTextComponentFix", inputType, outputType, dynamicOps -> pair -> Util.readTypedOrThrow(outputType, unflattenJson(dynamicOps, (String)pair.getSecond()), true).getValue() ); } private static Dynamic unflattenJson(DynamicOps ops, String json) { try { JsonElement jsonElement = JsonParser.parseString(json); if (!jsonElement.isJsonNull()) { return new Dynamic<>(ops, JsonOps.INSTANCE.convertTo(ops, jsonElement)); } } catch (Exception var3) { LOGGER.error("Failed to unflatten text component json: {}", json, var3); } return new Dynamic<>(ops, ops.createString(json)); } }