Search in sources :

Example 1 with AdvancedSuggestion

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

the class PlayerSuggestor method suggestCurrentWord.

@Override
public Optional<List<AdvancedSuggestion>> suggestCurrentWord(String text, StringRange range) {
    List<AdvancedSuggestion> newSuggestions = new ArrayList<>();
    Collection<String> names = getPlayerNames();
    for (String name : names) {
        if (text.equals("") || name.toLowerCase().startsWith(text.toLowerCase())) {
            newSuggestions.add(new AdvancedSuggestion(range, name));
        }
    }
    return Optional.of(newSuggestions);
}
Also used : AdvancedSuggestion(io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestion) ArrayList(java.util.ArrayList)

Example 2 with AdvancedSuggestion

use of io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestion 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 AdvancedSuggestion

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

the class ShortcutSuggestor method getSuggestions.

private List<AdvancedSuggestion> getSuggestions(String current, StringRange range) {
    ArrayList<AdvancedSuggestion> suggestions = new ArrayList<>();
    for (Shortcut shortcut : shortcuts) {
        if (current.length() == 0 || shortcut.name.toLowerCase().startsWith(current.toLowerCase())) {
            FluidText text = new FluidText();
            text.append(new RawText(shortcut.name, Style.EMPTY));
            suggestions.add(new AdvancedSuggestion(range, shortcut.replace, text, new RawText(shortcut.replace, Style.EMPTY)));
        }
    }
    return suggestions;
}
Also used : FluidText(io.github.darkkronicle.advancedchatcore.util.FluidText) AdvancedSuggestion(io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestion) ArrayList(java.util.ArrayList) RawText(io.github.darkkronicle.advancedchatcore.util.RawText)

Aggregations

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