use of com.easterlyn.util.text.BlockQuote in project Easterlyn by Easterlyn.
the class StringUtil method toJSON.
// TODO appears to not be parsing colors correctly
public static Collection<TextComponent> toJSON(@Nullable String message, Collection<QuoteConsumer> additionalHandlers) {
if (message == null) {
return Collections.emptyList();
}
ParsedText parsedText = new ParsedText();
StringBuilder builder = new StringBuilder();
Collection<QuoteConsumer> consumers = Stream.concat(QUOTE_CONSUMERS.stream(), additionalHandlers.stream()).collect(Collectors.toSet());
int maxIndex = message.length() - 1;
nextChar: for (int i = 0; i < message.length(); ++i) {
char c = message.charAt(i);
BlockQuoteMatcher matcher = BLOCK_QUOTES.get(c);
// noinspection ConstantConditions
do {
if (matcher == null) {
break;
}
BlockQuote quote = matcher.findQuote(message, i);
if (quote == null) {
break;
}
i += quote.getQuoteLength() - 1;
if (!matcher.allowAdditionalParsing()) {
if (quote.getQuoteMarks() != null) {
builder.append(quote.getQuoteMarks()).append(quote.getQuoteText()).append(quote.getQuoteMarks());
} else {
builder.append(quote.getQuoteText());
}
continue nextChar;
}
if (quote.getQuoteMarks() != null) {
builder.append(quote.getQuoteMarks());
}
String quoteText = quote.getQuoteText();
consumeQuote(parsedText, consumers, builder, quote.getQuoteText());
if (quote.getQuoteMarks() != null) {
builder.append(quote.getQuoteMarks());
}
continue nextChar;
} while (false);
if (c == ' ') {
builder.append(c);
continue;
}
int nextSpace = message.indexOf(' ', i);
if (nextSpace == -1) {
nextSpace = message.length();
}
consumeQuote(parsedText, consumers, builder, message.substring(i, nextSpace));
i = nextSpace - 1;
}
// Parse remaining text in builder
consumeQuote(parsedText, consumers, builder, null);
// The client will crash if the array is empty
if (parsedText.getComponents().isEmpty()) {
parsedText.addComponent(new TextComponent(""));
}
return parsedText.getComponents();
}
Aggregations