Skip to content

Commit

Permalink
perf: Improved default regex patterns performance
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Jul 1, 2022
1 parent f4a53c8 commit 9530834
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
import me.dreamerzero.chatregulator.listener.plugin.ReloadListener;
import me.dreamerzero.chatregulator.modules.Statistics;
import me.dreamerzero.chatregulator.utils.Constants;
import me.dreamerzero.chatregulator.utils.Placeholders;
import net.byteflux.libby.Library;
import net.byteflux.libby.VelocityLibraryManager;
import me.dreamerzero.chatregulator.placeholders.Placeholders;
import me.dreamerzero.chatregulator.placeholders.RegulatorExpansion;
import me.dreamerzero.chatregulator.placeholders.formatter.IFormatter;
import me.dreamerzero.chatregulator.placeholders.formatter.MiniPlaceholderFormatter;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/me/dreamerzero/chatregulator/config/Blacklist.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public static class Config{
Infractions module in commands and general chat""")
@Setting(value = "blocked-words")
private Pattern[] blockedPatterns = {
Pattern.compile("f(u|v|4)ck", Pattern.CASE_INSENSITIVE),
Pattern.compile("sh(i|@|l|j|1|y)t", Pattern.CASE_INSENSITIVE),
Pattern.compile("d(i|@|l|j|1|y)c(k)?", Pattern.CASE_INSENSITIVE),
Pattern.compile("b(i|@|l|j|1|y)tch", Pattern.CASE_INSENSITIVE),
Pattern.compile("(a|@|4|x)w(3|@|e|x)b(o|@|0|x|8)n(a|@|4|x)d(o|@|0|x|8)", Pattern.CASE_INSENSITIVE),
Pattern.compile("p(u|@|v)ssy", Pattern.CASE_INSENSITIVE),
Pattern.compile("f[uv4@]ck", Pattern.CASE_INSENSITIVE),
Pattern.compile("sh[i@lj1y]t", Pattern.CASE_INSENSITIVE),
Pattern.compile("d[i@lj1y]c(k)?", Pattern.CASE_INSENSITIVE),
Pattern.compile("b[i@lj1y]tch", Pattern.CASE_INSENSITIVE),
Pattern.compile("[a@4x]w[3@ex]b[o@0x8]n[a@4x]d[o@0x8]", Pattern.CASE_INSENSITIVE),
Pattern.compile("p[u@v]ssy", Pattern.CASE_INSENSITIVE),
Pattern.compile("(?:(?:https?|ftp|file):\\/\\/|www\\.|ftp\\.)(?:\\([-A-Z0-9+&@#\\/%=~_|$?!:,.]*\\)|[-A-Z0-9+&@#\\/%=~_|$?!:,.])*(?:\\([-A-Z0-9+&@#\\/%=~_|$?!:,.]*\\)|[A-Z0-9+&@#\\/%=~_|$])", Pattern.CASE_INSENSITIVE),
Pattern.compile("(i|@|l|j|1|y)mb(3|@|e|x)c(i|@|l|j|1|y)l", Pattern.CASE_INSENSITIVE),
Pattern.compile("m(o|@|0|x|8)th(3|@|e|x)rf(u|@|v)ck(3|@|e|x)r", Pattern.CASE_INSENSITIVE),
Pattern.compile("[i@lj1y]mb[3@ex]c[i@lj1y]l", Pattern.CASE_INSENSITIVE),
Pattern.compile("m[o@0x8]th[3@ex]rf[u@v]ck[3@ex]r", Pattern.CASE_INSENSITIVE),
Pattern.compile("\\$\\{(jndi|log4j|sys|env|main|marker|java|base64|lower|upper|web|docker|kubernetes|spring|jvmrunargs|date|ctx)\\:.*\\}", Pattern.CASE_INSENSITIVE)
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ public static void sendWarningMessage(InfractionPlayer infractor, Result result,
final TagResolver placeholder = TagResolver.resolver(
Placeholder.unparsed("infraction", result.getInfractionString()),
plugin.placeholders().getPlaceholders(infractor));
switch(((MainConfig.Warning)type.getConfig()).getWarningType()){
MainConfig.Warning config = (MainConfig.Warning)type.getConfig();
switch(config.getWarningType()){
case TITLE: sendTitle(message, infractor, placeholder); break;
case MESSAGE: infractor.sendMessage(plugin.getFormatter().parse(message, infractor.getPlayer(), placeholder)); break;
case ACTIONBAR: infractor.sendActionBar(plugin.getFormatter().parse(message, infractor.getPlayer(), placeholder)); break;
}
}

private static void sendTitle(String message, Audience player, TagResolver placeholder){
if (message.indexOf(';') != -1) {
int index = message.indexOf(';');
if (index != -1) {
sendSingleTitle(player, message, placeholder);
} else {
final String[] titleParts = message.split(";");
if(titleParts.length == 1){
if (titleParts.length == 1) {
sendSingleTitle(player, titleParts[0], placeholder);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.spongepowered.configurate.serialize.ScalarSerializer;
import org.spongepowered.configurate.serialize.SerializationException;

public final class CustomPatternSerializer extends ScalarSerializer<Pattern> {
final class CustomPatternSerializer extends ScalarSerializer<Pattern> {
CustomPatternSerializer() {
super(Pattern.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static class Commands extends CommandsConfig{}
public static class Flood implements Toggleable, Warning, Controllable, Executable {
@Comment("""
Enable flood check in the chat
(e.g.: \"aaaaaaaa\")""")
(e.g.: "aaaaaaaa")""")
private boolean enabled = true;

@Comment("""
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/me/dreamerzero/chatregulator/config/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,10 @@ public String getMessage(){
public static class General{
@Comment("""
Violation statistics message
This message will appear when using the \"/chatregulator stats\" command
This message will appear when using the "/chatregulator stats" command
Available Placeholders: <flood>, <spam>, <regular>, <command>, <unicode>, <caps>. <syntax>""")
private List<String> stats = List.of(
"<#3B4371>|-- <gradient:#67B26F:#4ca2cd>ChatRegulator</gradient> -------|",
"<#3B4371>|-- <gradient:#67B26F:#4ca2cd>ChatRegulator</gradient> -------| ",
"<#3B4371>| <red>General Stats</red>",
"<#3B4371>| <aqua>Regular Infractions:</aqua> <white><regular></white>",
"<#3B4371>| <aqua>Flood Infractions:</aqua> <white><flood></white>",
Expand All @@ -449,10 +449,10 @@ public static class General{

@Comment("""
Player statistics message
This message will appear when using the command \"/chatregulator player <someplayer>\"
This message will appear when using the command "/chatregulator player <someplayer>"
Available Placeholders: <player>, <flood>, <spam>, <regular>, <unicode>, <caps>, <syntax>""")
private List<String> player = List.of(
"<#3B4371>|-- <gradient:#67B26F:#4ca2cd>ChatRegulator</gradient> -------|",
"<#3B4371>|-- <gradient:#67B26F:#4ca2cd>ChatRegulator</gradient> -------| ",
"<#3B4371>| <gold><player></gold> <red>Stats</red>",
"<#3B4371>| <aqua>Regular Infractions:</aqua> <white><regular></white>",
"<#3B4371>| <aqua>Flood Infractions:</aqua> <white><flood></white>",
Expand Down Expand Up @@ -580,21 +580,21 @@ public static class Help{
@Comment("Plugin main help message")
@Setting(value = "main-help")
private List<String> main = List.of(
"<#3B4371>|-- <gradient:#67B26F:#4ca2cd>ChatRegulator</gradient> -------|",
"<#3B4371>|--- <gradient:#67B26F:#4ca2cd>ChatRegulator</gradient> -------|",
"<#3B4371>| <gold>+ <hover:show_text:'<gradient:#ffd89b:#19547b>Click on a section to view its commands'><gradient:#CAC531:#F3F9A7>Command Help</gradient></hover>",
"<#3B4371>| <hover:show_text:'<gradient:#ff4b1f:#ff9068>This command shows you the global statistics of infractions</gradient>'><gradient:#FF5F6D:#FFC371><command> <aqua>stats</aqua></hover>",
"<#3B4371>| <click:run_command:'/chatr help player'><hover:show_text:'<gradient:#ff4b1f:#ff9068>Obtain the infractions of a specific player</gradient> <gray>[<yellow>Click here for more</yellow>]'><gradient:#FF5F6D:#FFC371><command> <aqua>player</aqua></hover></click>",
"<#3B4371>| <click:run_command:'/chatr help reset'><hover:show_text:'<gradient:#ff4b1f:#ff9068>Resets a player infractions</gradient> <gray>[<yellow>Click here for more</yellow>]'><gradient:#FF5F6D:#FFC371><command> <aqua>reset</aqua></hover></click>",
"<#3B4371>| <click:run_command:'/chatr help clear'><hover:show_text:'<gradient:#ff4b1f:#ff9068>Cleans the chat of a player, server or network</gradient> <gray>[<yellow>Click here for more</yellow>]'><gradient:#FF5F6D:#FFC371><command> <aqua>clear</aqua></hover></click>",
"<#3B4371>|----------------------|"
"<#3B4371>|----------------------| "
);

@Comment("Help message for \"/chatregulator player <player>\" command")
private List<String> player = List.of(
"<#3B4371>|-- <gradient:#67B26F:#4ca2cd>ChatRegulator</gradient> -------|",
"<#3B4371>| <gold>+ <gradient:#CAC531:#F3F9A7>Player Help</gradient>",
"<#3B4371>| <click:suggest_command:'/chatr player <player>'><hover:show_text:'<gradient:#ff4b1f:#ff9068>This command shows you a player infractions</gradient>'><gradient:#FF5F6D:#FFC371><command> <aqua>player</aqua> <player></hover>",
"<#3B4371>|----------------------|"
"<#3B4371>|----------------------| "
);

@Comment("Help message for \"/chatregulator reset\" subcommands")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void onChat(final PlayerChatEvent event, final Continuation continuation)
return;
}
final Player player = event.getPlayer();
final AtomicReference<String> message = new AtomicReference<String>(event.getMessage());
final AtomicReference<String> message = new AtomicReference<>(event.getMessage());
final InfractionPlayer infractor = InfractionPlayer.get(player);
final EventWrapper<PlayerChatEvent> wrapper = new ChatWrapper(event, continuation);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.dreamerzero.chatregulator.utils;
package me.dreamerzero.chatregulator.placeholders;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ void illegalTest(){
String illegal = "ƕƘaea";
String expected = " aea";

UnicodeCheck check = UnicodeCheck.builder().controlType(ControlType.REPLACE).build();
UnicodeCheck check = UnicodeCheck.builder()
.controlType(ControlType.REPLACE)
.build();
Result result = check.check(illegal).join();

assertTrue(result.isInfraction());
Expand Down Expand Up @@ -80,7 +82,9 @@ void realTest(){

assertTrue(GeneralUtils.allowedPlayer(player, InfractionType.UNICODE));

UnicodeCheck check = UnicodeCheck.builder().controlType(ControlType.REPLACE).build();
UnicodeCheck check = UnicodeCheck.builder()
.controlType(ControlType.REPLACE)
.build();
Result result = check.check(randomMSG).join();

assertTrue(GeneralUtils.callViolationEvent(new EventBundle(InfractionPlayer.get(player), randomMSG, InfractionType.UNICODE, result, SourceType.CHAT), TestsUtils.createRegulator()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;

public final class PlaceholderTest {
final class PlaceholderTest {
private static final PlainTextComponentSerializer PLAIN_SERIALIZER = PlainTextComponentSerializer.plainText();

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import me.dreamerzero.chatregulator.objects.TestPlayer;
import me.dreamerzero.chatregulator.objects.TestPluginManager;
import me.dreamerzero.chatregulator.objects.TestProxy;
import me.dreamerzero.chatregulator.placeholders.Placeholders;
import me.dreamerzero.chatregulator.placeholders.formatter.IFormatter;
import me.dreamerzero.chatregulator.placeholders.formatter.NormalFormatter;

Expand Down

0 comments on commit 9530834

Please sign in to comment.