Search in sources :

Example 1 with AdvancedSuggestions

use of io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestions in project AdvancedChatBox by DarkKronicle.

the class ShortcutSuggestor method suggest.

@Override
public Optional<List<AdvancedSuggestions>> suggest(String string) {
    if (!string.contains(":")) {
        return Optional.empty();
    }
    ArrayList<AdvancedSuggestions> suggest = new ArrayList<>();
    for (int i = 0; i < string.length(); i++) {
        char c = string.charAt(i);
        if (c != ':') {
            continue;
        }
        int start = i;
        int end;
        while (true) {
            i++;
            if (i >= string.length()) {
                end = i;
                break;
            }
            char n = string.charAt(i);
            if (n == ' ') {
                end = i;
                break;
            } else if (n == ':') {
                i--;
                end = i + 1;
                break;
            }
        }
        if (end - start < 1) {
            break;
        }
        StringRange range = new StringRange(start, end);
        suggest.add(new AdvancedSuggestions(range, getSuggestions(string.substring(start + 1, end), range)));
    }
    return Optional.of(suggest);
}
Also used : ArrayList(java.util.ArrayList) AdvancedSuggestions(io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestions) StringRange(com.mojang.brigadier.context.StringRange)

Example 2 with AdvancedSuggestions

use of io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestions in project AdvancedChatBox by DarkKronicle.

the class CalculatorSuggestor method suggest.

@Override
public Optional<List<AdvancedSuggestions>> suggest(String text) {
    if (!text.contains("[") || !text.contains("]")) {
        return Optional.empty();
    }
    List<StringMatch> matches = SearchUtils.findMatches(text, BRACKET_REGEX, FindType.REGEX).orElse(null);
    if (matches == null) {
        return Optional.empty();
    }
    int last = -1;
    ArrayList<AdvancedSuggestions> suggest = new ArrayList<>();
    for (StringMatch m : matches) {
        if (m.start < last || m.end - m.start < 1) {
            // Don't want overlapping matches (just in case) or too small
            continue;
        }
        last = m.end;
        String string = m.match.substring(1, m.match.length() - 1);
        Expression expression = new Expression(string);
        double val = expression.calculate();
        String message = NAN;
        if (!Double.isNaN(val)) {
            message = String.valueOf(val);
        }
        StringRange range = new StringRange(m.start, m.end);
        suggest.add(new AdvancedSuggestions(range, new ArrayList<>(Collections.singleton(new AdvancedSuggestion(range, message)))));
    }
    if (suggest.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(suggest);
}
Also used : Expression(org.mariuszgromada.math.mxparser.Expression) ArrayList(java.util.ArrayList) AdvancedSuggestion(io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestion) StringMatch(io.github.darkkronicle.advancedchatcore.util.StringMatch) AdvancedSuggestions(io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestions) StringRange(com.mojang.brigadier.context.StringRange)

Example 3 with AdvancedSuggestions

use of io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestions in project AdvancedChatBox by DarkKronicle.

the class SpellCheckSuggestor method suggest.

@Override
public Optional<List<AdvancedSuggestions>> suggest(String text) {
    ArrayList<AdvancedSuggestions> suggestions = new ArrayList<>();
    try {
        List<RuleMatch> matches = lt.check(text);
        for (RuleMatch match : matches) {
            int fromPos = match.getFromPos();
            int toPos = match.getToPos();
            StringRange range = new StringRange(fromPos, toPos);
            suggestions.add(new AdvancedSuggestions(range, convertSuggestions(match, range)));
        }
    } catch (Exception e) {
        e.printStackTrace();
        return Optional.empty();
    }
    return Optional.of(suggestions);
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AdvancedSuggestions(io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestions) StringRange(com.mojang.brigadier.context.StringRange)

Aggregations

StringRange (com.mojang.brigadier.context.StringRange)3 AdvancedSuggestions (io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestions)3 ArrayList (java.util.ArrayList)3 AdvancedSuggestion (io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestion)1 StringMatch (io.github.darkkronicle.advancedchatcore.util.StringMatch)1 IOException (java.io.IOException)1 RuleMatch (org.languagetool.rules.RuleMatch)1 Expression (org.mariuszgromada.math.mxparser.Expression)1