新增领地方块/容器记录系统(回声碎片)与背包滚动自动备份
- 新增 land_block_log、land_container_log、inv_backup_auto 三张表 - LandRecordManager:领地内方块破坏/放置、容器打开/拿取自动记录;回声碎片工具右键/左键查询坐标历史记录 - AreaGUIManager 主菜单新增回声碎片按钮(位置33) - InventoryAutoBackup:每15分钟滚动备份在线玩家背包(每人保留3份),跳过PVP竞技场/练习场 - 每日定时清理超过7天的领地记录 - AreaProtection 钩子接入记录;注册 /landrec 命令
This commit is contained in:
@@ -129,6 +129,14 @@ public class AreaGUIManager implements Listener {
|
|||||||
"",
|
"",
|
||||||
"§e点击切换"));
|
"§e点击切换"));
|
||||||
|
|
||||||
|
// ★ 回声碎片:领地记录查询工具(位置33)
|
||||||
|
inv.setItem(33, createItem(Material.ECHO_SHARD, "§b§l回声碎片",
|
||||||
|
"§7获取领地记录查询工具",
|
||||||
|
"§7手持后右键/左键方块或容器",
|
||||||
|
"§7查看该处的破坏/拿取记录",
|
||||||
|
"",
|
||||||
|
"§e点击获取"));
|
||||||
|
|
||||||
// 返回按钮(位置49)
|
// 返回按钮(位置49)
|
||||||
inv.setItem(49, createItem(Material.ARROW, "§c§l返回主菜单", ""));
|
inv.setItem(49, createItem(Material.ARROW, "§c§l返回主菜单", ""));
|
||||||
|
|
||||||
@@ -876,6 +884,10 @@ public class AreaGUIManager implements Listener {
|
|||||||
openPublicBuildingList(p);
|
openPublicBuildingList(p);
|
||||||
} else if (raw == 49) {
|
} else if (raw == 49) {
|
||||||
plugin.getGui().openMain(p);
|
plugin.getGui().openMain(p);
|
||||||
|
} else if (raw == 33) {
|
||||||
|
// ★ 回声碎片:发放领地记录查询工具
|
||||||
|
p.closeInventory();
|
||||||
|
plugin.landRecordManager.giveEchoShard(p);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4870,6 +4870,10 @@ public class AreaProtection implements Listener {
|
|||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
p.sendMessage("§c§l[区域防护] §f禁止放置方块");
|
p.sendMessage("§c§l[区域防护] §f禁止放置方块");
|
||||||
}
|
}
|
||||||
|
// ★ 领地记录:记录成功放置的方块(事件未被取消才记录)
|
||||||
|
if (!e.isCancelled()) {
|
||||||
|
plugin.landRecordManager.recordBlockPlace(ac, p, e.getBlock());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
@@ -4930,6 +4934,10 @@ public class AreaProtection implements Listener {
|
|||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
p.sendMessage("§c§l[区域防护] §f禁止破坏方块");
|
p.sendMessage("§c§l[区域防护] §f禁止破坏方块");
|
||||||
}
|
}
|
||||||
|
// ★ 领地记录:记录成功破坏的方块(事件未被取消才记录)
|
||||||
|
if (!e.isCancelled()) {
|
||||||
|
plugin.landRecordManager.recordBlockBreak(ac, p, e.getBlock());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -5373,6 +5381,15 @@ public class AreaProtection implements Listener {
|
|||||||
|
|
||||||
// ★ 容器交互已启用时,无权限级别的玩家也允许访问(访客权限)
|
// ★ 容器交互已启用时,无权限级别的玩家也允许访问(访客权限)
|
||||||
// 只有denyContainer=true时才检查权限级别
|
// 只有denyContainer=true时才检查权限级别
|
||||||
|
|
||||||
|
// ★ 领地记录:记录实际打开的容器(含打开快照用于计算拿取差异)
|
||||||
|
String containerType;
|
||||||
|
if (loc.getBlock().getType() != org.bukkit.Material.AIR) {
|
||||||
|
containerType = loc.getBlock().getType().name();
|
||||||
|
} else {
|
||||||
|
containerType = holder.getClass().getSimpleName();
|
||||||
|
}
|
||||||
|
plugin.landRecordManager.recordContainerOpen(ac, p, loc, containerType, e.getInventory());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadWhitelists() {
|
public void loadWhitelists() {
|
||||||
|
|||||||
@@ -304,6 +304,53 @@ public class DatabaseManager {
|
|||||||
safeAdd(st, "menu_snowball",
|
safeAdd(st, "menu_snowball",
|
||||||
"INTEGER DEFAULT 1");
|
"INTEGER DEFAULT 1");
|
||||||
|
|
||||||
|
// 领地方块记录表:记录领地内谁破坏/放置了什么方块(7天滚动)
|
||||||
|
st.execute("CREATE TABLE IF NOT EXISTS "
|
||||||
|
+ "land_block_log ("
|
||||||
|
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||||
|
+ "land_name TEXT,"
|
||||||
|
+ "land_id INTEGER,"
|
||||||
|
+ "world TEXT,"
|
||||||
|
+ "x INTEGER,"
|
||||||
|
+ "y INTEGER,"
|
||||||
|
+ "z INTEGER,"
|
||||||
|
+ "player_name TEXT,"
|
||||||
|
+ "action TEXT,"
|
||||||
|
+ "material TEXT,"
|
||||||
|
+ "time INTEGER"
|
||||||
|
+ ")");
|
||||||
|
|
||||||
|
// 领地容器记录表:记录领地内容器的打开/拿取(7天滚动)
|
||||||
|
st.execute("CREATE TABLE IF NOT EXISTS "
|
||||||
|
+ "land_container_log ("
|
||||||
|
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||||
|
+ "land_name TEXT,"
|
||||||
|
+ "land_id INTEGER,"
|
||||||
|
+ "world TEXT,"
|
||||||
|
+ "x INTEGER,"
|
||||||
|
+ "y INTEGER,"
|
||||||
|
+ "z INTEGER,"
|
||||||
|
+ "player_name TEXT,"
|
||||||
|
+ "action TEXT,"
|
||||||
|
+ "container_type TEXT,"
|
||||||
|
+ "detail TEXT,"
|
||||||
|
+ "amount INTEGER,"
|
||||||
|
+ "time INTEGER"
|
||||||
|
+ ")");
|
||||||
|
|
||||||
|
// 背包自动滚动备份表(15分钟定时):每位玩家保留最近 3 份
|
||||||
|
st.execute("CREATE TABLE IF NOT EXISTS "
|
||||||
|
+ "inv_backup_auto ("
|
||||||
|
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||||
|
+ "player_name TEXT NOT NULL,"
|
||||||
|
+ "contents_b64 TEXT DEFAULT '',"
|
||||||
|
+ "armor_b64 TEXT DEFAULT '',"
|
||||||
|
+ "extra_b64 TEXT DEFAULT '',"
|
||||||
|
+ "level INTEGER DEFAULT 0,"
|
||||||
|
+ "experience REAL DEFAULT 0,"
|
||||||
|
+ "save_time INTEGER DEFAULT 0"
|
||||||
|
+ ")");
|
||||||
|
|
||||||
// 注:传送请求不再落库,仅驻留内存(配置项才落库),故不再建 teleport_requests 表
|
// 注:传送请求不再落库,仅驻留内存(配置项才落库),故不再建 teleport_requests 表
|
||||||
st.close();
|
st.close();
|
||||||
logger.info("[Sdf1_login] 数据库初始化完成");
|
logger.info("[Sdf1_login] 数据库初始化完成");
|
||||||
@@ -2114,6 +2161,231 @@ public class DatabaseManager {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==================== 领地方块/容器记录 ====================
|
||||||
|
|
||||||
|
/** 记录领地方块破坏/放置 */
|
||||||
|
public void recordLandBlock(String landName, int landId,
|
||||||
|
String world, int x, int y, int z,
|
||||||
|
String playerName, String action,
|
||||||
|
String material) {
|
||||||
|
try {
|
||||||
|
PreparedStatement ps = db.prepareStatement(
|
||||||
|
"INSERT INTO land_block_log"
|
||||||
|
+ " (land_name, land_id, world, x, y, z,"
|
||||||
|
+ " player_name, action, material, time)"
|
||||||
|
+ " VALUES (?,?,?,?,?,?,?,?,?,?)");
|
||||||
|
ps.setString(1, landName);
|
||||||
|
ps.setInt(2, landId);
|
||||||
|
ps.setString(3, world);
|
||||||
|
ps.setInt(4, x);
|
||||||
|
ps.setInt(5, y);
|
||||||
|
ps.setInt(6, z);
|
||||||
|
ps.setString(7, playerName);
|
||||||
|
ps.setString(8, action);
|
||||||
|
ps.setString(9, material);
|
||||||
|
ps.setLong(10, System.currentTimeMillis());
|
||||||
|
ps.executeUpdate();
|
||||||
|
ps.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 记录领地容器打开/拿取 */
|
||||||
|
public void recordLandContainer(String landName, int landId,
|
||||||
|
String world, int x, int y, int z,
|
||||||
|
String playerName, String action,
|
||||||
|
String containerType, String detail,
|
||||||
|
int amount) {
|
||||||
|
try {
|
||||||
|
PreparedStatement ps = db.prepareStatement(
|
||||||
|
"INSERT INTO land_container_log"
|
||||||
|
+ " (land_name, land_id, world, x, y, z,"
|
||||||
|
+ " player_name, action, container_type,"
|
||||||
|
+ " detail, amount, time)"
|
||||||
|
+ " VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
|
||||||
|
ps.setString(1, landName);
|
||||||
|
ps.setInt(2, landId);
|
||||||
|
ps.setString(3, world);
|
||||||
|
ps.setInt(4, x);
|
||||||
|
ps.setInt(5, y);
|
||||||
|
ps.setInt(6, z);
|
||||||
|
ps.setString(7, playerName);
|
||||||
|
ps.setString(8, action);
|
||||||
|
ps.setString(9, containerType);
|
||||||
|
ps.setString(10, detail);
|
||||||
|
ps.setInt(11, amount);
|
||||||
|
ps.setLong(12, System.currentTimeMillis());
|
||||||
|
ps.executeUpdate();
|
||||||
|
ps.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 查询某坐标最近的方块记录(按时间倒序) */
|
||||||
|
public List<Map<String, Object>> getLandBlockLogAt(
|
||||||
|
String world, int x, int y, int z, int limit) {
|
||||||
|
List<Map<String, Object>> list = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
PreparedStatement ps = db.prepareStatement(
|
||||||
|
"SELECT time, player_name, action, material"
|
||||||
|
+ " FROM land_block_log"
|
||||||
|
+ " WHERE world=? AND x=? AND y=? AND z=?"
|
||||||
|
+ " ORDER BY time DESC LIMIT ?");
|
||||||
|
ps.setString(1, world);
|
||||||
|
ps.setInt(2, x);
|
||||||
|
ps.setInt(3, y);
|
||||||
|
ps.setInt(4, z);
|
||||||
|
ps.setInt(5, limit > 0 ? limit : 20);
|
||||||
|
ResultSet rs = ps.executeQuery();
|
||||||
|
while (rs.next()) {
|
||||||
|
Map<String, Object> row =
|
||||||
|
new LinkedHashMap<>();
|
||||||
|
row.put("time", rs.getLong("time"));
|
||||||
|
row.put("player_name",
|
||||||
|
rs.getString("player_name"));
|
||||||
|
row.put("action", rs.getString("action"));
|
||||||
|
row.put("material", rs.getString("material"));
|
||||||
|
list.add(row);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 查询某坐标最近的容器记录(按时间倒序) */
|
||||||
|
public List<Map<String, Object>> getLandContainerLogAt(
|
||||||
|
String world, int x, int y, int z, int limit) {
|
||||||
|
List<Map<String, Object>> list = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
PreparedStatement ps = db.prepareStatement(
|
||||||
|
"SELECT time, player_name, action, container_type,"
|
||||||
|
+ " detail, amount"
|
||||||
|
+ " FROM land_container_log"
|
||||||
|
+ " WHERE world=? AND x=? AND y=? AND z=?"
|
||||||
|
+ " ORDER BY time DESC LIMIT ?");
|
||||||
|
ps.setString(1, world);
|
||||||
|
ps.setInt(2, x);
|
||||||
|
ps.setInt(3, y);
|
||||||
|
ps.setInt(4, z);
|
||||||
|
ps.setInt(5, limit > 0 ? limit : 20);
|
||||||
|
ResultSet rs = ps.executeQuery();
|
||||||
|
while (rs.next()) {
|
||||||
|
Map<String, Object> row =
|
||||||
|
new LinkedHashMap<>();
|
||||||
|
row.put("time", rs.getLong("time"));
|
||||||
|
row.put("player_name",
|
||||||
|
rs.getString("player_name"));
|
||||||
|
row.put("action", rs.getString("action"));
|
||||||
|
row.put("container_type",
|
||||||
|
rs.getString("container_type"));
|
||||||
|
row.put("detail", rs.getString("detail"));
|
||||||
|
row.put("amount", rs.getInt("amount"));
|
||||||
|
list.add(row);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 清理超过 cutoff(毫秒时间戳)的领地记录,返回删除行数 */
|
||||||
|
public int pruneLandRecordsOlderThan(long cutoff) {
|
||||||
|
int deleted = 0;
|
||||||
|
try {
|
||||||
|
PreparedStatement ps1 = db.prepareStatement(
|
||||||
|
"DELETE FROM land_block_log WHERE time < ?");
|
||||||
|
ps1.setLong(1, cutoff);
|
||||||
|
deleted += ps1.executeUpdate();
|
||||||
|
ps1.close();
|
||||||
|
|
||||||
|
PreparedStatement ps2 = db.prepareStatement(
|
||||||
|
"DELETE FROM land_container_log WHERE time < ?");
|
||||||
|
ps2.setLong(1, cutoff);
|
||||||
|
deleted += ps2.executeUpdate();
|
||||||
|
ps2.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return deleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 背包自动滚动备份(15分钟定时)====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存背包自动备份 —— 追加写入,每位玩家仅保留最近 3 份。
|
||||||
|
* 跳过 PVP 竞技场/测试场的判断由调用方(InventoryAutoBackup)负责。
|
||||||
|
*/
|
||||||
|
public void saveAutoBackup(String name,
|
||||||
|
String contents, String armor,
|
||||||
|
String extra, int level,
|
||||||
|
double experience) {
|
||||||
|
try {
|
||||||
|
PreparedStatement ps =
|
||||||
|
db.prepareStatement(
|
||||||
|
"INSERT INTO inv_backup_auto"
|
||||||
|
+ " (player_name, "
|
||||||
|
+ "contents_b64, "
|
||||||
|
+ "armor_b64, "
|
||||||
|
+ "extra_b64, "
|
||||||
|
+ "level, "
|
||||||
|
+ "experience, "
|
||||||
|
+ "save_time) "
|
||||||
|
+ "VALUES (?,?,?,?,?,?,?)");
|
||||||
|
ps.setString(1, name);
|
||||||
|
ps.setString(2, contents);
|
||||||
|
ps.setString(3, armor);
|
||||||
|
ps.setString(4, extra);
|
||||||
|
ps.setInt(5, level);
|
||||||
|
ps.setDouble(6, experience);
|
||||||
|
ps.setLong(7, System.currentTimeMillis());
|
||||||
|
ps.executeUpdate();
|
||||||
|
ps.close();
|
||||||
|
|
||||||
|
// 仅保留最近 3 份,删除该玩家更早的备份(先查后删,避免 LIMIT 子查询被优化)
|
||||||
|
try {
|
||||||
|
PreparedStatement sel =
|
||||||
|
db.prepareStatement(
|
||||||
|
"SELECT id FROM inv_backup_auto "
|
||||||
|
+ "WHERE player_name=? "
|
||||||
|
+ "ORDER BY save_time DESC LIMIT 3");
|
||||||
|
sel.setString(1, name);
|
||||||
|
ResultSet rs = sel.executeQuery();
|
||||||
|
List<Integer> keep = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
keep.add(rs.getInt("id"));
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
sel.close();
|
||||||
|
|
||||||
|
if (keep.size() == 3) {
|
||||||
|
StringBuilder in = new StringBuilder();
|
||||||
|
for (int i = 0; i < keep.size(); i++) {
|
||||||
|
if (i > 0) in.append(",");
|
||||||
|
in.append(keep.get(i));
|
||||||
|
}
|
||||||
|
PreparedStatement del = db.prepareStatement(
|
||||||
|
"DELETE FROM inv_backup_auto "
|
||||||
|
+ "WHERE player_name=? AND id NOT IN ("
|
||||||
|
+ in.toString() + ")");
|
||||||
|
del.setString(1, name);
|
||||||
|
del.executeUpdate();
|
||||||
|
del.close();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Connection getDb() {
|
public Connection getDb() {
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package Sdf1_login;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 背包滚动自动备份。
|
||||||
|
* <p>
|
||||||
|
* ★ 功能:
|
||||||
|
* 1. 由 Main 的 15 分钟定时任务驱动,遍历所有在线玩家。
|
||||||
|
* 2. 在 PVP 竞技场 / 练习场(测试场)的玩家不备份 —— 避免竞技场装备覆盖真实背包。
|
||||||
|
* 3. 序列化 主背包 / 护甲 / 副手(extra) / 等级 / 经验,写入 inv_backup_auto 表。
|
||||||
|
* 4. 每玩家保留最近 3 份(滚动覆盖),由 DatabaseManager.saveAutoBackup 负责清理。
|
||||||
|
* <p>
|
||||||
|
* 与登录时的 saveInventoryBackup 共用同一套存储语义(level=玩家等级,
|
||||||
|
* experience=当前等级进度 p.getExp()),便于后续统一还原。
|
||||||
|
*/
|
||||||
|
public class InventoryAutoBackup {
|
||||||
|
|
||||||
|
private final Main plugin;
|
||||||
|
|
||||||
|
/** 备份周期:15 分钟(单位 tick) */
|
||||||
|
public static final long INTERVAL_TICKS = 15L * 60L * 20L;
|
||||||
|
|
||||||
|
public InventoryAutoBackup(Main plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 对所有在线玩家执行一次滚动备份 */
|
||||||
|
public void runBackup() {
|
||||||
|
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||||
|
try {
|
||||||
|
backupPlayer(p);
|
||||||
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().warning(
|
||||||
|
"[自动备份] 玩家 " + p.getName()
|
||||||
|
+ " 备份失败: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void backupPlayer(Player p) {
|
||||||
|
String name = p.getName();
|
||||||
|
|
||||||
|
// ★ 跳过 PVP 竞技场:竞技场内不备份,避免 PVP 装备覆盖真实背包
|
||||||
|
if (plugin.getPVPArenaManager().isInPVPArena(name)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// ★ 跳过练习场 / 测试世界
|
||||||
|
if (plugin.getPVPTestManager().isInTestWorld(p)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack[] contents = p.getInventory().getContents();
|
||||||
|
ItemStack[] armor = p.getInventory().getArmorContents();
|
||||||
|
ItemStack[] extra = p.getInventory().getExtraContents();
|
||||||
|
|
||||||
|
String contentsB64, armorB64, extraB64;
|
||||||
|
try {
|
||||||
|
contentsB64 = InventorySerializer.toBase64(contents);
|
||||||
|
armorB64 = InventorySerializer.toBase64(armor);
|
||||||
|
extraB64 = InventorySerializer.toBase64(extra);
|
||||||
|
} catch (IOException e) {
|
||||||
|
plugin.getLogger().warning(
|
||||||
|
"[自动备份] 序列化失败 (" + name + "): "
|
||||||
|
+ e.getMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int level = p.getLevel();
|
||||||
|
double experience = p.getExp(); // 当前等级进度 0.0~1.0
|
||||||
|
plugin.getDb().saveAutoBackup(
|
||||||
|
name, contentsB64, armorB64, extraB64,
|
||||||
|
level, experience);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,351 @@
|
|||||||
|
package Sdf1_login;
|
||||||
|
|
||||||
|
import Sdf1_login.AreaProtection.AreaConfig;
|
||||||
|
|
||||||
|
import org.bukkit.*;
|
||||||
|
import org.bukkit.block.Block;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.block.Action;
|
||||||
|
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||||
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.bukkit.persistence.PersistentDataType;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 领地方块/容器记录系统 —— 回声碎片(echo_shard)工具。
|
||||||
|
* <p>
|
||||||
|
* ★ 功能:
|
||||||
|
* 1. 领地内方块破坏/放置 → 自动写入 land_block_log
|
||||||
|
* 2. 领地内容器打开/拿取 → 自动写入 land_container_log
|
||||||
|
* 3. 玩家手持回声碎片右键/左键方块或容器 → 聊天栏打印该坐标的历史记录
|
||||||
|
* 4. /landrec give [玩家] → 发放回声碎片(由插件/管理员发放)
|
||||||
|
* <p>
|
||||||
|
* 记录保留 7 天滚动(由 Main 的每日清理定时器调用 prune)。
|
||||||
|
*/
|
||||||
|
public class LandRecordManager implements Listener {
|
||||||
|
|
||||||
|
private final Main plugin;
|
||||||
|
|
||||||
|
/** 回声碎片物品的 NBT 标识 key */
|
||||||
|
public static final String ECHO_SHARD_KEY = "sdf1_echo_shard";
|
||||||
|
/** 回声碎片物品材质(1.19+ 存在) */
|
||||||
|
public static final Material ECHO_SHARD_MATERIAL = Material.ECHO_SHARD;
|
||||||
|
|
||||||
|
/** 记录查询默认条数 */
|
||||||
|
private static final int QUERY_LIMIT = 20;
|
||||||
|
/** 记录保留天数 */
|
||||||
|
public static final int RETENTION_DAYS = 7;
|
||||||
|
|
||||||
|
private final SimpleDateFormat sdf =
|
||||||
|
new SimpleDateFormat("MM-dd HH:mm");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 容器打开时的快照:用于关闭时计算"拿了什么"。
|
||||||
|
* key = 玩家名(MC 同一时刻只能打开一个容器 GUI,故按玩家唯一即可)。
|
||||||
|
*/
|
||||||
|
private final Map<String, ContainerSnapshot> openSnapshots =
|
||||||
|
new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
static class ContainerSnapshot {
|
||||||
|
String landName;
|
||||||
|
int landId;
|
||||||
|
String world;
|
||||||
|
int x, y, z;
|
||||||
|
String containerType;
|
||||||
|
Map<String, Integer> before = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public LandRecordManager(Main plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 回声碎片物品 ====================
|
||||||
|
|
||||||
|
/** 创建一枚回声碎片(带 NBT 标识,便于精准识别) */
|
||||||
|
public ItemStack createEchoShard() {
|
||||||
|
ItemStack item = new ItemStack(ECHO_SHARD_MATERIAL);
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
if (meta != null) {
|
||||||
|
meta.setDisplayName("§b§l回声碎片");
|
||||||
|
meta.setLore(Arrays.asList(
|
||||||
|
"§7右键/左键 方块或容器",
|
||||||
|
"§7查看领地内的破坏/拿取记录",
|
||||||
|
"§8(领地监控工具)"));
|
||||||
|
NamespacedKey key =
|
||||||
|
new NamespacedKey(plugin, ECHO_SHARD_KEY);
|
||||||
|
meta.getPersistentDataContainer().set(
|
||||||
|
key, PersistentDataType.BYTE, (byte) 1);
|
||||||
|
item.setItemMeta(meta);
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 判断物品是否为回声碎片(材质 + NBT 双重校验) */
|
||||||
|
public boolean isEchoShard(ItemStack item) {
|
||||||
|
if (item == null || item.getType() != ECHO_SHARD_MATERIAL)
|
||||||
|
return false;
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
if (meta == null) return false;
|
||||||
|
NamespacedKey key =
|
||||||
|
new NamespacedKey(plugin, ECHO_SHARD_KEY);
|
||||||
|
return meta.getPersistentDataContainer().has(
|
||||||
|
key, PersistentDataType.BYTE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 直接发放一枚回声碎片给玩家(GUI 菜单调用) */
|
||||||
|
public void giveEchoShard(Player p) {
|
||||||
|
ItemStack shard = createEchoShard();
|
||||||
|
HashMap<Integer, ItemStack> left =
|
||||||
|
p.getInventory().addItem(shard);
|
||||||
|
if (!left.isEmpty()) {
|
||||||
|
p.sendMessage("§c§l[回声碎片] §f你的背包已满,无法发放");
|
||||||
|
} else {
|
||||||
|
p.sendMessage("§a§l[回声碎片] §f你获得了§b回声碎片§f,右键/左键方块或容器查看领地记录");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 记录写入(由 AreaProtection 钩子调用)====================
|
||||||
|
|
||||||
|
/** 记录方块破坏 */
|
||||||
|
public void recordBlockBreak(AreaConfig ac, Player p,
|
||||||
|
Block block) {
|
||||||
|
recordBlock(ac, p, block, "break");
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 记录方块放置 */
|
||||||
|
public void recordBlockPlace(AreaConfig ac, Player p,
|
||||||
|
Block block) {
|
||||||
|
recordBlock(ac, p, block, "place");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recordBlock(AreaConfig ac, Player p, Block block,
|
||||||
|
String action) {
|
||||||
|
try {
|
||||||
|
int landId = plugin.areaProtection.getLandIdFromDb(
|
||||||
|
ac.name);
|
||||||
|
plugin.getDb().recordLandBlock(
|
||||||
|
ac.name, landId,
|
||||||
|
block.getWorld().getName(),
|
||||||
|
block.getX(), block.getY(), block.getZ(),
|
||||||
|
p.getName(), action,
|
||||||
|
block.getType().name());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 记录容器打开,并保存打开瞬间容器快照(用于关闭时计算拿取差量)。
|
||||||
|
* 仅在容器确实被允许打开时调用(钩子位于 AreaProtection
|
||||||
|
* 权限/取消检查之后)。
|
||||||
|
*/
|
||||||
|
public void recordContainerOpen(AreaConfig ac, Player p,
|
||||||
|
Location loc, String containerType,
|
||||||
|
Inventory inv) {
|
||||||
|
try {
|
||||||
|
int landId = plugin.areaProtection.getLandIdFromDb(
|
||||||
|
ac.name);
|
||||||
|
plugin.getDb().recordLandContainer(
|
||||||
|
ac.name, landId,
|
||||||
|
loc.getWorld().getName(),
|
||||||
|
loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(),
|
||||||
|
p.getName(), "open", containerType, "", 0);
|
||||||
|
|
||||||
|
ContainerSnapshot snap = new ContainerSnapshot();
|
||||||
|
snap.landName = ac.name;
|
||||||
|
snap.landId = landId;
|
||||||
|
snap.world = loc.getWorld().getName();
|
||||||
|
snap.x = loc.getBlockX();
|
||||||
|
snap.y = loc.getBlockY();
|
||||||
|
snap.z = loc.getBlockZ();
|
||||||
|
snap.containerType = containerType;
|
||||||
|
snap.before = scanInventory(inv);
|
||||||
|
openSnapshots.put(p.getName(), snap);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 容器关闭:计算拿取差量 ====================
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onContainerClose(InventoryCloseEvent e) {
|
||||||
|
if (!(e.getPlayer() instanceof Player)) return;
|
||||||
|
Player p = (Player) e.getPlayer();
|
||||||
|
ContainerSnapshot snap = openSnapshots.remove(p.getName());
|
||||||
|
if (snap == null) return;
|
||||||
|
|
||||||
|
Map<String, Integer> after =
|
||||||
|
scanInventory(e.getInventory());
|
||||||
|
for (Map.Entry<String, Integer> en :
|
||||||
|
snap.before.entrySet()) {
|
||||||
|
int taken = en.getValue() -
|
||||||
|
after.getOrDefault(en.getKey(), 0);
|
||||||
|
if (taken > 0) {
|
||||||
|
try {
|
||||||
|
plugin.getDb().recordLandContainer(
|
||||||
|
snap.landName, snap.landId,
|
||||||
|
snap.world, snap.x, snap.y, snap.z,
|
||||||
|
p.getName(), "take",
|
||||||
|
snap.containerType, en.getKey(), taken);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 扫描容器库存,按"类型名"聚合数量 */
|
||||||
|
private Map<String, Integer> scanInventory(Inventory inv) {
|
||||||
|
Map<String, Integer> map = new HashMap<>();
|
||||||
|
for (ItemStack it : inv.getContents()) {
|
||||||
|
if (it == null || it.getType() == Material.AIR)
|
||||||
|
continue;
|
||||||
|
map.merge(it.getType().name(),
|
||||||
|
it.getAmount(), Integer::sum);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 回声碎片查询(右键/左键)====================
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onShardInteract(PlayerInteractEvent e) {
|
||||||
|
ItemStack item = e.getItem();
|
||||||
|
if (item == null || !isEchoShard(item)) return;
|
||||||
|
Action a = e.getAction();
|
||||||
|
if (a != Action.RIGHT_CLICK_BLOCK && a != Action.LEFT_CLICK_BLOCK
|
||||||
|
&& a != Action.RIGHT_CLICK_AIR
|
||||||
|
&& a != Action.LEFT_CLICK_AIR) return;
|
||||||
|
|
||||||
|
Player p = e.getPlayer();
|
||||||
|
// 阻止误触方块/打开容器
|
||||||
|
e.setCancelled(true);
|
||||||
|
|
||||||
|
Block target = e.getClickedBlock();
|
||||||
|
if (target == null) {
|
||||||
|
// 视线选中的方块(非 deprecated 的 getTargetBlockExact)
|
||||||
|
Block sight = p.getTargetBlockExact(6);
|
||||||
|
if (sight != null && sight.getType() != Material.AIR)
|
||||||
|
target = sight;
|
||||||
|
}
|
||||||
|
if (target == null) {
|
||||||
|
p.sendMessage("§c§l[回声碎片] §f你面前没有可查询的方块或容器");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
queryAndPrint(p, target.getLocation());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void queryAndPrint(Player p, Location loc) {
|
||||||
|
String world = loc.getWorld().getName();
|
||||||
|
int x = loc.getBlockX(), y = loc.getBlockY(),
|
||||||
|
z = loc.getBlockZ();
|
||||||
|
|
||||||
|
List<Map<String, Object>> blocks =
|
||||||
|
plugin.getDb().getLandBlockLogAt(
|
||||||
|
world, x, y, z, QUERY_LIMIT);
|
||||||
|
List<Map<String, Object>> containers =
|
||||||
|
plugin.getDb().getLandContainerLogAt(
|
||||||
|
world, x, y, z, QUERY_LIMIT);
|
||||||
|
|
||||||
|
if (blocks.isEmpty() && containers.isEmpty()) {
|
||||||
|
p.sendMessage("§6§l[回声碎片] §f坐标 ("
|
||||||
|
+ x + "," + y + "," + z + ") 暂无领地记录");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
p.sendMessage("§6§l[回声碎片] §e坐标 ("
|
||||||
|
+ x + "," + y + "," + z + ") 的记录:");
|
||||||
|
|
||||||
|
if (!blocks.isEmpty()) {
|
||||||
|
p.sendMessage("§b— 方块记录 —");
|
||||||
|
for (Map<String, Object> r : blocks) {
|
||||||
|
String act = "break".equals(r.get("action"))
|
||||||
|
? "§c破坏" : "§a放置";
|
||||||
|
p.sendMessage(" §7" + fmt((Long) r.get("time"))
|
||||||
|
+ " §f" + r.get("player_name")
|
||||||
|
+ " " + act + " §e"
|
||||||
|
+ r.get("material"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!containers.isEmpty()) {
|
||||||
|
p.sendMessage("§b— 容器记录 —");
|
||||||
|
for (Map<String, Object> r : containers) {
|
||||||
|
String act = (String) r.get("action");
|
||||||
|
if ("open".equals(act)) {
|
||||||
|
p.sendMessage(" §7" + fmt((Long) r.get("time"))
|
||||||
|
+ " §f" + r.get("player_name")
|
||||||
|
+ " §d打开§7[" + r.get("container_type")
|
||||||
|
+ "]");
|
||||||
|
} else if ("take".equals(act)) {
|
||||||
|
p.sendMessage(" §7" + fmt((Long) r.get("time"))
|
||||||
|
+ " §f" + r.get("player_name")
|
||||||
|
+ " §c拿取 §e" + r.get("detail")
|
||||||
|
+ " §7x" + r.get("amount"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String fmt(long ms) {
|
||||||
|
return sdf.format(new Date(ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 命令:/landrec ====================
|
||||||
|
|
||||||
|
public boolean handleCommand(CommandSender sender,
|
||||||
|
String[] args) {
|
||||||
|
if (args.length == 0) {
|
||||||
|
sendHelp(sender);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
String sub = args[0];
|
||||||
|
if (sub.equalsIgnoreCase("give")) {
|
||||||
|
// 发放权限:给自己(self)任意玩家可发;发给他人需 OP 或领地管理员
|
||||||
|
boolean isOp = sender.isOp();
|
||||||
|
boolean isAreaAdmin = (sender instanceof Player)
|
||||||
|
&& plugin.areaProtection.isAreaAdmin((Player) sender);
|
||||||
|
|
||||||
|
Player target;
|
||||||
|
if (args.length >= 2) {
|
||||||
|
if (!isOp && !isAreaAdmin) {
|
||||||
|
sender.sendMessage("§c§l[回声碎片] §f你无权向他人发放回声碎片");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
target = Bukkit.getPlayerExact(args[1]);
|
||||||
|
if (target == null) {
|
||||||
|
sender.sendMessage("§c§l[回声碎片] §f玩家 " + args[1] + " 不在线");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else if (sender instanceof Player) {
|
||||||
|
target = (Player) sender;
|
||||||
|
} else {
|
||||||
|
sender.sendMessage("§c§l[回声碎片] §f请指定要发放的玩家");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
giveEchoShard(target);
|
||||||
|
if (target != sender)
|
||||||
|
sender.sendMessage("§a§l[回声碎片] §f已发放回声碎片给 " + target.getName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
sendHelp(sender);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendHelp(CommandSender sender) {
|
||||||
|
sender.sendMessage("§6§l[回声碎片] §e指令:");
|
||||||
|
sender.sendMessage(" §7/landrec give §f- 给自己发放一枚回声碎片");
|
||||||
|
sender.sendMessage(" §7/landrec give <玩家> §f- 向他人发放(需 OP/领地管理员)");
|
||||||
|
sender.sendMessage(" §7手持回声碎片右键/左键方块或容器 §f- 查看该处记录");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -150,6 +150,8 @@ public class Main extends JavaPlugin
|
|||||||
public AreaProtection areaProtection;
|
public AreaProtection areaProtection;
|
||||||
public AreaGUIManager areaGUIManager;
|
public AreaGUIManager areaGUIManager;
|
||||||
public AreaCLIManager areaCLIManager;
|
public AreaCLIManager areaCLIManager;
|
||||||
|
public LandRecordManager landRecordManager;
|
||||||
|
public InventoryAutoBackup inventoryAutoBackup;
|
||||||
private TeleportManager teleportMgr;
|
private TeleportManager teleportMgr;
|
||||||
private HomeManager homeMgr;
|
private HomeManager homeMgr;
|
||||||
private DeathReport deathReport;
|
private DeathReport deathReport;
|
||||||
@@ -597,6 +599,49 @@ public class Main extends JavaPlugin
|
|||||||
areaProtection.startEnforceTask();
|
areaProtection.startEnforceTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 15.1 ====领地记录系统(回声碎片)====
|
||||||
|
landRecordManager = new LandRecordManager(this);
|
||||||
|
getServer().getPluginManager()
|
||||||
|
.registerEvents(landRecordManager, this);
|
||||||
|
|
||||||
|
// 15.2 ====背包滚动自动备份(每15分钟)====
|
||||||
|
inventoryAutoBackup = new InventoryAutoBackup(this);
|
||||||
|
getServer().getScheduler().runTaskTimer(this,
|
||||||
|
() -> {
|
||||||
|
if (inventoryAutoBackup != null) {
|
||||||
|
inventoryAutoBackup.runBackup();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
InventoryAutoBackup.INTERVAL_TICKS, // 首跑延迟 15 分钟
|
||||||
|
InventoryAutoBackup.INTERVAL_TICKS); // 之后每 15 分钟
|
||||||
|
|
||||||
|
// 15.3 ====领地记录每日清理(保留 7 天)====
|
||||||
|
// 每天 0 点附近跑一次:用 24h 周期,首跑延迟到下一个整点对齐不强制,
|
||||||
|
// 这里简单采用「每小时检测一次,命中当天 0 点窗口即清理」过于繁琐,
|
||||||
|
// 故改为每日定时:延迟 1 小时后启动,之后每 24 小时执行一次。
|
||||||
|
long dayTicks = 24L * 60L * 60L * 20L;
|
||||||
|
getServer().getScheduler().runTaskTimer(this,
|
||||||
|
() -> {
|
||||||
|
try {
|
||||||
|
long cutoff = System.currentTimeMillis()
|
||||||
|
- (long) LandRecordManager.RETENTION_DAYS
|
||||||
|
* 24L * 60L * 60L * 1000L;
|
||||||
|
int removed = getDb().pruneLandRecordsOlderThan(
|
||||||
|
cutoff);
|
||||||
|
if (removed > 0) {
|
||||||
|
getLogger().info("[领地记录] 已清理 "
|
||||||
|
+ removed + " 条超过 "
|
||||||
|
+ LandRecordManager.RETENTION_DAYS
|
||||||
|
+ " 天的旧记录");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
getLogger().warning("[领地记录] 每日清理失败: "
|
||||||
|
+ e.getMessage());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
3600L, // 首跑:启动 1 小时后
|
||||||
|
dayTicks); // 之后每 24 小时
|
||||||
|
|
||||||
// ★ 启动时异步检查更新(GitHub/Gitee双通道)
|
// ★ 启动时异步检查更新(GitHub/Gitee双通道)
|
||||||
updateChecker = new UpdateChecker(this);
|
updateChecker = new UpdateChecker(this);
|
||||||
updateChecker.checkOnEnable();
|
updateChecker.checkOnEnable();
|
||||||
@@ -5154,6 +5199,21 @@ public class Main extends JavaPlugin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ★ 领地记录系统:/landrec 命令(回声碎片发放)
|
||||||
|
if (cmd.getName().equalsIgnoreCase("landrec")) {
|
||||||
|
if (landRecordManager == null) {
|
||||||
|
sender.sendMessage("§c领地记录系统未初始化");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return landRecordManager.handleCommand(sender, args);
|
||||||
|
} catch (Exception e) {
|
||||||
|
sender.sendMessage("§c执行出错: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ★ 独立 /update 命令 → 集群更新所有插件
|
// ★ 独立 /update 命令 → 集群更新所有插件
|
||||||
if (cmd.getName().equalsIgnoreCase("update")) {
|
if (cmd.getName().equalsIgnoreCase("update")) {
|
||||||
if (!isAdmin(sender)) {
|
if (!isAdmin(sender)) {
|
||||||
|
|||||||
@@ -102,6 +102,9 @@ commands:
|
|||||||
protect:
|
protect:
|
||||||
description: 区域保护
|
description: 区域保护
|
||||||
usage: /protect
|
usage: /protect
|
||||||
|
landrec:
|
||||||
|
description: 领地记录系统(回声碎片发放)
|
||||||
|
usage: /landrec give [玩家]
|
||||||
传送:
|
传送:
|
||||||
description: 传送至领地
|
description: 传送至领地
|
||||||
usage: /传送 <领地名>
|
usage: /传送 <领地名>
|
||||||
|
|||||||
Reference in New Issue
Block a user