How to get unformatted string from text

Ok, so I generate some formatted text and I store it in the inventory title. But when I want to parse that inventory title, normal string operations don’t work, as it’s been changed by coloring it.

Is there a way to get plain string from a formatted string?

Use Text.toPlain()

It doesn’t work

If I do this
System.out.println("Player just tried to click on the following inventory: \""+ Text.builder(e.getTargetInventory().getName().get()).style(TextStyles.RESET).build().toPlain() +"\"!");
the output is formatted (in the same way input is)
copyshot
and then the String#contains() doesn’t work properly

I personally had this issue not too long ago. Depending on how the text is built (typically with the chat colour codes) it won’t be able to become plain even after the toPlain function.

I got around this issue by calling it to plain, and then stripping the colour codes from the String output. As shown in my code below (I had the benefit of using a compatibility layer between sponge and Bukkit so thats why its needing to be references in two classes)

As you are probably not using a compatibility layer you can simply use a for loop.

String plain = text.toPlain();
for(TextColor colour : Sponge.getRegistery().getAllOf(TextColor.class)){
    plain = plain.replace(color.toString(), "");
}

Edit:
Oh and also the TextSerializer.deserialize(text) doesnt work either, i tried a lot of things, this was the only way i got it to work.

Thanks for your extensive reply, I’ll try them out and will respond!

1 Like

Hmm… somehow the last piece of code doesn’t do anything…

My current code and the response

@Listener
public void onInventoryClicked(ClickInventoryEvent e) {
    String title = e.getTargetInventory().getName().get();
    for(TextColor colour : Sponge.getRegistry().getAllOf(TextColor.class)){
        System.out.println("replacing "+colour.toString());
        title = title.replace(colour.toString(), "");
    }
    System.out.println("Player just tried to click on the following inventory: \""+ title +"\"!");
}

Response:

[19:08:35 INFO] [STDOUT]: replacing dark_red
[19:08:35 INFO] [STDOUT]: replacing green
[19:08:35 INFO] [STDOUT]: replacing dark_green
[19:08:35 INFO] [STDOUT]: replacing black
[19:08:35 INFO] [STDOUT]: replacing yellow
[19:08:35 INFO] [STDOUT]: replacing NONE
[19:08:35 INFO] [STDOUT]: replacing dark_blue
[19:08:35 INFO] [STDOUT]: replacing dark_purple
[19:08:35 INFO] [STDOUT]: replacing gold
[19:08:35 INFO] [STDOUT]: replacing red
[19:08:35 INFO] [STDOUT]: replacing aqua
[19:08:35 INFO] [STDOUT]: replacing gray
[19:08:35 INFO] [STDOUT]: replacing light_purple
[19:08:35 INFO] [STDOUT]: replacing blue
[19:08:35 INFO] [STDOUT]: replacing white
[19:08:35 INFO] [STDOUT]: replacing dark_aqua
[19:08:35 INFO] [STDOUT]: replacing dark_gray
[19:08:35 INFO] [STDOUT]: replacing reset
[19:08:35 INFO] [STDOUT]: Player just tried to click on the following inventory: "test-842"

copyshot

Ah the Text.toString() didnt work how i thought. I thought it would bring up the chat colour codes, instead it brings up the name.

You will need to use your own list of the chat colours and remove them. Here is a list of the chat colour codes (its the first code)

https://www.digminecraft.com/lists/color_list_pc.php

1 Like

Are you sure those chat colors are defined by “§” and a hex character? Wouldn’t we be able to see “§” in the output?

At least that’s how it works in spigot

Thanks, will try that anyways!

Yep. Thats how i did it with my compatibility layer. If minecraft detects a valid chat code it will change the following text into the colour and remove the code. Thats how it works in regular mc and craftbukkit (by extention spigot, paper, etc)

1 Like

It works flawlessly! Thanks!
copyshot

1 Like