add custom regex ability
and yes, readme is vibed XD
This commit is contained in:
@@ -16,11 +16,35 @@ By default the mod is enabled. Toggle it with:
|
||||
|
||||
## What it filters
|
||||
|
||||
When enabled, any player or system chat message matching either of these regex patterns (case-insensitive) is silently ignored:
|
||||
When enabled, any player or system chat message matching any of the configured regex patterns (case-insensitive) is silently ignored.
|
||||
|
||||
The default patterns are:
|
||||
|
||||
- `/.*cxvqo.*/i`
|
||||
- `/.*denisapain.*/i`
|
||||
|
||||
## Regex pattern management
|
||||
|
||||
Patterns can be added, removed, and listed at runtime. Each pattern is matched against the whole message, so include `.*` on both sides to ignore messages that merely contain text. Added patterns are compiled case-insensitively.
|
||||
|
||||
```
|
||||
/cxvqoignore addregex <regex>
|
||||
/cxvqoignore removeregex <index>
|
||||
/cxvqoignore listregex
|
||||
```
|
||||
|
||||
- `addregex <regex>` — adds a new pattern, e.g. `/cxvqoignore addregex .*skiddie.*`. The regex must be valid Java regex syntax, or the mod reports an error.
|
||||
- `removeregex <index>` — removes the pattern at the given index (see the indexes shown by `listregex`).
|
||||
- `listregex` — lists every configured pattern, each prefixed with its index for use with `removeregex`.
|
||||
|
||||
Example — ignore everyone whose name starts with "Foo":
|
||||
|
||||
```
|
||||
/cxvqoignore addregex .*Foo.*
|
||||
/cxvqoignore listregex
|
||||
/cxvqoignore removeregex 2
|
||||
```
|
||||
|
||||
## Requirements
|
||||
|
||||
- Minecraft: Java Edition **26.2**
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
package de.caydenno1.cxvqoignore.client;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.fabric.api.client.command.v2.ClientCommands;
|
||||
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
|
||||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
||||
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
|
||||
@@ -11,6 +21,7 @@ import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
|
||||
import net.minecraft.network.chat.Component;
|
||||
|
||||
import de.caydenno1.cxvqoignore.CxvqoIgnore;
|
||||
import de.caydenno1.cxvqoignore.CxvqoIgnoreRegexes;
|
||||
|
||||
public class CxvqoIgnoreClient implements ClientModInitializer {
|
||||
@Override
|
||||
@@ -20,7 +31,14 @@ public class CxvqoIgnoreClient implements ClientModInitializer {
|
||||
.then(LiteralArgumentBuilder.<FabricClientCommandSource>literal("enable")
|
||||
.executes(CxvqoIgnoreClient::enable))
|
||||
.then(LiteralArgumentBuilder.<FabricClientCommandSource>literal("disable")
|
||||
.executes(CxvqoIgnoreClient::disable)))
|
||||
.executes(CxvqoIgnoreClient::disable))
|
||||
.then(ClientCommands.argument("regex", StringArgumentType.greedyString())
|
||||
.executes(CxvqoIgnoreClient::addRegex))
|
||||
.then(ClientCommands.argument("index", IntegerArgumentType.integer(0))
|
||||
.suggests(CxvqoIgnoreClient::suggestIndexes)
|
||||
.executes(CxvqoIgnoreClient::removeRegex))
|
||||
.then(LiteralArgumentBuilder.<FabricClientCommandSource>literal("listregex")
|
||||
.executes(CxvqoIgnoreClient::listRegex)))
|
||||
);
|
||||
|
||||
ClientReceiveMessageEvents.ALLOW_CHAT.register((message, playerChatMessage, sender, boundChatType, timeStamp) ->
|
||||
@@ -42,4 +60,47 @@ public class CxvqoIgnoreClient implements ClientModInitializer {
|
||||
ctx.getSource().sendFeedback(Component.literal("CxvqoIgnore disabled"));
|
||||
return 1;
|
||||
}
|
||||
|
||||
private static int addRegex(CommandContext<FabricClientCommandSource> ctx) {
|
||||
String regex = StringArgumentType.getString(ctx, "regex");
|
||||
try {
|
||||
CxvqoIgnoreRegexes.add(regex);
|
||||
} catch (PatternSyntaxException e) {
|
||||
ctx.getSource().sendError(Component.literal("Invalid regex: " + e.getDescription()));
|
||||
return 0;
|
||||
}
|
||||
ctx.getSource().sendFeedback(Component.literal("Added regex: " + regex));
|
||||
return 1;
|
||||
}
|
||||
|
||||
private static int removeRegex(CommandContext<FabricClientCommandSource> ctx) {
|
||||
int index = IntegerArgumentType.getInteger(ctx, "index");
|
||||
if (CxvqoIgnoreRegexes.remove(index)) {
|
||||
ctx.getSource().sendFeedback(Component.literal("Removed regex at index " + index));
|
||||
return 1;
|
||||
}
|
||||
ctx.getSource().sendError(Component.literal("No regex at index " + index));
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static int listRegex(CommandContext<FabricClientCommandSource> ctx) {
|
||||
List<Pattern> patterns = CxvqoIgnoreRegexes.getPatterns();
|
||||
if (patterns.isEmpty()) {
|
||||
ctx.getSource().sendFeedback(Component.literal("No regex patterns configured"));
|
||||
return 1;
|
||||
}
|
||||
ctx.getSource().sendFeedback(Component.literal("Configured regex patterns (" + patterns.size() + "):"));
|
||||
for (int i = 0; i < patterns.size(); i++) {
|
||||
ctx.getSource().sendFeedback(Component.literal("[" + i + "] " + patterns.get(i).pattern()));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
private static CompletableFuture<Suggestions> suggestIndexes(CommandContext<FabricClientCommandSource> ctx, SuggestionsBuilder builder) {
|
||||
List<Pattern> patterns = CxvqoIgnoreRegexes.getPatterns();
|
||||
for (int i = 0; i < patterns.size(); i++) {
|
||||
builder.suggest(String.valueOf(i));
|
||||
}
|
||||
return builder.buildFuture();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package de.caydenno1.cxvqoignore;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
|
||||
import net.minecraft.network.chat.Component;
|
||||
@@ -14,11 +12,6 @@ public class CxvqoIgnore implements ModInitializer {
|
||||
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
||||
|
||||
public static final Pattern IGNORE_PATTERN = Pattern.compile(
|
||||
"(?:.*cxvqo.*)|(?:.*denisapain.*)",
|
||||
Pattern.CASE_INSENSITIVE
|
||||
);
|
||||
|
||||
private static boolean enabled = true;
|
||||
|
||||
public static boolean isEnabled() {
|
||||
@@ -30,7 +23,7 @@ public class CxvqoIgnore implements ModInitializer {
|
||||
}
|
||||
|
||||
public static boolean shouldIgnore(Component message) {
|
||||
return enabled && IGNORE_PATTERN.matcher(message.getString()).matches();
|
||||
return enabled && CxvqoIgnoreRegexes.shouldIgnore(message.getString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package de.caydenno1.cxvqoignore;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
public class CxvqoIgnoreRegexes {
|
||||
private static final List<Pattern> patterns = new ArrayList<>();
|
||||
|
||||
static {
|
||||
patterns.add(Pattern.compile("(?:.*cxvqo.*)", Pattern.CASE_INSENSITIVE));
|
||||
patterns.add(Pattern.compile("(?:.*denisapain.*)", Pattern.CASE_INSENSITIVE));
|
||||
}
|
||||
|
||||
public static void add(String regex) throws PatternSyntaxException {
|
||||
patterns.add(Pattern.compile(regex, Pattern.CASE_INSENSITIVE));
|
||||
}
|
||||
|
||||
public static boolean remove(int index) {
|
||||
if (index < 0 || index >= patterns.size()) {
|
||||
return false;
|
||||
}
|
||||
patterns.remove(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static List<Pattern> getPatterns() {
|
||||
return Collections.unmodifiableList(patterns);
|
||||
}
|
||||
|
||||
public static boolean shouldIgnore(String message) {
|
||||
for (Pattern pattern : patterns) {
|
||||
if (pattern.matcher(message).matches()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user