Advertisement
gamodu62100

Untitled

Mar 9th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. // lineLength = 80 (Chat Length)
  2. // lineLength = 45 (MOTD Length)
  3. public String centerText(String text, int lineLength) {
  4. char[] chars = text.toCharArray(); // Get a list of all characters in text
  5. boolean isBold = false;
  6. double length = 0;
  7. ChatColor pholder = null;
  8. for (int i = 0; i < chars.length; i++) { // Loop through all characters
  9. // Check if the character is a ColorCode..
  10. if (chars[i] == '&' && chars.length != (i + 1) && (pholder = ChatColor.getByChar(chars[i + 1])) != null) {
  11. if (pholder != ChatColor.UNDERLINE && pholder != ChatColor.ITALIC
  12. && pholder != ChatColor.STRIKETHROUGH && pholder != ChatColor.MAGIC) {
  13. isBold = (chars[i + 1] == 'l'); // Setting bold to true or false, depending on if the ChatColor is Bold.
  14. length--; // Removing one from the length, of the string, because we don't wanna count color codes.
  15. i += isBold ? 1 : 0;
  16. }
  17. } else {
  18. // If the character is not a color code:
  19. length++; // Adding a space
  20. length += (isBold ? (chars[i] != ' ' ? 0.1555555555555556 : 0) : 0); // Adding 0.156 spaces if the character is bold.
  21. }
  22. }
  23.  
  24. double spaces = (lineLength - length) / 2; // Getting the spaces to add by (max line length - length) / 2
  25.  
  26. // Adding the spaces
  27. StringBuilder builder = new StringBuilder();
  28. for (int i = 0; i < spaces; i++) {
  29. builder.append(' ');
  30. }
  31. String copy = builder.toString();
  32. builder.append(text).append(copy);
  33.  
  34. return builder.toString();
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement