diff --git a/src/main/java/Sdf1_login/AIChatManager.java b/src/main/java/Sdf1_login/AIChatManager.java index dff2569..dea0da7 100644 --- a/src/main/java/Sdf1_login/AIChatManager.java +++ b/src/main/java/Sdf1_login/AIChatManager.java @@ -37,6 +37,11 @@ public class AIChatManager { private final Map providerToModel = new HashMap<>(); // 厂家名 → 默认模型全名(@//厂家名/) private final List blacklist = new ArrayList<>(); + // ★ 第108批:快问快答题目拦截(防 AI 辅助作弊)——跨目录读 Sdf1_game 题目文件 + // 玩家把题目发给 AI 答题 → 直接婉拒,不请求 API;输出侧也兜底拦题目原文。 + private final List quizQuestions = new CopyOnWriteArrayList<>(); // 归一化后的问题文本 + private BukkitTask quizReloadTask = null; + // ★ 互动指标跳过日志静默:首次跳过记一条,之后只在状态变化(从≥→<)时再记 private boolean prevOnlineSufficient = true; @@ -837,6 +842,18 @@ public class AIChatManager { return; } + // ★ 第108批:快问快答题目拦截(防 AI 辅助作弊)——玩家把题目发给 AI 答题 → + // 直接角色化婉拒,不请求 API、不浪费 token。题目跨目录读自 Sdf1_game 插件。 + String quizHit = matchQuizQuestion(prompt); + if (quizHit != null) { + plugin.getLogger().info("[AI聊天] 快问快答题目拦截[题目:" + quizHit + "]: " + + prompt.substring(0, Math.min(50, prompt.length()))); + if (player != null) { + player.sendMessage("§d§l[AI] §f" + fallbackDodge()); + } + return; + } + // ★ 第71批-问题1:问题侧预拦截(不浪费 token) // 命中非游戏话题关键词 → 角色化婉拒,不请求模型 String offTopicHit = matchOffTopic(prompt); @@ -1462,6 +1479,13 @@ public class AIChatManager { return fallbackDodge(); } + // ★ 第108批:输出侧快问快答题目拦截(防 AI 把题目/答案原文吐出来) + String quizOutHit = matchQuizQuestion(r); + if (quizOutHit != null) { + plugin.getLogger().warning("[AI聊天] 输出守卫:回复命中快问快答题目[" + quizOutHit + "],拦截。原文={" + r + "}"); + return fallbackDodge(); + } + // ★ 第93批-问题4 + 第94批-问题1:管理员向内容输出侧检查(第3道闸) // 玩家问「命令方块怎么获得」时提示词能婉拒,但输出侧仍需兜底: // 若模型依然输出了管理员物品(命令方块/调试棒等)名字,按句粒度过滤—— @@ -1594,6 +1618,96 @@ public class AIChatManager { return null; } + // ==================== 第108批:快问快答题目拦截(防 AI 辅助作弊)==================== + + /** 题目目录:跨目录读 Sdf1_game 插件数据(本机/生产同口径 plugins/Sdf1_game/快问快答/题目) */ + private File getQuizQuestionDir() { + return new File(plugin.getDataFolder().getParentFile(), "Sdf1_game/快问快答/题目"); + } + + /** 加载/重载题目(解析三种格式,归一化后存内存) */ + private void loadQuizQuestions() { + quizQuestions.clear(); + File dir = getQuizQuestionDir(); + if (!dir.isDirectory()) { + plugin.getLogger().warning("[AI聊天] 快问快答题目目录不存在,题目拦截未启用: " + dir.getAbsolutePath()); + return; + } + File[] files = dir.listFiles((d, n) -> n.endsWith(".md")); + if (files == null || files.length == 0) { + plugin.getLogger().warning("[AI聊天] 快问快答题目目录无 .md 文件: " + dir.getAbsolutePath()); + return; + } + int count = 0; + for (File f : files) { + try { + for (String line : Files.readAllLines(f.toPath(), StandardCharsets.UTF_8)) { + String q = parseQuizQuestion(line); + if (q != null) { quizQuestions.add(q); count++; } + } + } catch (Exception e) { + plugin.getLogger().warning("[AI聊天] 读取题目文件失败: " + f.getName() + " " + e.getMessage()); + } + } + plugin.getLogger().info("[AI聊天] 快问快答题目已加载 " + count + " 条(来源: " + dir.getAbsolutePath() + ")"); + } + + /** 定时重载题目(每 5 分钟,题目文件可能被管理员更新) */ + private void startQuizReloadScheduler() { + stopQuizReloadScheduler(); + quizReloadTask = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> { + try { loadQuizQuestions(); } catch (Exception e) { + plugin.getLogger().warning("[AI聊天] 题目重载失败: " + e.getMessage()); + } + }, 5 * 60 * 20L, 5 * 60 * 20L); + } + + private void stopQuizReloadScheduler() { + if (quizReloadTask != null && !quizReloadTask.isCancelled()) quizReloadTask.cancel(); + quizReloadTask = null; + } + + /** 解析一行题目文本(三种格式)→ 归一化后的问题,非题目行返回 null */ + private String parseQuizQuestion(String line) { + if (line == null) return null; + String t = line.trim(); + if (t.isEmpty() || t.startsWith("#") || t.startsWith("答案:")) return null; + String q = null; + if (t.startsWith("|")) { + // 表格格式(选择题/问答题):| 问题 | ... | → 第一列 + String[] cols = t.split("\\|"); + if (cols.length >= 2) q = cols[1].trim(); + if (q == null || q.isEmpty() || q.equals("问题") || q.matches("-+")) return null; + } else { + // 填空题格式:编号. 问题(赏金) + java.util.regex.Matcher m = java.util.regex.Pattern + .compile("^\\d+\\.\\s*(.+)$").matcher(t); + if (m.matches()) { + q = m.group(1).trim(); + q = q.replaceAll("\\(\\d+\\)\\s*$", "").trim(); + } + } + if (q == null || q.isEmpty()) return null; + return normalizeQuizText(q); + } + + /** 归一化:去空白 + 去所有标点/符号(防空格/标点/全角半角变体绕过) */ + private String normalizeQuizText(String s) { + if (s == null) return null; + return s.replaceAll("[\\s\\p{P}\\p{S}]", ""); + } + + /** 匹配:玩家消息归一化后包含任一条目 → 返回命中的题目(归一化后),未命中返回 null */ + private String matchQuizQuestion(String text) { + if (text == null || quizQuestions.isEmpty()) return null; + String norm = normalizeQuizText(text); + if (norm.isEmpty()) return null; + for (String q : quizQuestions) { + if (!q.isEmpty() && norm.contains(q)) return q; + } + return null; + } + /** * 非游戏话题敏感词(问题侧+回复侧共用)。 * 原则:只放「游戏闲聊几乎不会出现」的词,通用词(证明/历史/公式等) @@ -2454,10 +2568,14 @@ public class AIChatManager { public void onEnable() { startAutoChatScheduler(); startHotReloadScheduler(); + // ★ 第108批:加载快问快答题目 + 每5分钟重载(题目文件可能被管理员更新) + loadQuizQuestions(); + startQuizReloadScheduler(); } public void onDisable() { stopAutoChatScheduler(); stopHotReloadScheduler(); + stopQuizReloadScheduler(); } }