1 package org.bukkit.util;
3 import org.bukkit.ChatColor;
5 import java.util.LinkedList;
38 public static ChatPage paginate(String unpaginatedString,
int pageNumber,
int lineLength,
int pageHeight) {
39 String[] lines =
wordWrap(unpaginatedString, lineLength);
41 int totalPages = lines.length / pageHeight + (lines.length % pageHeight == 0 ? 0 : 1);
42 int actualPageNumber = pageNumber <= totalPages ? pageNumber : totalPages;
44 int from = (actualPageNumber - 1) * pageHeight;
45 int to = from + pageHeight <= lines.length ? from + pageHeight : lines.length;
48 return new ChatPage(selectedLines, actualPageNumber, totalPages);
58 public static String[]
wordWrap(String rawString,
int lineLength) {
60 if (rawString == null) {
61 return new String[] {
""};
65 if (rawString.length() <= lineLength && !rawString.contains(
"\n")) {
66 return new String[] {rawString};
69 char[] rawChars = (rawString +
' ').toCharArray();
70 StringBuilder word =
new StringBuilder();
71 StringBuilder line =
new StringBuilder();
72 List<String> lines =
new LinkedList<String>();
73 int lineColorChars = 0;
75 for (
int i = 0; i < rawChars.length; i++) {
86 if (c ==
' ' || c ==
'\n') {
87 if (line.length() == 0 && word.length() > lineLength) {
88 for (String partialWord : word.toString().split(
"(?<=\\G.{" + lineLength +
"})")) {
89 lines.add(partialWord);
91 }
else if (line.length() + word.length() - lineColorChars == lineLength) {
93 lines.add(line.toString());
94 line =
new StringBuilder();
96 }
else if (line.length() + 1 + word.length() - lineColorChars > lineLength) {
97 for (String partialWord : word.toString().split(
"(?<=\\G.{" + lineLength +
"})")) {
98 lines.add(line.toString());
99 line =
new StringBuilder(partialWord);
103 if (line.length() > 0) {
108 word =
new StringBuilder();
111 lines.add(line.toString());
112 line =
new StringBuilder();
119 if(line.length() > 0) {
120 lines.add(line.toString());
127 for (
int i = 1; i < lines.size(); i++) {
128 final String pLine = lines.get(i-1);
129 final String subLine = lines.get(i);
137 return lines.toArray(
new String[lines.size()]);
142 private String[] lines;
143 private int pageNumber;
144 private int totalPages;
146 public ChatPage(String[] lines,
int pageNumber,
int totalPages) {
148 this.pageNumber = pageNumber;
149 this.totalPages = totalPages;