This commit is contained in:
@@ -44,6 +44,7 @@ import java.util.regex.Pattern;
|
||||
public class CustomScreenManager implements Listener {
|
||||
|
||||
private final JavaPlugin plugin;
|
||||
private final DenyListManager denyListManager;
|
||||
private final Map<UUID, PendingSign> pendingSigs = new ConcurrentHashMap<>();
|
||||
private final Map<UUID, String> draftSignatures = new ConcurrentHashMap<>();
|
||||
// ★ 125 续15d 知情同意门控:记录"已点过查看协议原文"的玩家UUID。
|
||||
@@ -65,6 +66,7 @@ public class CustomScreenManager implements Listener {
|
||||
|
||||
public CustomScreenManager(JavaPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
this.denyListManager = new DenyListManager(plugin);
|
||||
Bukkit.getPluginManager().registerEvents(this, plugin);
|
||||
plugin.getLogger().info("[自定义屏幕] 管理器已注册(用户协议 & 公告送达)");
|
||||
}
|
||||
@@ -139,6 +141,17 @@ public class CustomScreenManager implements Listener {
|
||||
* 公告/自定义屏幕目录:按 玩家名.标题.md 匹配,签收回执 = 玩家名.标题.md。
|
||||
*/
|
||||
private void checkAndShowPending(Player p) {
|
||||
// ★ 125 续15l-2 拒签集合门禁:命中 用户协议/deny.json 或 ban/通配封禁.json 的玩家
|
||||
// 不弹任何自定义屏幕,直接按正常拒签话术踢出(沿用 用户协议/action.json 的 denyAction,默认 kick)。
|
||||
if (denyListManager != null && denyListManager.isDenied(p)) {
|
||||
plugin.getLogger().info("[自定义屏幕] " + p.getName() + " 命中拒签集合(deny.json/通配封禁),不弹屏,直接踢出");
|
||||
String deny = loadAgreementDenyAction();
|
||||
kickByDenyAction(p, deny, false);
|
||||
pendingSigs.remove(p.getUniqueId());
|
||||
draftSignatures.remove(p.getUniqueId());
|
||||
return;
|
||||
}
|
||||
|
||||
String name = p.getName();
|
||||
|
||||
// 1. 扫描用户协议目录(全局协议:所有 .md 对所有玩家生效)
|
||||
@@ -160,7 +173,9 @@ public class CustomScreenManager implements Listener {
|
||||
return; // 无待签收
|
||||
}
|
||||
|
||||
// 合并所有待签收文件
|
||||
// ★ 125 续15l-1 签收顺序铁律:用户协议(agrFiles)严格优先于自定义通知(notiFiles)。
|
||||
// 合并后取第一个未签收的作为主弹窗;agrFiles 已排前,故玩家先签用户协议,
|
||||
// 签完 handleAccept 会重新调用 checkAndShowPending 自动弹出剩余的自定义通知。
|
||||
String[] all = new String[agrFiles.length + notiFiles.length];
|
||||
System.arraycopy(agrFiles, 0, all, 0, agrFiles.length);
|
||||
System.arraycopy(notiFiles, 0, all, agrFiles.length, notiFiles.length);
|
||||
@@ -332,46 +347,150 @@ public class CustomScreenManager implements Listener {
|
||||
|
||||
/**
|
||||
* 创建书与笔(含完整协议内容)。
|
||||
* ★ 125 续15l-1 分页优化:
|
||||
* 1. 封页 = 标题页(文件名/书名),正文从第 1 页起;
|
||||
* 2. 按「语义边界」分页:优先在中文句号/问号/叹号/分号/段落/章节标题处换页,
|
||||
* 避免一句被拦腰切断;
|
||||
* 3. 单页尽量写满 ~380 字符,不再一页只放一句话(解决第1页太空、总页数过多的问题)。
|
||||
*/
|
||||
private ItemStack createBookItem(String content, String fileName) {
|
||||
org.bukkit.inventory.ItemStack book = new org.bukkit.inventory.ItemStack(Material.BOOK, 1);
|
||||
org.bukkit.inventory.meta.ItemMeta meta = book.getItemMeta();
|
||||
if (meta instanceof org.bukkit.inventory.meta.BookMeta) {
|
||||
if (!(meta instanceof org.bukkit.inventory.meta.BookMeta)) {
|
||||
return book;
|
||||
}
|
||||
org.bukkit.inventory.meta.BookMeta bookMeta = (org.bukkit.inventory.meta.BookMeta) meta;
|
||||
bookMeta.setTitle("§6" + new File(fileName).getName());
|
||||
bookMeta.setAuthor("Sdf1_login");
|
||||
// 将协议内容分块写入(每页最多 387 字符)
|
||||
|
||||
String titleLine = new File(fileName).getName();
|
||||
String body = content == null ? "" : content.trim();
|
||||
|
||||
// 正文按行规整:去 markdown 残留、压缩空行
|
||||
body = body
|
||||
.replace("**", "")
|
||||
.replace("__", "")
|
||||
.replace("*", "")
|
||||
.replaceAll("(?m)^\\s*[-*+]\\s*", "• ")
|
||||
.replaceAll("\\n{3,}", "\n\n");
|
||||
|
||||
java.util.List<String> pages = new java.util.ArrayList<>();
|
||||
String[] lines = content.split("\\n");
|
||||
pages.add(titleLine); // 封页
|
||||
|
||||
if (!body.isEmpty()) {
|
||||
String[] paragraphs = body.split("\\n");
|
||||
StringBuilder page = new StringBuilder();
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
if (page.length() + lines[i].length() > 387) {
|
||||
if (page.length() > 0) {
|
||||
pages.add(page.toString());
|
||||
for (int i = 0; i < paragraphs.length; i++) {
|
||||
String p = paragraphs[i];
|
||||
if (p.isEmpty()) {
|
||||
// 空行:若当前页已有一定内容,保留为段落分隔;否则忽略
|
||||
if (page.length() > 60) page.append("\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 单段超长 → 按句拆
|
||||
if (p.length() > 380) {
|
||||
java.util.List<String> sentences = splitSentences(p);
|
||||
for (String s : sentences) {
|
||||
if (page.length() + s.length() + 1 > 380 && page.length() > 0) {
|
||||
pages.add(page.toString().trim());
|
||||
page = new StringBuilder();
|
||||
}
|
||||
if (lines[i].length() > 387) {
|
||||
// 超长行拆分
|
||||
for (int j = 0; j < lines[i].length(); j += 387) {
|
||||
int end = Math.min(j + 387, lines[i].length());
|
||||
pages.add(lines[i].substring(j, end));
|
||||
page.append(s);
|
||||
}
|
||||
} else {
|
||||
page.append(lines[i]).append("\n");
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
page.append(lines[i]).append("\n");
|
||||
|
||||
// 当前行加入是否超页
|
||||
if (page.length() + p.length() + 1 > 380) {
|
||||
// 超页前优先在语义边界(。!?;、章节标题)处截断
|
||||
int cut = findSemanticCut(page.toString(), 380);
|
||||
if (cut > 0) {
|
||||
pages.add(page.substring(0, cut).trim());
|
||||
page = new StringBuilder(page.substring(cut).trim());
|
||||
}
|
||||
if (page.length() + p.length() + 1 > 380) {
|
||||
pages.add(page.toString().trim());
|
||||
page = new StringBuilder();
|
||||
}
|
||||
}
|
||||
page.append(p);
|
||||
}
|
||||
if (page.length() > 0) {
|
||||
pages.add(page.toString());
|
||||
pages.add(page.toString().trim());
|
||||
}
|
||||
}
|
||||
|
||||
// 末尾页若过短(<20 字),并入上一页,避免孤页
|
||||
if (pages.size() > 2) {
|
||||
String last = pages.get(pages.size() - 1);
|
||||
String prev = pages.get(pages.size() - 2);
|
||||
if (last.length() < 20 && (prev.length() + last.length() + 1) <= 380) {
|
||||
pages.set(pages.size() - 2, prev + "\n" + last);
|
||||
pages.remove(pages.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
bookMeta.setPages(pages);
|
||||
book.setItemMeta(meta);
|
||||
}
|
||||
return book;
|
||||
}
|
||||
|
||||
/** 把一段文字按中文/英文句界拆成短句列表。 */
|
||||
private java.util.List<String> splitSentences(String text) {
|
||||
java.util.List<String> out = new java.util.ArrayList<>();
|
||||
String t = text.trim();
|
||||
int last = 0;
|
||||
for (int i = 0; i < t.length(); i++) {
|
||||
char c = t.charAt(i);
|
||||
if (c == '。' || c == '!' || c == '?' || c == ';' || c == '.' || c == '!' || c == '?' || c == ';') {
|
||||
String s = t.substring(last, i + 1).trim();
|
||||
if (!s.isEmpty()) out.add(s);
|
||||
last = i + 1;
|
||||
}
|
||||
}
|
||||
if (last < t.length()) {
|
||||
String tail = t.substring(last).trim();
|
||||
if (!tail.isEmpty()) out.add(tail);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** 在 limit 以内找最佳语义截断点(。!?;、章节号、行首)。 */
|
||||
private int findSemanticCut(String s, int limit) {
|
||||
if (s.length() <= limit) return s.length();
|
||||
int best = -1;
|
||||
for (int i = s.length() - 1; i >= 0; i--) {
|
||||
char c = s.charAt(i);
|
||||
if (c == '。' || c == '!' || c == '?' || c == ';' || c == '\n') {
|
||||
best = i + 1;
|
||||
break;
|
||||
}
|
||||
if ((c == '1' || c == '2' || c == '3' || c == '4' || c == '5'
|
||||
|| c == '6' || c == '7' || c == '8' || c == '9')) {
|
||||
String rest = s.substring(i + 1, Math.min(i + 3, s.length()));
|
||||
if (rest.startsWith("、") || rest.startsWith(" ") || rest.startsWith(".")) {
|
||||
best = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i < limit && c == '\n') {
|
||||
best = i + 1;
|
||||
}
|
||||
}
|
||||
if (best <= 0 || best > limit) {
|
||||
// 找不到语义边界,退化为按空格/逗号切
|
||||
for (int i = s.length() - 1; i >= 0; i--) {
|
||||
if (s.charAt(i) == ' ' || s.charAt(i) == ',' || s.charAt(i) == ',' || s.charAt(i) == '、') {
|
||||
best = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (best <= 0 || best > limit) best = limit;
|
||||
return best;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false)
|
||||
public void onInventoryClose(org.bukkit.event.inventory.InventoryCloseEvent e) {
|
||||
if (!(e.getPlayer() instanceof Player)) return;
|
||||
@@ -935,6 +1054,18 @@ public class CustomScreenManager implements Listener {
|
||||
cleanupUI(p);
|
||||
pendingSigs.remove(p.getUniqueId());
|
||||
draftSignatures.remove(p.getUniqueId());
|
||||
|
||||
// ★ 125 续15l-1 签收顺序铁律:用户协议签完后立即重查剩余待签项(通常是自定义通知)。
|
||||
// 若还有未签协议/通知 → 延迟 1 秒自动弹出下一个;若已无 → 静默结束。
|
||||
// 这样玩家先签用户协议,签完自动进入自定义通知,不会漏签。
|
||||
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
|
||||
if (!p.isOnline()) return;
|
||||
try {
|
||||
checkAndShowPending(p);
|
||||
} catch (Exception ex) {
|
||||
plugin.getLogger().warning("[自定义屏幕] " + p.getName() + " 签收后自动重弹异常: " + ex.getMessage());
|
||||
}
|
||||
}, 20L);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1091,6 +1222,9 @@ public class CustomScreenManager implements Listener {
|
||||
: "您已拒签用户协议,已被封禁 1 年。如有疑问请联系管理员。",
|
||||
java.time.Duration.ofDays(365), "Sdf1_login", false);
|
||||
plugin.getLogger().info("[自定义屏幕] " + p.getName() + " 已封禁 1 年(ban)");
|
||||
if (denyListManager != null) {
|
||||
denyListManager.addDeny(p.getName(), "action.json 拒签动作=ban");
|
||||
}
|
||||
break;
|
||||
case "banip":
|
||||
String ip = p.getAddress() != null ? p.getAddress().getAddress().getHostAddress() : "";
|
||||
@@ -1102,6 +1236,9 @@ public class CustomScreenManager implements Listener {
|
||||
p.banIp(ip, java.time.Duration.ofDays(365), "Sdf1_login", false);
|
||||
} catch (Exception ignored) {}
|
||||
plugin.getLogger().info("[自定义屏幕] " + p.getName() + " IP " + ip + " 已封禁 1 年(banip)");
|
||||
if (denyListManager != null) {
|
||||
denyListManager.addDeny(p.getName(), "action.json 拒签动作=banip");
|
||||
}
|
||||
} else {
|
||||
p.kick(Component.text("IP 获取失败,请重新登录并拒签。"));
|
||||
}
|
||||
@@ -1117,6 +1254,46 @@ public class CustomScreenManager implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取 用户协议/action.json 的 deny 值(默认 kick)。
|
||||
*/
|
||||
private String loadAgreementDenyAction() {
|
||||
try {
|
||||
File f = new File(plugin.getDataFolder(), AGREEMENT_DIR + "/action.json");
|
||||
if (f.exists()) {
|
||||
String raw = new String(Files.readAllBytes(f.toPath()), java.nio.charset.StandardCharsets.UTF_8);
|
||||
ActionConfig cfg = new ActionConfig();
|
||||
parseJsonConfig(raw, cfg);
|
||||
if (cfg.denyAction != null && !cfg.denyAction.isEmpty()) {
|
||||
return cfg.denyAction;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
return "kick";
|
||||
}
|
||||
|
||||
/**
|
||||
* 按指定 deny 动作踢出玩家(用于拒签集合门禁命中场景)。
|
||||
*/
|
||||
private void kickByDenyAction(Player p, String action, boolean jsonBlockSource) {
|
||||
action = normalizeAction(action);
|
||||
String msg = "您已被列入拒签集合,无法进入服务器。如有疑问请联系管理员。";
|
||||
switch (action) {
|
||||
case "del":
|
||||
Main main = (Main) plugin;
|
||||
try {
|
||||
if (main.getDb() != null) main.getDb().deleteUser(p.getName());
|
||||
main.markDeletedByDeny(p.getName());
|
||||
} catch (Exception ignored) {}
|
||||
p.kick(Component.text(msg));
|
||||
break;
|
||||
case "kick":
|
||||
default:
|
||||
p.kick(Component.text(msg));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理 UI:关闭当前 GUI 即可(GUI 物品不存盘,关闭自动消失)。
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,394 @@
|
||||
package Sdf1_login;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 125 续15l-2 用户协议拒签集合 deny.json 管理器。
|
||||
*
|
||||
* 职责:
|
||||
* 1. 玩家进入(CustomScreenManager.checkAndShowPending 入口)时检查 用户协议/deny.json
|
||||
* 是否命中当前玩家名(精确匹配或 * 通配符);命中则不弹自定义屏幕,直接按
|
||||
* 正常拒签话术踢出(沿用 用户协议/action.json 的 denyAction,默认 kick)。
|
||||
* 2. 监听 ban/通配封禁.json 中 endtime=0(永久)或 endtime-starttime≥10年 的条目,
|
||||
* 把对应通配模式懒加载到内存;玩家进入时重新扫一遍该文件(指纹未变则直接复用
|
||||
* 上次内存结果,不读磁盘)——这是“玩家加入时懒重载”语义。
|
||||
* 3. 手动在 deny.json 里追加的条目(如 "steve_*")同样支持通配符。
|
||||
*
|
||||
* 说明:deny.json 中若包含中文说明文本,本管理器只识别纯用户名/通配模式字符串,
|
||||
* 说明文本会被静默忽略(不参与匹配、不报错)。
|
||||
*/
|
||||
public class DenyListManager {
|
||||
|
||||
|
||||
private final JavaPlugin plugin;
|
||||
private final File denyJson;
|
||||
private final File wildcardBanJson;
|
||||
|
||||
/** 手动维护的 用户协议/deny.json 里解析出的用户/通配模式(小写)。 */
|
||||
private Set<String> cachedManualPatterns = new HashSet<>();
|
||||
/** ban/通配封禁.json 里满足 永久/≥10年 的 user 通配模式(小写)。 */
|
||||
private Set<String> cachedWildcardBanPatterns = new HashSet<>();
|
||||
private String manualFp = "";
|
||||
private String wildcardFp = "";
|
||||
private boolean loadedOnce = false;
|
||||
|
||||
private static final String WILDCARD_BAN_FILE = "ban/通配封禁.json";
|
||||
|
||||
public DenyListManager(JavaPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
this.denyJson = new File(plugin.getDataFolder(), "用户协议/deny.json");
|
||||
this.wildcardBanJson = new File(plugin.getDataFolder(), WILDCARD_BAN_FILE);
|
||||
ensureDenyJsonExists();
|
||||
ensureWildcardBanJsonExists();
|
||||
}
|
||||
|
||||
/**
|
||||
* 玩家进入时调用:
|
||||
* 1. 懒重载通配封禁文件到内存模式;
|
||||
* 2. 若当前玩家命中任意模式 → 返回 true(调用方应直接踢出/拒绝,不弹屏);
|
||||
* 3. 未命中 → false。
|
||||
*/
|
||||
public boolean isDenied(Player p) {
|
||||
if (p == null) return false;
|
||||
reloadWildcardBans();
|
||||
String name = p.getName().toLowerCase(Locale.ROOT);
|
||||
for (String pattern : cachedManualPatterns) {
|
||||
if (matches(name, pattern)) return true;
|
||||
}
|
||||
for (String pattern : cachedWildcardBanPatterns) {
|
||||
if (matches(name, pattern)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 供外部(如 /reg 回执锁)查询某玩家是否命中 deny 集合。 */
|
||||
public boolean isDeniedName(String playerName) {
|
||||
reloadWildcardBans();
|
||||
String name = playerName == null ? "" : playerName.toLowerCase(Locale.ROOT);
|
||||
for (String pattern : cachedManualPatterns) {
|
||||
if (matches(name, pattern)) return true;
|
||||
}
|
||||
for (String pattern : cachedWildcardBanPatterns) {
|
||||
if (matches(name, pattern)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 懒重载:读 用户协议/deny.json + ban/通配封禁.json,刷新内存 cachedPatterns。
|
||||
* 玩家加入时由 isDenied 调用;指纹未变时跳过磁盘读。
|
||||
*/
|
||||
private void reloadWildcardBans() {
|
||||
String mfp = fingerprint(denyJson);
|
||||
String wfp = fingerprint(wildcardBanJson);
|
||||
boolean changed = !mfp.equals(manualFp) || !wfp.equals(wildcardFp);
|
||||
if (loadedOnce && !changed) return;
|
||||
|
||||
// 1) 手动维护的 用户协议/deny.json
|
||||
cachedManualPatterns = filterPatterns(loadStringPatterns(denyJson));
|
||||
|
||||
// 2) ban/通配封禁.json 中满足 永久 或 ≥10年 的 user 通配模式
|
||||
cachedWildcardBanPatterns = filterPatterns(loadLongBanPatterns(wildcardBanJson));
|
||||
|
||||
manualFp = mfp;
|
||||
wildcardFp = wfp;
|
||||
loadedOnce = true;
|
||||
plugin.getLogger().info("[拒签集合] 懒重载完成:用户协议/deny.json + " + WILDCARD_BAN_FILE
|
||||
+ ",生效手动模式 " + cachedManualPatterns.size() + " 条"
|
||||
+ ",生效通配封禁模式 " + cachedWildcardBanPatterns.size() + " 条"
|
||||
+ "(" + joinAll() + ")");
|
||||
}
|
||||
|
||||
private String joinAll() {
|
||||
java.util.Set<String> all = new java.util.HashSet<>();
|
||||
all.addAll(cachedManualPatterns);
|
||||
all.addAll(cachedWildcardBanPatterns);
|
||||
return String.join(", ", all);
|
||||
}
|
||||
|
||||
private Set<String> filterPatterns(java.util.Set<String> raw) {
|
||||
java.util.Set<String> clean = new java.util.HashSet<>();
|
||||
for (String p : raw) {
|
||||
if (p == null) continue;
|
||||
p = p.trim().toLowerCase(Locale.ROOT);
|
||||
if (p.isEmpty()) continue;
|
||||
// 说明性文本通常包含中文或空格;纯用户名/通配模式(如 steve_*)不含空格
|
||||
if (p.contains(" ") || p.contains("年") || p.contains("月") || p.contains("封禁")) {
|
||||
continue;
|
||||
}
|
||||
clean.add(p);
|
||||
}
|
||||
return clean;
|
||||
}
|
||||
|
||||
/** 读 用户协议/deny.json 的字符串数组(支持说明文本,自动过滤)。 */
|
||||
private Set<String> loadStringPatterns(File f) {
|
||||
Set<String> out = new HashSet<>();
|
||||
try {
|
||||
byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
|
||||
String txt = new String(bytes, java.nio.charset.StandardCharsets.UTF_8);
|
||||
JsonElement root = JsonParser.parseString(txt);
|
||||
if (root.isJsonArray()) {
|
||||
for (JsonElement el : root.getAsJsonArray()) {
|
||||
if (el != null && el.isJsonPrimitive()) {
|
||||
out.add(el.getAsString());
|
||||
}
|
||||
}
|
||||
} else if (root.isJsonObject()) {
|
||||
// 兼容对象数组 [{name:...}] / [{user:...}]
|
||||
JsonElement arr = root.getAsJsonObject().get("items");
|
||||
if (arr != null && arr.isJsonArray()) {
|
||||
for (JsonElement el : arr.getAsJsonArray()) {
|
||||
if (el == null || !el.isJsonObject()) continue;
|
||||
JsonElement obj = el;
|
||||
String u = obj.getAsJsonObject().has("user")
|
||||
? obj.getAsJsonObject().get("user").getAsString()
|
||||
: obj.getAsJsonObject().get("name").getAsString();
|
||||
out.add(u);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
plugin.getLogger().warning("[拒签集合] 读取 " + f.getName() + " 失败: " + ex.getMessage());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** 读 ban/通配封禁.json,筛选 永久(endtime=0)或 跨度≥10年 的 user 模式。 */
|
||||
private Set<String> loadLongBanPatterns(File f) {
|
||||
Set<String> out = new HashSet<>();
|
||||
if (!f.exists()) return out;
|
||||
try {
|
||||
byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
|
||||
String txt = new String(bytes, java.nio.charset.StandardCharsets.UTF_8);
|
||||
JsonElement root = JsonParser.parseString(txt);
|
||||
if (!root.isJsonArray()) return out;
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
long tenYearsMs = 10L * 365L * 24 * 3600 * 1000;
|
||||
|
||||
for (JsonElement el : root.getAsJsonArray()) {
|
||||
if (el == null || !el.isJsonObject()) continue;
|
||||
JsonObject obj = el.getAsJsonObject();
|
||||
String user = str(obj, "user");
|
||||
if (user.isEmpty()) continue;
|
||||
|
||||
int endtime = 0;
|
||||
if (obj.has("endtime") && !obj.get("endtime").isJsonNull()) {
|
||||
try { endtime = obj.get("endtime").getAsInt(); } catch (Exception ignored) {}
|
||||
}
|
||||
String starttime = str(obj, "starttime");
|
||||
|
||||
boolean permanent = (endtime == 0);
|
||||
boolean longBan = false;
|
||||
if (!permanent) {
|
||||
long startMs = parseTime(starttime, now);
|
||||
long endMs = endtime; // 文件里 endtime 为秒或毫秒?见下方注释
|
||||
// 兼容:endtime 若 10 位按秒处理,13 位按毫秒处理
|
||||
if (endtime > 0 && endtime < 100_000_000_000L) {
|
||||
endMs = endtime * 1000L;
|
||||
}
|
||||
if (startMs > 0 && endMs > 0) {
|
||||
long span = endMs - startMs;
|
||||
if (span >= tenYearsMs) longBan = true;
|
||||
} else {
|
||||
// 时间戳解析失败但 endtime>0 → 保守按“长封禁”处理(≥10 年或未知)
|
||||
longBan = endMs >= now + tenYearsMs;
|
||||
}
|
||||
}
|
||||
|
||||
if (permanent || longBan) {
|
||||
out.add(user);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
plugin.getLogger().warning("[拒签集合] 读取 " + WILDCARD_BAN_FILE + " 失败: " + ex.getMessage());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** 通配符匹配:支持 *(任意串),其余字符精确;不含 * 则 equals。 */
|
||||
private static boolean matches(String name, String pattern) {
|
||||
if (name == null || pattern == null) return false;
|
||||
name = name.toLowerCase(Locale.ROOT);
|
||||
pattern = pattern.toLowerCase(Locale.ROOT);
|
||||
if (!pattern.contains("*")) {
|
||||
return name.equals(pattern);
|
||||
}
|
||||
String[] parts = pattern.split("\\*", -1);
|
||||
StringBuilder regex = new StringBuilder();
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
if (i > 0) regex.append(".*");
|
||||
regex.append(java.util.regex.Pattern.quote(parts[i]));
|
||||
}
|
||||
return java.util.regex.Pattern.compile(regex.toString()).matcher(name).matches();
|
||||
}
|
||||
|
||||
/** 文件指纹:长度 + 内容 hash,用于懒重载判变。 */
|
||||
private String fingerprint(File f) {
|
||||
if (f == null || !f.exists()) return "missing";
|
||||
try {
|
||||
byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
|
||||
return f.length() + "B/" + java.util.Arrays.hashCode(bytes);
|
||||
} catch (Exception ex) {
|
||||
return "err:" + ex.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
private static String str(JsonElement obj, String key) {
|
||||
JsonElement el = obj.getAsJsonObject().get(key);
|
||||
return (el == null || el.isJsonNull()) ? "" : el.getAsString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析中文时间 "2026年11月21日 12点15分" → 毫秒;失败返回 fallback。
|
||||
* 兼容 "yyyy年MM月dd日 HH点mm分"、ISO 格式、纯数字时间戳。
|
||||
*/
|
||||
private long parseTime(String s, long fallback) {
|
||||
if (s == null || s.trim().isEmpty()) return 0;
|
||||
s = s.trim();
|
||||
// 纯数字时间戳
|
||||
if (s.matches("\\d+")) {
|
||||
long v = Long.parseLong(s);
|
||||
return v < 100_000_000_000L ? v * 1000L : v;
|
||||
}
|
||||
// 中文格式
|
||||
try {
|
||||
String norm = s
|
||||
.replace("年", "-")
|
||||
.replace("月", "-")
|
||||
.replace("日", " ")
|
||||
.replace("点", ":")
|
||||
.replace("分", "");
|
||||
if (norm.endsWith(":")) norm = norm + "00";
|
||||
String[] bits = norm.trim().split("\\s+");
|
||||
String datePart = bits.length > 0 ? bits[0] : "";
|
||||
String timePart = bits.length > 1 ? bits[1] : "00:00:00";
|
||||
if (!timePart.matches("\\d{1,2}:\\d{2}(:\\d{2})?")) {
|
||||
// 若拆成 3 段(时:分:秒 被空格分开),重新拼
|
||||
if (bits.length >= 4) {
|
||||
timePart = bits[1] + ":" + bits[2] + (bits.length >= 5 ? ":" + bits[3] : ":00");
|
||||
}
|
||||
}
|
||||
if (timePart.equals("00:00:00") && bits.length == 1) {
|
||||
// 只有日期,补 00:00:00
|
||||
timePart = "00:00:00";
|
||||
}
|
||||
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
LocalDateTime ldt = LocalDateTime.parse(datePart + " " + timePart, fmt);
|
||||
return ldt.toInstant(java.time.ZoneOffset.of("+8")).toEpochMilli();
|
||||
} catch (Exception ex) {
|
||||
plugin.getLogger().fine("[拒签集合] 时间解析失败: " + s + " → " + ex.getMessage());
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
public void addDeny(String playerName, String reason) {
|
||||
if (playerName == null || playerName.isEmpty()) return;
|
||||
String entry = playerName.trim().toLowerCase(Locale.ROOT);
|
||||
if (entry.isEmpty()) return;
|
||||
|
||||
// 1) 如果该玩家已被永久/10年封禁(在 cachedWildcardBanPatterns 里),则不重复写 deny.json
|
||||
// 2) 否则写入 deny.json
|
||||
reloadWildcardBans();
|
||||
if (cachedWildcardBanPatterns.contains(entry)) {
|
||||
plugin.getLogger().info("[拒签集合] " + entry + " 已存在于 ban/通配封禁.json(永久/10年封禁),跳过写入 用户协议/deny.json");
|
||||
return;
|
||||
}
|
||||
|
||||
// 读现有 deny.json,追加到数组里(保持简单字符串数组格式)
|
||||
List<String> existing = loadStringPatternsRaw(denyJson);
|
||||
if (existing.contains(entry)) {
|
||||
plugin.getLogger().info("[拒签集合] " + entry + " 已存在于 用户协议/deny.json,跳过写入");
|
||||
return;
|
||||
}
|
||||
existing.add(entry);
|
||||
writeDenyJson(existing, reason);
|
||||
plugin.getLogger().info("[拒签集合] 玩家 " + entry + " 已写入 用户协议/deny.json(" + reason + ")");
|
||||
}
|
||||
|
||||
/** 读 deny.json 的原始字符串数组(不去重过滤,保留说明文本)。 */
|
||||
private List<String> loadStringPatternsRaw(File f) {
|
||||
List<String> out = new java.util.ArrayList<>();
|
||||
if (!f.exists()) return out;
|
||||
try {
|
||||
byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
|
||||
String txt = new String(bytes, java.nio.charset.StandardCharsets.UTF_8).trim();
|
||||
if (txt.isEmpty()) return out;
|
||||
JsonElement root = JsonParser.parseString(txt);
|
||||
if (root.isJsonArray()) {
|
||||
for (JsonElement el : root.getAsJsonArray()) {
|
||||
if (el != null && el.isJsonPrimitive()) {
|
||||
out.add(el.getAsString());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
out.add(txt); // 非数组时原样保留(避免数据丢失)
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
plugin.getLogger().warning("[拒签集合] 读取 用户协议/deny.json 原始内容失败: " + ex.getMessage());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private void writeDenyJson(List<String> values, String reason) {
|
||||
try {
|
||||
File parent = denyJson.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[\n");
|
||||
com.google.gson.Gson gson = new com.google.gson.Gson();
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
sb.append(" ").append(gson.toJson(values.get(i)));
|
||||
if (i < values.size() - 1) sb.append(",");
|
||||
sb.append("\n");
|
||||
}
|
||||
sb.append("]");
|
||||
Files.write(denyJson.toPath(), sb.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
} catch (IOException ex) {
|
||||
plugin.getLogger().severe("[拒签集合] 写入 用户协议/deny.json 失败: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureDenyJsonExists() {
|
||||
File parent = denyJson.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
if (!denyJson.exists()) {
|
||||
try {
|
||||
Files.write(denyJson.toPath(), new byte[0]);
|
||||
plugin.getLogger().info("[拒签集合] 已创建空的 用户协议/deny.json");
|
||||
} catch (IOException ex) {
|
||||
plugin.getLogger().warning("[拒签集合] 创建 用户协议/deny.json 失败: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureWildcardBanJsonExists() {
|
||||
File parent = wildcardBanJson.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
if (!wildcardBanJson.exists()) {
|
||||
try {
|
||||
Files.write(wildcardBanJson.toPath(),
|
||||
"[\n {\n \"time\": \"\",\n \"user\": \"\",\n \"msg\": \"\",\n \"starttime\": \"\",\n \"endtime\": 0\n }\n]".getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
plugin.getLogger().info("[拒签集合] 已创建空的 ban/通配封禁.json 模板");
|
||||
} catch (IOException ex) {
|
||||
plugin.getLogger().warning("[拒签集合] 创建 ban/通配封禁.json 失败: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -237,6 +237,8 @@ public class Main extends JavaPlugin
|
||||
private ShopManager shopManager; //商店
|
||||
private CustomScreenManager customScreenManager; //用户协议 & 公告送达(第125批)
|
||||
private RulesCLIManager rulesCLIManager; //用户协议查看 /rules(125续15g CLI+GUI 双通道)
|
||||
private DenyListManager denyListManager; //用户协议拒签集合(125续15l-2)
|
||||
private WildcardBanManager wildcardBanManager; //通配封禁监听(125续15l-3)
|
||||
// ★ 交易记录独立库(2026-10-11 任务5:正常商店订单/二手订单/玩家出售偏好)
|
||||
private TradeLogManager tradeLog;
|
||||
|
||||
@@ -521,6 +523,10 @@ public class Main extends JavaPlugin
|
||||
customScreenManager = new CustomScreenManager(this);
|
||||
// 125 续15g:/rules 双通道(CLI + GUI,书与笔展示已签协议原文)
|
||||
rulesCLIManager = new RulesCLIManager(this);
|
||||
// 125 续15l-2:用户协议拒签集合(deny.json + ban/通配封禁.json 懒重载)
|
||||
denyListManager = new DenyListManager(this);
|
||||
// 125 续15l-3:通配封禁监听(ban steve_* 等记录到 ban/通配封禁.json)
|
||||
wildcardBanManager = new WildcardBanManager(this);
|
||||
// shopManager.loadCategories(); // 构造器内已调用,此处重复
|
||||
|
||||
getServer().getPluginManager()
|
||||
@@ -3173,6 +3179,15 @@ public class Main extends JavaPlugin
|
||||
return;
|
||||
}
|
||||
|
||||
// ===== 125 续15l-3 通配封禁检查:ban steve_* 记录后,玩家进入先匹配则退回 =====
|
||||
if (wildcardBanManager != null && wildcardBanManager.isWildcardBanned(p)) {
|
||||
e.setJoinMessage(null);
|
||||
p.kickPlayer("§c§l[通配封禁] §f您命中通配封禁记录(如 steve_*),无法进入服务器。\n"
|
||||
+ "§7如封禁已到期,请联系管理员刷新 ban/通配封禁.json 或稍后重试。");
|
||||
getLogger().info("[通配封禁] 玩家 " + name + " 命中通配封禁,已被退回");
|
||||
return;
|
||||
}
|
||||
|
||||
// ===== 非法URL/非法域名用户名拦截(最高优先级) =====
|
||||
if (chatFilter != null && chatFilter.isEnabled()) {
|
||||
// 每次玩家加入时热重载非法域名后缀(支持热更新)
|
||||
@@ -4860,6 +4875,12 @@ public class Main extends JavaPlugin
|
||||
private boolean guardVanillaBan(CommandSender sender,
|
||||
String rawCommand) {
|
||||
if (rawCommand == null || rawCommand.isEmpty()) return true;
|
||||
|
||||
// ★ 125 续15l-3 通配封禁优先拦截:ban steve_* 等含 * 的目标直接记录,不交还原版
|
||||
if (wildcardBanManager != null && wildcardBanManager.guardWildcardBan(sender, rawCommand)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String lower = rawCommand.toLowerCase();
|
||||
// 兼容 minecraft:ban / minecraft:ban-ip 形式
|
||||
if (lower.startsWith("minecraft:")) {
|
||||
@@ -6337,6 +6358,9 @@ public class Main extends JavaPlugin
|
||||
if (parts.length < 2) return;
|
||||
String target = parts[1];
|
||||
|
||||
// ★ 125 续15l-3:含通配符的目标由 guardVanillaBan 记录,这里不再延迟读 BanList 广播
|
||||
if (target.contains("*")) return;
|
||||
|
||||
// ★ 延迟2tick执行(等Vanilla ban完成后读取BanList)
|
||||
Bukkit.getScheduler().runTaskLater(this, () -> {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
package Sdf1_login;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 125 续15l-3 通配封禁监听器:
|
||||
*
|
||||
* 监听原版 /ban、/ban-ip 命令(控制台 + 玩家),对形如 “steve_*” 的带通配符目标:
|
||||
* 1. 不向 MC 传通配符本身(原版 /ban 会把 “steve_*” 当成非法玩家名直接报 Unknown player);
|
||||
* 2. 把该通配封禁完整记录到 ban/通配封禁.json(字段:time、user、msg、starttime、endtime);
|
||||
* 3. 玩家进入时先检查是否匹配该文件,匹配则直接退回(不弹屏、不进游戏);
|
||||
* 4. 封禁到期(endtime>0 且已过期)后自动从 json 移除条目;endtime=0 永久封禁不移除。
|
||||
*
|
||||
* 普通 “ban user msg”(user 不含 *)直接交还原版执行,不写 json、不额外处理。
|
||||
* 用户明令“不注册顶替”:不创建/校验玩家注册状态,纯封禁记录。
|
||||
*/
|
||||
public class WildcardBanManager {
|
||||
|
||||
private static final String WILDCARD_BAN_FILE = "ban/通配封禁.json";
|
||||
private static final long TEN_YEARS_MS = 10L * 365L * 24 * 3600 * 1000;
|
||||
|
||||
private final JavaPlugin plugin;
|
||||
private final File jsonFile;
|
||||
private final Gson gson = new Gson();
|
||||
private final Set<String> cachedPatterns = new HashSet<>();
|
||||
private final Set<String> cachedExactNames = new HashSet<>();
|
||||
private String cachedFingerprint = "";
|
||||
private boolean loadedOnce = false;
|
||||
|
||||
public WildcardBanManager(JavaPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
this.jsonFile = new File(plugin.getDataFolder(), WILDCARD_BAN_FILE);
|
||||
ensureJsonExists();
|
||||
}
|
||||
|
||||
/** 供 Main.guardVanillaBan 调用:返回 true 表示已处理(调用方取消事件)。 */
|
||||
public boolean guardWildcardBan(org.bukkit.command.CommandSender sender, String rawCommand) {
|
||||
if (rawCommand == null) return false;
|
||||
String lower = rawCommand.toLowerCase(Locale.ROOT);
|
||||
if (lower.startsWith("minecraft:")) {
|
||||
rawCommand = rawCommand.substring(10);
|
||||
lower = lower.substring(10);
|
||||
}
|
||||
String[] parts = rawCommand.trim().split("\\s+");
|
||||
if (parts.length < 2) return false;
|
||||
|
||||
boolean isNameBan = "ban".equals(lower) || lower.startsWith("ban ");
|
||||
boolean isIpBan = "ban-ip".equals(lower) || lower.startsWith("ban-ip ")
|
||||
|| "banip".equals(lower) || lower.startsWith("banip ");
|
||||
if (!isNameBan && !isIpBan) return false;
|
||||
|
||||
String target = parts[1];
|
||||
// 普通 ban user msg(不含 *)→ 交还原版,本管理器不写 json、不拦截
|
||||
if (!target.contains("*")) return false;
|
||||
|
||||
// ---- 通配封禁:完整记录 + 不让 MC 执行 ----
|
||||
String msg = parts.length > 2 ? String.join(" ", java.util.Arrays.copyOfRange(parts, 2, parts.length)) : "";
|
||||
recordWildcardBan(target, msg, sender.getName());
|
||||
sender.sendMessage("§6§l[通配封禁] §f已记录通配封禁 §e" + target
|
||||
+ "§f(" + (msg.isEmpty() ? "无理由" : msg) + ")");
|
||||
return true;
|
||||
}
|
||||
|
||||
/** 玩家进入时检查是否匹配通配封禁。 */
|
||||
public boolean isWildcardBanned(Player p) {
|
||||
if (p == null) return false;
|
||||
pruneExpired();
|
||||
String name = p.getName().toLowerCase(Locale.ROOT);
|
||||
for (String pattern : cachedPatterns) {
|
||||
if (matches(name, pattern)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 清理 已到期 的通配封禁条目(endtime>0 且过期)。 */
|
||||
public void pruneExpired() {
|
||||
reload();
|
||||
List<JsonElement> entries = loadRaw();
|
||||
if (entries.isEmpty()) return;
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
List<JsonElement> kept = new ArrayList<>();
|
||||
int removed = 0;
|
||||
for (JsonElement el : entries) {
|
||||
if (el == null || !el.isJsonObject()) { kept.add(el); continue; }
|
||||
JsonObject obj = el.getAsJsonObject();
|
||||
int endtime = 0;
|
||||
if (obj.has("endtime") && !obj.get("endtime").isJsonNull()) {
|
||||
try { endtime = obj.get("endtime").getAsInt(); } catch (Exception ignored) {}
|
||||
}
|
||||
boolean expired = endtime > 0 && endtime < 100_000_000_000L && (endtime * 1000L) <= now;
|
||||
if (expired) {
|
||||
removed++;
|
||||
} else {
|
||||
kept.add(el);
|
||||
}
|
||||
}
|
||||
if (removed > 0) {
|
||||
writeRaw(kept);
|
||||
plugin.getLogger().info("[通配封禁] 已清理 " + removed + " 条到期记录");
|
||||
cachedFingerprint = ""; // 强制下次 reload
|
||||
}
|
||||
}
|
||||
|
||||
private void recordWildcardBan(String target, String msg, String source) {
|
||||
pruneExpired(); // 先清一遍到期条目,再追加
|
||||
List<JsonElement> entries = loadRaw();
|
||||
entries.add(buildEntry(target, msg, source));
|
||||
writeRaw(entries);
|
||||
cachedFingerprint = "";
|
||||
plugin.getLogger().info("[通配封禁] 新增记录 user=" + target + " msg=" + msg + " source=" + source
|
||||
+ "(共 " + entries.size() + " 条)");
|
||||
}
|
||||
|
||||
private JsonObject buildEntry(String target, String msg, String source) {
|
||||
JsonObject obj = new JsonObject();
|
||||
String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH点mm分"));
|
||||
obj.addProperty("time", nowStr);
|
||||
obj.addProperty("user", target);
|
||||
obj.addProperty("msg", msg == null ? "" : msg);
|
||||
obj.addProperty("source", source == null ? "console" : source);
|
||||
obj.addProperty("starttime", nowStr);
|
||||
obj.addProperty("endtime", 0); // 永久封禁
|
||||
return obj;
|
||||
}
|
||||
|
||||
/** 懒重载:指纹未变直接复用内存。 */
|
||||
private void reload() {
|
||||
String fp = fingerprint();
|
||||
if (loadedOnce && fp.equals(cachedFingerprint)) return;
|
||||
|
||||
cachedPatterns.clear();
|
||||
cachedExactNames.clear();
|
||||
|
||||
List<JsonElement> entries = loadRaw();
|
||||
for (JsonElement el : entries) {
|
||||
if (el == null || !el.isJsonObject()) continue;
|
||||
JsonObject obj = el.getAsJsonObject();
|
||||
String user = str(obj, "user");
|
||||
if (user.isEmpty()) continue;
|
||||
if (user.contains("*")) {
|
||||
cachedPatterns.add(user.toLowerCase(Locale.ROOT));
|
||||
} else {
|
||||
cachedExactNames.add(user.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
}
|
||||
cachedFingerprint = fp;
|
||||
loadedOnce = true;
|
||||
plugin.getLogger().fine("[通配封禁] 懒重载完成:通配模式 " + cachedPatterns.size()
|
||||
+ " 条,精确名单 " + cachedExactNames.size() + " 条");
|
||||
}
|
||||
|
||||
private List<JsonElement> loadRaw() {
|
||||
if (!jsonFile.exists()) return new ArrayList<>();
|
||||
try {
|
||||
byte[] bytes = Files.readAllBytes(Paths.get(jsonFile.getAbsolutePath()));
|
||||
String txt = new String(bytes, StandardCharsets.UTF_8).trim();
|
||||
if (txt.isEmpty() || txt.equals("[]")) return new ArrayList<>();
|
||||
JsonElement root = JsonParser.parseString(txt);
|
||||
if (root.isJsonArray()) {
|
||||
List<JsonElement> out = new ArrayList<>();
|
||||
for (JsonElement el : root.getAsJsonArray()) out.add(el);
|
||||
return out;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
plugin.getLogger().warning("[通配封禁] 读取 " + WILDCARD_BAN_FILE + " 失败: " + ex.getMessage());
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
private void writeRaw(List<JsonElement> entries) {
|
||||
try {
|
||||
File parent = jsonFile.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[\n");
|
||||
for (int i = 0; i < entries.size(); i++) {
|
||||
if (entries.get(i) == null) continue;
|
||||
sb.append(" ").append(gson.toJson(entries.get(i)));
|
||||
if (i < entries.size() - 1) sb.append(",");
|
||||
sb.append("\n");
|
||||
}
|
||||
sb.append("]");
|
||||
Files.write(jsonFile.toPath(), sb.toString().getBytes(StandardCharsets.UTF_8));
|
||||
} catch (IOException ex) {
|
||||
plugin.getLogger().severe("[通配封禁] 写入 " + WILDCARD_BAN_FILE + " 失败: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private String fingerprint() {
|
||||
if (!jsonFile.exists()) return "missing";
|
||||
try {
|
||||
byte[] bytes = Files.readAllBytes(Paths.get(jsonFile.getAbsolutePath()));
|
||||
return jsonFile.length() + "B/" + java.util.Arrays.hashCode(bytes);
|
||||
} catch (Exception ex) {
|
||||
return "err:" + ex.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureJsonExists() {
|
||||
File parent = jsonFile.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
if (!jsonFile.exists()) {
|
||||
try {
|
||||
Files.write(jsonFile.toPath(), "[]".getBytes(StandardCharsets.UTF_8));
|
||||
plugin.getLogger().info("[通配封禁] 已创建空模板 ban/通配封禁.json");
|
||||
} catch (IOException ex) {
|
||||
plugin.getLogger().warning("[通配封禁] 创建模板失败: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String str(JsonObject obj, String key) {
|
||||
JsonElement el = obj.get(key);
|
||||
return (el == null || el.isJsonNull()) ? "" : el.getAsString();
|
||||
}
|
||||
|
||||
private static boolean matches(String name, String pattern) {
|
||||
if (name == null || pattern == null) return false;
|
||||
name = name.toLowerCase(Locale.ROOT);
|
||||
pattern = pattern.toLowerCase(Locale.ROOT);
|
||||
if (!pattern.contains("*")) return name.equals(pattern);
|
||||
String[] parts = pattern.split("\\*", -1);
|
||||
StringBuilder regex = new StringBuilder();
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
if (i > 0) regex.append(".*");
|
||||
regex.append(Pattern.quote(parts[i]));
|
||||
}
|
||||
return Pattern.compile(regex.toString()).matcher(name).matches();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
[
|
||||
{
|
||||
"time": "2026年11月21日 12点15分",
|
||||
"user": "steve_*",
|
||||
"msg": "test ban",
|
||||
"starttime": "2026年11月21日 12点15分",
|
||||
"endtime": 0
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,9 @@
|
||||
[
|
||||
"以下是被拉黑、永久封禁、10年封禁的玩家",
|
||||
"这些玩家将自动加入本文件",
|
||||
"支持手动添加",
|
||||
"支持通配符",
|
||||
"比如setve_*",
|
||||
"表示所有steve开头的玩家都不能签署,防玩家开小号",
|
||||
"插件默认不用通配符"
|
||||
]
|
||||
Reference in New Issue
Block a user