Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
org.bukkit.util.ChatPaginator Class Reference

Classes

class  ChatPage
 

Static Public Member Functions

static ChatPage paginate (String unpaginatedString, int pageNumber)
 
static ChatPage paginate (String unpaginatedString, int pageNumber, int lineLength, int pageHeight)
 
static String[] wordWrap (String rawString, int lineLength)
 

Static Public Attributes

static final int GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH = 55
 
static final int AVERAGE_CHAT_PAGE_WIDTH = 65
 
static final int UNBOUNDED_PAGE_WIDTH = Integer.MAX_VALUE
 
static final int OPEN_CHAT_PAGE_HEIGHT = 20
 
static final int CLOSED_CHAT_PAGE_HEIGHT = 10
 
static final int UNBOUNDED_PAGE_HEIGHT = Integer.MAX_VALUE
 

Detailed Description

The ChatPaginator takes a raw string of arbitrary length and breaks it down into an array of strings appropriate for displaying on the Minecraft player console.

Definition at line 13 of file ChatPaginator.java.

Member Function Documentation

static ChatPage org.bukkit.util.ChatPaginator.paginate ( String  unpaginatedString,
int  pageNumber 
)
static

Breaks a raw string up into pages using the default width and height.

Parameters
unpaginatedStringThe raw string to break.
pageNumberThe page number to fetch.
Returns
A single chat page.

Definition at line 28 of file ChatPaginator.java.

28  {
29  return paginate(unpaginatedString, pageNumber, GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH, CLOSED_CHAT_PAGE_HEIGHT);
30  }
static ChatPage paginate(String unpaginatedString, int pageNumber)
static ChatPage org.bukkit.util.ChatPaginator.paginate ( String  unpaginatedString,
int  pageNumber,
int  lineLength,
int  pageHeight 
)
static

Breaks a raw string up into pages using a provided width and height.

Parameters
unpaginatedStringThe raw string to break.
pageNumberThe page number to fetch.
lineLengthThe desired width of a chat line.
pageHeightThe desired number of lines in a page.
Returns
A single chat page.

Definition at line 41 of file ChatPaginator.java.

References org.bukkit.util.ChatPaginator.wordWrap().

41  {
42  String[] lines = wordWrap(unpaginatedString, lineLength);
43 
44  int totalPages = lines.length / pageHeight + (lines.length % pageHeight == 0 ? 0 : 1);
45  int actualPageNumber = pageNumber <= totalPages ? pageNumber : totalPages;
46 
47  int from = (actualPageNumber - 1) * pageHeight;
48  int to = from + pageHeight <= lines.length ? from + pageHeight : lines.length;
49  String[] selectedLines = Java15Compat.Arrays_copyOfRange(lines, from, to);
50 
51  return new ChatPage(selectedLines, actualPageNumber, totalPages);
52  }
static String[] wordWrap(String rawString, int lineLength)
static String [] org.bukkit.util.ChatPaginator.wordWrap ( String  rawString,
int  lineLength 
)
static

Breaks a raw string up into a series of lines. Words are wrapped using spaces as decimeters and the newline character is respected.

Parameters
rawStringThe raw string to break.
lineLengthThe length of a line of text.
Returns
An array of word-wrapped lines.

Definition at line 62 of file ChatPaginator.java.

References org.bukkit.ChatColor.COLOR_CHAR, org.bukkit.ChatColor.getByChar(), and org.bukkit.ChatColor.WHITE.

Referenced by org.bukkit.util.ChatPaginator.paginate().

62  {
63  // A null string is a single line
64  if (rawString == null) {
65  return new String[] {""};
66  }
67 
68  // A string shorter than the lineWidth is a single line
69  if (rawString.length() <= lineLength && !rawString.contains("\n")) {
70  return new String[] {rawString};
71  }
72 
73  char[] rawChars = (rawString + ' ').toCharArray(); // add a trailing space to trigger pagination
74  StringBuilder word = new StringBuilder();
75  StringBuilder line = new StringBuilder();
76  List<String> lines = new LinkedList<String>();
77  int lineColorChars = 0;
78 
79  for (int i = 0; i < rawChars.length; i++) {
80  char c = rawChars[i];
81 
82  // skip chat color modifiers
83  if (c == ChatColor.COLOR_CHAR) {
84  word.append(ChatColor.getByChar(rawChars[i + 1]));
85  lineColorChars += 2;
86  i++; // Eat the next character as we have already processed it
87  continue;
88  }
89 
90  if (c == ' ' || c == '\n') {
91  if (line.length() == 0 && word.length() > lineLength) { // special case: extremely long word begins a line
92  for (String partialWord : word.toString().split("(?<=\\G.{" + lineLength + "})")) {
93  lines.add(partialWord);
94  }
95  } else if (line.length() + word.length() - lineColorChars == lineLength) { // Line exactly the correct length...newline
96  line.append(word);
97  lines.add(line.toString());
98  line = new StringBuilder();
99  lineColorChars = 0;
100  } else if (line.length() + 1 + word.length() - lineColorChars > lineLength) { // Line too long...break the line
101  for (String partialWord : word.toString().split("(?<=\\G.{" + lineLength + "})")) {
102  lines.add(line.toString());
103  line = new StringBuilder(partialWord);
104  }
105  lineColorChars = 0;
106  } else {
107  if (line.length() > 0) {
108  line.append(' ');
109  }
110  line.append(word);
111  }
112  word = new StringBuilder();
113 
114  if (c == '\n') { // Newline forces the line to flush
115  lines.add(line.toString());
116  line = new StringBuilder();
117  }
118  } else {
119  word.append(c);
120  }
121  }
122 
123  if(line.length() > 0) { // Only add the last line if there is anything to add
124  lines.add(line.toString());
125  }
126 
127  // Iterate over the wrapped lines, applying the last color from one line to the beginning of the next
128  if (lines.get(0).length() == 0 || lines.get(0).charAt(0) != ChatColor.COLOR_CHAR) {
129  lines.set(0, ChatColor.WHITE + lines.get(0));
130  }
131  for (int i = 1; i < lines.size(); i++) {
132  final String pLine = lines.get(i-1);
133  final String subLine = lines.get(i);
134 
135  char color = pLine.charAt(pLine.lastIndexOf(ChatColor.COLOR_CHAR) + 1);
136  if (subLine.length() == 0 || subLine.charAt(0) != ChatColor.COLOR_CHAR) {
137  lines.set(i, ChatColor.getByChar(color) + subLine);
138  }
139  }
140 
141  return lines.toArray(new String[lines.size()]);
142  }

The documentation for this class was generated from the following file: