mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-27 13:06:02 +01:00
TextWrapper now wraps text at the proper width and also prevent splitting unless it is needed
This commit is contained in:
parent
7729c8b517
commit
1db4eab7e3
@ -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};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user