Fix TooltipHelper formatting

This commit is contained in:
Snownee 2021-01-14 11:52:48 +08:00
parent 14154fe239
commit 5d0674067f

View file

@ -170,7 +170,7 @@ public class TooltipHelper {
StringBuilder currentLine = new StringBuilder(); StringBuilder currentLine = new StringBuilder();
int width = 0; int width = 0;
for (String word : words) { for (String word : words) {
int newWidth = font.getStringWidth(word); int newWidth = font.getStringWidth(word.replaceAll("_", ""));
if (width + newWidth > maxWidthPerLine) { if (width + newWidth > maxWidthPerLine) {
if (width > 0) { if (width > 0) {
String line = currentLine.toString(); String line = currentLine.toString();
@ -190,24 +190,22 @@ public class TooltipHelper {
} }
// Format // Format
IFormattableTextComponent lineStart = StringTextComponent.EMPTY.copy(); IFormattableTextComponent lineStart = new StringTextComponent(Strings.repeat(" ", indent));
for (int i = 0; i < indent; i++)
lineStart.append(" ");
lineStart.formatted(defaultColor); lineStart.formatted(defaultColor);
List<ITextComponent> formattedLines = new ArrayList<>(lines.size()); List<ITextComponent> formattedLines = new ArrayList<>(lines.size());
Couple<TextFormatting> f = Couple.create(highlightColor, defaultColor); Couple<TextFormatting> f = Couple.create(highlightColor, defaultColor);
boolean currentlyHighlighted = false;
for (String string : lines) { for (String string : lines) {
boolean currentlyHighlighted = false;
IFormattableTextComponent currentComponent = lineStart.copy(); IFormattableTextComponent currentComponent = lineStart.copy();
String[] split = string.split("_"); String[] split = string.split("_");
for (String part : split) { for (String part : split) {
currentComponent.append(new StringTextComponent(part).formatted(f.get(currentlyHighlighted))); currentComponent.append(new StringTextComponent(part).formatted(f.get(currentlyHighlighted)));
if (split.length != 1) currentlyHighlighted = !currentlyHighlighted;
currentlyHighlighted = !currentlyHighlighted;
} }
formattedLines.add(currentComponent); formattedLines.add(currentComponent);
currentlyHighlighted = !currentlyHighlighted;
} }