diff --git a/src/main/java/io/github/darkkronicle/advancedchatfilters/FiltersHandler.java b/src/main/java/io/github/darkkronicle/advancedchatfilters/FiltersHandler.java index 12f44a0..990e06e 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatfilters/FiltersHandler.java +++ b/src/main/java/io/github/darkkronicle/advancedchatfilters/FiltersHandler.java @@ -7,13 +7,18 @@ */ package io.github.darkkronicle.advancedchatfilters; +import io.github.darkkronicle.Konstruct.functions.Variable; import io.github.darkkronicle.Konstruct.parser.NodeProcessor; import io.github.darkkronicle.Konstruct.parser.ParseContext; +import io.github.darkkronicle.Konstruct.type.KonstructObject; +import io.github.darkkronicle.Konstruct.type.ListObject; import io.github.darkkronicle.advancedchatcore.interfaces.IMessageFilter; import io.github.darkkronicle.advancedchatcore.konstruct.AdvancedChatKonstruct; +import io.github.darkkronicle.advancedchatcore.konstruct.StringMatchObject; import io.github.darkkronicle.advancedchatcore.util.Color; import io.github.darkkronicle.advancedchatcore.util.FluidText; import io.github.darkkronicle.advancedchatcore.util.SearchResult; +import io.github.darkkronicle.advancedchatcore.util.StringMatch; import io.github.darkkronicle.advancedchatfilters.config.Filter; import io.github.darkkronicle.advancedchatfilters.config.FiltersConfigStorage; import io.github.darkkronicle.advancedchatfilters.filters.ColorFilter; @@ -21,6 +26,7 @@ import io.github.darkkronicle.advancedchatfilters.filters.ParentFilter; import io.github.darkkronicle.advancedchatfilters.filters.ReplaceFilter; import java.util.ArrayList; +import java.util.List; import java.util.Optional; import io.github.darkkronicle.advancedchatfilters.filters.processors.ActionBarProcessor; @@ -139,12 +145,30 @@ public static ParentFilter createFilter(Filter filter) { return filt; } - public ParseContext createFilterContext(ReplaceFilter filter, FluidText text, SearchResult result) { + public ParseContext createFilterContext(ReplaceFilter filter, FluidText text, SearchResult result, StringMatch match) { ParseContext context = processor.createContext(); + context.addLocalVariable("input", Variable.of(text.getString())); + context.addLocalVariable("match", Variable.of(new StringMatchObject(match))); + List> list = new ArrayList<>(); + for (StringMatch m : result.getMatches()) { + list.add(new StringMatchObject(m)); + } + context.addLocalVariable("matches", Variable.of( + new ListObject(list) + )); return context; } public ParseContext createTextContext(FluidText text, SearchResult search) { - return processor.createContext(); + ParseContext context = processor.createContext(); + context.addLocalVariable("input", Variable.of(text.getString())); + List> list = new ArrayList<>(); + for (StringMatch match : search.getMatches()) { + list.add(new StringMatchObject(match)); + } + context.addLocalVariable("matches", Variable.of( + new ListObject(list) + )); + return context; } } diff --git a/src/main/java/io/github/darkkronicle/advancedchatfilters/config/gui/GuiFilterEditor.java b/src/main/java/io/github/darkkronicle/advancedchatfilters/config/gui/GuiFilterEditor.java index b06fd53..c62f587 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatfilters/config/gui/GuiFilterEditor.java +++ b/src/main/java/io/github/darkkronicle/advancedchatfilters/config/gui/GuiFilterEditor.java @@ -18,6 +18,7 @@ import fi.dy.masa.malilib.gui.button.IButtonActionListener; import fi.dy.masa.malilib.gui.widgets.WidgetDropDownList; import fi.dy.masa.malilib.util.StringUtils; +import io.github.darkkronicle.Konstruct.NodeException; import io.github.darkkronicle.advancedchatcore.ModuleHandler; import io.github.darkkronicle.advancedchatcore.config.gui.widgets.WidgetColor; import io.github.darkkronicle.advancedchatcore.config.gui.widgets.WidgetLabelHoverable; @@ -234,7 +235,7 @@ y, getWidth() / 2 - 1, replaceString.setMaxLength(64000); replaceString.setText(filter.getReplaceTo().config.getStringValue()); - y = backgroundColor.getY() + 20; + y = backgroundColor.getY() + 25; String testText = StringUtils.translate("advancedchatfilters.button.test"); int testWidth = StringUtils.getStringWidth(testText) + 10; @@ -254,11 +255,13 @@ private void updateTestMessage() { filter.getReplace(), filter.getReplaceTextColor().config.getBooleanValue() ? filter.getTextColor().config.get() : null ); - ParentFilter parent = new ParentFilter( - filter.getFind(), - filter.getFindString().config.getStringValue().replace("&", "§"), - filter.getStripColors().config.getBooleanValue() - ); + ParentFilter parent; + parent = new ParentFilter( + filter.getFind(), + filter.getFindString().config.getStringValue().replace("&", "§"), + filter.getStripColors().config.getBooleanValue() + ); + outputMessage = new ArrayList<>(); FluidText inputText = new FluidText(RawText.withFormatting("Input Message: ", Formatting.BOLD, Formatting.GRAY)); String testString = test.getText().replaceAll("&", "§"); @@ -267,7 +270,14 @@ private void updateTestMessage() { } else { outputMessage.add(inputText.append(new RawText(testString, Style.EMPTY))); } - SearchResult result = SearchResult.searchOf(testString, parent.getFindString(), parent.getFindType()); + SearchResult result; + try { + result = SearchResult.searchOf(testString, parent.getFindString(), parent.getFindType()); + } catch (Exception e) { + outputMessage = new ArrayList<>(); + outputMessage.add(new FluidText(RawText.withFormatting("RegEx parsing error! " + e.getMessage(), Formatting.RED))); + return; + } boolean searchSuccess = result.size() > 0; outputMessage.add(new FluidText(RawText.withFormatting("Matched: ", Formatting.BOLD, Formatting.GRAY)) .append( @@ -275,10 +285,15 @@ private void updateTestMessage() { ) ); FluidText input = StyleFormatter.formatText(new FluidText(new RawText(testString, Style.EMPTY))); - FluidText output = StyleFormatter.formatText(testFilter.filter(parent, input, input, result).orElse(input)); - FluidText outputText = new FluidText(RawText.withFormatting("Output Message: ", Formatting.BOLD, Formatting.GRAY)); - outputText.getRawTexts().addAll(output.getRawTexts()); - outputMessage.add(outputText); + try { + FluidText output = StyleFormatter.formatText(testFilter.filter(parent, input, input, result).orElse(input)); + FluidText outputText = new FluidText(RawText.withFormatting("Output Message: ", Formatting.BOLD, Formatting.GRAY)); + outputText.getRawTexts().addAll(output.getRawTexts()); + outputMessage.add(outputText); + } catch (NodeException e) { + outputMessage = new ArrayList<>(); + outputMessage.add(new FluidText(RawText.withFormatting("Konstruct error! " + e.getMessage(), Formatting.RED))); + } } private int addLabel(int x, int y, IConfigBase config) { diff --git a/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/ParentFilter.java b/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/ParentFilter.java index 5833e54..f7e5108 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/ParentFilter.java +++ b/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/ParentFilter.java @@ -7,12 +7,7 @@ */ package io.github.darkkronicle.advancedchatfilters.filters; -import io.github.darkkronicle.advancedchatcore.util.Color; -import io.github.darkkronicle.advancedchatcore.util.FindType; -import io.github.darkkronicle.advancedchatcore.util.FluidText; -import io.github.darkkronicle.advancedchatcore.util.RawText; -import io.github.darkkronicle.advancedchatcore.util.SearchResult; -import io.github.darkkronicle.advancedchatcore.util.StringMatch; +import io.github.darkkronicle.advancedchatcore.util.*; import io.github.darkkronicle.advancedchatfilters.FiltersHandler; import io.github.darkkronicle.advancedchatfilters.interfaces.IFilter; import java.util.ArrayList; @@ -156,7 +151,7 @@ public FilterResult filter(FluidText text, FluidText unfiltered) { for (IFilter filter : filters) { Optional newtext = filter.filter(this, text, unfiltered, search); if (newtext.isPresent()) { - text = newtext.get(); + text = StyleFormatter.formatText(newtext.get()); if (color != null) { // Make sure forward filter gets the correct background color text.setBackground(color); diff --git a/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/matchreplace/OnlyMatchTextReplace.java b/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/matchreplace/OnlyMatchTextReplace.java index 301ea63..2ff047e 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/matchreplace/OnlyMatchTextReplace.java +++ b/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/matchreplace/OnlyMatchTextReplace.java @@ -48,8 +48,9 @@ public static FluidText.StringInsert getReplacement(ReplaceFilter filter, FluidT } public static FluidText formatMessage(RawText current, ReplaceFilter filter, FluidText text, SearchResult result, StringMatch match) { - ParseContext context = FiltersHandler.getInstance().createFilterContext(filter, text, result); + ParseContext context = FiltersHandler.getInstance().createFilterContext(filter, text, result, match); String message = filter.replaceTo.parse(context).getContent().getString(); + message = result.getGroupReplacements(message, getMatchIndex(result, match)); return new FluidText(current.withMessage(message)); } diff --git a/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/processors/KonstructProcessor.java b/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/processors/KonstructProcessor.java index f8a4f18..9963e3b 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/processors/KonstructProcessor.java +++ b/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/processors/KonstructProcessor.java @@ -38,7 +38,6 @@ public Result processMatches(FluidText text, @Nullable FluidText unfiltered, @Nu return Result.PROCESSED; } - @Override public JsonObject save() { JsonObject obj = new JsonObject();