Skip to content

Commit

Permalink
Add alternative event-based chat listener & make all config reloadable
Browse files Browse the repository at this point in the history
  • Loading branch information
HaHaWTH committed Mar 3, 2024
1 parent 04161db commit 57cfde5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
17 changes: 4 additions & 13 deletions src/main/java/io/wdsj/asw/AdvancedSensitiveWords.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,10 @@ public void onEnable() {
metrics.addCustomChart(new SimplePie("default_list", () -> String.valueOf(settingsManager.getProperty(PluginSettings.ENABLE_DEFAULT_WORDS))));
metrics.addCustomChart(new SimplePie("mode", () -> checkProtocolLib() ? "Fast" : "Compatibility"));
metrics.addCustomChart(new SimplePie("java_vendor", TimingUtils::getJvmVendor));
if (settingsManager.getProperty(PluginSettings.ENABLE_SIGN_EDIT_CHECK)) {
getServer().getPluginManager().registerEvents(new SignListener(), this);
}
if (settingsManager.getProperty(PluginSettings.ENABLE_ANVIL_EDIT_CHECK)) {
getServer().getPluginManager().registerEvents(new AnvilListener(), this);
}
if (settingsManager.getProperty(PluginSettings.ENABLE_BOOK_EDIT_CHECK)) {
getServer().getPluginManager().registerEvents(new BookListener(), this);
}
if (settingsManager.getProperty(PluginSettings.ENABLE_PLAYER_NAME_CHECK)) {
getServer().getPluginManager().registerEvents(new PlayerLoginListener(), this);
}
getServer().getPluginManager().registerEvents(new SignListener(), this);
getServer().getPluginManager().registerEvents(new AnvilListener(), this);
getServer().getPluginManager().registerEvents(new BookListener(), this);
getServer().getPluginManager().registerEvents(new PlayerLoginListener(), 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 Expand Up @@ -147,7 +139,6 @@ public void doInitTasks() {
isInitialized = true;
});
}

