Closed D0gmaDev closed 1 year ago
Hi, this happens because the legacy color codes don't actually support hex colors. Bukkit has implemented their own logic for creating chat components from such strings, which is why it works in sendMessage
and similar methods from Bukkit. Since you're now the one creating the component, this parsing is never done and the client ends up with a different chat component than expected.
// This is what you end up with:
BungeeComponentWrapper wrapper = new BungeeComponentWrapper(new BaseComponent[]{new TranslatableComponent("2")});
BungeeComponentWrapper localized = wrapper.localized("test-lang");
ComponentSerializer.toString(localized); // {"components":[{"extra":[{"text":"§x§4§e§6§6§5§4colored"}],"text":""}]}
// This is what you expect:
BaseComponent[] legacyParsed = TextComponent.fromLegacyText(coloredString2);
ComponentSerializer.toString(legacyParsed); // {"color":"#4e6654","text":"colored"}
The second string is red because the last character of the hex code is 4
, which is the legacy color code for dark red.
Instead of using legacy color codes, you should set the color in the chat components themselves:
String coloredString1 = "purple";
String coloredString2 = "colored";
String coloredString3 = "gray";
/* [...] */
Item translateItem = new SimpleItem(new ItemBuilder(Material.BOOK).addLoreLines(
new ComponentBuilder(new TranslatableComponent("1")).color(ChatColor.DARK_PURPLE).create(),
new ComponentBuilder(new TranslatableComponent("2")).color(ChatColor.of("#4e6654")).create(),
new ComponentBuilder(new TranslatableComponent("3")).color(ChatColor.GRAY).create()
));
I'd also recommend you to use adventure text components. This library is built into Paper, but can also be used in Bukkit plugins. Their components are much easier to work with. The only downside is that you'll need to manually wrap those components in AdventureComponentWrapper
before passing them to any InvUI method. (This is to retain compatibility with Spigot)
Hi again @NichtStudioCode Thank you for your explainations, now I pefectly get why it's doing that. However, I'm afraid it remains a problem for me if the inventory's implementation doesn't know in advance what color(s) the translation needs to be.
Currently I have string like that in a file:
casier_reason=<c:#4e6654>Reason: §7%s
and some colorize method before passing it to the language HashMap
public static String colorize(String source) {
return COLOR_PATTERN.matcher(source).replaceAll(matchResult -> ChatColor.of("#" + matchResult.group(1)).toString());
}
Maybe I can somehow replicate the way sendMessage() converts colors ?
UPDATE: Looking in the TextComponent#fromLegacyText
, maybe I just need to replicate that ?
I've added ComponentLocalizer#setComponentCreator
in v1.1
.
You should now be able to use Bukkit's legacy format using this:
BungeeComponentLocalizer.getInstance().setComponentCreator((text) -> {
BaseComponent[] components = TextComponent.fromLegacyText(text);
TextComponent outer = new TextComponent();
for (BaseComponent child : components) {
outer.addExtra(child);
}
return outer;
});
Hi, while changing my String litterals to translated components, I faced this unexpected behaviour regarding the translation of custom colors, as they appear wrongly (InvUI 1.0 & Spigot 1.19)
Here is my test code:
I expected to get two similar items, but this is the result:
In this example it appears red, but for other custom colors it might appear white or any other color. However, the two
sendMessage()
produce expected results. Thank you for your help