#New way to convert a Text object json string to formatted text.

4 messages · Page 1 of 1 (latest)

long mica
#
public static OrderedText getItemInfoFormatted(ItemStack item) {
        Text itemName = item.getName().copy();
        OrderedText formatted;
        Text text1 = Text.Serialization.fromJson("\"" + " - " + (item.getMaxDamage() - item.getDamage()) + "\"", DynamicRegistryManager.EMPTY);
        Text text = Text.Serialization.fromJson("\"" + "\u00a77\u00bb " + "\"",DynamicRegistryManager.EMPTY).append(itemName);

        if(item.isDamageable()) {
            if(item.getDamage() != 0) {

                text = Text.Serialization.fromJson("\"" + "\u00a77\u00bb " + "\"",DynamicRegistryManager.EMPTY).append(itemName).append(text1);
            }
        }
        formatted = text.asOrderedText();
        return formatted;
    }

I had this code on 1.21.4 that would take the json string of Minecraft's Text object and format with colors/styles etc. (Ex the colored text on an item, etc). Whats the new way to do this on 1.21.6?

icy wigeon
#

codec + jsonops probably

#

but why even use serialization in that method, you can do that without

long mica
#

old code, so wasnt sure the best way to do it and it worked fine for my use case