summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTravis Watkins <amaranth@ubuntu.com>2012-05-16 18:09:33 -0500
committerTravis Watkins <amaranth@ubuntu.com>2012-05-16 23:47:14 -0500
commit3ee75ab6d3c5207d047623278012926804925325 (patch)
tree2152be8b8452f0720c23b97b509706d1257c1fb1 /src
parentb6d611d9be5f2d72a8c37d6a4c66edeab24209b6 (diff)
downloadcraftbukkit-3ee75ab6d3c5207d047623278012926804925325.tar
craftbukkit-3ee75ab6d3c5207d047623278012926804925325.tar.gz
craftbukkit-3ee75ab6d3c5207d047623278012926804925325.tar.lz
craftbukkit-3ee75ab6d3c5207d047623278012926804925325.tar.xz
craftbukkit-3ee75ab6d3c5207d047623278012926804925325.zip
Replace TextWrapper with simple newline splitting. Fixes BUKKIT-1662
TextWrapper used to try to ensure a message would wrap correctly on the client by counting the width of the characters in pixels and wrapping before hitting that limit. This was needed because the client would lose color information when wrapping and could not handle long lines of text. Now that both of these problems are solved in the client we can replace TextWrapper with simple code to split the message into multiple packets on newlines and ensure chat colors carry across to the new packet.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/bukkit/craftbukkit/TextWrapper.java95
1 files changed, 16 insertions, 79 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/TextWrapper.java b/src/main/java/org/bukkit/craftbukkit/TextWrapper.java
index 015ea857..61384ae1 100644
--- a/src/main/java/org/bukkit/craftbukkit/TextWrapper.java
+++ b/src/main/java/org/bukkit/craftbukkit/TextWrapper.java
@@ -1,88 +1,25 @@
package org.bukkit.craftbukkit;
-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
- };
- private static final char COLOR_CHAR = '\u00A7';
- private static final int CHAT_WINDOW_WIDTH = 320;
- private static final int CHAT_STRING_LENGTH = 119;
- private static final String allowedChars = net.minecraft.server.SharedConstants.allowedCharacters;
-
- public static String[] wrapText(final String text) {
- final StringBuilder out = new StringBuilder();
- char colorChar = 'f';
- int lineWidth = 0;
- int lineLength = 0;
-
- // Go over the message char by char.
- for (int i = 0; i < text.length(); i++) {
- char ch = text.charAt(i);
-
- // Get the color
- if (ch == COLOR_CHAR && i < text.length() - 1) {
- // We might need a linebreak ... so ugly ;(
- if (lineLength + 2 > CHAT_STRING_LENGTH) {
- out.append('\n');
- lineLength = 0;
- if (colorChar != 'f' && colorChar != 'F') {
- out.append(COLOR_CHAR).append(colorChar);
- lineLength += 2;
- }
- }
- colorChar = text.charAt(++i);
- out.append(COLOR_CHAR).append(colorChar);
- lineLength += 2;
- continue;
- }
+import java.util.ArrayList;
+import java.util.List;
+import org.bukkit.ChatColor;
- // 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;
+public class TextWrapper {
+ public static List<String> wrapText(final String text) {
+ ArrayList<String> output = new ArrayList<String>();
+ String[] lines = text.split("\n");
+ String lastColor = null;
+
+ for (int i = 0; i < lines.length; i++) {
+ String line = lines[i];
+ if (lastColor != null) {
+ line = lastColor + line;
}
- // Find the width
- final int width = characterWidths[index];
-
- // See if we need a linebreak
- if (lineLength + 1 > CHAT_STRING_LENGTH || lineWidth + width >= CHAT_WINDOW_WIDTH) {
- out.append('\n');
- lineLength = 0;
-
- // Re-apply the last color if it isn't the default
- if (colorChar != 'f' && colorChar != 'F') {
- out.append(COLOR_CHAR).append(colorChar);
- lineLength += 2;
- }
- lineWidth = width;
- } else {
- lineWidth += width;
- }
- out.append(ch);
- lineLength++;
+ output.add(line);
+ lastColor = ChatColor.getLastColors(line);
}
- // Return it split
- return out.toString().split("\n");
+ return output;
}
} \ No newline at end of file