protected List<TextLine> handleTextFormatting(MOGuideEntry entry, String text, int width) { List<Object> shortCodeSplits = new ArrayList<>(); Matcher matcher = Pattern.compile(shortcodePattern).matcher(text); int lastEnd = 0; while (matcher.find()) { shortCodeSplits.add(text.substring(lastEnd, matcher.start())); String shortcode = matcher.group(); shortCodeSplits.add(handleShortCode(decodeShortcode(shortcode))); lastEnd = matcher.end(); } shortCodeSplits.add(text.substring(lastEnd, text.length())); List<TextChunk> textChunks = new ArrayList<>(); for (Object o : shortCodeSplits) { if (o instanceof String) { for (String s : ((String) o).split(" ")) { if (!s.isEmpty()) { textChunks.add(new TextChunk(handleVariables(s.trim(), entry), getFontRenderer())); } } } else if (o instanceof TextChunk) { textChunks.add((TextChunk) o); } } List<TextLine> lines = new ArrayList<>(); TextLine line = new TextLine(); lines.add(line); for (int i = 0; i < textChunks.size(); i++) { int w = calculateWidth(null, textChunks.get(i), null); if (i > 0 && i < textChunks.size() - 1) { w = calculateWidth(textChunks.get(i - 1), textChunks.get(i), textChunks.get(i + 1)); } if (line.getWidth() + w > width) { line = new TextLine(); lines.add(line); } line.addChunk(textChunks.get(i)); } return lines; }