Skip to content

Commit

Permalink
Cooler filters
Browse files Browse the repository at this point in the history
DarkKronicle committed Jan 31, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7c5c8b0 commit 4770101
Showing 5 changed files with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -7,20 +7,26 @@
*/
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;
import io.github.darkkronicle.advancedchatfilters.filters.ForwardFilter;
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<KonstructObject<?>> 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<KonstructObject<?>> list = new ArrayList<>();
for (StringMatch match : search.getMatches()) {
list.add(new StringMatchObject(match));
}
context.addLocalVariable("matches", Variable.of(
new ListObject(list)
));
return context;
}
}
Original file line number Diff line number Diff line change
@@ -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,18 +270,30 @@ 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(
RawText.withFormatting(String.valueOf(searchSuccess), searchSuccess ? Formatting.GREEN : Formatting.RED)
)
);
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) {
Original file line number Diff line number Diff line change
@@ -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<FluidText> 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);
Original file line number Diff line number Diff line change
@@ -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));
}

Original file line number Diff line number Diff line change
@@ -38,7 +38,6 @@ public Result processMatches(FluidText text, @Nullable FluidText unfiltered, @Nu
return Result.PROCESSED;
}


@Override
public JsonObject save() {
JsonObject obj = new JsonObject();

0 comments on commit 4770101

Please sign in to comment.