[SPIGOT-839] Error when using .setFormat() with chat message that contains percent symbol '%' Created: 26/Apr/15 Updated: 06/May/15 Resolved: 06/May/15 |
|
Status: | Closed |
Project: | Spigot |
Component/s: | None |
Affects Version/s: | None |
Fix Version/s: | None |
Type: | Bug | Priority: | Minor |
Reporter: | Dominik | Assignee: | Unassigned |
Resolution: | Invalid | Votes: | 0 |
Labels: | chat, error, event | ||
Environment: |
Occurred on both Windows and on Linux, doesn't seem to be important |
Plugin: | own plugin |
Description |
Steps how to reproduce: 1. Create plugin that changes chat formatting with the following code: Listener.java @EventHandler(priority = EventPriority.NORMAL) public void playerChatFormatting(AsyncPlayerChatEvent e) { e.setFormat(ChatColor.GRAY + e.getPlayer().getName() + ChatColor.WHITE + ": " + e.getMessage()); } 2. Client sends chat message containing percent char, like "I am 100% sure" 3. Event causes error:
Somewhere I read this is due to '%' being an escape character, maybe that helps. |
Comments |
Comment by Jikoo [ 01/May/15 ] |
Yes, %2$s will be replaced by the message from getMessage when the event is completed. At the most basic level, you could event.setFormat("%s: %s");
This would result in a message that looks like Name: message event.setFormat(player.getDisplayName() + ": %2$s");
The only "bug" is that you did not read the event's documentation properly.
|
Comment by Dominik [ 30/Apr/15 ] |
@Jikoo If I am not supposed to use setFormat(), how would I do it? Or did you mean I shouldn't use getMessage() and use the "%2$s" instead? If this is the case, this method I used would still work if Spigot was bugfree. |
Comment by Jikoo [ 30/Apr/15 ] |
Actually, Jannik8500's solution is correct. The message should not be inserted into the format using setFormat. "%2$s" will be replaced by the message when the event runs to completion. See http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html |
Comment by Paxination [ 27/Apr/15 ] |
"Client sends chat message containing percent char, like "I am 100% sure"" That wouldnt fix his issue. He's trying to format the chat. And it already contains the % character. Not add it in. |
Comment by Jannik8500 [ 27/Apr/15 ] |
Use: e.setFormat(ChatColor.GRAY + e.getPlayer().getName() + ChatColor.WHITE + ": " + "%2$s"); |