[SPIGOT-3132] TextComponent, colors not working correctly Created: 16/Mar/17 Updated: 19/Mar/17 Resolved: 16/Mar/17 |
|
Status: | Resolved |
Project: | Spigot |
Component/s: | None |
Affects Version/s: | None |
Fix Version/s: | None |
Type: | Bug | Priority: | Minor |
Reporter: | MrFiliper | Assignee: | Unassigned |
Resolution: | Incomplete | Votes: | 0 |
Labels: | 1.11, Chat, ChatColor, TextComponent |
Attachments: |
![]() |
Description |
Create new empty TextComponent rowMessage = new TextComponent(""), after that, add extra to rowMessage with colours (rowMessage.addExtra(....). Send message to player and message not coloured full. |
Comments |
Comment by pokechu22 [ 19/Mar/17 ] |
What part of the message do you want to add a hover message to? You can't directly add a hover message to a normal string, currently. But the sample you gave doesn't have a clean way of adding a hover message either. Once my PR is finished, the game will automatically add the standard hover message (as found on vanilla servers) with the player's UUID and the ability to PM them by clicking their name (if you use %1$s in the format string). Other hover messages won't be automatically usable, but the same framework can be added to the general component library. Also, one alternative is to use TextMessage.fromLegacyText if you need to parse non-JSON data from configs. |
Comment by MrFiliper [ 18/Mar/17 ] |
Ok, but how can I add hover message to string? I must create and parse JSON message from config? Its that right way? |
Comment by pokechu22 [ 18/Mar/17 ] |
Yes, that'd do it. If you're going to use ChatColor, use the text-based messages as they internally translate it to components. Don't mix components and ChatColor, or bad stuff will happen. You'd want something like this: String profilMessage, middleMessage; profilMessage = ChatColor.translateAlternateColorCodes('&', SkyBlock.getPlugin().getConfig().getString(path + ".profil") .replace("%playerName%", e.getPlayer().getName()) .replace("%playerGroup%", playerGroup) .replace("%messageTime%", dateFormat.format(date))); middleMessage = ChatColor.translateAlternateColorCodes('&', SkyBlock.getPlugin().getConfig().getString(path + ".message") + e.getMessage()); String rowMessage = profilMessage + middleMessage; for(Player player : Bukkit.getOnlinePlayers()) { player.sendMessage(rowMessage); } If you want components and custom formatting (or even, decently componented text in general), so that you can use hover events or whatever, it's currently fairly awkward. I'm working on a PR that improves component support in craftbukkit (and thus spigot) so that player names actually keep their hover events (among other things). Once that PR has been merged, you'd implement as something like this (assuming this is an AsyncPlayerChatEvent) - in fact, this is probably how you should currently be implementing it, as it'll still handle everything you want for you... Don't reimplement the format API if possible. String profile = ChatColor.translateAlternateColorCodes('&', SkyBlock.getPlugin().getConfig().getString(path + ".profil") .replace("%playerName%", "%1$s") // Don't manually fill in the name .replace("%playerGroup%", playerGroup) .replace("%messageTime%", dateFormat.format(date))); String message = ChatColor.translateAlternateColorCodes('&', SkyBlock.getPlugin().getConfig().getString(path + ".message") + "%2$s"; e.setFormat(profile + message); // That's it; the actual sending will be handled automatically. |
Comment by MrFiliper [ 18/Mar/17 ] |
Ok, code here, I use ChatColor, thats the problem? TextComponent rowMessage = new TextComponent(""); TextComponent profilMessage, middleMessage; profilMessage = new TextComponent(ChatColor.translateAlternateColorCodes('&', SkyBlock.getPlugin().getConfig().getString(path + ".profil") .replace("%playerName%", e.getPlayer().getName()) .replace("%playerGroup%", playerGroup) .replace("%messageTime%", dateFormat.format(date)))); middleMessage = new TextComponent(ChatColor.translateAlternateColorCodes('&', SkyBlock.getPlugin().getConfig().getString(path + ".message") + e.getMessage())); rowMessage.addExtra(profilMessage); rowMessage.addExtra(middleMessage); for(Player player : Bukkit.getOnlinePlayers()) { player.spigot().sendMessage(rowMessage); } |
Comment by pokechu22 [ 18/Mar/17 ] |
To clarify: This works correctly: /tellraw @p {"text":"<name> ","extra":[{"color":"red","text":"LLLLLLLLLL break for demo LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"}]} This breaks, because it directly inserts a §-based format code and that is not allowed: /tellraw @p {"text":"<name> ","extra":[{"text":"\u00a7cLLLLLLLLLL break for demo LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"}]} Do not put § into component text. Just don't. Use the appropriate color and style setting methods instead.
If you're formatting components the correct way and it still doesn't work, please add a test case to this ticket. |
Comment by pokechu22 [ 18/Mar/17 ] |
This is a client issue when using raw §-based formatting (including directly embedding ChatColor constants). However, when using components, it shouldn't be an issue; can you provide the full code please? If it is a vanilla bug, I can create the appropriate ticket there. |
Comment by md_5 [ 16/Mar/17 ] |
No complete code, likely an issue with your code or the client |