diff --git a/src/main/java/Sdf1_login/AreaCLIManager.java b/src/main/java/Sdf1_login/AreaCLIManager.java index 9d02e77..9e61b47 100644 --- a/src/main/java/Sdf1_login/AreaCLIManager.java +++ b/src/main/java/Sdf1_login/AreaCLIManager.java @@ -108,6 +108,12 @@ public class AreaCLIManager { p.sendMessage(clickableAction("ℹ", "当前领地信息", "/protect info")); + // ★ 管理员配置入口 + if (areaProtect.isAreaAdmin(p)) { + p.sendMessage(clickableAction("⚙", "全局配置", + "/protect cli config")); + } + // 设置 int uiMode = 1; try { uiMode = plugin.getDb().getUiMode(p.getName()); } catch (Exception ignored) {} @@ -117,6 +123,77 @@ public class AreaCLIManager { p.sendMessage(Component.text("§7§l───────────────────────────────")); } + // ==================== 全局配置(CLI交互式) ==================== + + /** + * ★ CLI全局配置页面:显示当前配置项,点击可修改 + */ + public void showConfigPage(Player p) { + p.sendMessage(header("全局配置")); + + String price = areaProtect.getAreaConfigValue("create_price_per_sqm"); + String maxLands = areaProtect.getAreaConfigValue("max_lands_per_player"); + String height = areaProtect.getAreaConfigValue("default_height"); + String peace = areaProtect.getAreaConfigValue("peace_mode_max_duration"); + + // 创建价格 + p.sendMessage(Component.empty() + .append(Component.text("§a创建价格(每㎡): §f" + (price != null ? price : "10") + " ")) + .append(Component.text("§a[+10] ") + .clickEvent(ClickEvent.suggestCommand("/protect config create_price " + safeParseInt(price, 10, 10))) + .hoverEvent(HoverEvent.showText(Component.text("§e点击后输入新值(比当前大=加钱,比当前小=减钱)")))) + .append(Component.text("§c[-10] ") + .clickEvent(ClickEvent.suggestCommand("/protect config create_price " + safeParseInt(price, 10, -10))) + .hoverEvent(HoverEvent.showText(Component.text("§e点击后输入新值(比当前大=加钱,比当前小=减钱)")))) + ); + + // 每人最大领地数 + p.sendMessage(Component.empty() + .append(Component.text("§a每人最大领地数: §f" + (maxLands != null ? maxLands : "5") + " ")) + .append(Component.text("§a[+1] ") + .clickEvent(ClickEvent.suggestCommand("/protect config max_lands " + safeParseInt(maxLands, 5, 1))) + .hoverEvent(HoverEvent.showText(Component.text("§e点击后输入新值")))) + .append(Component.text("§c[-1] ") + .clickEvent(ClickEvent.suggestCommand("/protect config max_lands " + safeParseInt(maxLands, 5, -1))) + .hoverEvent(HoverEvent.showText(Component.text("§e点击后输入新值")))) + ); + + // 默认高度 + p.sendMessage(Component.empty() + .append(Component.text("§a默认高度: §f" + (height != null ? height : "255") + " ")) + .append(Component.text("§a[+10] ") + .clickEvent(ClickEvent.suggestCommand("/protect config default_height " + safeParseInt(height, 255, 10))) + .hoverEvent(HoverEvent.showText(Component.text("§e点击后输入新值")))) + .append(Component.text("§c[-10] ") + .clickEvent(ClickEvent.suggestCommand("/protect config default_height " + safeParseInt(height, 255, -10))) + .hoverEvent(HoverEvent.showText(Component.text("§e点击后输入新值")))) + ); + + // 和平时长 + p.sendMessage(Component.empty() + .append(Component.text("§a和平模式最大时长(秒): §f" + (peace != null ? peace : "3600") + " ")) + .append(Component.text("§a[+600] ") + .clickEvent(ClickEvent.suggestCommand("/protect config peace_duration " + safeParseInt(peace, 3600, 600))) + .hoverEvent(HoverEvent.showText(Component.text("§e点击后输入新值")))) + .append(Component.text("§c[-600] ") + .clickEvent(ClickEvent.suggestCommand("/protect config peace_duration " + safeParseInt(peace, 3600, -600))) + .hoverEvent(HoverEvent.showText(Component.text("§e点击后输入新值")))) + ); + + p.sendMessage(Component.text("§7提示: 点击修改值后,可在聊天栏输入任意数字进行精确调整")); + p.sendMessage(Component.text("§7支持: 纯数字、中文数字、中文大写、罗马数字、英文数字")); + + p.sendMessage(clickableAction("◀", "返回主菜单", "/protect cli menu")); + p.sendMessage(Component.text("§7§l───────────────────────────────")); + } + + private int safeParseInt(String val, int defaultVal, int delta) { + try { + if (val != null) return Integer.parseInt(val) + delta; + } catch (Exception ignored) {} + return defaultVal + delta; + } + // ==================== 领地列表 ==================== public void showLandList(Player p, int page) { @@ -409,6 +486,12 @@ public class AreaCLIManager { return; } + // ★ 删除冷却期间修改权限 → 自动取消删除 + if (areaProtect.hasPendingDelete(landName)) { + areaProtect.cancelPendingDelete(landName); + p.sendMessage(Component.text("§e§l[防护] §f检测到权限修改,已自动取消领地删除")); + } + // 切换权限 boolean oldState = false; switch (permKey) { diff --git a/src/main/java/Sdf1_login/AreaGUIManager.java b/src/main/java/Sdf1_login/AreaGUIManager.java index a651212..6fdb56a 100644 --- a/src/main/java/Sdf1_login/AreaGUIManager.java +++ b/src/main/java/Sdf1_login/AreaGUIManager.java @@ -819,6 +819,14 @@ public class AreaGUIManager implements Listener { * ★ 获取圈地工具(木棍) */ private void giveWandTool(Player p) { + // ★ 5秒冷却 + long now = System.currentTimeMillis(); + Long lastGive = areaProtect.wandCooldownMap.get(p.getUniqueId()); + if (lastGive != null && now - lastGive < 5000) { + long remaining = (5000 - (now - lastGive)) / 1000 + 1; + p.sendMessage("§c请等待 " + remaining + " 秒后再获取"); + return; + } ItemStack wand = new ItemStack(Material.STICK); ItemMeta meta = wand.getItemMeta(); if (meta != null) { @@ -844,54 +852,37 @@ public class AreaGUIManager implements Listener { } p.getInventory().addItem(wand); p.sendMessage("§a已获取圈地工具(木棍),左键/右键选择区域边界"); + areaProtect.wandCooldownMap.put(p.getUniqueId(), now); } /** - * ★ 处理管理员面板点击 + * ★ 处理管理员面板点击 - 改为聊天栏输入模式 + * 点击后打开聊天栏,玩家输入任意数字,比原来大=加钱,比原来小=减钱 + * 支持:纯数字、中文数字、中文大写、罗马数字、英文数字 */ private void handleAdminPanelClick(Player p, int raw, boolean leftClick, boolean rightClick) { - // 读取当前值 - int pricePerSqm = 10, maxLands = 5, defaultHeight = 255, peaceDuration = 3600; - try { - String v; - v = areaProtect.getAreaConfigValue("create_price_per_sqm"); - if (v != null) pricePerSqm = Integer.parseInt(v); - v = areaProtect.getAreaConfigValue("max_lands_per_player"); - if (v != null) maxLands = Integer.parseInt(v); - v = areaProtect.getAreaConfigValue("default_height"); - if (v != null) defaultHeight = Integer.parseInt(v); - v = areaProtect.getAreaConfigValue("peace_mode_max_duration"); - if (v != null) peaceDuration = Integer.parseInt(v); - } catch (Exception ignored) {} - if (raw == 11) { - // 每平米价格 - if (leftClick) pricePerSqm += 10; - else if (rightClick) pricePerSqm = Math.max(0, pricePerSqm - 10); - areaProtect.setAreaConfigValue("create_price_per_sqm", String.valueOf(pricePerSqm)); - p.sendMessage("§a每平米价格已更新: §f" + pricePerSqm + " 元/㎡"); - openAdminPanel(p); + // 每平米价格 - 打开聊天栏输入 + p.closeInventory(); + p.sendMessage("§e§l[配置] §f请输入新的每平米创建价格(当前值会自动对比):"); + p.sendMessage("§7支持: 纯数字123、中文一二三、大写壹佰贰拾叁、罗马I II III、英文one two three"); + // 设置等待状态,监听玩家聊天输入 + areaProtect.setPendingConfigInput(p.getUniqueId(), "create_price_per_sqm"); } else if (raw == 13) { // 最大领地数 - if (leftClick) maxLands += 1; - else if (rightClick) maxLands = Math.max(1, maxLands - 1); - areaProtect.setAreaConfigValue("max_lands_per_player", String.valueOf(maxLands)); - p.sendMessage("§a最大领地数已更新: §f" + maxLands + " 个"); - openAdminPanel(p); + p.closeInventory(); + p.sendMessage("§e§l[配置] §f请输入新的每人最大领地数:"); + areaProtect.setPendingConfigInput(p.getUniqueId(), "max_lands_per_player"); } else if (raw == 15) { // 默认高度 - if (leftClick) defaultHeight += 32; - else if (rightClick) defaultHeight = Math.max(32, defaultHeight - 32); - areaProtect.setAreaConfigValue("default_height", String.valueOf(defaultHeight)); - p.sendMessage("§a默认高度已更新: §f" + defaultHeight + " 格"); - openAdminPanel(p); + p.closeInventory(); + p.sendMessage("§e§l[配置] §f请输入新的默认高度:"); + areaProtect.setPendingConfigInput(p.getUniqueId(), "default_height"); } else if (raw == 22) { // 和平模式时间 - if (leftClick) peaceDuration += 600; - else if (rightClick) peaceDuration = Math.max(60, peaceDuration - 600); - areaProtect.setAreaConfigValue("peace_mode_max_duration", String.valueOf(peaceDuration)); - p.sendMessage("§a和平模式时间已更新: §f" + peaceDuration + " 秒"); - openAdminPanel(p); + p.closeInventory(); + p.sendMessage("§e§l[配置] §f请输入新的和平模式最大时长(秒):"); + areaProtect.setPendingConfigInput(p.getUniqueId(), "peace_mode_max_duration"); } else if (raw == 48) { openMainMenu(p); } diff --git a/src/main/java/Sdf1_login/AreaProtection.java b/src/main/java/Sdf1_login/AreaProtection.java index 04ded1b..3bb3bab 100644 --- a/src/main/java/Sdf1_login/AreaProtection.java +++ b/src/main/java/Sdf1_login/AreaProtection.java @@ -93,6 +93,10 @@ public class AreaProtection implements Listener { } } private final Map pendingDeletes = new ConcurrentHashMap<>(); + // ★ 圈地工具冷却:UUID → 上次获取时间(package-private供AreaGUIManager访问) + final Map wandCooldownMap = new ConcurrentHashMap<>(); + // ★ 管理员配置输入等待:UUID → 配置key(GUI点击后等待玩家聊天输入) + private final Map pendingConfigInput = new ConcurrentHashMap<>(); // ★ 全局配置默认值 private int globalCreatePricePerSqm = 10; // 每㎡创建价格 @@ -4673,6 +4677,25 @@ public class AreaProtection implements Listener { return true; } Player p = (Player) sender; + // ★ 5秒冷却 + long now = System.currentTimeMillis(); + Long lastGive = wandCooldownMap.get(p.getUniqueId()); + if (lastGive != null && now - lastGive < 5000) { + long remaining = (5000 - (now - lastGive)) / 1000 + 1; + p.sendMessage("§c§l[防护] §f请等待 " + remaining + " 秒后再获取"); + return true; + } + // ★ 检查是否已拥有 + for (ItemStack item : p.getInventory().getContents()) { + if (item != null && item.getType() == WAND + && item.hasItemMeta() && item.getItemMeta() != null + && item.getItemMeta().hasDisplayName() + && item.getItemMeta().getDisplayName().contains("区域选择工具")) { + p.sendMessage("§e§l[防护] §f你已拥有圈地工具"); + wandCooldownMap.put(p.getUniqueId(), now); + return true; + } + } ItemStack tool = new ItemStack(WAND); ItemMeta meta = tool.getItemMeta(); meta.setDisplayName("§a§l区域选择工具"); @@ -4681,6 +4704,7 @@ public class AreaProtection implements Listener { tool.setItemMeta(meta); p.getInventory().addItem(tool); p.sendMessage("§a§l[防护] §f已获取选择工具"); + wandCooldownMap.put(p.getUniqueId(), now); return true; } @@ -4804,6 +4828,10 @@ public class AreaProtection implements Listener { // ★ 切换后刷新到访客授权页面(停留当前页面,不退出) plugin.areaCLIManager.refreshCurrentPage(p); break; + case "config": + // ★ CLI全局配置页面 + plugin.areaCLIManager.showConfigPage(p); + break; case "create": // 跳转到创建命令 // 直接转发 @@ -5940,6 +5968,8 @@ public class AreaProtection implements Listener { AreaConfig ac = areas.get(areaName); ac.owner = args[2]; saveAreaToDb(ac); + // ★ 删除冷却期间变更所有者 → 自动取消删除 + cancelPendingDelete(areaName); sender.sendMessage("§a已设置 §e" + areaName + " §a的所有者为 §e" + args[2]); return true; } @@ -5967,6 +5997,11 @@ public class AreaProtection implements Listener { sender.sendMessage("§c需要领地所有者或管理员权限"); return true; } + // ★ 删除冷却期间添加成员 → 自动取消删除 + if (hasPendingDelete(ac.name)) { + cancelPendingDelete(ac.name); + p.sendMessage("§e§l[防护] §f检测到成员变更,已自动取消领地删除"); + } if (args.length < 2) { sender.sendMessage("§c用法: /protect addvisitor <玩家>"); return true; @@ -5982,6 +6017,11 @@ public class AreaProtection implements Listener { sender.sendMessage("§c需要领地所有者或管理员权限"); return true; } + // ★ 删除冷却期间添加成员 → 自动取消删除 + if (hasPendingDelete(areaName)) { + cancelPendingDelete(areaName); + p.sendMessage("§e§l[防护] §f检测到成员变更,已自动取消领地删除"); + } addPlayerToAreaWhitelist(areaName, playerName); sender.sendMessage("§a已添加 §e" + playerName + " §a为 §e" + areaName + " §a的访客"); return true; @@ -6009,6 +6049,11 @@ public class AreaProtection implements Listener { sender.sendMessage("§c需要领地所有者或管理员权限"); return true; } + // ★ 删除冷却期间移除成员 → 自动取消删除 + if (hasPendingDelete(ac.name)) { + cancelPendingDelete(ac.name); + p.sendMessage("§e§l[防护] §f检测到成员变更,已自动取消领地删除"); + } if (args.length < 2) { sender.sendMessage("§c用法: /protect removevisitor <玩家>"); return true; @@ -6024,6 +6069,11 @@ public class AreaProtection implements Listener { sender.sendMessage("§c需要领地所有者或管理员权限"); return true; } + // ★ 删除冷却期间移除成员 → 自动取消删除 + if (hasPendingDelete(areaName)) { + cancelPendingDelete(areaName); + p.sendMessage("§e§l[防护] §f检测到成员变更,已自动取消领地删除"); + } removePlayerFromAreaWhitelist(areaName, playerName); sender.sendMessage("§a已移除 §e" + playerName + " §a的 §e" + areaName + " §a访客权限"); return true; @@ -6203,7 +6253,8 @@ public class AreaProtection implements Listener { /** * 如果领地有待删除请求,取消它(玩家操作领地时自动解除冻结) */ - private void cancelPendingDelete(String areaName) { + // ★ package-private供AreaCLIManager访问 + void cancelPendingDelete(String areaName) { PendingDelete pd = pendingDeletes.remove(areaName); if (pd != null) { pd.task.cancel(); @@ -6552,6 +6603,235 @@ public class AreaProtection implements Listener { updateAreaConfig(key, value); } + // ==================== 管理员配置输入(GUI→聊天栏) ==================== + + /** + * ★ 设置等待配置输入状态 + */ + public void setPendingConfigInput(java.util.UUID uuid, String configKey) { + pendingConfigInput.put(uuid, configKey); + } + + /** + * ★ 检查并处理配置输入(在onPlayerChat中调用) + * @return true 如果是配置输入,已处理 + */ + public boolean handleConfigInput(Player p, String message) { + String configKey = pendingConfigInput.remove(p.getUniqueId()); + if (configKey == null) return false; + + // ★ 智能数字解析:支持纯数字、中文数字、中文大写、罗马数字、英文数字 + Integer newValue = parseSmartNumber(message.trim()); + if (newValue == null) { + p.sendMessage("§c§l[配置] §f无法识别输入的数字: " + message); + p.sendMessage("§7支持: 纯数字123、中文一二三、大写壹佰贰拾叁、罗马I II III、英文one two three"); + return true; + } + + // 读取当前值 + int currentValue = 0; + try { + String v = getAreaConfigValue(configKey); + if (v != null) currentValue = Integer.parseInt(v); + } catch (Exception ignored) {} + + // 应用新值(如果比原来大=加钱,比原来小=减钱,直接使用绝对值) + setAreaConfigValue(configKey, String.valueOf(newValue)); + + // 更新内存变量 + switch (configKey) { + case "create_price_per_sqm": globalCreatePricePerSqm = newValue; break; + case "max_lands_per_player": globalMaxLandsPerPlayer = newValue; break; + case "default_height": globalDefaultHeight = newValue; break; + } + + String configName = getConfigNameByKey(configKey); + p.sendMessage("§a§l[配置] §f" + configName + " 已更新: §e" + currentValue + " → " + newValue); + + // 刷新GUI + if (plugin.areaGUIManager != null) { + plugin.areaGUIManager.openAdminPanel(p); + } + return true; + } + + private String getConfigNameByKey(String key) { + switch (key) { + case "create_price_per_sqm": return "创建价格(每㎡)"; + case "max_lands_per_player": return "每人最大领地数"; + case "default_height": return "默认高度"; + case "peace_mode_max_duration": return "和平模式最大时长(秒)"; + default: return key; + } + } + + /** + * ★ 智能数字解析:支持多种格式 + * 纯数字、中文数字、中文大写、罗马数字、英文数字 + */ + public static Integer parseSmartNumber(String input) { + if (input == null || input.isEmpty()) return null; + input = input.trim().toLowerCase(); + + // 1. 纯数字 + try { + return Integer.parseInt(input); + } catch (Exception ignored) {} + + // 2. 中文数字(小写) + Integer result = parseChineseNumber(input); + if (result != null) return result; + + // 3. 罗马数字 + result = parseRomanNumber(input); + if (result != null) return result; + + // 4. 英文数字单词 + result = parseEnglishNumber(input); + if (result != null) return result; + + return null; + } + + /** + * 解析中文数字(一~九十九万九千九百九十九) + */ + private static Integer parseChineseNumber(String input) { + if (input.isEmpty()) return null; + + // 中文大写数字映射 + String[][] chineseMap = { + {"零", "0"}, {"一", "1"}, {"二", "2"}, {"三", "3"}, {"四", "4"}, + {"五", "5"}, {"六", "6"}, {"七", "7"}, {"八", "8"}, {"九", "9"}, + {"壹", "1"}, {"贰", "2"}, {"叁", "3"}, {"肆", "4"}, {"伍", "5"}, + {"陆", "6"}, {"柒", "7"}, {"捌", "8"}, {"玖", "9"}, + {"十", "10"}, {"百", "100"}, {"千", "1000"}, {"万", "10000"}, + {"拾", "10"}, {"佰", "100"}, {"仟", "1000"} + }; + + // 替换中文大写为小写 + String normalized = input; + for (String[] pair : chineseMap) { + normalized = normalized.replace(pair[0], pair[1]); + } + + // 检查是否包含数字 + if (!normalized.matches("[0-9零一二三四五六七八九十百千万]+")) { + return null; + } + + // 简单解析:直接替换后计算 + try { + // 处理 "十二" 这种格式 → "12" + normalized = normalized.replace("零", ""); + normalized = normalized.replace("一十", "10"); + normalized = normalized.replace("二十", "20"); + normalized = normalized.replace("三十", "30"); + normalized = normalized.replace("四十", "40"); + normalized = normalized.replace("五十", "50"); + normalized = normalized.replace("六十", "60"); + normalized = normalized.replace("七十", "70"); + normalized = normalized.replace("八十", "80"); + normalized = normalized.replace("九十", "90"); + + // 处理 "一百" 格式 + if (normalized.contains("百")) { + String[] parts = normalized.split("百"); + int hundreds = Integer.parseInt(parts[0]) * 100; + int tens = parts.length > 1 && !parts[1].isEmpty() ? Integer.parseInt(parts[1]) : 0; + return hundreds + tens; + } + + // 处理 "千" 格式 + if (normalized.contains("千")) { + String[] parts = normalized.split("千"); + int thousands = Integer.parseInt(parts[0]) * 1000; + int rest = parts.length > 1 && !parts[1].isEmpty() ? Integer.parseInt(parts[1]) : 0; + return thousands + rest; + } + + // 处理 "万" 格式 + if (normalized.contains("万")) { + String[] parts = normalized.split("万"); + int tenThousands = Integer.parseInt(parts[0]) * 10000; + int rest = parts.length > 1 && !parts[1].isEmpty() ? Integer.parseInt(parts[1]) : 0; + return tenThousands + rest; + } + + // 纯数字 + return Integer.parseInt(normalized); + } catch (Exception e) { + return null; + } + } + + /** + * 解析罗马数字 + */ + private static Integer parseRomanNumber(String input) { + input = input.toUpperCase(); + if (!input.matches("[IVXLCDM]+")) return null; + + int result = 0; + int prev = 0; + for (int i = input.length() - 1; i >= 0; i--) { + int val = 0; + switch (input.charAt(i)) { + case 'I': val = 1; break; + case 'V': val = 5; break; + case 'X': val = 10; break; + case 'L': val = 50; break; + case 'C': val = 100; break; + case 'D': val = 500; break; + case 'M': val = 1000; break; + } + if (val < prev) result -= val; + else result += val; + prev = val; + } + return result > 0 ? result : null; + } + + /** + * 解析英文数字单词 + */ + private static Integer parseEnglishNumber(String input) { + input = input.toLowerCase().trim(); + switch (input) { + case "zero": return 0; + case "one": return 1; + case "two": return 2; + case "three": return 3; + case "four": return 4; + case "five": return 5; + case "six": return 6; + case "seven": return 7; + case "eight": return 8; + case "nine": return 9; + case "ten": return 10; + case "eleven": return 11; + case "twelve": return 12; + case "thirteen": return 13; + case "fourteen": return 14; + case "fifteen": return 15; + case "sixteen": return 16; + case "seventeen": return 17; + case "eighteen": return 18; + case "nineteen": return 19; + case "twenty": return 20; + case "thirty": return 30; + case "forty": return 40; + case "fifty": return 50; + case "sixty": return 60; + case "seventy": return 70; + case "eighty": return 80; + case "ninety": return 90; + case "hundred": return 100; + case "thousand": return 1000; + default: return null; + } + } + /** * 检查玩家的访客权限是否有效(未过期) */ diff --git a/src/main/java/Sdf1_login/Main.java b/src/main/java/Sdf1_login/Main.java index 7fb8698..8da0b7a 100644 --- a/src/main/java/Sdf1_login/Main.java +++ b/src/main/java/Sdf1_login/Main.java @@ -2525,6 +2525,11 @@ public class Main extends JavaPlugin return; } + // ★ 管理员配置输入拦截(GUI点击后聊天栏输入) + if (areaProtection != null && areaProtection.handleConfigInput(p, msg)) { + e.setCancelled(true); + return; + } // ===== 菜单聊天输入 ===== if (getMenu().isEditing(p.getName())) { @@ -6142,7 +6147,7 @@ public class Main extends JavaPlugin // ★ cli: 子命令 if (sub.equals("cli")) { return filterTab( - Arrays.asList("menu", "lands", "manage", "members", "visitorperm", "toggle", "create"), + Arrays.asList("menu", "lands", "manage", "members", "visitorperm", "toggle", "create", "config"), args[1]); } } @@ -6199,6 +6204,12 @@ public class Main extends JavaPlugin players.add(pl.getName()); return filterTab(players, args[2]); } + // ★ config: 第三层是配置key + if (sub.equals("config")) { + return filterTab( + Arrays.asList("create_price", "max_lands", "default_height", "peace_duration"), + args[2]); + } // ★ cli manage/members/visitorperm/toggle: 第三层是领地名 if (sub.equals("cli")) { String action = args[1].toLowerCase(); diff --git a/src/main/java/Sdf1_login/WebManager.java b/src/main/java/Sdf1_login/WebManager.java index 159e900..0318c50 100644 --- a/src/main/java/Sdf1_login/WebManager.java +++ b/src/main/java/Sdf1_login/WebManager.java @@ -270,6 +270,8 @@ public class WebManager { sslCircuitOpenUntil = 0; sslConsecutiveFailures = 0; plugin.getLogger().info("[Web通信] SSL断路器冷却结束,恢复轮询"); + // ★ 重建HttpClient,清除可能损坏的连接状态 + rebuildHttpClient(); return false; } @@ -295,6 +297,9 @@ public class WebManager { * ★ 彻底方案:用 java.net.http.HttpClient 替代 HttpsURLConnection * HttpsURLConnection 内部调用 factory.createSocket()(无参版本)绕过了自定义工厂的配置 * HttpClient 原生处理 ALPN/SNI/TLS协商,完全不走HttpsURLConnection的工厂机制 + * + * ★ 信任策略:有证→通过,没证→拦截(信任所有证书,包括自签名) + * ★ CF兼容:强制TLS 1.2 + 指定密码套件 + 自动重建连接 */ private void initSSL() { final TrustManager[] trustAllCerts = new TrustManager[]{ @@ -306,25 +311,77 @@ public class WebManager { }; try { - // ★ 使用通用TLS(不限版本),让Java协商最佳TLS版本 - SSLContext sc = SSLContext.getInstance("TLS"); + // ★ 使用TLS 1.2(CF兼容性最好,TLS 1.3与CF边缘偶发不兼容) + SSLContext sc = SSLContext.getInstance("TLSv1.2"); sc.init(null, trustAllCerts, new SecureRandom()); + // ★ 配置SSL参数:指定兼容CF的密码套件 + javax.net.ssl.SSLParameters sslParams = sc.getDefaultSSLParameters(); + sslParams.setCipherSuites(new String[]{ + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", + "TLS_RSA_WITH_AES_128_GCM_SHA256", + "TLS_RSA_WITH_AES_256_GCM_SHA384", + "TLS_RSA_WITH_AES_128_CBC_SHA256", + "TLS_RSA_WITH_AES_256_CBC_SHA384" + }); + sslParams.setProtocols(new String[]{"TLSv1.2"}); + // ★ java.net.http.HttpClient + HTTP/1.1 // 不用HTTP/2:CF代理对HTTP/2的某些请求模式可能导致PHP返回500 // HTTP/1.1更稳定,且curl(也是HTTP/1.1)测试正常 cfHttpClient = HttpClient.newBuilder() .sslContext(sc) - .connectTimeout(Duration.ofSeconds(10)) + .sslParameters(sslParams) + .connectTimeout(Duration.ofSeconds(15)) .version(HttpClient.Version.HTTP_1_1) .build(); - plugin.getLogger().info("[Web通信] SSL已初始化(java.net.http.HttpClient, TLS自适应, 信任所有证书, HTTP/1.1)"); + plugin.getLogger().info("[Web通信] SSL已初始化(TLSv1.2, 信任所有证书, CF兼容密码套件, HTTP/1.1)"); } catch (Exception e) { plugin.getLogger().severe("[Web通信] SSL完全初始化失败: " + e.getMessage()); } } + /** + * ★ 重建HttpClient(SSL断路器冷却后调用,清除可能损坏的连接状态) + */ + private void rebuildHttpClient() { + try { + final TrustManager[] trustAllCerts = new TrustManager[]{ + new X509TrustManager() { + public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } + public void checkClientTrusted(X509Certificate[] certs, String authType) {} + public void checkServerTrusted(X509Certificate[] certs, String authType) {} + } + }; + SSLContext sc = SSLContext.getInstance("TLSv1.2"); + sc.init(null, trustAllCerts, new SecureRandom()); + + javax.net.ssl.SSLParameters sslParams = sc.getDefaultSSLParameters(); + sslParams.setCipherSuites(new String[]{ + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" + }); + sslParams.setProtocols(new String[]{"TLSv1.2"}); + + cfHttpClient = HttpClient.newBuilder() + .sslContext(sc) + .sslParameters(sslParams) + .connectTimeout(Duration.ofSeconds(15)) + .version(HttpClient.Version.HTTP_1_1) + .build(); + + plugin.getLogger().info("[Web通信] HttpClient已重建(清除连接状态)"); + } catch (Exception e) { + plugin.getLogger().warning("[Web通信] HttpClient重建失败: " + e.getMessage()); + } + } + // ==================== 统一HTTP请求方法(java.net.http.HttpClient) ==================== /**