[TUTORIAL] Percentages and Chat Formatting

Discussion in 'General Help' started by GamerzKing, Oct 17, 2015.

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

    GamerzKing

    Percentages and Chat Formatting

    Hello everyone! My name is GamerzKing, and I am creating this tutorial to explain how to fix the issue when percentages break the chat format, and why it happens. So, let's begin.

    The reason the chat format breaks when a player uses a "%" in chat, is because Minecraft has special chat formatting already programmed in it. For example, if you type "%n <message>", it will put your message on another line, or if you type "%s", it puts your username is chat, etc.

    So recently I have discovered a way to bypass this method, and not break the chat format. I tried to search for solutions on my own on the Bukkit forums, but came to nothing, so here is my solution/fix for this:

    Code:
    Player player = event.getPlayer();
    String message = event.getMessage();
    Firstly, I create my variables that I will use for my chat format.

    Code:
    message = message.replaceAll("%", "%%");
    Secondly, and most importantly, I will replace the strings. This will replace all "%"'s with two "%"'s in the string "message".

    Code:
    event.setFormat(ChatColor.GRAY + player.getDisplayName() + " " + ChatColor.RESET + message);
    I will then set my chat format, as usual. And of course, you can set your own colors and messages, these are just examples to help you with this tutorial.

    Code:
            Player player = event.getPlayer();
            String message = event.getMessage();
          
            message = message.replaceAll("%", "%%");
          
            event.setFormat(ChatColor.GRAY + player.getDisplayName() + " " + ChatColor.RESET + message);
    TL;DR

    Hope I helped everyone looking for this solution! If you have any other questions regarding this thread, feel free to message me, or reply to this thread, and I will reply as soon as possible.

    Thanks,
    - GamerzKing​
     
Thread Status:
Not open for further replies.

Share This Page