Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 109 lines 4.6 kB view raw
1package org.bukkit.craftbukkit; 2 3public class TextWrapper { 4 private static final int[] characterWidths = new int[] { 1, 9, 9, 8, 8, 8, 8, 7, 9, 8, 9, 9, 8, 9, 9, 9, 8, 8, 8, 8, 9, 9, 8, 9, 8, 8, 8, 8, 8, 9, 9, 9, 4, 2, 5, 6, 6, 6, 6, 3, 5, 5, 5, 6, 2, 6, 2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 5, 6, 5, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 4, 6, 6, 3, 6, 6, 6, 6, 6, 5, 6, 6, 2, 6, 5, 3, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 5, 2, 5, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 2, 6, 6, 8, 9, 9, 6, 6, 6, 8, 8, 6, 8, 8, 8, 8, 8, 6, 6, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 6, 9, 9, 9, 5, 9, 9, 8, 7, 7, 8, 7, 8, 8, 8, 7, 8, 8, 7, 9, 9, 6, 7, 7, 7, 7, 7, 9, 6, 7, 8, 7, 6, 6, 9, 7, 6, 7, 1 }; 5 private static final char COLOR_CHAR = '\u00A7'; 6 private static final int CHAT_WINDOW_WIDTH = 320; 7 private static final int CHAT_STRING_LENGTH = 119; 8 private static final String allowedChars = net.minecraft.server.FontAllowedCharacters.allowedCharacters; 9 10 public static String[] wrapText(final String text) { 11 final StringBuilder out = new StringBuilder(); 12 char colorChar = 'f'; 13 int lineWidth = 0; 14 int lineLength = 0; 15 16 // Go over the message char by char. 17 for (int i = 0; i < text.length(); i++) { 18 char ch = text.charAt(i); 19 20 // Get the color 21 if (ch == COLOR_CHAR && i < text.length() - 1) { 22 // We might need a linebreak ... so ugly ;( 23 if (lineLength + 2 > CHAT_STRING_LENGTH) { 24 out.append('\n'); 25 lineLength = 0; 26 if (colorChar != 'f' && colorChar != 'F') { 27 out.append(COLOR_CHAR).append(colorChar); 28 lineLength += 2; 29 } 30 } 31 colorChar = text.charAt(++i); 32 out.append(COLOR_CHAR).append(colorChar); 33 lineLength += 2; 34 continue; 35 } 36 37 // Figure out if it's allowed 38 int index = allowedChars.indexOf(ch); 39 if (index == -1) { 40 // Invalid character .. skip it. 41 continue; 42 } else { 43 // Sadly needed as the allowedChars string misses the first 44 index += 32; 45 } 46 47 // Find the width 48 final int width = characterWidths[index]; 49 50 // See if we need a linebreak 51 if (lineLength + 1 > CHAT_STRING_LENGTH || lineWidth + width >= CHAT_WINDOW_WIDTH) { 52 out.append('\n'); 53 lineLength = 0; 54 55 // Re-apply the last color if it isn't the default 56 if (colorChar != 'f' && colorChar != 'F') { 57 out.append(COLOR_CHAR).append(colorChar); 58 lineLength += 2; 59 } 60 lineWidth = width; 61 } else { 62 lineWidth += width; 63 } 64 out.append(ch); 65 lineLength++; 66 } 67 68 // Return it split 69 return out.toString().split("\n"); 70 } 71 72 // uberbukkit 73 public static String[] wrapTextLegacy(final String text) { 74 final StringBuilder out = new StringBuilder(); 75 char colorChar = 'f'; 76 int lineWidth = 0; 77 boolean hasColored = true; 78 for (int i = 0; i < text.length(); i++) { 79 char ch = text.charAt(i); 80 if (ch == '\u00A7' && i < text.length() - 1) { 81 i++; 82 colorChar = text.charAt(i); 83 hasColored = false; 84 continue; 85 } else if (ch >= characterWidths.length) { 86 ch = (char) (characterWidths.length - 1); 87 } 88 final int width = characterWidths[(int) ch]; 89 if (lineWidth + width >= CHAT_WINDOW_WIDTH) { 90 out.append('\n'); 91 if (colorChar != 'f') { 92 out.append('\u00A7'); 93 out.append(colorChar); 94 } 95 out.append(ch); 96 lineWidth = width; 97 } else { 98 if (!hasColored) { 99 out.append('\u00A7'); 100 out.append(colorChar); 101 hasColored = true; 102 } 103 out.append(ch); 104 lineWidth += width; 105 } 106 } 107 return out.toString().split("\n"); 108 } 109}