Yapperyapps

CenterText

May 28th, 2022 (edited)
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. public String center(String s, int maxPixels) {
  2.     int charPixelCount = MinecraftFont.Font.getWidth(ChatColor.stripColor(s));
  3.     charPixelCount += getBoldPixels(s);
  4.    
  5.     int spaceCount = 0;
  6.     if(charPixelCount < maxPixels) {
  7.         int pixelsLeft = maxPixels-charPixelCount;
  8.         int spacing = (int) Math.ceil(pixelsLeft/3);
  9.         spaceCount = spacing/3;
  10.     }
  11.    
  12.     if(spaceCount % 2 == 0)
  13.         spaceCount++;
  14.    
  15.     if(spaceCount > 0) {
  16.         StringBuilder sb = new StringBuilder();
  17.         for(int i = 1; i <= spaceCount; i++)
  18.             sb.append(" ");
  19.         return sb.toString()+s;
  20.     }else {
  21.         return s;
  22.     }
  23. }
  24.  
  25. private int getBoldPixels(String s) {
  26.     int additionalPixels = 0;
  27.    
  28.     Character previousChar = null;
  29.     boolean isBold = false;
  30.    
  31.     for(char character : removeColor(colorize(s)).toCharArray()) {
  32.         if(previousChar != null) {
  33.             if(previousChar == '&' && character == 'l' || character == 'L')
  34.                 isBold = !isBold;
  35.             if(character != '&' && character != 'ยง')
  36.                 if(isBold)
  37.                     additionalPixels++;
  38.         }
  39.         previousChar = character;
  40.     }
  41.     return additionalPixels;
  42. }
Add Comment
Please, Sign In to add comment