Search in sources :

Example 1 with BlockQuote

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();
}
Also used : BlockQuoteMatcher(com.easterlyn.util.text.BlockQuoteMatcher) TextComponent(net.md_5.bungee.api.chat.TextComponent) BlockQuote(com.easterlyn.util.text.BlockQuote) ParsedText(com.easterlyn.util.text.ParsedText) QuoteConsumer(com.easterlyn.util.text.QuoteConsumer) StaticQuoteConsumer(com.easterlyn.util.text.StaticQuoteConsumer)

Aggregations

BlockQuote (com.easterlyn.util.text.BlockQuote)1 BlockQuoteMatcher (com.easterlyn.util.text.BlockQuoteMatcher)1 ParsedText (com.easterlyn.util.text.ParsedText)1 QuoteConsumer (com.easterlyn.util.text.QuoteConsumer)1 StaticQuoteConsumer (com.easterlyn.util.text.StaticQuoteConsumer)1 TextComponent (net.md_5.bungee.api.chat.TextComponent)1