From 85a8947e235939fc195ce3841efe9c054c1cc69a Mon Sep 17 00:00:00 2001 From: youpaishidifu <1@ypshidifu.cn> Date: Thu, 19 Nov 2026 12:20:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC125=E6=89=B9=E7=BB=AD4:=20Java?= =?UTF-8?q?=E7=89=88=E6=94=B9=E7=94=A8MC=E5=8E=9F=E7=94=9F=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=B1=8F=E5=B9=95=EF=BC=88Dialog=20API?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - showCustomScreen 用 Dialog.create + ClickEvent.showDialog 弹出对话框 - DialogBase:标题+协议正文(截断500字)+签名输入框 - ConfirmationType:签收(yes)+拒签(no)双按钮 - canCloseWithEscape=false 强制表态 - onCustomClick 捕获 PlayerCustomClickEvent: - IP 匹配在线玩家(PlayerCommonConnection 无 UUID 方法) - 读 DialogResponseView.getText("signature") - 有签名→校验签收;无签名→拒签 - 编译通过 EXIT=0 --- .../java/Sdf1_login/CustomScreenManager.java | 137 +++++++++++++++--- 1 file changed, 116 insertions(+), 21 deletions(-) diff --git a/src/main/java/Sdf1_login/CustomScreenManager.java b/src/main/java/Sdf1_login/CustomScreenManager.java index 73978a3..a9f914c 100644 --- a/src/main/java/Sdf1_login/CustomScreenManager.java +++ b/src/main/java/Sdf1_login/CustomScreenManager.java @@ -427,33 +427,82 @@ public class CustomScreenManager implements Listener { * 改用 Inventory GUI + 书与笔(完整协议塞入书页),死循环弹窗强制表态。 */ /** - * Java 版玩家:聊天确认(输入签名 → 回复 sign 签收 / deny 拒签)。 - * Paper 26.x Dialog API 需 Registry + builder,无法动态创建实例,改用聊天流。 - * 死循环:未表态前每次上线都重新提示。 + * Java 版玩家:MC 1.21.6+ 自定义屏幕(Dialog API)。 + * 用 Dialog.create + ClickEvent.showDialog 弹出对话框, + * 签收按钮 → 确认;拒签按钮 → 执行拒签动作。 + * 死循环:未表态前每次上线都重新弹。 */ private void showCustomScreen(Player p, String pendingFile, String ackFileName, String content, ActionConfig actions) { PendingSign ps = new PendingSign(pendingFile, ackFileName, content, actions); pendingSigs.put(p.getUniqueId(), ps); - // 分块发送协议内容(每段 5 行) - String[] lines = content.split("\\n"); - int chunk = 5; - for (int i = 0; i < lines.length; i += chunk) { - int end = Math.min(i + chunk, lines.length); - StringBuilder sb = new StringBuilder(); - for (int j = i; j < end; j++) { - sb.append(lines[j]); - if (j < end - 1) sb.append("\n"); - } - p.sendMessage(Component.text(sb.toString()).color(net.kyori.adventure.text.format.NamedTextColor.GRAY)); - } + try { + String title = "【待签收】" + new File(pendingFile).getName(); + String body = content.length() > 500 ? content.substring(0, 500) + "...(全文请右键书查看)" : content; - p.sendMessage(Component.text("")); - p.sendMessage(Component.text("§6§l【待签收】" + new File(pendingFile).getName())); - p.sendMessage(Component.text("§7请输入您的游戏ID作为签名,然后回复:")); - p.sendMessage(Component.text(" §a/sign§r = 签收 | §c/deny§r = 拒签(执行 " + ps.actions.denyAction + ")")); - p.sendMessage(Component.text("§e未表态前每次上线都会重新提示,必须表态")); - plugin.getLogger().info("[自定义屏幕] " + p.getName() + " Java版聊天确认已启动: " + new File(pendingFile).getName()); + // 构建 DialogBase:标题 + 正文 + 签名输入框 + io.papermc.paper.registry.data.dialog.DialogBase base = + io.papermc.paper.registry.data.dialog.DialogBase.builder( + Component.text(title) + ) + .canCloseWithEscape(false) + .afterAction(io.papermc.paper.registry.data.dialog.DialogBase.DialogAfterAction.NONE) + .body(java.util.List.of( + io.papermc.paper.registry.data.dialog.body.DialogBody.plainMessage( + Component.text(body).color(net.kyori.adventure.text.format.NamedTextColor.GRAY), + 400 + ) + )) + .inputs(java.util.List.of( + io.papermc.paper.registry.data.dialog.input.DialogInput.text( + "signature", + 300, + Component.text("签名(输入您的游戏ID)"), + true, + "", + 64, + null + ) + )) + .build(); + + // ConfirmationType:签收(yes)+ 拒签(no) + io.papermc.paper.registry.data.dialog.type.ConfirmationType type = + io.papermc.paper.registry.data.dialog.type.DialogType.confirmation( + io.papermc.paper.registry.data.dialog.ActionButton.create( + Component.text("§a§l✔ 签收"), + Component.text("确认签收用户协议"), + 200, + null + ), + io.papermc.paper.registry.data.dialog.ActionButton.create( + Component.text("§c§l✘ 拒签"), + Component.text("执行 " + ps.actions.denyAction + " 动作"), + 200, + null + ) + ); + + io.papermc.paper.dialog.Dialog dialog = io.papermc.paper.dialog.Dialog.create( + builder -> builder.empty() + .base(base) + .type(type) + ); + + // 发送对话框(通过 ClickEvent.showDialog 触发) + p.sendMessage(Component.empty().clickEvent( + net.kyori.adventure.text.event.ClickEvent.showDialog(dialog) + )); + + plugin.getLogger().info("[自定义屏幕] " + p.getName() + " Java版自定义屏幕已弹出: " + new File(pendingFile).getName()); + + } catch (Exception ex) { + plugin.getLogger().warning("[自定义屏幕] " + p.getName() + " 自定义屏幕失败,fallback 聊天: " + ex.getMessage()); + p.sendMessage(Component.text("§6【待签收】" + new File(pendingFile).getName())); + p.sendMessage(Component.text("§7请输入您的游戏ID作为签名,然后回复:")); + p.sendMessage(Component.text(" §a/sign§r = 签收 | §c/deny§r = 拒签(执行 " + ps.actions.denyAction + ")")); + p.sendMessage(Component.text("§e未表态前每次上线都会重新提示,必须表态")); + } } private org.bukkit.inventory.meta.ItemMeta buildMeta(Material mat, String displayName, String lore) { @@ -777,6 +826,52 @@ public class CustomScreenManager implements Listener { } } + // ==================== 自定义屏幕 Dialog 点击处理 ==================== + + /** + * 处理 Java 版自定义屏幕(Dialog API)的按钮点击。 + * 签收按钮 → 读取签名输入框 → 校验 → 落盘回执; + * 拒签按钮 → 执行拒签动作。 + */ + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false) + public void onCustomClick(io.papermc.paper.event.player.PlayerCustomClickEvent e) { + if (e.getDialogResponseView() == null) return; + + // 通过 IP 匹配在线玩家 + var connection = e.getCommonConnection(); + if (connection == null) return; + + java.net.SocketAddress addr = connection.getAddress(); + if (!(addr instanceof java.net.InetSocketAddress)) return; + String ip = ((java.net.InetSocketAddress) addr).getAddress().getHostAddress(); + + Player p = null; + for (Player online : Bukkit.getOnlinePlayers()) { + if (online.getAddress() != null && online.getAddress().getAddress().getHostAddress().equals(ip)) { + p = online; + break; + } + } + if (p == null) return; + + PendingSign ps = pendingSigs.get(p.getUniqueId()); + if (ps == null) return; + + io.papermc.paper.dialog.DialogResponseView view = e.getDialogResponseView(); + String signature = view.getText("signature"); + plugin.getLogger().info("[自定义屏幕] " + p.getName() + " 自定义屏幕点击, 签名=" + signature); + + if (signature != null && !signature.isEmpty()) { + if (signature.equalsIgnoreCase(p.getName())) { + handleAccept(p, ps, signature); + } else { + p.sendMessage(Component.text("§c签名「" + signature + "」与您的游戏ID不匹配,请重新上线")); + } + } else { + confirmDeny(p, new File(ps.pendingFile).getName(), ""); + } + } + /** * 处理 /sdf1_login sign [signature] 命令(备用,无需指定标题——自动匹配当前待签收)。 * @return true 已处理,false 未处理(非签收命令)