diff --git a/README.md b/README.md index 619e858..ac0cd06 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,31 @@ # AdvancedChatFilters -AdvancedChat filters allows for complex \ No newline at end of file +AdvancedChatFilters allows for complex manipulation of incoming chat messages. + +Please submit bugs to the [issue tracker](https://github.com/DarkKronicle/AdvancedChatFilters/issues). Join the [Discord](https://discord.gg/WnaE3uZxDA) for more help! + +## Dependencies + +[AdvancedChatCore](https://github.com/DarkKronicle/AdvancedChatCore) is required to run the mod. + +## Features + +- Replace an advanced search with specified text +- Change the color of the match +- Replace the full message +- Rainbow specific text +- Convert specific text to OwO +- Convert numbers into Roman Numerals +- Child filters for complex actions +- Send a chat message to the Action Bar +- Narrate a message +- Play a sound when a match is found +- Many complex options for in complex matching and replacing + +## Credits n' more + +Code & Mastermind: DarkKronicle + +Update to 1.16.3: lmichaelis + +Language & Proofreading: Chronos22 diff --git a/build.gradle b/build.gradle index a105151..0e70fe8 100644 --- a/build.gradle +++ b/build.gradle @@ -78,8 +78,15 @@ processResources { // ensure that the encoding is set to UTF-8, no matter what the system default is // this fixes some edge cases with special characters not displaying correctly // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html -tasks.withType(JavaCompile) { - options.encoding = "UTF-8" +tasks.withType(JavaCompile).configureEach { + // ensure that the encoding is set to UTF-8, no matter what the system default is + // this fixes some edge cases with special characters not displaying correctly + // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html + // If Javadoc is generated, this must be specified in that task too. + it.options.encoding = "UTF-8" + + // Minecraft 1.17 (21w19a) upwards uses Java 16. + it.options.release = 16 } // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task diff --git a/src/main/java/io/github/darkkronicle/advancedchatfilters/FiltersHandler.java b/src/main/java/io/github/darkkronicle/advancedchatfilters/FiltersHandler.java index 7222646..0538a0d 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatfilters/FiltersHandler.java +++ b/src/main/java/io/github/darkkronicle/advancedchatfilters/FiltersHandler.java @@ -80,7 +80,7 @@ public static ParentFilter createFilter(Filter filter) { if (!filter.getActive().config.getBooleanValue()) { return null; } - ParentFilter filt = new ParentFilter(filter.getFind(), filter.getFindString().config.getStringValue()); + ParentFilter filt = new ParentFilter(filter.getFind(), filter.getFindString().config.getStringValue(), filter.getStripColors().config.getBooleanValue()); if (filter.getReplace() != null) { if (filter.getReplace().useChildren()) { ReplaceFilter f = new ReplaceFilter(filter.getReplaceTo().config.getStringValue().replaceAll("&", "§"), filter.getReplace(), null); diff --git a/src/main/java/io/github/darkkronicle/advancedchatfilters/config/Filter.java b/src/main/java/io/github/darkkronicle/advancedchatfilters/config/Filter.java index 5ec075a..05cfb22 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatfilters/config/Filter.java +++ b/src/main/java/io/github/darkkronicle/advancedchatfilters/config/Filter.java @@ -62,6 +62,12 @@ private static String translate(String key) { private ConfigStorage.SaveableConfig active = ConfigStorage.SaveableConfig.fromConfig("active", new ConfigBoolean(translate("active"), false, translate("info.active"))); + /** + * Whether or not it should be used to filter chat messages currently. + */ + private ConfigStorage.SaveableConfig stripColors = ConfigStorage.SaveableConfig.fromConfig("stripColors", + new ConfigBoolean(translate("stripcolors"), true, translate("info.stripcolors"))); + /** * The Expression to find a match. The way it is interpreted is defined by findType. */ @@ -117,6 +123,7 @@ public IMatchReplace getReplace() { private final ImmutableList> options = ImmutableList.of( name, active, + stripColors, findString, findType, replaceType, 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 300a89f..719329d 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/ParentFilter.java +++ b/src/main/java/io/github/darkkronicle/advancedchatfilters/filters/ParentFilter.java @@ -1,6 +1,8 @@ package io.github.darkkronicle.advancedchatfilters.filters; import io.github.darkkronicle.advancedchatcore.util.FindType; +import io.github.darkkronicle.advancedchatcore.util.RawText; +import io.github.darkkronicle.advancedchatcore.util.StringMatch; import io.github.darkkronicle.advancedchatfilters.FiltersHandler; import io.github.darkkronicle.advancedchatfilters.interfaces.IFilter; import io.github.darkkronicle.advancedchatcore.util.ColorUtil; @@ -10,6 +12,7 @@ import lombok.Value; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.text.Style; import java.util.ArrayList; import java.util.List; @@ -31,12 +34,14 @@ public static class FilterResult { private List forwardFilters; private final FindType findType; private final String findString; + private final boolean stripColors; - public ParentFilter(FindType findType, String findString) { + public ParentFilter(FindType findType, String findString, boolean stripColors) { filters = new ArrayList<>(); forwardFilters = new ArrayList<>(); this.findString = findString; this.findType = findType; + this.stripColors = stripColors; } public List getFilters() { @@ -53,8 +58,44 @@ public void addForwardFilter(ForwardFilter forwardFilter) { forwardFilters.add(forwardFilter); } + private static String getWithColors(FluidText text) { + StringBuilder builder = new StringBuilder(); + Style previous = Style.EMPTY; + for (RawText t : text.getRawTexts()) { + if (t.getStyle().getColor() != null && !t.getStyle().getColor().equals(previous.getColor())) { + previous = t.getStyle(); + String hex = Integer.toHexString(previous.getColor().getRgb()); + builder.append(hex); + } + builder.append(t.getMessage()); + } + return builder.toString(); + } + + private static String getWithoutColors(String input) { + SearchResult hex = SearchResult.searchOf(input, "§\\[[a-fA-F0-9]{6}\\]", FindType.REGEX); + int last = -1; + StringBuilder builder = new StringBuilder(); + for (StringMatch match : hex.getMatches()) { + if (last <= -1) { + last = match.end; + continue; + } + builder.append(input, last, match.start); + last = match.end; + } + builder.append(input, last, input.length()); + return builder.toString(); + } + public FilterResult filter(FluidText text, FluidText unfiltered) { - SearchResult search = SearchResult.searchOf(text.getString(), findString, findType); + String searchString; + if (stripColors) { + searchString = getWithColors(text); + } else { + searchString = text.getString(); + } + SearchResult search = SearchResult.searchOf(searchString, findString, findType); if (search.size() == 0) { return FilterResult.EMPTY; } diff --git a/src/main/resources/assets/advancedchatfilters/icon.png b/src/main/resources/assets/advancedchatfilters/icon.png new file mode 100644 index 0000000..62d7825 Binary files /dev/null and b/src/main/resources/assets/advancedchatfilters/icon.png differ