- /rules 默认 CLI:展示玩家已签过的全部协议(用户协议+玩家通知),支持随时调出原文,每页150字封顶,过长截断分页,支持 prev/next/back/quit + 聊天输入协议名/数字快速跳 - /rules gui:基岩版用 GUI(书与笔),Java 版也走 GUI 分支(唤醒书与笔 GUI 展示协议原文) - 服务器菜单(T_MAIN)信誉积分(slot40)旁边 slot41 加「用户协议」按钮(WRITABLE_BOOK 图标)→ 点击调起 /rules gui 数据来源: - 用户协议:用户协议/签收回执/<玩家名>.md(回执里的「来源:xxx.md」指回原文) - 玩家通知:公告/自定义屏幕/签收回执/玩家名.标题.md GUI 标题 = "RULES_MENU"(ASCII,基岩版可识别),BOOK 物品 lore 含协议名+前100字预览。 openBook 复用 CustomScreenManager 范式:Material.BOOK 可读 + Player.openBook(BookMeta) 全屏阅读,Geyser 转发正常。 编译 rc=0,jar 35b0dd96。
This commit is contained in:
@@ -231,6 +231,14 @@ public class GUIManager implements Listener {
|
||||
"§7上报好人好事/举报违规",
|
||||
"§7查看我的积分与明细"));
|
||||
|
||||
// ★ 125 续15g 用户协议 - 41号位(信誉积分旁边)
|
||||
// 点击 → 调起 /rules gui(书与笔展示已签协议原文;基岩版也走 GUI 分支)
|
||||
g.setItem(41, mkItem(Material.WRITABLE_BOOK,
|
||||
"§b§l用户协议",
|
||||
"§7查看已签协议原文",
|
||||
"§7CLI 模式: /rules",
|
||||
"§7GUI 模式: /rules gui"));
|
||||
|
||||
|
||||
|
||||
// ===== Row0(0-8): 自定义菜单项 =====
|
||||
|
||||
@@ -236,6 +236,7 @@ public class Main extends JavaPlugin
|
||||
|
||||
private ShopManager shopManager; //商店
|
||||
private CustomScreenManager customScreenManager; //用户协议 & 公告送达(第125批)
|
||||
private RulesCLIManager rulesCLIManager; //用户协议查看 /rules(125续15g CLI+GUI 双通道)
|
||||
// ★ 交易记录独立库(2026-10-11 任务5:正常商店订单/二手订单/玩家出售偏好)
|
||||
private TradeLogManager tradeLog;
|
||||
|
||||
@@ -518,6 +519,8 @@ public class Main extends JavaPlugin
|
||||
Bukkit.getPluginManager().registerEvents(shopManager, this);
|
||||
// 第125批:用户协议 & 公告送达
|
||||
customScreenManager = new CustomScreenManager(this);
|
||||
// 125 续15g:/rules 双通道(CLI + GUI,书与笔展示已签协议原文)
|
||||
rulesCLIManager = new RulesCLIManager(this);
|
||||
// shopManager.loadCategories(); // 构造器内已调用,此处重复
|
||||
|
||||
getServer().getPluginManager()
|
||||
@@ -8462,6 +8465,14 @@ public class Main extends JavaPlugin
|
||||
} else {
|
||||
p.sendMessage("§c信誉积分模块未初始化");
|
||||
}
|
||||
} else if (slot == 41) {
|
||||
// ★ 125 续15g 用户协议入口(信誉积分旁边)→ 调起 /rules gui
|
||||
p.closeInventory();
|
||||
if (rulesCLIManager != null) {
|
||||
rulesCLIManager.openGUI(p);
|
||||
} else {
|
||||
p.sendMessage("§c用户协议模块未初始化");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9319,6 +9330,18 @@ public class Main extends JavaPlugin
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// ★ 125 续15g:/rules 双通道(CLI + GUI,展示已签协议原文)
|
||||
if (cmd.getName().equalsIgnoreCase("rules")) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage("§c仅限玩家使用");
|
||||
return true;
|
||||
}
|
||||
if (rulesCLIManager == null) {
|
||||
sender.sendMessage("§c/rules 管理器未初始化,请重启插件。");
|
||||
return true;
|
||||
}
|
||||
return rulesCLIManager.handleCommand((Player) sender, args);
|
||||
}
|
||||
// ★ menu 子命令 - 重载菜单配置
|
||||
if (cmd.getName().equalsIgnoreCase("sdf1_login")
|
||||
&& args.length >= 2
|
||||
|
||||
@@ -0,0 +1,506 @@
|
||||
package Sdf1_login;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.BookMeta;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* ★ 125 续15g:/rules 指令双通道(CLI + GUI)
|
||||
*
|
||||
* 设计:
|
||||
* - /rules 默认走 CLI(展示已签协议列表 + 支持随时调出原文)
|
||||
* - /rules gui 走 GUI(书与笔:每个协议一个 BOOK 物品,lore 含协议名 + 前 100 字预览)
|
||||
* - /rules <协议名> 直接调出指定协议原文(CLI 分页)
|
||||
* - /rules <协议名> <页码> 调出指定页
|
||||
* - 聊天输入 数字/协议名/prev/next/back/quit 控制 CLI 分页
|
||||
*
|
||||
* 数据来源:
|
||||
* - 用户协议:`用户协议/签收回执/<玩家名>.md`(全局协议,回执里的「来源:xxx.md」指回原文)
|
||||
* - 玩家通知:`公告/自定义屏幕/签收回执/玩家名.标题.md`(回执里的「来源:xxx.md」指回原文)
|
||||
*/
|
||||
public class RulesCLIManager implements Listener {
|
||||
|
||||
private final Main plugin;
|
||||
|
||||
// 每个玩家当前正在查看的协议(null = 列表页)
|
||||
private final Map<UUID, String> viewing = new ConcurrentHashMap<>();
|
||||
// 每个玩家当前页码
|
||||
private final Map<UUID, Integer> currentPage = new ConcurrentHashMap<>();
|
||||
// CLI 会话中允许"聊天输入"的窗口
|
||||
private final Set<UUID> cliActive = ConcurrentHashMap.newKeySet();
|
||||
// 每页字数
|
||||
private static final int PAGE_SIZE = 150;
|
||||
|
||||
// 签收回执目录常量(与 CustomScreenManager 对齐)
|
||||
private static final String AGREEMENT_DIR = "用户协议";
|
||||
private static final String NOTIFICATION_DIR = "公告/自定义屏幕";
|
||||
private static final String ACK_DIR_AGREEMENT = AGREEMENT_DIR + "/签收回执";
|
||||
private static final String ACK_DIR_NOTIFICATION = NOTIFICATION_DIR + "/签收回执";
|
||||
|
||||
// GUI 标题(ASCII,基岩版可识别)
|
||||
private static final String GUI_TITLE = "RULES_MENU";
|
||||
|
||||
public RulesCLIManager(Main plugin) {
|
||||
this.plugin = plugin;
|
||||
Bukkit.getPluginManager().registerEvents(this, plugin);
|
||||
plugin.getLogger().info("[用户协议] /rules 双通道管理器已注册(CLI + GUI)");
|
||||
}
|
||||
|
||||
// ==================== 数据读取 ====================
|
||||
|
||||
/**
|
||||
* 拉取该玩家已签过的全部协议。
|
||||
* 返回 LinkedHashMap(保持顺序):key=协议展示名,value=原文绝对路径。
|
||||
* 协议名 = 回执文件名(去掉 .md 后缀),原文从回执里的「来源:xxx.md」字段取。
|
||||
*/
|
||||
public LinkedHashMap<String, String> listSignedAgreements(String playerName) {
|
||||
LinkedHashMap<String, String> result = new LinkedHashMap<>();
|
||||
// 用户协议:玩家名.md
|
||||
File agrDir = new File(plugin.getDataFolder(), ACK_DIR_AGREEMENT);
|
||||
if (agrDir.exists()) {
|
||||
File ack = new File(agrDir, playerName + ".md");
|
||||
if (ack.exists() && ack.length() > 0) {
|
||||
String content = readFile(ack);
|
||||
String source = parseSource(content);
|
||||
File sourceFile = new File(agrDir.getParentFile(), source != null ? source : "用户协议.md");
|
||||
if (sourceFile.exists()) {
|
||||
result.put(playerName + ".md", sourceFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
// 玩家通知:玩家名.标题.md
|
||||
File notiDir = new File(plugin.getDataFolder(), ACK_DIR_NOTIFICATION);
|
||||
if (notiDir.exists()) {
|
||||
File[] files = notiDir.listFiles((d, n) -> n.endsWith(".md") && n.indexOf('.') != n.lastIndexOf('.'));
|
||||
if (files != null) {
|
||||
for (File f : files) {
|
||||
String base = f.getName();
|
||||
int dot = base.indexOf('.');
|
||||
if (dot < 0) continue;
|
||||
String title = base.substring(dot + 1);
|
||||
if (title.equalsIgnoreCase(playerName)) continue; // 跳过全局协议回执
|
||||
String content = readFile(f);
|
||||
String source = parseSource(content);
|
||||
File sourceFile = new File(notiDir.getParentFile(), source != null ? source : title);
|
||||
if (sourceFile.exists()) {
|
||||
result.put("公告:" + source, sourceFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String parseSource(String receiptContent) {
|
||||
if (receiptContent == null) return null;
|
||||
for (String line : receiptContent.split("\\n")) {
|
||||
if (line.startsWith("来源:")) {
|
||||
return line.substring(3).trim();
|
||||
}
|
||||
if (line.startsWith("来源:")) {
|
||||
return line.substring(3).trim();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String readFile(File f) {
|
||||
try {
|
||||
return new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8);
|
||||
} catch (Exception ex) {
|
||||
plugin.getLogger().warning("[用户协议] 读文件失败 " + f.getAbsolutePath() + ": " + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== CLI 入口 ====================
|
||||
|
||||
/**
|
||||
* 处理 /rules 命令。
|
||||
* @param args /rules 后的参数(不含 rules 本身)
|
||||
* @return true 已处理
|
||||
*/
|
||||
public boolean handleCommand(Player p, String[] args) {
|
||||
String sub = args.length > 0 ? args[0].toLowerCase() : "";
|
||||
|
||||
// /rules gui → 走 GUI
|
||||
if (sub.equals("gui") || sub.equals("菜单")) {
|
||||
openGUI(p);
|
||||
return true;
|
||||
}
|
||||
|
||||
// /rules prev|next|back|quit
|
||||
if (sub.equals("prev")) {
|
||||
movePage(p, -1);
|
||||
return true;
|
||||
}
|
||||
if (sub.equals("next")) {
|
||||
movePage(p, +1);
|
||||
return true;
|
||||
}
|
||||
if (sub.equals("back")) {
|
||||
closeViewer(p);
|
||||
return true;
|
||||
}
|
||||
if (sub.equals("quit") || sub.equals("q")) {
|
||||
cliActive.remove(p.getUniqueId());
|
||||
p.sendMessage(Component.text("§7已退出协议查看模式。"));
|
||||
return true;
|
||||
}
|
||||
|
||||
// /rules <协议名> [页码] → 直接调出原文
|
||||
if (args.length >= 1 && !sub.isEmpty()
|
||||
&& !sub.equals("prev") && !sub.equals("next")
|
||||
&& !sub.equals("back") && !sub.equals("quit") && !sub.equals("q")) {
|
||||
String target = args[0];
|
||||
int page = args.length > 1 ? parsePage(args[1]) : 1;
|
||||
openAgreement(p, target, page);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 无参 → 进入 CLI 列表页
|
||||
if (args.length == 0 || sub.isEmpty()) {
|
||||
cliActive.add(p.getUniqueId());
|
||||
showList(p);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private int parsePage(String s) {
|
||||
try {
|
||||
int v = Integer.parseInt(s);
|
||||
return Math.max(1, v);
|
||||
} catch (Exception e) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== CLI 列表页 ====================
|
||||
|
||||
private void showList(Player p) {
|
||||
String name = p.getName();
|
||||
LinkedHashMap<String, String> signed = listSignedAgreements(name);
|
||||
p.sendMessage(Component.text("§d§l[用户协议] §f您已签收的协议(共 §e" + signed.size() + " §f份):"));
|
||||
if (signed.isEmpty()) {
|
||||
p.sendMessage(Component.text("§7(暂无签收回执。请先完成用户协议签收流程,再 /rules 查看原文。)"));
|
||||
} else {
|
||||
int i = 0;
|
||||
for (String key : signed.keySet()) {
|
||||
i++;
|
||||
p.sendMessage(Component.text(" §e" + i + "§f " + Component.text(key)));
|
||||
}
|
||||
p.sendMessage(Component.text("§7输入 1~" + i + " 调出原文 | /rules prev|next 翻页 | back 返回 | quit 退出 | /rules gui 开 GUI"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调出指定协议原文(CLI 分页展示)。
|
||||
* @param target 协议展示名(与 listSignedAgreements 的 key 对应)或数字序号
|
||||
*/
|
||||
private void openAgreement(Player p, String target, int page) {
|
||||
String name = p.getName();
|
||||
LinkedHashMap<String, String> signed = listSignedAgreements(name);
|
||||
if (signed.isEmpty()) {
|
||||
p.sendMessage(Component.text("§c您尚未签收任何协议。"));
|
||||
return;
|
||||
}
|
||||
// 数字选择("1"/"2")或精确名
|
||||
String key = null;
|
||||
if (target.matches("\\d+")) {
|
||||
int idx = Integer.parseInt(target) - 1;
|
||||
List<String> keys = new ArrayList<>(signed.keySet());
|
||||
if (idx >= 0 && idx < keys.size()) key = keys.get(idx);
|
||||
} else {
|
||||
key = target;
|
||||
// 容错:带「公告:」前缀的协议名允许省略前缀
|
||||
if (!signed.containsKey(key) && !key.startsWith("公告:")) {
|
||||
key = "公告:" + key;
|
||||
if (!signed.containsKey(key)) key = null;
|
||||
}
|
||||
}
|
||||
if (key == null || !signed.containsKey(key)) {
|
||||
p.sendMessage(Component.text("§c未找到协议「" + target + "」。当前已签:" + signed.keySet()));
|
||||
return;
|
||||
}
|
||||
String content = readFile(new File(signed.get(key)));
|
||||
if (content == null) {
|
||||
p.sendMessage(Component.text("§c协议原文读取失败。"));
|
||||
return;
|
||||
}
|
||||
// 分页
|
||||
List<String> pages = paginate(content, PAGE_SIZE);
|
||||
int pg = Math.max(1, Math.min(page, pages.size()));
|
||||
viewing.put(p.getUniqueId(), key);
|
||||
currentPage.put(p.getUniqueId(), pg);
|
||||
cliActive.add(p.getUniqueId());
|
||||
renderPage(p, key, pages, pg);
|
||||
}
|
||||
|
||||
private void movePage(Player p, int dir) {
|
||||
String key = viewing.get(p.getUniqueId());
|
||||
if (key == null) {
|
||||
p.sendMessage(Component.text("§7当前未查看任何协议,先 /rules <名> 调出。"));
|
||||
return;
|
||||
}
|
||||
int cur = currentPage.getOrDefault(p.getUniqueId(), 1);
|
||||
String content = findAgreementContent(key, p.getName());
|
||||
int total = content == null ? 1 : paginate(content, PAGE_SIZE).size();
|
||||
int next = cur + dir;
|
||||
if (next < 1 || next > total) {
|
||||
p.sendMessage(Component.text("§7已是第 " + (next < 1 ? "1" : String.valueOf(total)) + " 页(共 " + total + " 页),无法" + (dir < 0 ? "往前" : "往后") + "翻页。"));
|
||||
return;
|
||||
}
|
||||
currentPage.put(p.getUniqueId(), next);
|
||||
renderPage(p, key, null, next); // null = 重新读原文
|
||||
}
|
||||
|
||||
private void closeViewer(Player p) {
|
||||
viewing.remove(p.getUniqueId());
|
||||
currentPage.remove(p.getUniqueId());
|
||||
p.sendMessage(Component.text("§7已返回协议列表。输入数字 1~N 调出,或 /rules 重看列表,quit 退出。"));
|
||||
}
|
||||
|
||||
private String findAgreementContent(String key, String playerName) {
|
||||
LinkedHashMap<String, String> signed = listSignedAgreements(playerName);
|
||||
String path = signed.get(key);
|
||||
if (path == null) return null;
|
||||
return readFile(new File(path));
|
||||
}
|
||||
|
||||
/** 渲染单页 */
|
||||
private void renderPage(Player p, String key, List<String> pages, int page) {
|
||||
if (pages == null) pages = paginate(findAgreementContent(key, p.getName()), PAGE_SIZE);
|
||||
int total = pages.size();
|
||||
int pg = Math.max(1, Math.min(page, total));
|
||||
String text = pages.get(pg - 1);
|
||||
p.sendMessage(Component.text("§d§l[用户协议] §f「" + key + "」 §7(" + pg + "/" + total + " 页)"));
|
||||
for (String line : text.split("\\n")) {
|
||||
if (line.isEmpty()) continue;
|
||||
p.sendMessage(Component.text("§f" + line));
|
||||
}
|
||||
p.sendMessage(Component.text("§7prev 上一页 | next 下一页 | back 返回列表 | quit 退出 | /rules gui 开 GUI"));
|
||||
}
|
||||
|
||||
// ==================== 聊天输入拦截(CLI 模式下支持"协议名/数字"快速跳) ====================
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onChat(org.bukkit.event.player.AsyncPlayerChatEvent e) {
|
||||
if (!cliActive.contains(e.getPlayer().getUniqueId())) return;
|
||||
String msg = e.getMessage().trim();
|
||||
if (msg.isEmpty()) return;
|
||||
Player p = e.getPlayer();
|
||||
String low = msg.toLowerCase();
|
||||
if (low.equals("quit") || low.equals("q")) {
|
||||
cliActive.remove(p.getUniqueId());
|
||||
p.sendMessage(Component.text("§7已退出协议查看模式。"));
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
if (low.equals("prev")) {
|
||||
movePage(p, -1);
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
if (low.equals("next")) {
|
||||
movePage(p, +1);
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
if (low.equals("back")) {
|
||||
closeViewer(p);
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
// 数字 1~N → 跳协议
|
||||
if (msg.matches("\\d+")) {
|
||||
openAgreement(p, msg, 1);
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
// 协议名(含「公告:xxx」或纯 xxx.md)→ 跳协议
|
||||
openAgreement(p, msg, 1);
|
||||
e.setCancelled(true);
|
||||
}
|
||||
|
||||
/** 分页:每页最多 pageSize 字,按行切分(一行不跨页) */
|
||||
private List<String> paginate(String content, int pageSize) {
|
||||
List<String> pages = new ArrayList<>();
|
||||
if (content == null || content.isEmpty()) {
|
||||
pages.add("(空协议原文)");
|
||||
return pages;
|
||||
}
|
||||
StringBuilder cur = new StringBuilder();
|
||||
int curLen = 0;
|
||||
for (String line : content.split("\\n")) {
|
||||
int addLen = line.length() + 1; // +1 换行
|
||||
if (curLen + addLen > pageSize && cur.length() > 0) {
|
||||
pages.add(cur.toString().trim());
|
||||
cur.setLength(0);
|
||||
curLen = 0;
|
||||
}
|
||||
cur.append(line).append('\n');
|
||||
curLen += addLen;
|
||||
if (curLen >= pageSize) {
|
||||
pages.add(cur.toString().trim());
|
||||
cur.setLength(0);
|
||||
curLen = 0;
|
||||
}
|
||||
}
|
||||
if (cur.length() > 0) pages.add(cur.toString().trim());
|
||||
if (pages.isEmpty()) pages.add("(空协议原文)");
|
||||
return pages;
|
||||
}
|
||||
|
||||
// ==================== GUI(书与笔) ====================
|
||||
|
||||
/**
|
||||
* 打开 GUI:每个已签协议一个 WRITABLE_BOOK 物品(lore = 协议名 + 前 100 字预览)。
|
||||
* 点 BOOK → 打开书与笔(WRITABLE_BOOK 全屏阅读,可翻页/合上),Java 版 + 基岩版都可用。
|
||||
*/
|
||||
public void openGUI(Player p) {
|
||||
LinkedHashMap<String, String> signed = listSignedAgreements(p.getName());
|
||||
int size = 54;
|
||||
Inventory inv = Bukkit.createInventory(null, size, GUI_TITLE);
|
||||
int slot = 0;
|
||||
for (String key : signed.keySet()) {
|
||||
if (slot >= size) break;
|
||||
String content = readFile(new File(signed.get(key)));
|
||||
String preview = truncatePreview(content, 100);
|
||||
ItemStack book = new ItemStack(Material.WRITABLE_BOOK, 1);
|
||||
ItemMeta meta = book.getItemMeta();
|
||||
meta.setDisplayName("§e§l📜 " + key);
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore.add("§7协议原文前 100 字预览:");
|
||||
lore.add("§f" + (preview != null ? preview : "(空)"));
|
||||
lore.add("");
|
||||
lore.add("§a点击 打开书与笔 阅读全文");
|
||||
lore.add("§7可翻页 / 合上 / 随时返回菜单");
|
||||
meta.setLore(lore);
|
||||
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
|
||||
book.setItemMeta(meta);
|
||||
inv.setItem(slot, book);
|
||||
slot++;
|
||||
}
|
||||
// 空槽提示
|
||||
if (signed.isEmpty()) {
|
||||
ItemStack hint = new ItemStack(Material.BARRIER, 1);
|
||||
ItemMeta hm = hint.getItemMeta();
|
||||
hm.setDisplayName("§c§l未签收任何协议");
|
||||
hm.setLore(java.util.Arrays.asList("§7先完成用户协议签收流程,再 /rules gui 查看原文。"));
|
||||
hint.setItemMeta(hm);
|
||||
inv.setItem(0, hint);
|
||||
}
|
||||
p.openInventory(inv);
|
||||
}
|
||||
|
||||
private String truncatePreview(String content, int maxChars) {
|
||||
if (content == null) return null;
|
||||
content = content.replaceAll("\\s+", " ").trim();
|
||||
if (content.length() <= maxChars) return content;
|
||||
return content.substring(0, maxChars) + "…";
|
||||
}
|
||||
|
||||
/** GUI 点击:点 BOOK → 打开书与笔(WRITABLE_BOOK 全屏阅读) */
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onGuiClick(InventoryClickEvent e) {
|
||||
String title = e.getView().getTitle();
|
||||
if (!GUI_TITLE.equals(title)) return;
|
||||
if (!(e.getWhoClicked() instanceof Player)) return;
|
||||
Player p = (Player) e.getWhoClicked();
|
||||
int raw = e.getRawSlot();
|
||||
if (raw < 0) return; // 玩家自身物品栏
|
||||
ItemStack clicked = e.getView().getTopInventory().getItem(raw);
|
||||
if (clicked == null || clicked.getType() != Material.WRITABLE_BOOK) return;
|
||||
e.setCancelled(true);
|
||||
ItemMeta meta = clicked.getItemMeta();
|
||||
String display = meta.getDisplayName();
|
||||
if (display == null) return;
|
||||
// 去掉 color code 与「§e§l📜 」前缀
|
||||
String key = display
|
||||
.replaceAll("§e§l", "")
|
||||
.replaceAll("📜", "")
|
||||
.replaceAll("§[0-9a-fk-orA-FK-OR]", "")
|
||||
.trim();
|
||||
openBook(p, key);
|
||||
}
|
||||
|
||||
/** 打开书与笔(Material.BOOK 可读 + Player.openBook 全屏阅读,Geyser 基岩版也兼容) */
|
||||
private void openBook(Player p, String key) {
|
||||
String content = findAgreementContent(key, p.getName());
|
||||
if (content == null) {
|
||||
p.sendMessage(Component.text("§c协议原文「" + key + "」读取失败。"));
|
||||
return;
|
||||
}
|
||||
// 创建 BOOK 物品(含协议内容分页,每页最多 387 字符,vanilla 书页容量)
|
||||
ItemStack book = createBookItem(content, key);
|
||||
if (book == null) {
|
||||
p.sendMessage(Component.text("§c书与笔创建失败。"));
|
||||
return;
|
||||
}
|
||||
ItemMeta meta = book.getItemMeta();
|
||||
if (meta instanceof BookMeta) {
|
||||
BookMeta bm = (BookMeta) meta;
|
||||
p.openBook(bm);
|
||||
p.sendMessage(Component.text("§a已打开书与笔「" + key + "」。右键翻页,合上后再次右键可继续。"));
|
||||
} else {
|
||||
p.sendMessage(Component.text("§c书与笔元数据创建失败。"));
|
||||
}
|
||||
}
|
||||
|
||||
/** 创建书与笔(Material.BOOK 可读版,含完整协议内容分页)。复用 CustomScreenManager 范式。 */
|
||||
private ItemStack createBookItem(String content, String fileName) {
|
||||
ItemStack book = new ItemStack(Material.BOOK, 1);
|
||||
ItemMeta meta = book.getItemMeta();
|
||||
if (meta instanceof BookMeta) {
|
||||
BookMeta bm = (BookMeta) meta;
|
||||
bm.setTitle("§6" + fileName);
|
||||
bm.setAuthor("Sdf1_login");
|
||||
// 协议内容分块写入(每页最多 387 字符,vanilla 书页容量)
|
||||
List<String> pages = new ArrayList<>();
|
||||
String[] lines = content.split("\\n");
|
||||
StringBuilder page = new StringBuilder();
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
String line = lines[i];
|
||||
if (page.length() + line.length() > 387) {
|
||||
if (page.length() > 0) {
|
||||
pages.add(page.toString());
|
||||
page.setLength(0);
|
||||
}
|
||||
if (line.length() > 387) {
|
||||
// 超长行拆分
|
||||
for (int j = 0; j < line.length(); j += 387) {
|
||||
int end = Math.min(j + 387, line.length());
|
||||
pages.add(line.substring(j, end));
|
||||
}
|
||||
} else {
|
||||
page.append(line).append('\n');
|
||||
}
|
||||
} else {
|
||||
page.append(line).append('\n');
|
||||
}
|
||||
}
|
||||
if (page.length() > 0) pages.add(page.toString());
|
||||
bm.setPages(pages);
|
||||
book.setItemMeta(meta);
|
||||
}
|
||||
return book;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user