@Override
public void onDisable() {
if (checkProtocolLib()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/wdsj/asw/listener/EventChatListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public class EventChatListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onChat(AsyncPlayerChatEvent event) {
if (!isInitialized) return;
if (!isInitialized || isPacketBased()) return;
Player player = event.getPlayer();
if (player.hasPermission("advancedsensitivewords.bypass")) return;
if (isAuthMeAvailable && settingsManager.getProperty(PluginSettings.ENABLE_AUTHME_COMPATIBILITY)) {
Expand Down Expand Up @@ -71,7 +71,7 @@ public void onChat(AsyncPlayerChatEvent event) {

@EventHandler(priority = EventPriority.LOWEST)
public void onCommand(PlayerCommandPreprocessEvent event) {
if (!isInitialized) return;
if (!isInitialized || isPacketBased()) return;
Player player = event.getPlayer();
if (player.hasPermission("advancedsensitivewords.bypass")) return;
if (isAuthMeAvailable && settingsManager.getProperty(PluginSettings.ENABLE_AUTHME_COMPATIBILITY)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
}
}
private boolean shouldNotProcess(Player player, String message) {
if (isInitialized && !player.hasPermission("advancedsensitivewords.bypass") && !isCommandAndWhiteListed(message)) {
if (isInitialized && isPacketBased() && !player.hasPermission("advancedsensitivewords.bypass") && !isCommandAndWhiteListed(message)) {
if (isAuthMeAvailable && settingsManager.getProperty(PluginSettings.ENABLE_AUTHME_COMPATIBILITY)) {
if (!fr.xephi.authme.api.v3.AuthMeApi.getInstance().isAuthenticated(player)) return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void onPacketReceiving(@NotNull com.comphenix.protocol.events.PacketEvent
Player player = event.getPlayer();
assert player != null; // In some cases, player maybe null
String message = event.getPacket().getStrings().read(0);
if (isCommandAndWhiteListed(message) || player.hasPermission("advancedsensitivewords.bypass"))
if (isCommandAndWhiteListed(message) || player.hasPermission("advancedsensitivewords.bypass") || !isPacketBased())
return;
if (isAuthMeAvailable && settingsManager.getProperty(PluginSettings.ENABLE_AUTHME_COMPATIBILITY)) {
if (!fr.xephi.authme.api.v3.AuthMeApi.getInstance().isAuthenticated(player)) return;
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/io/wdsj/asw/setting/PluginSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public class PluginSettings implements SettingsHolder {
public static final Property<String> REPLACEMENT = newProperty("Plugin.replacement", "*");
@Comment("预定义替换(需要先加入blackList)(如包含相同敏感词,长的放前面)(用|来分隔敏感词和替换词)")
public static final Property<List<String>> DEFINED_REPLACEMENT = newListProperty("Plugin.definedReplacement", "失业|灵活就业");
@Comment("*是否启用告示牌检测")
@Comment("是否启用告示牌检测")
public static final Property<Boolean> ENABLE_SIGN_EDIT_CHECK = newProperty("Plugin.enableSignEditCheck", true);
@Comment("*是否启用铁砧重命名检测")
@Comment("是否启用铁砧重命名检测")
public static final Property<Boolean> ENABLE_ANVIL_EDIT_CHECK = newProperty("Plugin.enableAnvilEditCheck", true);
@Comment("*是否启用书检测")
@Comment("是否启用书检测")
public static final Property<Boolean> ENABLE_BOOK_EDIT_CHECK = newProperty("Plugin.enableBookEditCheck", true);
@Comment("*是否启用玩家名称检测(推荐支持中文名的服务器开启)")
@Comment("是否启用玩家名称检测(推荐支持中文名的服务器开启)")
public static final Property<Boolean> ENABLE_PLAYER_NAME_CHECK = newProperty("Plugin.enablePlayerNameCheck", false);
@Comment("是否启用API接口(非必要请勿关闭)")
public static final Property<Boolean> ENABLE_API = newProperty("Plugin.enableApi", true);
Expand Down Expand Up @@ -75,6 +75,9 @@ public class PluginSettings implements SettingsHolder {
@Comment({"是否关闭插件启动时的求赞助消息:(",
"赞助链接: https://afdian.net/a/114514woxiuyuan/"})
public static final Property<Boolean> DISABLE_DONATION = newProperty("Plugin.disableDonation", false);
@Comment({"插件处理聊天检测的方式: 基于数据包/事件(packet/event)",
"注意: 事件模式无法使用聊天上下文检测"})
public static final Property<String> CHAT_DETECTION_MODE = newProperty("Chat.detectionMode", "packet");
@Comment("替换还是取消(replace/cancel)")
public static final Property<String> CHAT_METHOD = newProperty("Chat.method", "replace");
@Comment({"取消后是否发送假消息(仅取消模式可用)(支持PAPI)(Inspired by Bilibili Avalon System)",
Expand Down Expand Up @@ -125,7 +128,7 @@ public class PluginSettings implements SettingsHolder {

@Override
public void registerComments(CommentsConfiguration conf) {
conf.setComment("", "AdvancedSensitiveWords 配置文件", "所有配置项均支持重载(标*的配置项仅支持重载关闭)");
conf.setComment("", "AdvancedSensitiveWords 配置文件", "所有配置项均支持重载");
conf.setComment("Plugin", "插件总配置");
conf.setComment("Plugin.compatibility", "插件兼容配置");
conf.setComment("Chat", "聊天检测配置");
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/io/wdsj/asw/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,22 @@ public static String getSplitCommandArgs(String command) {
if (splitCommand.length <= 1) return "";
return String.join(" ", Arrays.copyOfRange(splitCommand, 1, splitCommand.length));
}
public static boolean isPacketBased() {
return settingsManager.getProperty(PluginSettings.CHAT_DETECTION_MODE).equalsIgnoreCase("packet");
}


public static boolean isCommandAndWhiteListed(String command) {
if (!command.startsWith("/")) return false;
List<String> whitelist = settingsManager.getProperty(PluginSettings.CHAT_COMMAND_WHITE_LIST);
String[] splitCommand = command.split(" ");
for (String s : whitelist) {
if (splitCommand[0].equalsIgnoreCase(s)) {
return !settingsManager.getProperty(PluginSettings.CHAT_INVERT_WHITELIST);
if (!command.startsWith("/")) return false;
List<String> whitelist = settingsManager.getProperty(PluginSettings.CHAT_COMMAND_WHITE_LIST);
String[] splitCommand = command.split(" ");
for (String s : whitelist) {
if (splitCommand[0].equalsIgnoreCase(s)) {
return !settingsManager.getProperty(PluginSettings.CHAT_INVERT_WHITELIST);
}
}
return settingsManager.getProperty(PluginSettings.CHAT_INVERT_WHITELIST);
}
return settingsManager.getProperty(PluginSettings.CHAT_INVERT_WHITELIST);
}

public static String getMinecraftVersion() {
return Bukkit.getBukkitVersion().split("-")[0];
Expand Down

0 comments on commit 57cfde5

Please sign in to comment.