Centering A Message In Chat

Discussion in 'Plugin Development' started by BungeeTheCookie, Jun 2, 2014.

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

    BungeeTheCookie

    Do not give me the answer, "Just use spaces and hope it works." I want to actually be able to center the String in the chat given using a method, and if it is too large for the chat, it will go onto the next line. I also want it to support color codes. RainoBoy97 I was looking at your thread earlier and you said you accomplished it through the StringUtils, but which class are you referring to?
     
  2. adjust the spaces dymacially according to the displayed character length? I was thinking of implementing this into my own plugin, but I do it manually xD.
     
  3. Offline

    BungeeTheCookie

    How do you do that? I want it to do it automatically using a method, not manually.
    EDIT: What is the maximum length of a text?
     
  4. Offline

    Axe2760

    BungeeTheCookie I just counted, I believe it's 100 characters.

    The place I'd start is breaking up the message into lines (that are small enough so that they won't be wrapped) to send to the player separately, applying the spacing to each line individually.

    The problem I see is for players who for some reason use chat as 40px wide in the multiplayer settings, it would be pretty much impossible to not allow the string to be wrapped in it.
     
  5. Offline

    BungeeTheCookie

    Get the player's chat size using reflection? I just want some code, not an explanation. But thank you for your time to try and help me, but can you please paste in some code that we can use as a starting point? :D
     
  6. Offline

    Axe2760

    BungeeTheCookie I have literally no idea how you would do anything like this code-wise, sorry! :p Given the fact that players can change their chat size, at least.
     
  7. I understand that. The length for one line is 52 characters on the standard sized chat. I would go from there. It would be near impossible for a plugin to see the size of the chat the player actually has as it would be client side. I didnt even know they could do that I thought it would just adjust it dynamically, and keeping the amount of characters per line.

    Correct me if I'm wrong just thinking out loud :)
     
  8. Offline

    ccrama

    Untested.

    Code:java
    1. String s = "yourstring";
    2. int length = s.length;
    3. int spaces = ((52-length) /2); //due to rounding, may be one character off.
    4. int i = 0;
    5. do {
    6. s = " " + s;
    7. i +=1;
    8. } while (i < spaces);
    9. return s;


    Should work within 1 space of center!
     
  9. its 52 :)
     
    Axe2760 likes this.
  10. Offline

    L33m4n123


    It's not like there's a

    StringUtils.center(string, size); method
     
  11. Offline

    ccrama


    Had no idea that existed. The only reason you might want to use my method is a custom character instead of a space.
     
  12. Offline

    fireblast709

    Idk how helpful it will be, but there is also a ChatPaginator class in org.bukkit.util.
     
  13. Offline

    L33m4n123


    StringUtils.center(string, size, charOrStringYouWantToUseToCenterIt);
     
    ZeusAllMighty11 likes this.
  14. Offline

    ccrama

    Ok, you win.
     
    L33m4n123 likes this.
  15. Offline

    mkezar

    Just use spaces and hope it works
     
  16. Offline

    BungeeTheCookie

    ccrama L33m4n123 fireblast709 teozfrank Axe2760
    Thank chuu!!!!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  17. wow you put alot of thought into that

    Sweeeeeet :D Where is that class may I ask xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  18. Offline

    BungeeTheCookie

  19. no need to make a double method to do the same thing lol. Just call it as normal. Make sure that your importing: org.apache.commons.lang.StringUtils; as thats the one you want to use :)
     
  20. Offline

    L33m4n123

    org.apache.commons.lang.. is delivered with bukkit
     
  21. Offline

    BungeeTheCookie

    I know, but would it work for all chat sizes? I am making a double method for my own reasons. :p Dammit there is no StringUtils.center(String, String); just (string, char).

    EDIT: Would using replaceAll() to colorize a character off-set the centering?
     
  22. Offline

    ZeusAllMighty11

    BungeeTheCookie

    No it won't work for all chat sizes. It works for whatever one you give it.

    AFAIK you can't get a player's chat width via reflection.. Only language and a few other personal details
     
  23. there is both...
     
  24. Offline

    Necrodoom

    You mean Java.
     
  25. Offline

    L33m4n123

    No I do not mean Java. It's a library for Java however just in "pure, default" jdk you do not have acces to the "org.apache" library but bukkit delivers it. Same as Google Guava for example
     
  26. Offline

    BungeeTheCookie

    No, i am not using an integer, just a String and a char. L33m4n123 teozfrank No there isn't. There is just (String, char). I am using the method without the Integer.
     
  27. Offline

    L33m4n123

    Sorry I don't get what you want^^
     
  28. Offline

    BungeeTheCookie

    Ok. There is a method in StringUtils that does not take an Integer. It takes a String, and a char (for the spaces character I think.) I was wondering if that method would work for all chat sizes. Also, would using the replaceAll() method to colorize the char used in the method off-set the centering?
     
  29. Offline

    L33m4n123

    The only way to find that out would be testing I guess ;)
     
  30. Offline

    BungeeTheCookie

    :| Ok. Thanks! Im making a minigame plugin and I am using all of these methods to loop to a specific one, so I may test it in a day or two (unless you can test it :))
     
Thread Status:
Not open for further replies.

Share This Page