feat: 用户组系统重构 - 领地创建价格/上限/默认权限差异化
- 重构UserGroupManager: 废弃资源包功能,新增领地相关字段 - land_price_per_sqm: 每㎡创建价格(-1=全局) - max_lands: 每人最大领地数(-1=全局) - default_perms: 默认deny_*权限JSON - 领地创建时自动读取用户组专属价格和上限 - CLI命令: grouplist/groupset/groupadd/groupdel/groupdelconfig - syncLandData新增用户组同步到PHP - PHP后端: list_user_groups/get_user_group/update_user_group等API - DB: usergroup.db表结构自动迁移
This commit is contained in:
@@ -5005,7 +5005,8 @@ public class AreaProtection implements Listener {
|
||||
"取消删除", "canceldelete",
|
||||
"removemember", "addmember",
|
||||
"config", "配置",
|
||||
"public", "公共", "listpublic", "公共列表", "tpb", "传送公共"
|
||||
"public", "公共", "listpublic", "公共列表", "tpb", "传送公共",
|
||||
"group", "用户组", "groupadd", "groupdel", "grouplist", "groupset", "groupdelconfig"
|
||||
));
|
||||
|
||||
String first = original[0].toLowerCase();
|
||||
@@ -5489,22 +5490,27 @@ public class AreaProtection implements Listener {
|
||||
p.sendMessage("§c区域已存在");
|
||||
return true;
|
||||
}
|
||||
// ★ 检查每人领地数量上限
|
||||
// ★ 检查每人领地数量上限(用户组专属上限)
|
||||
long playerLandCount = areas.values().stream()
|
||||
.filter(a -> p.getName().equalsIgnoreCase(a.owner))
|
||||
.count();
|
||||
if (playerLandCount >= globalMaxLandsPerPlayer) {
|
||||
p.sendMessage("§c§l[防护] §f已达领地上限!每人最多 §e" + globalMaxLandsPerPlayer + " §f个领地");
|
||||
UserGroupManager ugm = plugin.getUserGroup();
|
||||
int playerMaxLands = (ugm != null) ? ugm.getPlayerMaxLands(p.getName(), globalMaxLandsPerPlayer) : globalMaxLandsPerPlayer;
|
||||
if (playerLandCount >= playerMaxLands) {
|
||||
String groupHint = (ugm != null && ugm.getHighestGroup(p.getName()) != null)
|
||||
? "(用户组: " + ugm.getHighestGroup(p.getName()).displayName + ")" : "";
|
||||
p.sendMessage("§c§l[防护] §f已达领地上限!每人最多 §e" + playerMaxLands + " §f个领地" + groupHint);
|
||||
return true;
|
||||
}
|
||||
Location l1 = pos1.get(u);
|
||||
Location l2 = pos2.get(u);
|
||||
|
||||
// ★ 领地计费:计算面积并扣除余额
|
||||
// ★ 领地计费:计算面积并扣除余额(用户组专属价格)
|
||||
int width = Math.abs(l2.getBlockX() - l1.getBlockX()) + 1;
|
||||
int length = Math.abs(l2.getBlockZ() - l1.getBlockZ()) + 1;
|
||||
int area = width * length;
|
||||
int cost = area * globalCreatePricePerSqm;
|
||||
int effectivePricePerSqm = (ugm != null) ? ugm.getPlayerLandPricePerSqm(p.getName(), globalCreatePricePerSqm) : globalCreatePricePerSqm;
|
||||
int cost = area * effectivePricePerSqm;
|
||||
if (cost > 0) {
|
||||
BondManager bm = plugin.getBonds();
|
||||
if (bm != null) {
|
||||
@@ -5513,11 +5519,12 @@ public class AreaProtection implements Listener {
|
||||
p.sendMessage("§c§l[防护] §f余额不足!需要 §e" + cost + " §f债券,当前余额 §e" + balance);
|
||||
return true;
|
||||
}
|
||||
if (!bm.deductBonds(p.getName(), cost, "land_create", p.getName(), p.getName(), "创建领地: " + areaName + " (" + area + "㎡×" + globalCreatePricePerSqm + ")")) {
|
||||
if (!bm.deductBonds(p.getName(), cost, "land_create", p.getName(), p.getName(), "创建领地: " + areaName + " (" + area + "㎡×" + effectivePricePerSqm + ")")) {
|
||||
p.sendMessage("§c§l[防护] §f扣费失败,请稍后重试");
|
||||
return true;
|
||||
}
|
||||
p.sendMessage("§a§l[防护] §f创建领地扣除 §e" + cost + " §f债券(" + area + "㎡×" + globalCreatePricePerSqm + "/㎡)");
|
||||
String priceSource = (effectivePricePerSqm != globalCreatePricePerSqm) ? "(用户组优惠价)" : "";
|
||||
p.sendMessage("§a§l[防护] §f创建领地扣除 §e" + cost + " §f债券(" + area + "㎡×" + effectivePricePerSqm + "/㎡)" + priceSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5884,6 +5891,130 @@ public class AreaProtection implements Listener {
|
||||
return true;
|
||||
}
|
||||
|
||||
// ===== 用户组管理 =====
|
||||
if (sub.equals("grouplist") || sub.equals("用户组")) {
|
||||
UserGroupManager ugm = plugin.getUserGroup();
|
||||
if (ugm == null) { sender.sendMessage("§c用户组系统未初始化"); return true; }
|
||||
Map<String, UserGroupManager.UserGroupConfig> groups = ugm.getGroupConfigs();
|
||||
if (groups.isEmpty()) {
|
||||
sender.sendMessage("§e暂无用户组定义");
|
||||
} else {
|
||||
sender.sendMessage("§a§l=== 用户组列表 ===");
|
||||
for (UserGroupManager.UserGroupConfig cfg : groups.values()) {
|
||||
String priceStr = cfg.landPricePerSqm >= 0 ? cfg.landPricePerSqm + "/㎡" : "全局默认";
|
||||
String maxStr = cfg.maxLands >= 0 ? String.valueOf(cfg.maxLands) : "全局默认";
|
||||
sender.sendMessage("§f" + cfg.displayColor + cfg.displayName
|
||||
+ " §7(name=" + cfg.name + ", 优先级=" + cfg.priority
|
||||
+ ", 价格=" + priceStr + ", 上限=" + maxStr + ")");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sub.equals("groupset")) {
|
||||
if (!sender.hasPermission("sdf1.admin")) {
|
||||
sender.sendMessage("§c需要管理员权限"); return true;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
sender.sendMessage("§e用法: /protect groupset <组名> [显示名] [颜色] [优先级] [每㎡价格] [最大领地数]");
|
||||
return true;
|
||||
}
|
||||
String groupName = args[1].toLowerCase();
|
||||
UserGroupManager ugm = plugin.getUserGroup();
|
||||
if (ugm == null) { sender.sendMessage("§c用户组系统未初始化"); return true; }
|
||||
UserGroupManager.UserGroupConfig cfg = ugm.getGroupConfig(groupName);
|
||||
if (cfg == null) cfg = new UserGroupManager.UserGroupConfig();
|
||||
cfg.name = groupName;
|
||||
if (args.length >= 3) cfg.displayName = args[2];
|
||||
if (args.length >= 4) cfg.displayColor = args[3];
|
||||
if (args.length >= 5) {
|
||||
try { cfg.priority = Integer.parseInt(args[4]); } catch (Exception ignored) {}
|
||||
}
|
||||
if (args.length >= 6) {
|
||||
try { cfg.landPricePerSqm = Integer.parseInt(args[5]); } catch (Exception ignored) {}
|
||||
}
|
||||
if (args.length >= 7) {
|
||||
try { cfg.maxLands = Integer.parseInt(args[6]); } catch (Exception ignored) {}
|
||||
}
|
||||
if (cfg.displayName.isEmpty()) cfg.displayName = groupName;
|
||||
ugm.saveGroupConfigToDB(cfg);
|
||||
ugm.loadGroupConfigs();
|
||||
sender.sendMessage("§a已创建/更新用户组: §f" + cfg.displayColor + cfg.displayName
|
||||
+ " §7(name=" + groupName + ", 价格=" + cfg.landPricePerSqm + "/㎡"
|
||||
+ ", 上限=" + cfg.maxLands + ")");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sub.equals("groupdelconfig")) {
|
||||
if (!sender.hasPermission("sdf1.admin")) {
|
||||
sender.sendMessage("§c需要管理员权限"); return true;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
sender.sendMessage("§e用法: /protect groupdelconfig <组名>");
|
||||
return true;
|
||||
}
|
||||
String groupName = args[1].toLowerCase();
|
||||
UserGroupManager ugm = plugin.getUserGroup();
|
||||
if (ugm == null) { sender.sendMessage("§c用户组系统未初始化"); return true; }
|
||||
if (groupName.equals(UserGroupManager.DEFAULT_GROUP)) {
|
||||
sender.sendMessage("§c不能删除默认组"); return true;
|
||||
}
|
||||
if (ugm.deleteGroupConfig(groupName)) {
|
||||
sender.sendMessage("§a已删除用户组: " + groupName);
|
||||
} else {
|
||||
sender.sendMessage("§c未找到用户组: " + groupName);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sub.equals("groupadd")) {
|
||||
if (args.length < 3) {
|
||||
sender.sendMessage("§e用法: /protect groupadd <玩家> <组名>");
|
||||
return true;
|
||||
}
|
||||
String playerName = args[1];
|
||||
String groupName = args[2].toLowerCase();
|
||||
UserGroupManager ugm = plugin.getUserGroup();
|
||||
if (ugm == null) { sender.sendMessage("§c用户组系统未初始化"); return true; }
|
||||
if (ugm.getGroupConfig(groupName) == null) {
|
||||
sender.sendMessage("§c未找到用户组: " + groupName);
|
||||
return true;
|
||||
}
|
||||
if (ugm.addPlayer(playerName, groupName, sender.getName(), 0)) {
|
||||
UserGroupManager.UserGroupConfig cfg = ugm.getGroupConfig(groupName);
|
||||
sender.sendMessage("§a已将 §f" + playerName + " §a加入用户组: §f" + (cfg != null ? cfg.displayName : groupName));
|
||||
// 通知在线玩家
|
||||
Player target = plugin.getServer().getPlayerExact(playerName);
|
||||
if (target != null) {
|
||||
target.sendMessage("§a§l[用户组] §f你已被加入用户组: §e" + (cfg != null ? cfg.displayName : groupName));
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("§c操作失败");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sub.equals("groupdel")) {
|
||||
if (args.length < 3) {
|
||||
sender.sendMessage("§e用法: /protect groupdel <玩家> <组名>");
|
||||
return true;
|
||||
}
|
||||
String playerName = args[1];
|
||||
String groupName = args[2].toLowerCase();
|
||||
UserGroupManager ugm = plugin.getUserGroup();
|
||||
if (ugm == null) { sender.sendMessage("§c用户组系统未初始化"); return true; }
|
||||
if (ugm.removePlayer(playerName, groupName)) {
|
||||
sender.sendMessage("§a已将 §f" + playerName + " §a移出用户组: " + groupName);
|
||||
Player target = plugin.getServer().getPlayerExact(playerName);
|
||||
if (target != null) {
|
||||
target.sendMessage("§a§l[用户组] §f你已被移出用户组: §e" + groupName);
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("§c操作失败(玩家可能不在此组)");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ===== 物品黑名单 =====
|
||||
if (sub.equals("additem")) {
|
||||
if (args.length == 2) {
|
||||
|
||||
@@ -7,15 +7,25 @@ import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 用户组管理器(重构版)
|
||||
* 负责:
|
||||
* 1. 用户组定义(名称、颜色、优先级)
|
||||
* 2. 领地相关参数:创建价格倍率、每人最大领地数、默认权限
|
||||
* 3. 玩家↔用户组关联(带过期时间)
|
||||
* 4. 给玩家发送展示名前缀(scoreboard tag)
|
||||
*/
|
||||
public class UserGroupManager {
|
||||
|
||||
private final Main plugin;
|
||||
private Connection db;
|
||||
private final Map<String, UserGroupConfig> groupConfigs = new ConcurrentHashMap<>();
|
||||
|
||||
// 默认组名(所有玩家都属于此组,不需要手动加入)
|
||||
public static final String DEFAULT_GROUP = "default";
|
||||
|
||||
public UserGroupManager(Main plugin) {
|
||||
this.plugin = plugin;
|
||||
initDB();
|
||||
@@ -23,7 +33,7 @@ public class UserGroupManager {
|
||||
startExpiryChecker();
|
||||
}
|
||||
|
||||
// ===== DB =====
|
||||
// ==================== DB ====================
|
||||
|
||||
private void initDB() {
|
||||
try {
|
||||
@@ -32,18 +42,23 @@ public class UserGroupManager {
|
||||
db = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getAbsolutePath());
|
||||
Statement st = db.createStatement();
|
||||
st.execute("PRAGMA journal_mode=WAL");
|
||||
|
||||
// 主表:用户组配置
|
||||
st.execute("CREATE TABLE IF NOT EXISTS user_group_config ("
|
||||
+ "group_name TEXT PRIMARY KEY,"
|
||||
+ "display_name TEXT DEFAULT '',"
|
||||
+ "display_color TEXT DEFAULT '§f',"
|
||||
+ "display_emoji TEXT DEFAULT '',"
|
||||
+ "priority INTEGER DEFAULT 0,"
|
||||
+ "is_permanent INTEGER DEFAULT 1,"
|
||||
+ "duration_minutes INTEGER DEFAULT 0,"
|
||||
+ "pack_url TEXT DEFAULT '',"
|
||||
+ "pack_hash TEXT DEFAULT '',"
|
||||
+ "pack_prompt TEXT DEFAULT 'Custom Resource Pack',"
|
||||
+ "pack_size INTEGER DEFAULT 0)");
|
||||
// === 领地相关 ===
|
||||
+ "land_price_per_sqm INTEGER DEFAULT -1," // -1 = 使用全局值
|
||||
+ "max_lands INTEGER DEFAULT -1," // -1 = 使用全局值
|
||||
+ "default_perms TEXT DEFAULT '{}'," // JSON: 默认deny_*标志
|
||||
+ "is_permanent INTEGER DEFAULT 1," // 保留兼容
|
||||
+ "duration_minutes INTEGER DEFAULT 0" // 保留兼容
|
||||
+ ")");
|
||||
|
||||
// 玩家↔用户组关联
|
||||
st.execute("CREATE TABLE IF NOT EXISTS user_group_member ("
|
||||
+ "player_name TEXT NOT NULL,"
|
||||
+ "group_name TEXT NOT NULL,"
|
||||
@@ -51,16 +66,58 @@ public class UserGroupManager {
|
||||
+ "added_time INTEGER DEFAULT 0,"
|
||||
+ "expiry_time INTEGER DEFAULT 0,"
|
||||
+ "PRIMARY KEY(player_name, group_name))");
|
||||
|
||||
// === 迁移:给旧表加新列 ===
|
||||
try { st.executeUpdate("ALTER TABLE user_group_config ADD COLUMN land_price_per_sqm INTEGER DEFAULT -1"); } catch (Exception ignored) {}
|
||||
try { st.executeUpdate("ALTER TABLE user_group_config ADD COLUMN max_lands INTEGER DEFAULT -1"); } catch (Exception ignored) {}
|
||||
try { st.executeUpdate("ALTER TABLE user_group_config ADD COLUMN default_perms TEXT DEFAULT '{}'"); } catch (Exception ignored) {}
|
||||
try { st.executeUpdate("ALTER TABLE user_group_config ADD COLUMN is_permanent INTEGER DEFAULT 1"); } catch (Exception ignored) {}
|
||||
try { st.executeUpdate("ALTER TABLE user_group_config ADD COLUMN duration_minutes INTEGER DEFAULT 0"); } catch (Exception ignored) {}
|
||||
|
||||
st.close();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("[UserGroup] DB init failed: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 配置加载 =====
|
||||
// ==================== 配置加载 ====================
|
||||
|
||||
/** 从 usergroup.db 加载所有组配置到内存 */
|
||||
public void loadGroupConfigs() {
|
||||
groupConfigs.clear();
|
||||
try {
|
||||
Statement st = db.createStatement();
|
||||
ResultSet rs = st.executeQuery("SELECT * FROM user_group_config");
|
||||
while (rs.next()) {
|
||||
UserGroupConfig cfg = new UserGroupConfig();
|
||||
cfg.name = rs.getString("group_name");
|
||||
cfg.displayName = rs.getString("display_name");
|
||||
cfg.displayColor = rs.getString("display_color");
|
||||
cfg.displayEmoji = rs.getString("display_emoji");
|
||||
cfg.priority = rs.getInt("priority");
|
||||
cfg.landPricePerSqm = rs.getInt("land_price_per_sqm");
|
||||
cfg.maxLands = rs.getInt("max_lands");
|
||||
cfg.defaultPerms = rs.getString("default_perms");
|
||||
cfg.permanent = rs.getInt("is_permanent") == 1;
|
||||
cfg.durationMinutes = rs.getInt("duration_minutes");
|
||||
if (cfg.name != null && !cfg.name.isEmpty()) {
|
||||
groupConfigs.put(cfg.name, cfg);
|
||||
}
|
||||
}
|
||||
rs.close();
|
||||
st.close();
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("[UserGroup] loadGroupConfigs failed: " + e.getMessage());
|
||||
}
|
||||
|
||||
// 同时从 txt 文件加载(兼容旧格式,覆盖DB中的同名组)
|
||||
loadFromTxtFiles();
|
||||
|
||||
plugin.getLogger().info("[UserGroup] 已加载 " + groupConfigs.size() + " 个用户组");
|
||||
}
|
||||
|
||||
/** 从 pluginDir/用户组/*.txt 加载旧格式配置 */
|
||||
private void loadFromTxtFiles() {
|
||||
File dir = new File(plugin.getDataFolder(), "用户组");
|
||||
if (!dir.exists()) dir.mkdirs();
|
||||
File[] files = dir.listFiles((d, n) -> n.endsWith(".txt"));
|
||||
@@ -68,46 +125,40 @@ public class UserGroupManager {
|
||||
|
||||
for (File f : files) {
|
||||
try {
|
||||
List<String> lines = java.nio.file.Files.readAllLines(
|
||||
f.toPath(), StandardCharsets.UTF_8);
|
||||
UserGroupConfig cfg = parseGroupConfig(lines);
|
||||
List<String> lines = java.nio.file.Files.readAllLines(f.toPath(), StandardCharsets.UTF_8);
|
||||
UserGroupConfig cfg = parseTxtGroupConfig(lines);
|
||||
if (cfg != null) {
|
||||
groupConfigs.put(cfg.name, cfg);
|
||||
saveGroupConfigToDB(cfg);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("[UserGroup] Failed to load: " + f.getName());
|
||||
plugin.getLogger().warning("[UserGroup] Failed to load txt: " + f.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private UserGroupConfig parseGroupConfig(List<String> lines) {
|
||||
private UserGroupConfig parseTxtGroupConfig(List<String> lines) {
|
||||
UserGroupConfig cfg = new UserGroupConfig();
|
||||
boolean inBlock = false;
|
||||
|
||||
for (String raw : lines) {
|
||||
String l = raw;
|
||||
|
||||
// 块注释
|
||||
if (l.contains("<!--")) { inBlock = true; continue; }
|
||||
if (l.contains("-->")) { inBlock = false; continue; }
|
||||
if (l.contains("/*")) { inBlock = true; continue; }
|
||||
if (l.contains("*/")) { inBlock = false; continue; }
|
||||
if (inBlock) continue;
|
||||
|
||||
// 行注释
|
||||
l = l.trim();
|
||||
if (l.isEmpty()) continue;
|
||||
if (l.startsWith("#") || l.startsWith("//")) continue;
|
||||
|
||||
// 行内注释
|
||||
int h = l.indexOf('#');
|
||||
int s = l.indexOf("//");
|
||||
if (h >= 0) l = l.substring(0, h).trim();
|
||||
if (s >= 0 && (h < 0 || s < h)) l = l.substring(0, s).trim();
|
||||
if (l.isEmpty()) continue;
|
||||
|
||||
// 解析键值对
|
||||
String key = l;
|
||||
String val = "";
|
||||
int idx = l.indexOf('\uff1a');
|
||||
@@ -120,38 +171,19 @@ public class UserGroupManager {
|
||||
}
|
||||
|
||||
switch (key) {
|
||||
case "组名":
|
||||
case "name": cfg.name = val; break;
|
||||
case "显示名":
|
||||
case "display": cfg.displayName = val; break;
|
||||
case "颜色":
|
||||
case "color": cfg.displayColor = val; break;
|
||||
case "符号":
|
||||
case "emoji": cfg.displayEmoji = val; break;
|
||||
case "优先级":
|
||||
case "priority":
|
||||
case "组名": case "name": cfg.name = val; break;
|
||||
case "显示名": case "display": cfg.displayName = val; break;
|
||||
case "颜色": case "color": cfg.displayColor = val; break;
|
||||
case "符号": case "emoji": cfg.displayEmoji = val; break;
|
||||
case "优先级": case "priority":
|
||||
try { cfg.priority = Integer.parseInt(val); } catch (Exception ignored) {}
|
||||
break;
|
||||
case "类型":
|
||||
case "type":
|
||||
case "类型": case "type":
|
||||
cfg.permanent = val.equalsIgnoreCase("永久") || val.equalsIgnoreCase("permanent");
|
||||
break;
|
||||
case "时长":
|
||||
case "duration":
|
||||
case "时长": case "duration":
|
||||
try { cfg.durationMinutes = Integer.parseInt(val); } catch (Exception ignored) {}
|
||||
break;
|
||||
case "资源包URL":
|
||||
case "pack_url": cfg.packUrl = val; break;
|
||||
case "资源包Hash":
|
||||
case "pack_hash": cfg.packHash = val; break;
|
||||
case "资源包提示":
|
||||
case "pack_prompt": cfg.packPrompt = val; break;
|
||||
case "资源包大小":
|
||||
case "pack_size":
|
||||
try { cfg.packSize = Integer.parseInt(val); } catch (Exception ignored) {}
|
||||
break;
|
||||
case "本地文件":
|
||||
case "local_file": cfg.localFile = val; break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,33 +191,47 @@ public class UserGroupManager {
|
||||
return cfg;
|
||||
}
|
||||
|
||||
private void saveGroupConfigToDB(UserGroupConfig cfg) {
|
||||
/** 保存组配置到DB */
|
||||
public void saveGroupConfigToDB(UserGroupConfig cfg) {
|
||||
try {
|
||||
PreparedStatement ps = db.prepareStatement(
|
||||
"INSERT OR REPLACE INTO user_group_config "
|
||||
+ "(group_name, display_name, display_color, display_emoji,"
|
||||
+ " priority, is_permanent, duration_minutes,"
|
||||
+ " pack_url, pack_hash, pack_prompt, pack_size)"
|
||||
+ " VALUES (?,?,?,?,?,?,?,?,?,?,?)");
|
||||
+ " priority, land_price_per_sqm, max_lands, default_perms,"
|
||||
+ " is_permanent, duration_minutes)"
|
||||
+ " VALUES (?,?,?,?,?,?,?,?,?,?)");
|
||||
ps.setString(1, cfg.name);
|
||||
ps.setString(2, cfg.displayName);
|
||||
ps.setString(3, cfg.displayColor);
|
||||
ps.setString(4, cfg.displayEmoji);
|
||||
ps.setInt(5, cfg.priority);
|
||||
ps.setInt(6, cfg.permanent ? 1 : 0);
|
||||
ps.setInt(7, cfg.durationMinutes);
|
||||
ps.setString(8, cfg.packUrl);
|
||||
ps.setString(9, cfg.packHash);
|
||||
ps.setString(10, cfg.packPrompt);
|
||||
ps.setInt(11, cfg.packSize);
|
||||
ps.setInt(6, cfg.landPricePerSqm);
|
||||
ps.setInt(7, cfg.maxLands);
|
||||
ps.setString(8, cfg.defaultPerms != null ? cfg.defaultPerms : "{}");
|
||||
ps.setInt(9, cfg.permanent ? 1 : 0);
|
||||
ps.setInt(10, cfg.durationMinutes);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
plugin.getLogger().warning("[UserGroup] saveGroupConfigToDB failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 玩家操作 =====
|
||||
/** 删除组配置 */
|
||||
public boolean deleteGroupConfig(String groupName) {
|
||||
try {
|
||||
PreparedStatement ps = db.prepareStatement("DELETE FROM user_group_config WHERE group_name=?");
|
||||
ps.setString(1, groupName);
|
||||
int rows = ps.executeUpdate();
|
||||
ps.close();
|
||||
groupConfigs.remove(groupName);
|
||||
return rows > 0;
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 玩家操作 ====================
|
||||
|
||||
public boolean addPlayer(String player, String groupName, String addedBy, long durationMinutes) {
|
||||
UserGroupConfig cfg = groupConfigs.get(groupName);
|
||||
@@ -207,11 +253,9 @@ public class UserGroupManager {
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
plugin.getLogger().warning("[UserGroup] addPlayer failed: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
Player p = plugin.getServer().getPlayerExact(player);
|
||||
if (p != null) sendResourcePack(p);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -225,11 +269,11 @@ public class UserGroupManager {
|
||||
ps.close();
|
||||
return rows > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 获取玩家所属的所有有效组(排除过期) */
|
||||
public List<Map<String, Object>> getPlayerGroups(String player) {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
try {
|
||||
@@ -254,6 +298,10 @@ public class UserGroupManager {
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家最高优先级的有效用户组
|
||||
* 如果没有加入任何组,返回 null(调用方应使用全局默认值)
|
||||
*/
|
||||
public UserGroupConfig getHighestGroup(String player) {
|
||||
List<Map<String, Object>> groups = getPlayerGroups(player);
|
||||
long now = System.currentTimeMillis();
|
||||
@@ -284,7 +332,41 @@ public class UserGroupManager {
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 过期自动移除 =====
|
||||
// ==================== 领地相关查询 ====================
|
||||
|
||||
/**
|
||||
* 获取玩家的领地创建价格(每㎡)
|
||||
* 返回:组专属价格 或 全局默认
|
||||
*/
|
||||
public int getPlayerLandPricePerSqm(String player, int globalDefault) {
|
||||
UserGroupConfig cfg = getHighestGroup(player);
|
||||
if (cfg == null) return globalDefault;
|
||||
return cfg.landPricePerSqm >= 0 ? cfg.landPricePerSqm : globalDefault;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家的最大领地持有数
|
||||
* 返回:组专属上限 或 全局默认
|
||||
*/
|
||||
public int getPlayerMaxLands(String player, int globalDefault) {
|
||||
UserGroupConfig cfg = getHighestGroup(player);
|
||||
if (cfg == null) return globalDefault;
|
||||
return cfg.maxLands >= 0 ? cfg.maxLands : globalDefault;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家的默认领地权限 JSON
|
||||
* 返回:组默认权限 或 空JSON "{}"
|
||||
*/
|
||||
public String getPlayerDefaultPerms(String player) {
|
||||
UserGroupConfig cfg = getHighestGroup(player);
|
||||
if (cfg == null || cfg.defaultPerms == null || cfg.defaultPerms.isEmpty()) {
|
||||
return "{}";
|
||||
}
|
||||
return cfg.defaultPerms;
|
||||
}
|
||||
|
||||
// ==================== 过期检查 ====================
|
||||
|
||||
private void startExpiryChecker() {
|
||||
new BukkitRunnable() {
|
||||
@@ -292,7 +374,7 @@ public class UserGroupManager {
|
||||
public void run() {
|
||||
checkExpiry();
|
||||
}
|
||||
}.runTaskTimerAsynchronously(plugin, 1200L, 1200L); // 每分钟检查
|
||||
}.runTaskTimerAsynchronously(plugin, 1200L, 1200L); // 每60秒检查
|
||||
}
|
||||
|
||||
private void checkExpiry() {
|
||||
@@ -318,40 +400,37 @@ public class UserGroupManager {
|
||||
del.setString(2, e[1]);
|
||||
del.executeUpdate();
|
||||
del.close();
|
||||
plugin.getLogger().info("[UserGroup] 过期移除: " + e[0] + " -> " + e[1]);
|
||||
plugin.getLogger().info("[UserGroup] 过期移除: " + e[0] + " → " + e[1]);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 资源包分发 =====
|
||||
|
||||
public void sendResourcePack(Player p) {
|
||||
UserGroupConfig cfg = getHighestGroup(p.getName());
|
||||
if (cfg == null || cfg.packUrl == null || cfg.packUrl.isEmpty()) return;
|
||||
|
||||
String url = cfg.packUrl;
|
||||
String prompt = cfg.packPrompt != null
|
||||
? cfg.packPrompt : "Custom Resource Pack";
|
||||
|
||||
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
|
||||
try {
|
||||
p.setResourcePack(url, new byte[0], prompt, false);
|
||||
plugin.getLogger().info("[UserGroup] 资源包已发送: "
|
||||
+ p.getName() + " -> " + cfg.displayName);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning(
|
||||
"[UserGroup] 资源包发送失败: " + e.getMessage());
|
||||
}
|
||||
}, 40L);
|
||||
}
|
||||
// ==================== 工具方法 ====================
|
||||
|
||||
public Map<String, UserGroupConfig> getGroupConfigs() {
|
||||
return groupConfigs;
|
||||
}
|
||||
|
||||
// ===== 配置类 =====
|
||||
/** 获取所有组名列表 */
|
||||
public Set<String> getAllGroupNames() {
|
||||
return groupConfigs.keySet();
|
||||
}
|
||||
|
||||
/** 获取组配置(返回副本) */
|
||||
public UserGroupConfig getGroupConfig(String groupName) {
|
||||
return groupConfigs.get(groupName);
|
||||
}
|
||||
|
||||
/** 获取全部组列表(含默认组) */
|
||||
public List<UserGroupConfig> getAllGroups() {
|
||||
List<UserGroupConfig> list = new ArrayList<>(groupConfigs.values());
|
||||
list.sort((a, b) -> b.priority - a.priority);
|
||||
return list;
|
||||
}
|
||||
|
||||
// ==================== 配置类 ====================
|
||||
|
||||
public static class UserGroupConfig {
|
||||
public String name;
|
||||
@@ -359,12 +438,12 @@ public class UserGroupManager {
|
||||
public String displayColor = "§f";
|
||||
public String displayEmoji = "";
|
||||
public int priority = 0;
|
||||
// 领地相关
|
||||
public int landPricePerSqm = -1; // -1 = 使用全局
|
||||
public int maxLands = -1; // -1 = 使用全局
|
||||
public String defaultPerms = "{}"; // JSON
|
||||
// 兼容旧字段
|
||||
public boolean permanent = true;
|
||||
public int durationMinutes = 0;
|
||||
public String packUrl = "";
|
||||
public String packHash = "";
|
||||
public String packPrompt = "Custom Resource Pack";
|
||||
public int packSize = 0;
|
||||
public String localFile = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3026,6 +3026,42 @@ public class WebManager {
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("[防护-sync] 权限同步异常: " + e.getMessage());
|
||||
}
|
||||
|
||||
// 5. 同步用户组配置
|
||||
try {
|
||||
UserGroupManager ugm = plugin.getUserGroup();
|
||||
if (ugm != null) {
|
||||
Map<String, UserGroupManager.UserGroupConfig> groups = ugm.getGroupConfigs();
|
||||
if (!groups.isEmpty()) {
|
||||
StringBuilder gs = new StringBuilder("[");
|
||||
boolean first = true;
|
||||
for (UserGroupManager.UserGroupConfig cfg : groups.values()) {
|
||||
if (!first) gs.append(",");
|
||||
gs.append("{");
|
||||
gs.append("\"group_name\":\"").append(escapeJson(cfg.name)).append("\",");
|
||||
gs.append("\"display_name\":\"").append(escapeJson(cfg.displayName)).append("\",");
|
||||
gs.append("\"display_color\":\"").append(escapeJson(cfg.displayColor)).append("\",");
|
||||
gs.append("\"display_emoji\":\"").append(escapeJson(cfg.displayEmoji)).append("\",");
|
||||
gs.append("\"priority\":").append(cfg.priority).append(",");
|
||||
gs.append("\"land_price_per_sqm\":").append(cfg.landPricePerSqm).append(",");
|
||||
gs.append("\"max_lands\":").append(cfg.maxLands).append(",");
|
||||
gs.append("\"default_perms\":\"").append(escapeJson(cfg.defaultPerms != null ? cfg.defaultPerms : "{}")).append("\"");
|
||||
gs.append("}");
|
||||
first = false;
|
||||
}
|
||||
gs.append("]");
|
||||
|
||||
Map<String, String> gParams = new LinkedHashMap<>();
|
||||
gParams.put("action", "sync_user_groups");
|
||||
gParams.put("secret", secretKey);
|
||||
gParams.put("groups", gs.toString());
|
||||
String gResp = httpGet("api/sync.php", gParams);
|
||||
plugin.getLogger().fine("[防护-sync] 用户组同步: " + groups.size() + "个 → " + gResp);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("[防护-sync] 用户组同步异常: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user