2011-02-20 13:38:27 +01:00
|
|
|
package org.bukkit.craftbukkit;
|
|
|
|
|
2011-03-12 16:58:05 +01:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2011-04-25 18:14:06 +02:00
|
|
|
import javax.sound.sampled.LineListener;
|
|
|
|
|
2011-02-20 13:38:27 +01:00
|
|
|
public class TextWrapper {
|
|
|
|
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
|
|
|
|
};
|
2011-04-25 18:14:06 +02:00
|
|
|
private static final char COLOR_CHAR = '\u00A7';
|
2011-03-12 16:58:05 +01:00
|
|
|
private static final int CHAT_WINDOW_WIDTH = 320;
|
2011-04-25 18:14:06 +02:00
|
|
|
private static final int CHAT_STRING_LENGTH = 119;
|
|
|
|
private static final String allowedChars = net.minecraft.server.FontAllowedCharacters.a;
|
2011-02-20 13:38:27 +01:00
|
|
|
|
|
|
|
public static String[] wrapText(final String text) {
|
|
|
|
final StringBuilder out = new StringBuilder();
|
|
|
|
char colorChar = 'f';
|
|
|
|
int lineWidth = 0;
|
2011-04-25 18:14:06 +02:00
|
|
|
int lineLenght = 0;
|
|
|
|
|
|
|
|
// Go over the message char by char.
|
2011-02-20 13:38:27 +01:00
|
|
|
for (int i = 0; i < text.length(); i++) {
|
|
|
|
char ch = text.charAt(i);
|
2011-04-25 18:14:06 +02:00
|
|
|
|
|
|
|
// Get the color
|
|
|
|
if (ch == COLOR_CHAR && i < text.length() - 1) {
|
|
|
|
// We might need a linebreak ... so ugly ;(
|
|
|
|
if (lineLenght + 2 > CHAT_STRING_LENGTH) {
|
|
|
|
out.append('\n');
|
|
|
|
lineLenght = 0;
|
|
|
|
if (colorChar != 'f') {
|
|
|
|
out.append(COLOR_CHAR).append(colorChar);
|
|
|
|
lineLenght += 2;
|
|
|
|
}
|
|
|
|
}
|
2011-03-12 16:58:05 +01:00
|
|
|
colorChar = text.charAt(++i);
|
2011-04-25 18:14:06 +02:00
|
|
|
out.append(COLOR_CHAR).append(colorChar);
|
|
|
|
lineLenght += 2;
|
2011-02-20 13:38:27 +01:00
|
|
|
continue;
|
|
|
|
}
|
2011-04-25 18:14:06 +02:00
|
|
|
|
|
|
|
// Figure out if it's allowed
|
|
|
|
int index = allowedChars.indexOf(ch);
|
|
|
|
if (index == -1) {
|
|
|
|
// Invalid character .. skip it.
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// Sadly needed as the allowedChars string misses the first
|
|
|
|
index += 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the width
|
|
|
|
final int width = characterWidths[ index ];
|
|
|
|
|
|
|
|
// See if we need a linebreak
|
|
|
|
if (lineLenght + 1 > CHAT_STRING_LENGTH || lineWidth + width >= CHAT_WINDOW_WIDTH) {
|
2011-02-20 13:38:27 +01:00
|
|
|
out.append('\n');
|
2011-04-25 18:14:06 +02:00
|
|
|
lineLenght = 0;
|
|
|
|
|
|
|
|
// Re-apply the last color if it isn't the default
|
2011-02-20 13:38:27 +01:00
|
|
|
if (colorChar != 'f') {
|
2011-04-25 18:14:06 +02:00
|
|
|
out.append(COLOR_CHAR).append(colorChar);
|
|
|
|
lineLenght += 2;
|
2011-02-20 13:38:27 +01:00
|
|
|
}
|
|
|
|
lineWidth = width;
|
|
|
|
} else {
|
|
|
|
lineWidth += width;
|
|
|
|
}
|
2011-04-25 18:14:06 +02:00
|
|
|
out.append(ch);
|
|
|
|
lineLenght++;
|
2011-02-20 13:38:27 +01:00
|
|
|
}
|
2011-03-12 16:58:05 +01:00
|
|
|
|
2011-04-25 18:14:06 +02:00
|
|
|
// Return it split
|
|
|
|
return out.toString().split("\n");
|
2011-02-20 13:38:27 +01:00
|
|
|
}
|
|
|
|
}
|