use of io.github.darkkronicle.advancedchatcore.util.StringMatch in project AdvancedChatBox by DarkKronicle.
the class ColorCodeFormatter method format.
@Override
public Optional<FluidText> format(FluidText text, @Nullable ParseResults<CommandSource> parse) {
if (parse != null) {
return Optional.empty();
}
String string = text.getString();
if (!string.contains("&")) {
return Optional.empty();
}
SearchResult search = SearchResult.searchOf(string, "(?i)&[0-9A-FK-OR]", FindType.REGEX);
if (search.size() == 0) {
return Optional.empty();
}
int index = 0;
Style last = Style.EMPTY;
FluidText formatted = new FluidText();
for (StringMatch match : search.getMatches()) {
formatted.append(text.truncate(new StringMatch("", index, match.start)).fillStyle(last));
Formatting format = Formatting.byCode(match.match.charAt(1));
last = last.withFormatting(format);
index = match.start;
}
FluidText small = text.truncate(new StringMatch("", index, string.length()));
if (small != null && !small.getString().isEmpty()) {
formatted.append(small.fillStyle(last));
}
return Optional.of(formatted);
}
use of io.github.darkkronicle.advancedchatcore.util.StringMatch in project AdvancedChatBox by DarkKronicle.
the class SpellCheckSuggestor method getHover.
private static Text getHover(String message) {
String text = ChatBoxConfigStorage.SpellChecker.HOVER_TEXT.config.getStringValue();
text = text.replaceAll("&", "ยง");
Optional<StringMatch> match = SearchUtils.getMatch(message, "<suggestion>(.+)</suggestion>", FindType.REGEX);
if (match.isEmpty()) {
text = text.replaceAll("\\$1", message).replaceAll("\\$2", "").replaceAll("\\$3", "");
return StyleFormatter.formatText(new FluidText(new RawText(text, Style.EMPTY)));
}
StringMatch stringMatch = match.get();
String start = message.substring(0, stringMatch.start);
String end = message.substring(stringMatch.end);
String middle = message.substring(stringMatch.start + 12, stringMatch.end - 13);
text = text.replaceAll("\\$1", start).replaceAll("\\$2", middle).replaceAll("\\$3", end);
return StyleFormatter.formatText(new FluidText(new RawText(text, Style.EMPTY)));
}
use of io.github.darkkronicle.advancedchatcore.util.StringMatch in project AdvancedChatBox by DarkKronicle.
the class ChatFormatter method format.
/**
* Format's the chat box contents
*
* @param string Contents
* @return Formatted FluidText. If nothing is changed it will be the contents with Style.EMPTY
*/
public FluidText format(String string) {
FluidText text = new FluidText(new RawText(string, Style.EMPTY));
if (string.length() == 0) {
return text;
}
if (suggestor.getAllSuggestions() != null) {
HashMap<StringMatch, FluidText.StringInsert> format = new HashMap<>();
for (AdvancedSuggestions suggestions : suggestor.getAllSuggestions()) {
if (suggestions.getSuggestions().isEmpty()) {
// Don't want to format if there's nothing there...
continue;
}
boolean atLeastOne = false;
for (AdvancedSuggestion suggestion : suggestions.getSuggestions()) {
if (suggestion.getText().length() > 0) {
atLeastOne = true;
break;
}
}
if (!atLeastOne) {
continue;
}
StringRange range = suggestions.getRange();
int start = range.getStart();
int end = range.getEnd();
if (end > string.length()) {
end = string.length();
}
if (start < 0) {
start = 0;
}
String matchString = string.subSequence(start, end).toString();
format.put(new StringMatch(matchString, start, end), (current, match) -> {
Style style = Style.EMPTY;
style = style.withFormatting(Formatting.UNDERLINE);
TextColor textColor = TextColor.fromRgb(ChatBoxConfigStorage.General.AVAILABLE_SUGGESTION_COLOR.config.get().color());
style = style.withColor(textColor);
return new FluidText(new RawText(matchString, style));
});
}
text.replaceStrings(format);
}
for (ChatFormatterRegistry.ChatFormatterOption option : ChatFormatterRegistry.getInstance().getAll()) {
if (!option.isActive()) {
continue;
}
Optional<FluidText> otext = option.getOption().format(text, suggestor.getParse());
if (otext.isPresent()) {
text = otext.get();
}
}
return text;
}
use of io.github.darkkronicle.advancedchatcore.util.StringMatch in project AdvancedChatBox by DarkKronicle.
the class CommandColorer method format.
@Override
public Optional<FluidText> format(FluidText text, @Nullable ParseResults<CommandSource> parse) {
if (parse == null) {
if (text.getString().charAt(0) == '/') {
return Optional.of(new FluidText(RawText.withColor(text.getString(), CommandColorerStorage.ERROR_COLOR.config.get())));
}
return Optional.empty();
}
CommandContextBuilder<CommandSource> commandContextBuilder = parse.getContext().getLastChild();
HashMap<StringMatch, FluidText.StringInsert> replace = new HashMap<>();
int lowest = -1;
int max = 0;
int index = 0;
String string = text.getString();
int length = string.length();
Colors.Palette palette = Colors.getInstance().get(CommandColorerStorage.DEFAULT_PALETTE.config.getStringValue()).orElse(Colors.getInstance().getDefault());
TreeSet<CommandSection<?>> sections = new TreeSet<>(compileObjects(parse, string));
// Arguments
for (CommandSection<?> section : sections) {
int start = section.getMatch().start;
int end = Math.min(section.getMatch().end, length);
StringMatch match = new StringMatch(string.subSequence(start, end).toString(), start, end);
if (lowest == -1 || start < lowest) {
lowest = start;
}
if (end > max) {
max = end;
}
Color color = palette.getColors().get(index % palette.getColors().size());
replace.put(match, (current, match1) -> {
if (current.getStyle().equals(Style.EMPTY)) {
return new FluidText(RawText.withColor(match1.match, color));
}
return new FluidText(new RawText(match1.match, current.getStyle()));
});
index += 1;
}
if (lowest > -1) {
replace.put(new StringMatch(text.getString().substring(0, lowest), 0, lowest), (current, match) -> {
if (current.getStyle().equals(Style.EMPTY)) {
return new FluidText(RawText.withColor(match.match, CommandColorerStorage.COMMAND_COLOR.config.get()));
}
return new FluidText(new RawText(match.match, current.getStyle()));
});
}
if (max != string.length()) {
replace.put(new StringMatch(text.getString().substring(max, string.length()), max, string.length()), (current, match) -> {
if (current.getStyle().equals(Style.EMPTY)) {
return new FluidText(RawText.withColor(match.match, CommandColorerStorage.ERROR_COLOR.config.get()));
}
return new FluidText(new RawText(match.match, current.getStyle()));
});
}
text.replaceStrings(replace);
return Optional.of(text);
}
use of io.github.darkkronicle.advancedchatcore.util.StringMatch in project AdvancedChatBox by DarkKronicle.
the class JSONFormatter method format.
@Override
public Optional<FluidText> format(FluidText text, @Nullable ParseResults<CommandSource> parse) {
String content = text.getString();
Optional<List<StringMatch>> omatches = SearchUtils.findMatches(content, "\\{.+\\}", FindType.REGEX);
if (!omatches.isPresent()) {
return Optional.empty();
}
List<StringMatch> matches = omatches.get();
HashMap<StringMatch, FluidText.StringInsert> replace = new HashMap<>();
for (StringMatch m : matches) {
replace.put(m, (current, match) -> colorJson(match.match));
}
text.replaceStrings(replace);
return Optional.of(text);
}
Aggregations