玩家实测「每出进一次领地多给1个违禁品,不需要退出重进」, 115批的onQuit清暂存防不住在线出进循环。按用户思路上日志抓现行: 1. 没收时给每件物品盖PDC批次章(nanoTime),日志打: 玩家/领地/入口(栈顶5帧)/物品明细/数量/批次号/returnOnLeave 2. 退还前扫背包+副手:同批次带标物品仍在玩家手上=插件已提前 返还过→本批暂存丢弃不再还(防重复退还+1) 3. 退还日志:退给谁/领地/批次/数量/明细/拦截多少 4. 清暂存日志:退出时清多少件 5. 空没收日志:进了没收却没收到(排查入口异常) Paper 26.2 API变更:ItemStack.getPersistentDataContainer()返回 只读View,set走editPersistentDataContainer(Consumer)。
This commit is contained in:
@@ -13,6 +13,8 @@ import org.bukkit.event.entity.*;
|
||||
import org.bukkit.event.player.*;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.bukkit.event.block.BlockBurnEvent;
|
||||
@@ -112,6 +114,10 @@ public class AreaProtection implements Listener {
|
||||
// 玩家进入领地被没收时暂存;退出领地时若 returnOnLeave=true 则归还,否则清空(不退)
|
||||
public final Map<java.util.UUID, java.util.Map<String, java.util.List<org.bukkit.inventory.ItemStack>>> confiscatedStore
|
||||
= new ConcurrentHashMap<>();
|
||||
// ★ 第118批:违禁品批次打标 Key(PDC)——没收时给每件物品盖 nanoTime 批次章,
|
||||
// 退还前扫背包/副手:同批次已带标物品仍在玩家手上 = 插件已提前返还过 → 拦截重复退还
|
||||
private static final NamespacedKey CONFISCATE_BATCH_KEY =
|
||||
new NamespacedKey("sdf1_login", "conf_batch");
|
||||
// ★ 过户领地待输入新所有者:UUID → [landName]
|
||||
public final Map<java.util.UUID, String[]> pendingTransferInput = new ConcurrentHashMap<>();
|
||||
// ★ 用户组配置编辑待输入:UUID → [groupName, field("price"|"maxlands"|"priority")]
|
||||
@@ -13094,6 +13100,8 @@ public class AreaProtection implements Listener {
|
||||
|
||||
|
||||
public int handleConfiscate(Player p, AreaConfig ac) {
|
||||
// ★ 第118批:批次号(nanoTime,日志抓现行用)
|
||||
final long confBatch = System.nanoTime();
|
||||
// 管理员和领地所有者免检
|
||||
if (hasPermission(p, ac, PermissionLevel.OWNER)) return 0;
|
||||
Set<String> allItems = new HashSet<>(ac.confiscateItems);
|
||||
@@ -13121,7 +13129,10 @@ public class AreaProtection implements Listener {
|
||||
for (int i = 0; i < all.length; i++) {
|
||||
if (all[i] != null && all[i].getType() == mat) {
|
||||
count += all[i].getAmount();
|
||||
captured.add(all[i].clone());
|
||||
ItemStack cap = all[i].clone();
|
||||
final long batchTag = confBatch;
|
||||
cap.editPersistentDataContainer(view -> view.set(CONFISCATE_BATCH_KEY, PersistentDataType.LONG, Long.valueOf(batchTag)));
|
||||
captured.add(cap);
|
||||
all[i] = null;
|
||||
}
|
||||
}
|
||||
@@ -13130,7 +13141,10 @@ public class AreaProtection implements Listener {
|
||||
ItemStack off = p.getInventory().getItemInOffHand();
|
||||
if (off != null && off.getType() == mat) {
|
||||
count += off.getAmount();
|
||||
captured.add(off.clone());
|
||||
ItemStack capOff = off.clone();
|
||||
final long batchTagOff = confBatch;
|
||||
capOff.editPersistentDataContainer(view -> view.set(CONFISCATE_BATCH_KEY, PersistentDataType.LONG, Long.valueOf(batchTagOff)));
|
||||
captured.add(capOff);
|
||||
p.getInventory().setItemInOffHand(null);
|
||||
}
|
||||
|
||||
@@ -13154,6 +13168,13 @@ public class AreaProtection implements Listener {
|
||||
java.util.List<ItemStack> store = byPlayer.computeIfAbsent(ac.name,
|
||||
k -> new ArrayList<>());
|
||||
store.addAll(captured);
|
||||
// ★ 第118批:没收日志(抓现行)——谁、哪个领地、从哪个调用点、收多少、批次
|
||||
String capDetail = "";
|
||||
for (ItemStack cs : captured) capDetail += cs.getType() + "x" + cs.getAmount() + " ";
|
||||
plugin.getLogger().warning("[违禁品-没收] 玩家=" + p.getName() + " 领地=" + ac.name
|
||||
+ " 批次=" + confBatch + " 入口=" + confiscateCaller()
|
||||
+ " 物品=[" + capDetail.trim() + "] 数量=" + totalRemoved
|
||||
+ " returnOnLeave=" + ac.returnOnLeave);
|
||||
|
||||
String msg = (ac.confiscateMsg != null
|
||||
&& !ac.confiscateMsg.isEmpty())
|
||||
@@ -13189,6 +13210,11 @@ public class AreaProtection implements Listener {
|
||||
Bukkit.getConsoleSender(), finalCmd));
|
||||
}
|
||||
}
|
||||
if (totalRemoved == 0) {
|
||||
plugin.getLogger().info("[违禁品-没收] 玩家=" + p.getName() + " 领地=" + ac.name
|
||||
+ " 批次=" + confBatch + " 入口=" + confiscateCaller()
|
||||
+ " 本次没收=0件(背包无该领地违禁品,或已全部没收过)");
|
||||
}
|
||||
return totalRemoved;
|
||||
}
|
||||
public void clearBlacklists() {
|
||||
@@ -13216,12 +13242,35 @@ public class AreaProtection implements Listener {
|
||||
AreaConfig ac = areas.get(areaName);
|
||||
if (ac == null || !ac.returnOnLeave) {
|
||||
// 默认不退还:仅清空暂存
|
||||
plugin.getLogger().warning("[违禁品-退还] 玩家=" + p.getName() + " 领地=" + areaName
|
||||
+ " returnOnLeave=false 暂存" + store.size() + "件已丢弃(不退还)");
|
||||
return;
|
||||
}
|
||||
|
||||
// ★ 第118批:退还前扫背包/副手——暂存物品带批次标(PDC),若玩家手上已持有
|
||||
// 同批带标物品,说明这批已被插件返还过一次(提前返还/重复处理)→ 本批丢弃,
|
||||
// 不再重复退还,杜绝「每出进一次 +1」。
|
||||
int tagAlreadyHeld = 0;
|
||||
List<ItemStack> toGive = new ArrayList<>();
|
||||
for (ItemStack it : store) {
|
||||
if (it == null || it.getType().isAir()) continue;
|
||||
long tag = readConfiscateBatch(it);
|
||||
if (tag > 0 && playerHoldsBatchTag(p, tag)) {
|
||||
tagAlreadyHeld += it.getAmount();
|
||||
} else {
|
||||
toGive.add(it);
|
||||
}
|
||||
}
|
||||
if (tagAlreadyHeld > 0) {
|
||||
plugin.getLogger().warning("[违禁品-拦截] 玩家=" + p.getName() + " 领地=" + areaName
|
||||
+ " 检出提前返还!" + tagAlreadyHeld + "件暂存物在背包/副手中已带同批次标"
|
||||
+ "(插件此前已返还过)→ 本批不再重复退还,暂存丢弃");
|
||||
}
|
||||
if (toGive.isEmpty()) return;
|
||||
|
||||
int returned = 0;
|
||||
StringBuilder names = new StringBuilder();
|
||||
for (ItemStack it : store) {
|
||||
for (ItemStack it : toGive) {
|
||||
if (it == null || it.getType().isAir()) continue;
|
||||
java.util.HashMap<Integer, ItemStack> left =
|
||||
p.getInventory().addItem(it);
|
||||
@@ -13236,12 +13285,52 @@ public class AreaProtection implements Listener {
|
||||
names.append("§e").append(ItemNameResolver.toChinese(it.getType()))
|
||||
.append(" x").append(it.getAmount());
|
||||
}
|
||||
// ★ 第118批:退还日志(抓现行)——退给谁、哪个领地、多少、批次、拦截多少
|
||||
long firstTag = (toGive.isEmpty() ? -1 : readConfiscateBatch(toGive.get(0)));
|
||||
plugin.getLogger().warning("[违禁品-退还] 玩家=" + p.getName() + " 领地=" + areaName
|
||||
+ " 批次=" + firstTag + " 退还=" + returned + "件"
|
||||
+ " 明细=[" + names.toString() + "]"
|
||||
+ " 拦截=" + tagAlreadyHeld + "件");
|
||||
if (returned > 0) {
|
||||
p.sendMessage("§a§l[区域防护] §f你离开领地 §e" + areaName
|
||||
+ "§f,你的物品在此领地被设为违禁品,离开时已自动退还: " + names);
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 第118批:违禁品批次工具(PDC 打标 + 日志抓现行) ====================
|
||||
|
||||
/** 读取暂存物品上的没收批次号(无标返回 -1) */
|
||||
private static long readConfiscateBatch(ItemStack it) {
|
||||
if (it == null) return -1;
|
||||
Long v = it.getPersistentDataContainer().get(CONFISCATE_BATCH_KEY, PersistentDataType.LONG);
|
||||
return v == null ? -1 : v;
|
||||
}
|
||||
|
||||
/** 玩家背包(45)+副手是否持有带指定批次标的物品 */
|
||||
private boolean playerHoldsBatchTag(Player p, long batch) {
|
||||
for (ItemStack it : p.getInventory().getContents()) {
|
||||
if (it != null && readConfiscateBatch(it) == batch) return true;
|
||||
}
|
||||
ItemStack off = p.getInventory().getItemInOffHand();
|
||||
return off != null && readConfiscateBatch(off) == batch;
|
||||
}
|
||||
|
||||
/** 当前调用点(栈顶 5 帧),定位没收/退还是从哪个事件入口进来的 */
|
||||
private static String confiscateCaller() {
|
||||
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int printed = 0;
|
||||
for (int i = 2; i < st.length && printed < 5; i++) {
|
||||
String cl = st[i].getClassName();
|
||||
if (!cl.startsWith("Sdf1_login.AreaProtection") && !cl.startsWith("java.lang")) {
|
||||
sb.append(cl.substring(cl.lastIndexOf('.') + 1)).append('#').append(st[i].getMethodName())
|
||||
.append(':').append(st[i].getLineNumber()).append(' ');
|
||||
printed++;
|
||||
}
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* ★ 玩家退出时清空其违禁品暂存(防离线退还漏洞)。
|
||||
* <p>
|
||||
@@ -13257,7 +13346,16 @@ public class AreaProtection implements Listener {
|
||||
*/
|
||||
public void clearConfiscatedStore(UUID uid) {
|
||||
if (uid == null) return;
|
||||
confiscatedStore.remove(uid);
|
||||
java.util.Map<String, java.util.List<ItemStack>> m = confiscatedStore.remove(uid);
|
||||
if (m != null && !m.isEmpty()) {
|
||||
int total = 0;
|
||||
for (java.util.List<ItemStack> l : m.values())
|
||||
for (ItemStack it : l) if (it != null) total += it.getAmount();
|
||||
plugin.getLogger().warning("[违禁品-清暂存] 玩家UID=" + uid
|
||||
+ " 退出清暂存 " + total + "件(玩家="
|
||||
+ (Bukkit.getPlayer(uid) != null ? Bukkit.getPlayer(uid).getName() : "离线")
|
||||
+ ")入口=" + confiscateCaller());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
|
||||
Reference in New Issue
Block a user