Skip to content

Commit

Permalink
Add broadcast detection
Browse files Browse the repository at this point in the history
  • Loading branch information
HaHaWTH committed Mar 3, 2024
1 parent 2a57d17 commit e699776
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/io/wdsj/asw/AdvancedSensitiveWords.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public void onEnable() {
if (settingsManager.getProperty(PluginSettings.ENABLE_PLAYER_NAME_CHECK)) {
getServer().getPluginManager().registerEvents(new PlayerLoginListener(), this);
}
if (settingsManager.getProperty(PluginSettings.CHAT_BROADCAST_CHECK)) {
getServer().getPluginManager().registerEvents(new BroadCastListener(), this);
}
long endTime = System.currentTimeMillis();
getLogger().info("AdvancedSensitiveWords is enabled!(took " + (endTime - startTime) + "ms)");
// bro, don't bytecode this, you can just disable it in the config TAT
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/io/wdsj/asw/listener/BroadCastListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.wdsj.asw.listener;

import io.wdsj.asw.setting.PluginSettings;
import io.wdsj.asw.util.Utils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.server.BroadcastMessageEvent;

import java.util.List;

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

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 = event.getMessage();
long startTime = System.currentTimeMillis();
List<String> censoredWordList = sensitiveWordBs.findAll(originalMessage);
if (!censoredWordList.isEmpty()) {
messagesFilteredNum.getAndIncrement();
String processedMessage = sensitiveWordBs.replace(originalMessage);
if (settingsManager.getProperty(PluginSettings.CHAT_METHOD).equalsIgnoreCase("cancel")) {
event.setCancelled(true);
} else {
event.setMessage(processedMessage);
}
if (settingsManager.getProperty(PluginSettings.LOG_VIOLATION)) {
Utils.logViolation("服务器广播消息(IP: 无)(Chat)(BroadCast)", originalMessage + censoredWordList);
}
long endTime = System.currentTimeMillis();
addProcessStatistic(endTime, startTime);
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/io/wdsj/asw/setting/PluginSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class PluginSettings implements SettingsHolder {
public static final Property<Boolean> CHAT_FAKE_MESSAGE_ON_CANCEL = newProperty("Chat.fakeMessageOnCancel", false);
@Comment("是否发送消息提醒(和假消息冲突)")
public static final Property<Boolean> CHAT_SEND_MESSAGE = newProperty("Chat.sendMessage", true);
@Comment("*是否启用服务器广播消息检测(仅提供取消和替换模式,配置跟随聊天检测)(不会触发API事件)")
public static final Property<Boolean> CHAT_BROADCAST_CHECK = newProperty("Chat.broadcastCheck", true);
@Comment("是否开启聊天上下文检测(仅提供取消和假消息模式,配置跟随聊天检测)")
public static final Property<Boolean> CHAT_CONTEXT_CHECK = newProperty("Chat.contextCheck", false);
@Comment("最大检测上下文大小")
Expand Down

0 comments on commit e699776

Please sign in to comment.