fix: GUI验证码中文提示+标题显示物品名+失败后重新抽题+防止拖拽

This commit is contained in:
2026-07-04 17:18:27 +08:00
parent bca19bf3d0
commit 2618cc5d80
2 changed files with 78 additions and 18 deletions
+70 -15
View File
@@ -191,6 +191,69 @@ public class ChatFilterManager {
}); });
} }
/** 中文物品名映射表 */
private static final Map<String, String> MATERIAL_CN = new HashMap<>();
static {
MATERIAL_CN.put("EMERALD", "绿宝石");
MATERIAL_CN.put("DIAMOND", "钻石");
MATERIAL_CN.put("IRON_INGOT", "铁锭");
MATERIAL_CN.put("GOLD_INGOT", "金锭");
MATERIAL_CN.put("STONE", "石头");
MATERIAL_CN.put("COBBLESTONE", "圆石");
MATERIAL_CN.put("SAND", "沙子");
MATERIAL_CN.put("GRAVEL", "砂砾");
MATERIAL_CN.put("REDSTONE", "红石粉");
MATERIAL_CN.put("LAPIS_LAZULI", "青金石");
MATERIAL_CN.put("QUARTZ", "石英");
MATERIAL_CN.put("APPLE", "苹果");
MATERIAL_CN.put("BOWL", "碗");
MATERIAL_CN.put("CAKE", "蛋糕");
MATERIAL_CN.put("BOOK", "书");
MATERIAL_CN.put("FLINT", "燧石");
MATERIAL_CN.put("FLINT_AND_STEEL", "打火石");
MATERIAL_CN.put("LEATHER_HELMET", "皮革头盔");
MATERIAL_CN.put("LEATHER_CHESTPLATE", "皮革上衣");
MATERIAL_CN.put("LEATHER_LEGGINGS", "皮革护腿");
MATERIAL_CN.put("LEATHER_BOOTS", "皮革靴子");
MATERIAL_CN.put("GOLDEN_APPLE", "附魔金苹果");
MATERIAL_CN.put("OBSIDIAN", "黑曜石");
MATERIAL_CN.put("DIAMOND_HELMET", "钻石头盔");
MATERIAL_CN.put("DIAMOND_CHESTPLATE", "钻石护甲");
MATERIAL_CN.put("DIAMOND_LEGGINGS", "钻石护腿");
MATERIAL_CN.put("DIAMOND_BOOTS", "钻石靴子");
MATERIAL_CN.put("IRON_HELMET", "铁头盔");
MATERIAL_CN.put("IRON_CHESTPLATE", "铁护甲");
MATERIAL_CN.put("IRON_LEGGINGS", "铁护腿");
MATERIAL_CN.put("IRON_BOOTS", "铁靴子");
MATERIAL_CN.put("WOODEN_SWORD", "木剑");
MATERIAL_CN.put("STONE_SWORD", "石剑");
MATERIAL_CN.put("IRON_SWORD", "铁剑");
MATERIAL_CN.put("GOLDEN_SWORD", "金剑");
MATERIAL_CN.put("DIAMOND_SWORD", "钻石剑");
MATERIAL_CN.put("GREEN_CONCRETE", "绿色混凝土");
MATERIAL_CN.put("LIME_CONCRETE", "淡绿色混凝土");
MATERIAL_CN.put("JUNGLE_PLANKS", "丛林木板");
MATERIAL_CN.put("ACACIA_PLANKS", "金合欢木板");
MATERIAL_CN.put("BLAZE_ROD", "烈焰棒");
MATERIAL_CN.put("MAGMA_CREAM", "熔浆膏");
MATERIAL_CN.put("CLAY_BALL", "黏土球");
MATERIAL_CN.put("MUSIC_DISC_13", "音乐唱片C13");
MATERIAL_CN.put("MUSIC_DISC_PIGSTEP", "音乐唱片PigStep");
MATERIAL_CN.put("END_CRYSTAL", "末影水晶");
MATERIAL_CN.put("GLOWSTONE", "萤石粉");
MATERIAL_CN.put("DRAGON_EGG", "龙蛋");
MATERIAL_CN.put("NETHERITE_SCRAP", "下界合金碎片");
MATERIAL_CN.put("CREEPER_HEAD", "爬行者头颅");
MATERIAL_CN.put("GOLDEN_APPLE", "金苹果");
MATERIAL_CN.put("ENCHANTED_GOLDEN_APPLE", "附魔金苹果");
}
/** 生成中文物品名 */
private String toChineseName(String materialName) {
String upper = materialName.toUpperCase();
return MATERIAL_CN.getOrDefault(upper, materialName.replace("_", " "));
}
/** 生成GUI验证码:N个物品+1个正确答案,30秒超时 */ /** 生成GUI验证码:N个物品+1个正确答案,30秒超时 */
private void generateGUIChallenge(String playerName, Random rand) { private void generateGUIChallenge(String playerName, Random rand) {
// 丰富物品池,保证足够多样性 // 丰富物品池,保证足够多样性
@@ -215,6 +278,7 @@ public class ChatFilterManager {
// 随机选1个正确答案 // 随机选1个正确答案
Material correctItem = materials[rand.nextInt(materials.length)]; Material correctItem = materials[rand.nextInt(materials.length)];
String chineseName = toChineseName(correctItem.name());
// 选N-1个干扰项(5~10个物品,其中1个正确,其余干扰) // 选N-1个干扰项(5~10个物品,其中1个正确,其余干扰)
int totalItems = 5 + rand.nextInt(6); // 5~10 int totalItems = 5 + rand.nextInt(6); // 5~10
@@ -233,18 +297,9 @@ public class ChatFilterManager {
// 随机打乱物品位置 // 随机打乱物品位置
Collections.shuffle(itemsToPlace, rand); Collections.shuffle(itemsToPlace, rand);
// 计算正确答案在数组中的索引 // 存储:正确答案的Material名(用英文做校验),显示用中文名
int correctIndex = 0;
for (int i = 0; i < itemsToPlace.size(); i++) {
if (itemsToPlace.get(i) == correctItem) {
correctIndex = i;
break;
}
}
// 存储:正确答案的Material名 + 总物品数
VerificationData vd = new VerificationData(0, 0, 0, VerificationType.GUI); VerificationData vd = new VerificationData(0, 0, 0, VerificationType.GUI);
vd.guiTargets = correctItem.name(); vd.guiTargets = correctItem.name(); // 校验用英文
vd.type = VerificationType.GUI; vd.type = VerificationType.GUI;
verificationData.put(playerName, vd); verificationData.put(playerName, vd);
@@ -252,9 +307,8 @@ public class ChatFilterManager {
Player p = org.bukkit.Bukkit.getPlayer(playerName); Player p = org.bukkit.Bukkit.getPlayer(playerName);
if (p != null && p.isOnline()) { if (p != null && p.isOnline()) {
try { try {
// 在GUI标题中明确显示目标物品名 // 在GUI标题中明确显示中文目标物品名
String targetName = correctItem.name().replace("_", " "); Inventory inv = Bukkit.createInventory(null, 27, "§e§l点击" + chineseName + "完成验证");
Inventory inv = Bukkit.createInventory(null, 27, "§e§l点击" + targetName + "完成验证");
// 填充灰色玻璃半透明方块作为背景 // 填充灰色玻璃半透明方块作为背景
ItemStack glass = new ItemStack(Material.GRAY_STAINED_GLASS_PANE); ItemStack glass = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
@@ -270,7 +324,8 @@ public class ChatFilterManager {
for (int slot : availableSlots) { for (int slot : availableSlots) {
if (itemIdx >= itemsToPlace.size()) break; if (itemIdx >= itemsToPlace.size()) break;
Material mat = itemsToPlace.get(itemIdx); Material mat = itemsToPlace.get(itemIdx);
inv.setItem(slot, mkItem(mat, "§f" + mat.name().replace("_", " "), "")); String cnName = toChineseName(mat.name());
inv.setItem(slot, mkItem(mat, "§f" + cnName, ""));
itemIdx++; itemIdx++;
} }
+8 -3
View File
@@ -2618,9 +2618,11 @@ public class Main extends JavaPlugin
e.setCancelled(true); e.setCancelled(true);
return; return;
} else if (checkResult == ChatFilterManager.VerificationResult.FAILED) { } else if (checkResult == ChatFilterManager.VerificationResult.FAILED) {
chatFilter.pendingMessages.remove(p.getName()); // ★ 数学题回答错误:重新抽题而非静默
chatFilter.pendingMessages.put(p.getName(), msg);
chatFilter.generateMathVerification(p.getName());
p.sendMessage("§c§l[验证码] §c回答错误,请重新回答");
e.setCancelled(true); e.setCancelled(true);
p.sendMessage("§c§l[验证码] §c回答错误,消息已被拦截");
return; return;
} }
// PENDING → GUI验证码,忽略聊天输入,让消息被缓存等待GUI点击 // PENDING → GUI验证码,忽略聊天输入,让消息被缓存等待GUI点击
@@ -3179,8 +3181,9 @@ public class Main extends JavaPlugin
Player p = (Player) e.getWhoClicked(); Player p = (Player) e.getWhoClicked();
// ===== 验证码GUI点击处理 ===== // ===== 验证码GUI点击处理 =====
// 标题格式: "§e§l点击XX完成验证" (XX为中文物品名)
String title = e.getView().getTitle(); String title = e.getView().getTitle();
if ("§e§l点击验证码".equals(title)) { if (title != null && title.contains("点击") && title.contains("完成验证")) {
e.setCancelled(true); e.setCancelled(true);
if (e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR) return; if (e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR) return;
String clickedMat = e.getCurrentItem().getType().name(); String clickedMat = e.getCurrentItem().getType().name();
@@ -3198,6 +3201,8 @@ public class Main extends JavaPlugin
p.closeInventory(); p.closeInventory();
chatFilter.pendingMessages.remove(p.getName()); chatFilter.pendingMessages.remove(p.getName());
p.sendMessage("§c§l[验证码] §c点击错误,消息已被拦截"); p.sendMessage("§c§l[验证码] §c点击错误,消息已被拦截");
// ★ 失败后重新抽题
chatFilter.generateMathVerification(p.getName());
} }
} }
return; return;