Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
HaHaWTH committed Mar 10, 2024
1 parent a38fcd6 commit d5afdb6
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/main/java/io/wdsj/asw/listener/AnvilListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static io.wdsj.asw.AdvancedSensitiveWords.*;
import static io.wdsj.asw.util.TimingUtils.addProcessStatistic;
import static io.wdsj.asw.util.Utils.getIgnoreFormatCodeRegex;
import static io.wdsj.asw.util.Utils.messagesFilteredNum;

public class AnvilListener implements Listener {
Expand All @@ -39,6 +40,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(getIgnoreFormatCodeRegex(), "");
List<String> censoredWords = AdvancedSensitiveWords.sensitiveWordBs.findAll(originalItemName);
if (!censoredWords.isEmpty()) {
long startTime = System.currentTimeMillis();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/wdsj/asw/listener/BookListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +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(Utils.getIgnoreFormatCodeRegex(), "");
boolean isBookCached = BookCache.isBookCached(originalPage);
List<String> censoredWordList = isBookCached && isCacheEnabled ? BookCache.getCachedBookSensitiveWordList(originalPage) : AdvancedSensitiveWords.sensitiveWordBs.findAll(originalPage);
if (!censoredWordList.isEmpty()) {
Expand All @@ -67,6 +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(Utils.getIgnoreFormatCodeRegex(), "");
List<String> censoredWordListAuthor = AdvancedSensitiveWords.sensitiveWordBs.findAll(originalAuthor);
if (!censoredWordListAuthor.isEmpty()) {
String processedAuthor = AdvancedSensitiveWords.sensitiveWordBs.replace(originalAuthor);
Expand All @@ -85,6 +87,7 @@ public void onBook(PlayerEditBookEvent event) {

String originalTitle = event.getNewBookMeta().getTitle();
if (originalTitle != null) {
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalTitle = originalTitle.replaceAll(Utils.getIgnoreFormatCodeRegex(), "");
List<String> censoredWordListTitle = AdvancedSensitiveWords.sensitiveWordBs.findAll(originalTitle);
if (!censoredWordListTitle.isEmpty()) {
String processedTitle = AdvancedSensitiveWords.sensitiveWordBs.replace(originalTitle);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/wdsj/asw/listener/BroadCastListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class BroadCastListener implements Listener {
public void onBroadCast(BroadcastMessageEvent event) {
if (!isInitialized || !settingsManager.getProperty(PluginSettings.CHAT_BROADCAST_CHECK)) return;
String originalMessage = event.getMessage();
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalMessage = originalMessage.replaceAll(Utils.getIgnoreFormatCodeRegex(), "");
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 @@ -34,6 +34,7 @@ public void onPlayerHeldItem(PlayerItemHeldEvent event) {
if (meta != null && meta.hasDisplayName()) {
String originalName = meta.getDisplayName();
long startTime = System.currentTimeMillis();
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) originalName = originalName.replaceAll(Utils.getIgnoreFormatCodeRegex(), "");
List<String> censoredWordList = sensitiveWordBs.findAll(originalName);
if (!censoredWordList.isEmpty()) {
messagesFilteredNum.getAndIncrement();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/wdsj/asw/listener/SignListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +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 != null) originalMessage = originalMessage.replaceAll(Utils.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,8 +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 = wrapperPlayClientChatMessage.getMessage();
if (shouldNotProcess(player, originalMessage)) return;
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
List<String> censoredWords = AdvancedSensitiveWords.sensitiveWordBs.findAll(originalMessage);
Expand Down Expand Up @@ -110,7 +109,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
}
} else if (packetType == PacketType.Play.Client.CHAT_COMMAND) {
WrapperPlayClientChatCommand wrapperPlayClientChatCommand = new WrapperPlayClientChatCommand(event);
String originalCommand = 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 = 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
18 changes: 0 additions & 18 deletions src/main/java/io/wdsj/asw/method/CharIgnore.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.github.houbb.sensitive.word.api.ISensitiveWordCharIgnore;
import com.github.houbb.sensitive.word.api.context.InnerSensitiveWordContext;
import io.wdsj.asw.setting.PluginSettings;
import io.wdsj.asw.util.Utils;

import java.util.Set;

Expand All @@ -13,23 +12,6 @@
public class CharIgnore implements ISensitiveWordCharIgnore {
@Override
public boolean ignore(int i, char[] chars, InnerSensitiveWordContext innerSensitiveWordContext) {
if (settingsManager.getProperty(PluginSettings.IGNORE_FORMAT_CODE)) {
String regex = Utils.getIgnoreFormatCodeRegex();
if (i + 1 < chars.length) {
String nextTwoChars = String.valueOf(chars[i]) +
chars[i + 1];
if (nextTwoChars.matches(regex)) {
return true;
}
}
if (i - 1 >= 0) {
String nextTwoChars = String.valueOf(chars[i - 1]) +
chars[i];
if (nextTwoChars.matches(regex)) {
return true;
}
}
}
Set<Character> SET = StringUtil.toCharSet(settingsManager.getProperty(PluginSettings.IGNORE_CHAR));
char c = chars[i];
return SET.contains(c);
Expand Down

0 comments on commit d5afdb6

Please sign in to comment.