From b835860b2e26ba01f42e91db0b6073032a2dad28 Mon Sep 17 00:00:00 2001 From: youpaishidifu <1@ypshidifu.cn> Date: Tue, 3 Nov 2026 19:25:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC73=E6=89=B9=EF=BC=9A=E9=BB=91=E5=90=8D?= =?UTF-8?q?=E5=8D=95=E5=A2=9E=E9=87=8F=E9=87=8D=E8=BD=BD=EF=BC=88CRC32?= =?UTF-8?q?=E6=8C=87=E7=BA=B9=E5=AF=B9=E6=AF=94+=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=BA=A7=E5=A2=9E=E5=88=A0=E6=94=B9=EF=BC=89+=E8=A1=A5?= =?UTF-8?q?=E5=85=A8http.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Sdf1_login/AIChatManager.java | 52 +++++++++++++++++++-- src/main/resources/openai/黑名单/http.json | 32 +++++++++++-- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/src/main/java/Sdf1_login/AIChatManager.java b/src/main/java/Sdf1_login/AIChatManager.java index 523a3ae..ea39b82 100644 --- a/src/main/java/Sdf1_login/AIChatManager.java +++ b/src/main/java/Sdf1_login/AIChatManager.java @@ -50,6 +50,10 @@ public class AIChatManager { // ★ 第61批-续1:endpoint 找不到的状态变化日志静默 private boolean prevEndpointNull = false; + // ★ 第73批-增量重载:黑名单目录文件管理 + private final Map> blacklistFileKeywords = new HashMap<>(); // 文件名 -> 关键词列表 + private final Map blacklistFileHashes = new HashMap<>(); // 文件名 -> CRC32 指纹 + // 默认模型 private String defaultModelFull = ""; @@ -262,23 +266,56 @@ public class AIChatManager { } } - // ★ 第73批:自动扫描 openai/黑名单/ 目录下的所有 json 文件 + // ★ 第73批-增量重载:扫描 openai/黑名单/ 目录下的所有 json 文件 + // 原理:用 CRC32 指纹对比文件内容。文件没变→内存不动、闭嘴; + // 新增文件→加载并打印一次;文件变了→清掉该文件旧关键词、加载新关键词、打印一次; + // 文件被删→从内存移除该文件的关键词。 File blacklistDir = new File(openaiDir, "黑名单"); if (blacklistDir.exists() && blacklistDir.isDirectory()) { File[] blFiles = blacklistDir.listFiles((dir, name) -> name.endsWith(".json")); if (blFiles != null) { + Set currentFileNames = new HashSet<>(); for (File blFile : blFiles) { + currentFileNames.add(blFile.getName()); + String fileName = blFile.getName(); try { String jsonContent = Files.readString(blFile.toPath(), StandardCharsets.UTF_8); + String newHash = crc32(jsonContent); + String oldHash = blacklistFileHashes.get(fileName); + if (newHash.equals(oldHash)) { + // 文件内容没变,内存里已加载的关键词不动,不打印 + continue; + } + // 文件新增或内容变动:加载新关键词 + List newKeywords = new ArrayList<>(); JsonArray blArray = JsonParser.parseString(jsonContent).getAsJsonArray(); for (JsonElement el : blArray) { if (el.isJsonPrimitive()) { - blacklist.add(el.getAsString()); + newKeywords.add(el.getAsString()); } } - plugin.getLogger().info("[AI聊天] 已加载黑名单文件: " + blFile.getName()); + // 如果是已有文件变动:先从全局黑名单中移除该文件的旧关键词 + if (blacklistFileKeywords.containsKey(fileName)) { + blacklist.removeAll(blacklistFileKeywords.get(fileName)); + plugin.getLogger().info("[AI聊天] 黑名单文件已更新: " + fileName + "(" + newKeywords.size() + " 个关键词)"); + } else { + plugin.getLogger().info("[AI聊天] 已加载黑名单文件: " + fileName + "(" + newKeywords.size() + " 个关键词)"); + } + // 追加新关键词到全局黑名单并更新内存 + blacklist.addAll(newKeywords); + blacklistFileKeywords.put(fileName, newKeywords); + blacklistFileHashes.put(fileName, newHash); } catch (Exception e) { - plugin.getLogger().warning("[AI聊天] 加载黑名单文件失败: " + blFile.getName() + " - " + e.getMessage()); + plugin.getLogger().warning("[AI聊天] 加载黑名单文件失败: " + fileName + " - " + e.getMessage()); + } + } + // 处理被删除的文件:移除其关键词 + for (String oldFile : blacklistFileKeywords.keySet()) { + if (!currentFileNames.contains(oldFile)) { + blacklist.removeAll(blacklistFileKeywords.get(oldFile)); + blacklistFileKeywords.remove(oldFile); + blacklistFileHashes.remove(oldFile); + plugin.getLogger().info("[AI聊天] 黑名单文件已移除: " + oldFile); } } } @@ -1574,6 +1611,13 @@ public class AIChatManager { return true; } + /** ★ 第73批:计算字符串的 CRC32 指纹(十六进制字符串),用于黑名单文件增量重载 */ + private String crc32(String content) { + java.util.zip.CRC32 crc = new java.util.zip.CRC32(); + crc.update(content.getBytes(java.nio.charset.StandardCharsets.UTF_8)); + return Long.toHexString(crc.getValue()); + } + /** * ★ 第73批:从相对路径加载黑名单。 *

diff --git a/src/main/resources/openai/黑名单/http.json b/src/main/resources/openai/黑名单/http.json index e529d9c..b7b97e3 100644 --- a/src/main/resources/openai/黑名单/http.json +++ b/src/main/resources/openai/黑名单/http.json @@ -1,7 +1,29 @@ [ - "404", + "400", + "401", "403", - "200", - "201", - "202" -] \ No newline at end of file + "404", + "405", + "408", + "409", + "410", + "418", + "422", + "429", + "500", + "501", + "502", + "503", + "504", + "505", + "507", + "508", + "509", + "510", + "511", + "HTTP", + "http", + "HTTPS", + "https", + "nighx" +]