Percentage symbol breaking chat formatting

Discussion in 'Plugin Development' started by Captain Dory, Jan 10, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Captain Dory

    Hi there,

    I'm having a problem with editing the chat formatting.
    This is my current code for the specific section:
    PHP:
    e.setFormat(ChatColor.translateAlternateColorCodes('&'"&a" "&7: " e.getMessage()));
    ('s' is the player's name)

    If someone were to type a message that has the percentage symbol (%) in it, the formatting is voided and the message appears as it would in vanilla.

    I've seen some threads for this but they didn't fit my problem. Any help would be appreciated :)
     
  2. Offline

    blablubbabc

    The format string of the chat event is going to be used as format string, for which % has special meaning.

    So instead of inserting the player name and chat message directly into the format add %1$s for the playername and %2$s for the message. Those are formatting placeholders and will get automatically replaced by bukkit, with the player's display name and the chat message.
     
    Konato_K likes this.
  3. Offline

    1Rogue

    Because that's not how .setFormat() works. You don't insert the message into .setFormat(), it does that on it's own.

    You need two %s tokens in your string. The first represents the player's name, the second represents the message:

    Code:java
    1. e.setFormat(ChatColor.translateAlternateColorCodes('&', "&a%s&7: %s"));
     
  4. Offline

    16davidjm6

    The problem is with the message, not the formatting.
    I had the same problem when making a chat plugin. This fixed it.

    String message = e.getMessage().replaceAll("%", "%%");
    e.setMessage(message);
     
  5. Offline

    1Rogue

    Nooooooo the problem is with the formatting. You're just replacing any case of "%" with "%%" effectively escaping the % because of how java.util.Formatter works. By the javadoc for the event, you should be leaving tokens to be replaced in your format, not inserting them yourselves.
     
  6. Offline

    Captain Dory

    Thank you. c:
     
Thread Status:
Not open for further replies.

Share This Page