Skip to content

Commit

Permalink
Make regex configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
HaHaWTH committed Mar 10, 2024
1 parent eab48b9 commit b5cf9a6
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/main/java/io/wdsj/asw/AdvancedSensitiveWords.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public final class AdvancedSensitiveWords extends JavaPlugin {
public static SettingsManager settingsManager;
public static SettingsManager messagesManager;
private static AdvancedSensitiveWords instance;
public static final String IGNORE_FORMAT_CODE_REGEX = "[§&][a-zA-Z\\d]";
private static TaskScheduler scheduler;

public static TaskScheduler getScheduler() {
Expand Down Expand Up @@ -158,6 +157,9 @@ public void doInitTasks() {
isInitialized = true;
});
}
public static String getIgnoreFormatCodeRegex() {
return "[§" + settingsManager.getProperty(PluginSettings.ALT_COLOR_CODE) + "][0-9A-Fa-fK-Ok-oRr]";
}

@Override
public void onDisable() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/wdsj/asw/listener/AnvilListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void onAnvil(InventoryClickEvent event) {
ItemMeta itemMeta = outputItem.getItemMeta();
if (itemMeta != null && itemMeta.hasDisplayName()) {
String originalItemName = itemMeta.getDisplayName();
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalItemName = originalItemName.replaceAll(IGNORE_FORMAT_CODE_REGEX, "");
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalItemName = originalItemName.replaceAll(getIgnoreFormatCodeRegex(), "");
List<String> censoredWords = AdvancedSensitiveWords.sensitiveWordBs.findAll(originalItemName);
if (!censoredWords.isEmpty()) {
long startTime = System.currentTimeMillis();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/wdsj/asw/listener/BookListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void onBook(PlayerEditBookEvent event) {
if (bookMeta.hasPages()) {
for (String originalPage : originalPages) {
if (skipReturnLine) originalPage = originalPage.replace("\n", "").replace("§0", "");
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalPage = originalPage.replaceAll(IGNORE_FORMAT_CODE_REGEX, "");
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalPage = originalPage.replaceAll(getIgnoreFormatCodeRegex(), "");
boolean isBookCached = BookCache.isBookCached(originalPage);
List<String> censoredWordList = isBookCached && isCacheEnabled ? BookCache.getCachedBookSensitiveWordList(originalPage) : AdvancedSensitiveWords.sensitiveWordBs.findAll(originalPage);
if (!censoredWordList.isEmpty()) {
Expand All @@ -68,7 +68,7 @@ public void onBook(PlayerEditBookEvent event) {
}
String originalAuthor = event.getNewBookMeta().getAuthor();
if (originalAuthor != null) {
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalAuthor = originalAuthor.replaceAll(IGNORE_FORMAT_CODE_REGEX, "");
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalAuthor = originalAuthor.replaceAll(getIgnoreFormatCodeRegex(), "");
List<String> censoredWordListAuthor = AdvancedSensitiveWords.sensitiveWordBs.findAll(originalAuthor);
if (!censoredWordListAuthor.isEmpty()) {
String processedAuthor = AdvancedSensitiveWords.sensitiveWordBs.replace(originalAuthor);
Expand All @@ -88,7 +88,7 @@ public void onBook(PlayerEditBookEvent event) {
String originalTitle = event.getNewBookMeta().getTitle();
if (originalTitle != null) {
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE))
originalTitle = originalTitle.replaceAll(IGNORE_FORMAT_CODE_REGEX, "");
originalTitle = originalTitle.replaceAll(getIgnoreFormatCodeRegex(), "");
List<String> censoredWordListTitle = AdvancedSensitiveWords.sensitiveWordBs.findAll(originalTitle);
if (!censoredWordListTitle.isEmpty()) {
String processedTitle = AdvancedSensitiveWords.sensitiveWordBs.replace(originalTitle);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/wdsj/asw/listener/BroadCastListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class BroadCastListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onBroadCast(BroadcastMessageEvent event) {
if (!isInitialized || !settingsManager.getProperty(PluginSettings.CHAT_BROADCAST_CHECK)) return;
String originalMessage = settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE) ? event.getMessage().replaceAll(IGNORE_FORMAT_CODE_REGEX, "") : event.getMessage();
String originalMessage = settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE) ? event.getMessage().replaceAll(getIgnoreFormatCodeRegex(), "") : event.getMessage();
long startTime = System.currentTimeMillis();
List<String> censoredWordList = sensitiveWordBs.findAll(originalMessage);
if (!censoredWordList.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void onPlayerHeldItem(PlayerItemHeldEvent event) {
ItemMeta meta = item.getItemMeta();
if (meta != null && meta.hasDisplayName()) {
String originalName = meta.getDisplayName();
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalName = originalName.replaceAll(getIgnoreFormatCodeRegex(), "");
long startTime = System.currentTimeMillis();
List<String> censoredWordList = sensitiveWordBs.findAll(originalName);
if (!censoredWordList.isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/wdsj/asw/listener/SignListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void onSign(SignChangeEvent event) {
StringBuilder originalMultiMessages = new StringBuilder();
for (int line = 0; line < 4; ++line) {
String originalMessage = event.getLine(line);
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalMessage = originalMessage.replaceAll(IGNORE_FORMAT_CODE_REGEX, "");
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE) && originalMessage != null) originalMessage = originalMessage.replaceAll(getIgnoreFormatCodeRegex(), "");
assert originalMessage != null;
List<String> censoredWordList = AdvancedSensitiveWords.sensitiveWordBs.findAll(originalMessage);
if (!censoredWordList.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
String userName = user.getName();
if (packetType == PacketType.Play.Client.CHAT_MESSAGE) {
WrapperPlayClientChatMessage wrapperPlayClientChatMessage = new WrapperPlayClientChatMessage(event);
String originalMessage = settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE) ? wrapperPlayClientChatMessage.getMessage().replaceAll(IGNORE_FORMAT_CODE_REGEX, "") : wrapperPlayClientChatMessage.getMessage();
String originalMessage = settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE) ? wrapperPlayClientChatMessage.getMessage().replaceAll(getIgnoreFormatCodeRegex(), "") : wrapperPlayClientChatMessage.getMessage();
if (shouldNotProcess(player, originalMessage)) return;
long startTime = System.currentTimeMillis();
// Word check
Expand Down Expand Up @@ -110,7 +110,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
}
} else if (packetType == PacketType.Play.Client.CHAT_COMMAND) {
WrapperPlayClientChatCommand wrapperPlayClientChatCommand = new WrapperPlayClientChatCommand(event);
String originalCommand = settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE) ? wrapperPlayClientChatCommand.getCommand().replaceAll(IGNORE_FORMAT_CODE_REGEX, "") : wrapperPlayClientChatCommand.getCommand();
String originalCommand = settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE) ? wrapperPlayClientChatCommand.getCommand().replaceAll(getIgnoreFormatCodeRegex(), "") : wrapperPlayClientChatCommand.getCommand();
if (shouldNotProcess(player, "/" + originalCommand)) return;
long startTime = System.currentTimeMillis();
List<String> censoredWords = AdvancedSensitiveWords.sensitiveWordBs.findAll(originalCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void onPacketReceiving(@NotNull com.comphenix.protocol.events.PacketEvent
if (event.getPacketType() == com.comphenix.protocol.PacketType.Play.Client.CHAT && isInitialized) {
Player player = event.getPlayer();
assert player != null; // In some cases, player maybe null
String message = settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE) ? event.getPacket().getStrings().read(0).replaceAll(IGNORE_FORMAT_CODE_REGEX, "") : event.getPacket().getStrings().read(0);
String message = settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE) ? event.getPacket().getStrings().read(0).replaceAll(getIgnoreFormatCodeRegex(), "") : event.getPacket().getStrings().read(0);
if (isCommandAndWhiteListed(message) || player.hasPermission("advancedsensitivewords.bypass"))
return;
if (isAuthMeAvailable && settingsManager.getProperty(PluginSettings.ENABLE_AUTHME_COMPATIBILITY)) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/wdsj/asw/setting/PluginSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ public class PluginSettings implements SettingsHolder {
@Comment({"默认跳过字符",
"Default characters to ignore"})
public static final Property<String> IGNORE_CHAR = newProperty("Plugin.ignoreChar", "`-—=~~!!@#$%^&§*()_+[]{}\\|;:'\"“”,,.。、()<>??¥【】《》 ");
@Comment({"忽略格式化符号§&",
@Comment({"忽略格式化符号§",
"Ignore format codes"})
public static final Property<Boolean> IGNORE_FORMAT_CODE = newProperty("Plugin.ignoreFormatCode", false);
@Comment({"备用格式化符号",
"Alternate code codes"})
public static final Property<String> ALT_COLOR_CODE = newProperty("Plugin.altColorCode", "&");

@Comment({"检测大小写",
"Whether to ignore case"})
Expand Down

0 comments on commit b5cf9a6

Please sign in to comment.