Center text?

Discussion in 'Spigot Plugin Development' started by yoje4li, May 17, 2015.

  1. Hello,
    I have a short question.
    How do I center tekst in the middle of chat?
    Does someone have a method of something?

    Thank you.
     
  2. A method would look like this and wouldn't be too hard to make:
    • Find out the max width of the line in the chat box.
    • Find out the width of every character.
    • Calculate the width of the sent message by the sum of width of each character on that line.
    • Add spaces on either side until you reach max width.
    There's no native support for centered text. When you go with my solution it might only work on your screen. People with a different texture pack, or a resized chat box might get the message with an offset.
     
  3. Why not simply use StringUtils#center(String str, int size);
    where str = the Text you want to center
    size = the max characters a line has

    you can also use

    StringUtils#center(String str, int size, char pad);
    where pad = the char you want to use to center it,, Example

    StringUtils.center("Hey", 13, '#');

    would return

    #####Hey#####
     
  4. That would work on monospaced characters, Minecraft doesn't have that. It will make a huge difference in a string like iiiiiiiiiiiii or wwwwwwww. But I think that method works like what I described above. Didn't know about the StringUtils method, so thanks for putting that out here. :)
     
  5. Ok I am not sure what font MC uses. That's true. But it definitly is easier than the way you suggested. Especially as some languages have different fonts and thus it would be obsolete again^^
     
  6. -changes chatbox width in the client settings-
    Oops, broke it.
     
    • Like Like x 2
    • Funny Funny x 1