From 1db4eab7e3fd21462c958198e25d29aaa9ef267d Mon Sep 17 00:00:00 2001 From: Erik Broes Date: Sat, 12 Mar 2011 16:58:05 +0100 Subject: [PATCH] TextWrapper now wraps text at the proper width and also prevent splitting unless it is needed --- .../org/bukkit/craftbukkit/TextWrapper.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/TextWrapper.java b/src/main/java/org/bukkit/craftbukkit/TextWrapper.java index 46c1c2cd3b..f20388d93e 100644 --- a/src/main/java/org/bukkit/craftbukkit/TextWrapper.java +++ b/src/main/java/org/bukkit/craftbukkit/TextWrapper.java @@ -1,5 +1,7 @@ package org.bukkit.craftbukkit; +import java.util.regex.Pattern; + 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, @@ -19,18 +21,19 @@ public class TextWrapper { 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 int CHAT_WINDOW_WIDTH = 318; + private static final int CHAT_WINDOW_WIDTH = 320; + private static final Pattern pattern = Pattern.compile("\n.*\u00A7", Pattern.DOTALL); public static String[] wrapText(final String text) { final StringBuilder out = new StringBuilder(); char colorChar = 'f'; int lineWidth = 0; + int lineCount = 0; boolean hasColored = true; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if (ch == '\u00A7' && i < text.length() - 1) { - i++; - colorChar = text.charAt(i); + colorChar = text.charAt(++i); hasColored = false; continue; } else if (ch >= characterWidths.length) { @@ -39,6 +42,7 @@ public class TextWrapper { final int width = characterWidths[(int) ch]; if (lineWidth + width >= CHAT_WINDOW_WIDTH) { out.append('\n'); + lineCount++; if (colorChar != 'f') { out.append('\u00A7'); out.append(colorChar); @@ -55,6 +59,12 @@ public class TextWrapper { lineWidth += width; } } - return out.toString().split("\n"); + + // See if we need to split the string + String result = out.toString(); + if (pattern.matcher(result).find()) return result.split("\n"); + + if (lineCount > 0) result.replace("\n", ""); + return new String[] {result}; } }