From 561f73664585ead28f0cacd5dcbb44f91d9c84d4 Mon Sep 17 00:00:00 2001 From: rmichela Date: Tue, 6 Mar 2012 01:15:00 -0500 Subject: [Bleeding] ChatPaginator now preserves the color of a line after wrapping the line of text. Fixes BUKKIT-1048 --- src/main/java/org/bukkit/util/ChatPaginator.java | 33 +++++++++- src/test/java/org/bukkit/ChatPaginatorTest.java | 83 +++++++++++++----------- 2 files changed, 76 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/bukkit/util/ChatPaginator.java b/src/main/java/org/bukkit/util/ChatPaginator.java index 49d32417..4afdf340 100644 --- a/src/main/java/org/bukkit/util/ChatPaginator.java +++ b/src/main/java/org/bukkit/util/ChatPaginator.java @@ -1,5 +1,7 @@ package org.bukkit.util; +import org.bukkit.ChatColor; + import java.util.LinkedList; import java.util.List; @@ -68,22 +70,35 @@ public class ChatPaginator { StringBuilder word = new StringBuilder(); StringBuilder line = new StringBuilder(); List lines = new LinkedList(); + int lineColorChars = 0; + + for (int i = 0; i < rawChars.length; i++) { + char c = rawChars[i]; + + // skip chat color modifiers + if (c == ChatColor.COLOR_CHAR) { + word.append(ChatColor.getByChar(rawChars[i + 1])); + lineColorChars += 2; + i++; // Eat the next character as we have already processed it + continue; + } - for (char c : rawChars) { if (c == ' ' || c == '\n') { if (line.length() == 0 && word.length() > lineLength) { // special case: extremely long word begins a line for (String partialWord : word.toString().split("(?<=\\G.{" + lineLength + "})")) { lines.add(partialWord); } - } else if (line.length() + word.length() == lineLength) { // Line exactly the correct length...newline + } else if (line.length() + word.length() - lineColorChars == lineLength) { // Line exactly the correct length...newline line.append(word); lines.add(line.toString()); line = new StringBuilder(); - } else if (line.length() + 1 + word.length() > lineLength) { // Line too long...break the line + lineColorChars = 0; + } else if (line.length() + 1 + word.length() - lineColorChars > lineLength) { // Line too long...break the line for (String partialWord : word.toString().split("(?<=\\G.{" + lineLength + "})")) { lines.add(line.toString()); line = new StringBuilder(partialWord); } + lineColorChars = 0; } else { if (line.length() > 0) { line.append(' '); @@ -104,6 +119,18 @@ public class ChatPaginator { if(line.length() > 0) { // Only add the last line if there is anything to add lines.add(line.toString()); } + + // Iterate over the wrapped lines, applying the last color from one line to the beginning of the next + if (lines.get(0).charAt(0) != ChatColor.COLOR_CHAR) { + lines.set(0, ChatColor.WHITE + lines.get(0)); + } + for (int i = 1; i < lines.size(); i++) { + String pLine = lines.get(i-1); + char color = pLine.charAt(pLine.lastIndexOf(ChatColor.COLOR_CHAR) + 1); + if (lines.get(i).charAt(0) != ChatColor.COLOR_CHAR) { + lines.set(i, ChatColor.getByChar(color) + lines.get(i)); + } + } return lines.toArray(new String[0]); } diff --git a/src/test/java/org/bukkit/ChatPaginatorTest.java b/src/test/java/org/bukkit/ChatPaginatorTest.java index 6e20456e..0a8acae9 100644 --- a/src/test/java/org/bukkit/ChatPaginatorTest.java +++ b/src/test/java/org/bukkit/ChatPaginatorTest.java @@ -12,12 +12,12 @@ import static org.junit.Assert.assertThat; public class ChatPaginatorTest { @Test public void testWordWrap1() { - String rawString = "123456789 123456789 123456789"; + String rawString = ChatColor.RED + "123456789 123456789 123456789"; String[] lines = ChatPaginator.wordWrap(rawString, 19); assertThat(lines.length, is(2)); - assertThat(lines[0], is("123456789 123456789")); - assertThat(lines[1], is("123456789")); + assertThat(lines[0], is(ChatColor.RED + "123456789 123456789")); + assertThat(lines[1], is(ChatColor.RED.toString() + "123456789")); } @Test @@ -26,19 +26,19 @@ public class ChatPaginatorTest { String[] lines = ChatPaginator.wordWrap(rawString, 22); assertThat(lines.length, is(2)); - assertThat(lines[0], is("123456789 123456789")); - assertThat(lines[1], is("123456789")); + assertThat(lines[0], is(ChatColor.WHITE.toString() + "123456789 123456789")); + assertThat(lines[1], is(ChatColor.WHITE.toString() + "123456789")); } @Test public void testWordWrap3() { - String rawString = "123456789 123456789 123456789"; + String rawString = ChatColor.RED + "123456789 " + ChatColor.RED + ChatColor.RED + "123456789 " + ChatColor.RED + "123456789"; String[] lines = ChatPaginator.wordWrap(rawString, 16); assertThat(lines.length, is(3)); - assertThat(lines[0], is("123456789")); - assertThat(lines[1], is("123456789")); - assertThat(lines[2], is("123456789")); + assertThat(lines[0], is(ChatColor.RED + "123456789")); + assertThat(lines[1], is(ChatColor.RED.toString() + ChatColor.RED + "123456789")); + assertThat(lines[2], is(ChatColor.RED + "123456789")); } @Test @@ -47,8 +47,8 @@ public class ChatPaginatorTest { String[] lines = ChatPaginator.wordWrap(rawString, 19); assertThat(lines.length, is(2)); - assertThat(lines[0], is("123456789 123456789")); - assertThat(lines[1], is("123456789 12345")); + assertThat(lines[0], is(ChatColor.WHITE.toString() + "123456789 123456789")); + assertThat(lines[1], is(ChatColor.WHITE.toString() + "123456789 12345")); } @Test @@ -57,8 +57,8 @@ public class ChatPaginatorTest { String[] lines = ChatPaginator.wordWrap(rawString, 19); assertThat(lines.length, is(2)); - assertThat(lines[0], is("123456789")); - assertThat(lines[1], is("123456789 123456789")); + assertThat(lines[0], is(ChatColor.WHITE.toString() + "123456789")); + assertThat(lines[1], is(ChatColor.WHITE.toString() + "123456789 123456789")); } @Test @@ -67,8 +67,8 @@ public class ChatPaginatorTest { String[] lines = ChatPaginator.wordWrap(rawString, 19); assertThat(lines.length, is(2)); - assertThat(lines[0], is("12345678 23456789")); - assertThat(lines[1], is("123456789")); + assertThat(lines[0], is(ChatColor.WHITE.toString() + "12345678 23456789")); + assertThat(lines[1], is(ChatColor.WHITE.toString() + "123456789")); } @Test @@ -77,8 +77,8 @@ public class ChatPaginatorTest { String[] lines = ChatPaginator.wordWrap(rawString, 19); assertThat(lines.length, is(2)); - assertThat(lines[0], is("12345678 23456789")); - assertThat(lines[1], is("123456789")); + assertThat(lines[0], is(ChatColor.WHITE.toString() + "12345678 23456789")); + assertThat(lines[1], is(ChatColor.WHITE.toString() + "123456789")); } @Test @@ -87,12 +87,12 @@ public class ChatPaginatorTest { String[] lines = ChatPaginator.wordWrap(rawString, 6); assertThat(lines.length, is(6)); - assertThat(lines[0], is("123456")); - assertThat(lines[1], is("789")); - assertThat(lines[2], is("123456")); - assertThat(lines[3], is("789")); - assertThat(lines[4], is("123456")); - assertThat(lines[5], is("789")); + assertThat(lines[0], is(ChatColor.WHITE.toString() + "123456")); + assertThat(lines[1], is(ChatColor.WHITE.toString() + "789")); + assertThat(lines[2], is(ChatColor.WHITE.toString() + "123456")); + assertThat(lines[3], is(ChatColor.WHITE.toString() + "789")); + assertThat(lines[4], is(ChatColor.WHITE.toString() + "123456")); + assertThat(lines[5], is(ChatColor.WHITE.toString() + "789")); } @Test @@ -101,13 +101,13 @@ public class ChatPaginatorTest { String[] lines = ChatPaginator.wordWrap(rawString, 6); assertThat(lines.length, is(7)); - assertThat(lines[0], is("1234")); - assertThat(lines[1], is("123456")); - assertThat(lines[2], is("789")); - assertThat(lines[3], is("123456")); - assertThat(lines[4], is("789")); - assertThat(lines[5], is("123456")); - assertThat(lines[6], is("789")); + assertThat(lines[0], is(ChatColor.WHITE.toString() + "1234")); + assertThat(lines[1], is(ChatColor.WHITE.toString() + "123456")); + assertThat(lines[2], is(ChatColor.WHITE.toString() + "789")); + assertThat(lines[3], is(ChatColor.WHITE.toString() + "123456")); + assertThat(lines[4], is(ChatColor.WHITE.toString() + "789")); + assertThat(lines[5], is(ChatColor.WHITE.toString() + "123456")); + assertThat(lines[6], is(ChatColor.WHITE.toString() + "789")); } @Test @@ -116,8 +116,17 @@ public class ChatPaginatorTest { String[] lines = ChatPaginator.wordWrap(rawString, 19); assertThat(lines.length, is(2)); - assertThat(lines[0], is("123456789")); - assertThat(lines[1], is("123456789")); + assertThat(lines[0], is(ChatColor.WHITE.toString() + "123456789")); + assertThat(lines[1], is(ChatColor.WHITE.toString() + "123456789")); + } + + @Test + public void testWordWrap11() { + String rawString = ChatColor.RED + "a a a " + ChatColor.BLUE + "a a"; + String[] lines = ChatPaginator.wordWrap(rawString, 9); + + assertThat(lines.length, is(1)); + assertThat(lines[0], is(ChatColor.RED + "a a a " + ChatColor.BLUE + "a a")); } @Test @@ -128,8 +137,8 @@ public class ChatPaginatorTest { assertThat(page.getPageNumber(), is(1)); assertThat(page.getTotalPages(), is(4)); assertThat(page.getLines().length, is(2)); - assertThat(page.getLines()[0], is("1234")); - assertThat(page.getLines()[1], is("123456")); + assertThat(page.getLines()[0], is(ChatColor.WHITE.toString() + "1234")); + assertThat(page.getLines()[1], is(ChatColor.WHITE.toString() + "123456")); } @Test @@ -140,8 +149,8 @@ public class ChatPaginatorTest { assertThat(page.getPageNumber(), is(2)); assertThat(page.getTotalPages(), is(4)); assertThat(page.getLines().length, is(2)); - assertThat(page.getLines()[0], is("789")); - assertThat(page.getLines()[1], is("123456")); + assertThat(page.getLines()[0], is(ChatColor.WHITE.toString() + "789")); + assertThat(page.getLines()[1], is(ChatColor.WHITE.toString() + "123456")); } @Test @@ -152,6 +161,6 @@ public class ChatPaginatorTest { assertThat(page.getPageNumber(), is(4)); assertThat(page.getTotalPages(), is(4)); assertThat(page.getLines().length, is(1)); - assertThat(page.getLines()[0], is("789")); + assertThat(page.getLines()[0], is(ChatColor.WHITE.toString() + "789")); } } -- cgit v1.2.3