Solved Center text in chat

Discussion in 'Plugin Development' started by RainoBoy97, Jul 5, 2013.

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

    RainoBoy97

    Hello again,

    Does anyone have a method that center's a string in chat? (normal chat width)

    Thanks,
    RainoBoy97
     
  2. Offline

    GodzOfMadness

    RainoBoy97 Just add a bunch of spaces and pray? I don't know if you can get the width of the chat that the client put unless with a client modification(Don't quote me on that, I have no idea. Just a guess).
     
  3. Offline

    xTrollxDudex

  4. Offline

    RainoBoy97

    I want to make a method, that adds spaces in front of the text. So I can use it on other stuff too :p
     
  5. Offline

    GodzOfMadness

    RainoBoy97
    public void addAmazingSpaces(String input){
    Bukkit.broadcastMessage("[SPACE] [SPACE] [SPACE] [SPACE]" + input);
    }
    or just add player as a parameter and then do player.sendMessage(..)
     
  6. Offline

    ThunderWaffeMC

    RainoBoy97 Yeah just use event.getPlayer().sendMessage(" (amount of spaces here) "); in front of the methods.
     
  7. Offline

    RainoBoy97

    That wont work for all messages, as some are longer that others.
     
  8. Offline

    GodzOfMadness

    RainoBoy97
    public void addAmazingSpaces(String spaces, String input){
    Bukkit.broadcastMessage(spaces + input);
    }
     
  9. Offline

    Jogy34

    Considering that players can do client side editing of how wide the chat is, I don't think you would be able to do this effectively. If you wanted to you can try to set up ratios to figure out how many spaces you have to add before a message though. That would really just be trial and error though.
     
  10. Offline

    RainoBoy97

    I know that players can edit chat widht etc, but thats their problem xD
     
  11. Offline

    GodzOfMadness

    RainoBoy97 It's your problem for not being boss enough to hack the persons computer to install a mod to calculate the width they have for their chat! ;)

    NOTE: I do not encourage anyone to hack another person's computer under ANY circumstances!
     
    Garris0n and CaptainBern like this.
  12. Like.
     
  13. Offline

    RainoBoy97

    -.-
     
  14. Offline

    chasechocolate

  15. Offline

    Techy4198

    just wanna point out, player.sendMessage("message") doesn't make the player send the message, which i believe is what you want, it actually sends a message TO that player, which ISN'T what you want.
     
  16. Offline

    RainoBoy97

    Uhm, I know how that works... :p
    player.chat(string) makes the player send the message.
    player.sendMessage(string) sends a message to the player.
     
  17. Offline

    AmShaegar

    This works for my client chat width. Don't know if that helps.

    Code:java
    1. private String centerText(String text) {
    2. int maxWidth = 80,
    3. spaces = (int) Math.round((maxWidth-1.4*text.length())/2);
    4. return StringUtils.repeat(" ", spaces)+text;
    5. }
     
  18. Offline

    RainoBoy97

    Works good for strings without color, but the more color you add the more to the left it goes :s
     
    AmShaegar likes this.
  19. Offline

    AmShaegar

    Nice hint. Should be fixed:
    Code:java
    1. private String centerText(String text) {
    2. int maxWidth = 80,
    3. spaces = (int) Math.round((maxWidth-1.4*ChatColor.stripColor(text).length())/2);
    4. return StringUtils.repeat(" ", spaces)+text;
    5. }
     
  20. Offline

    RainoBoy97

    Didnt do anything :(

    I checked the StringUtils class, and found the method center(String, Width), tried that and seems like it worked better :p

    Thanks to the guys that helped, and to those who didnt (wink wink)..
     
Thread Status:
Not open for further replies.

Share This Page