Mirror von
https://github.com/PaperMC/Paper.git
synchronisiert 2024-12-20 21:40:06 +01:00
TextWrapper now wraps text at the proper width and also prevent splitting unless it is needed
Dieser Commit ist enthalten in:
Ursprung
7729c8b517
Commit
1db4eab7e3
@ -1,5 +1,7 @@
|
|||||||
package org.bukkit.craftbukkit;
|
package org.bukkit.craftbukkit;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class TextWrapper {
|
public class TextWrapper {
|
||||||
private static final int[] characterWidths = new int[] {
|
private static final int[] characterWidths = new int[] {
|
||||||
1, 9, 9, 8, 8, 8, 8, 7, 9, 8, 9, 9, 8, 9, 9, 9,
|
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,
|
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
|
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) {
|
public static String[] wrapText(final String text) {
|
||||||
final StringBuilder out = new StringBuilder();
|
final StringBuilder out = new StringBuilder();
|
||||||
char colorChar = 'f';
|
char colorChar = 'f';
|
||||||
int lineWidth = 0;
|
int lineWidth = 0;
|
||||||
|
int lineCount = 0;
|
||||||
boolean hasColored = true;
|
boolean hasColored = true;
|
||||||
for (int i = 0; i < text.length(); i++) {
|
for (int i = 0; i < text.length(); i++) {
|
||||||
char ch = text.charAt(i);
|
char ch = text.charAt(i);
|
||||||
if (ch == '\u00A7' && i < text.length() - 1) {
|
if (ch == '\u00A7' && i < text.length() - 1) {
|
||||||
i++;
|
colorChar = text.charAt(++i);
|
||||||
colorChar = text.charAt(i);
|
|
||||||
hasColored = false;
|
hasColored = false;
|
||||||
continue;
|
continue;
|
||||||
} else if (ch >= characterWidths.length) {
|
} else if (ch >= characterWidths.length) {
|
||||||
@ -39,6 +42,7 @@ public class TextWrapper {
|
|||||||
final int width = characterWidths[(int) ch];
|
final int width = characterWidths[(int) ch];
|
||||||
if (lineWidth + width >= CHAT_WINDOW_WIDTH) {
|
if (lineWidth + width >= CHAT_WINDOW_WIDTH) {
|
||||||
out.append('\n');
|
out.append('\n');
|
||||||
|
lineCount++;
|
||||||
if (colorChar != 'f') {
|
if (colorChar != 'f') {
|
||||||
out.append('\u00A7');
|
out.append('\u00A7');
|
||||||
out.append(colorChar);
|
out.append(colorChar);
|
||||||
@ -55,6 +59,12 @@ public class TextWrapper {
|
|||||||
lineWidth += width;
|
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};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren