第124批续24-A:靶场还原空背包根因修复(PVP-owned 快照改读 PVP DB)
Auto Release / build (push) Successful in 2m30s

根因(视频复现 10:49:41-54 现场):
/pvp test 进场时序 PVPArenaManager 先备份真实背包到DB+清空+发PVP专属装备,
PVPTestManager backupInventory 回收PVP/练习装备后活背包=空 → 走 allEmpty 分支
只标记 inventoryOwnedByPvp、未快照真实物 → 还原补回0件=空背包。

修复:
- backupInventory allEmpty 分支改用 readPVPPickupFromDb 从 PVP 系统 DB
  (plugin.getDb().getPvpInventoryBackup) 快照真实进场前背包到 pendingPVPPickup。
- restoreInventory PVP-owned 路径改用 pendingPVPPickup(已含真实物),
  不再二次读 DB(消除续24初版的「读DB+补快照」双重添加)。
- restoreInventory 净背包路径优先用 pendingPVPPickup 还原(真实净物),
  脏备份 b 仅兜底,杜绝 PVP 装备残留主世界。
This commit is contained in:
youpaishidifu
2026-11-18 10:57:44 +08:00
parent 9a49e38709
commit 30ff8cdfd3
+66 -65
View File
@@ -971,14 +971,22 @@ public class PVPTestManager implements Listener {
int stripped = 回收PVPSpecialEquip(p);
boolean allEmpty = isInventoryEmpty(p);
if (allEmpty) {
// ★ 续24:全空 = 真实背包已被 PVP 系统备份到 DB(进场时序:PVP 先备份+清空+发装备,
// 靶场随后看到的是 PVP 专属装备被回收后的空包)。此时必须从 PVP 系统 DB 取
// 真实进场前背包快照到 pendingPVPPickup,供还原后补回;否则还原=空。
inventoryOwnedByPvp.add(p.getUniqueId());
InventoryBackup dbPickup = readPVPPickupFromDb(p);
if (dbPickup != null) {
pendingPVPPickup.put(p.getUniqueId(), dbPickup);
}
int dbCount = 0;
if (dbPickup != null) for (ItemStack it : dbPickup.contents) if (it != null) dbCount++;
plugin.getLogger().info("[PVP测试] 玩家 " + p.getName()
+ " 进场背包回收 " + stripped + " 件PVP/练习装备后为空(真实背包归PVP系统DB),"
+ "本系统不备份,还原交由 PVP 系统 DB 备份");
+ "已从PVP DB快照真实净物品 " + dbCount + " 件,死亡/离场还原后将补回");
return;
}
// ★ 续24:备份前快照真实净物品(已排除PVP/练习装备)到 pendingPVPPickup,
// 供 restoreInventory 在还原后补回,防止还原读到脏背包(含PVP装备)导致空/混装。
// ★ 非全空:玩家带入了非PVP物品,直接快照当前活背包(已回收PVP/练习装备)
InventoryBackup pickupBackup = new InventoryBackup(
p.getInventory().getContents().clone(),
p.getInventory().getArmorContents().clone(),
@@ -999,6 +1007,28 @@ public class PVPTestManager implements Listener {
plugin.getLogger().info("[PVP测试] 已备份玩家 " + p.getName() + " 的背包");
}
/**
* ★ 第124批续24:从 PVP 系统 DB 读取该玩家进场前真实背包,转为 InventoryBackup。
* 仅当 PVPArenaManager 在进场时把真实背包写入了 DB(按玩家名)时有效。
* 读取失败/缺失 → 返回 null(调用方按 0 件处理,仅回收练习装备)。
*/
private InventoryBackup readPVPPickupFromDb(Player p) {
try {
String[] db = plugin.getDb().getPvpInventoryBackup(p.getName());
if (db == null || db.length < 5) return null;
ItemStack[] contents = deserializePvpItems(db[0]);
ItemStack[] armor = deserializePvpItems(db[1]);
ItemStack off = deserializePvpItem(db[2]);
int lvl = Integer.parseInt(db[3]);
float exp = Float.parseFloat(db[4]);
return new InventoryBackup(contents, armor, off, lvl, exp);
} catch (Exception ex) {
plugin.getLogger().warning("[PVP测试] 玩家 " + p.getName()
+ " 读PVP系统DB快照进场背包失败: " + ex);
return null;
}
}
/** 背包是否全空(主背包+盔甲+副手+经验全 0) */
private boolean isInventoryEmpty(Player p) {
for (ItemStack it : p.getInventory().getContents()) if (it != null) return false;
@@ -1013,53 +1043,26 @@ public class PVPTestManager implements Listener {
// → PVPTestManager 绝不能用「空备份」覆盖玩家背包;还原交给 PVPArenaManager.restoreInventory
// (它读 DB 真实进场前备份),这里只清练习生物、不动背包。
if (inventoryOwnedByPvp.remove(p.getUniqueId())) {
// ★ 续22:背包由 PVP 系统在进场时备份到 DB(按玩家名,真实进场前背包)。
// 这里直接读 PVP 系统 DB 备份还原,避免用本系统「空备份」覆盖真实背包。
// 还原成功后不删 DB(PVP 系统 forceLeaveArena 会兜底 deletePvpInventoryBackup),
// 只回收混入的练习装备(显示名含「练习」),防止练习装备带出主世界。
String[] pvpDbBackup = plugin.getDb().getPvpInventoryBackup(p.getName());
if (pvpDbBackup != null && pvpDbBackup.length >= 5) {
try {
ItemStack[] contents = deserializePvpItems(pvpDbBackup[0]);
ItemStack[] armor = deserializePvpItems(pvpDbBackup[1]);
ItemStack off = deserializePvpItem(pvpDbBackup[2]);
int lvl = Integer.parseInt(pvpDbBackup[3]);
float exp = Float.parseFloat(pvpDbBackup[4]);
p.getInventory().setContents(contents);
p.getInventory().setArmorContents(armor);
p.getInventory().setItemInOffHand(off);
p.setLevel(lvl);
p.setExp(exp);
int reclaimed = 回收PVPSpecialEquip(p);
plugin.getLogger().info("[PVP测试] 玩家 " + p.getName()
+ " 背包归PVP系统→读DB真实备份还原"
+ (reclaimed > 0 ? "(并回收 " + reclaimed + " 件练习/PVP装备)" : ""));
} catch (Exception ex) {
plugin.getLogger().warning("[PVP测试] 玩家 " + p.getName()
+ " 读PVP系统DB备份还原失败,回退仅回收练习装备: " + ex);
回收PVPSpecialEquip(p);
}
} else {
// PVP 系统已删 DB 备份(forceLeaveArena 先跑)→ 无法还原真实背包,仅回收练习装备
int reclaimed = 回收PVPSpecialEquip(p);
plugin.getLogger().warning("[PVP测试] 玩家 " + p.getName()
+ " 背包归PVP系统但DB备份已缺失,仅回收 " + reclaimed + " 件练习装备(真实背包需手动备份恢复)");
}
// ★ 续24:PVP-owned 路径也补回进场时快照的真实净物品
// ★ 续24:背包所有权归 PVP 系统。进场时 PVP 系统已把「真实进场前背包」写入 DB,
// backupInventory 时本系统已把该 DB 快照读入 pendingPVPPickup(见 readPVPPickupFromDb)。
// 还原时直接用 pendingPVPPickup(真实物品),避免再读 DB 造成双重添加;
// 最后只回收混入的练习装备。若快照缺失(DB 读取失败)→ 仅回收练习装备。
InventoryBackup pickup = pendingPVPPickup.remove(p.getUniqueId());
if (pickup != null) {
for (ItemStack it : pickup.contents) {
if (it != null) p.getInventory().addItem(it.clone());
}
p.getInventory().setContents(pickup.contents);
p.getInventory().setArmorContents(pickup.armor);
if (pickup.offHand != null) p.getInventory().setItemInOffHand(pickup.offHand.clone());
p.getInventory().setItemInOffHand(pickup.offHand != null ? pickup.offHand.clone() : null);
p.setLevel(pickup.level);
p.setExp(pickup.exp);
int pickupCount = 0;
for (ItemStack it : pickup.contents) if (it != null) pickupCount++;
plugin.getLogger().info("[PVP测试] 玩家 " + p.getName()
+ " PVP-owned路径已补回进场前真实净物品 " + pickupCount + " 件");
}
int reclaimed = 回收PVPSpecialEquip(p);
int pickupCount = 0;
if (pickup != null) for (ItemStack it : pickup.contents) if (it != null) pickupCount++;
plugin.getLogger().info("[PVP测试] 玩家 " + p.getName()
+ (pickup != null
? " PVP-owned路径已还原进场前真实背包 " + pickupCount + " 件"
: " PVP-owned路径快照缺失,仅回收练习装备")
+ (reclaimed > 0 ? "(并回收 " + reclaimed + " 件PVP/练习装备,防混回主世界)" : ""));
return;
}
InventoryBackup b = inventoryBackups.remove(p.getUniqueId());
@@ -1067,35 +1070,33 @@ public class PVPTestManager implements Listener {
plugin.getLogger().warning("[PVP测试] 未找到玩家 " + p.getName() + " 的背包备份");
return;
}
// ★ 续24:优先用 pendingPVPPickup(进场时快照的真实净物品,已排除PVP/练习装备)。
// b(脏备份) 仅作兜底:若快照缺失才还原脏备份并回收。避免脏PVP装备残留主世界。
InventoryBackup pickup = pendingPVPPickup.remove(p.getUniqueId());
if (pickup != null) {
p.getInventory().setContents(pickup.contents);
p.getInventory().setArmorContents(pickup.armor);
p.getInventory().setItemInOffHand(pickup.offHand != null ? pickup.offHand.clone() : null);
p.setLevel(pickup.level);
p.setExp(pickup.exp);
int pickupCount = 0;
for (ItemStack it : pickup.contents) if (it != null) pickupCount++;
int reclaimed = 回收PVPSpecialEquip(p);
plugin.getLogger().info("[PVP测试] 已还原玩家 " + p.getName() + " 的背包(快照净物 "
+ pickupCount + " 件)"
+ (reclaimed > 0 ? "(并回收 " + reclaimed + " 件PVP/练习装备,防混回主世界)" : ""));
} else {
// 快照缺失 → 兜底:还原脏备份 + 回收所有PVP/练习装备
p.getInventory().setContents(b.contents);
p.getInventory().setArmorContents(b.armor);
p.getInventory().setItemInOffHand(b.offHand);
p.setLevel(b.level);
p.setExp(b.exp);
// ★ 第124批续20(2026-11-17):还原后回收「PVP专属装备 + 练习装备」。
// /pvp test 时 PVPArenaManager 与 PVPTestManager 两套系统都会备份/发装备,
// 玩家死亡复活(onRespawn)/超时遣返(scheduleKickTimeout) 还原的是「进靶场那一刻
// 含 PVP 专属装备(末地合金镐等)的背包」,若不回收,PVP/练习装备会混回主世界背包
// (用户视频:死亡返回带出竞技场装备 + 背包混乱)。这里统一把带 PVP 标记的装备清掉。
int reclaimed = 回收PVPSpecialEquip(p);
// ★ 续24:补回进场时快照的真实净物品(PVP/练习装备被回收后补回真实物品)
InventoryBackup pickup = pendingPVPPickup.remove(p.getUniqueId());
if (pickup != null) {
for (ItemStack it : pickup.contents) {
if (it != null) p.getInventory().addItem(it.clone());
}
p.getInventory().setArmorContents(pickup.armor);
if (pickup.offHand != null) p.getInventory().setItemInOffHand(pickup.offHand.clone());
p.setLevel(pickup.level);
p.setExp(pickup.exp);
int pickupCount = 0;
for (ItemStack it : pickup.contents) if (it != null) pickupCount++;
plugin.getLogger().info("[PVP测试] 玩家 " + p.getName()
+ " 已补回进场前真实净物品 " + pickupCount + " 件(PVP/练习装备已回收)");
}
plugin.getLogger().info("[PVP测试] 已还原玩家 " + p.getName() + " 的背包"
plugin.getLogger().info("[PVP测试] 已还原玩家 " + p.getName() + " 的背包(兜底脏备份)"
+ (reclaimed > 0 ? "(并回收 " + reclaimed + " 件PVP/练习装备,防混回主世界)" : ""));
}
}
/**
* ★ 第124批续20:回收背包里的 PVP 专属装备(lore 含「PVP竞技场专属」)