Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void onPlayerItemHeld(PlayerItemHeldEvent event) {
if (blockId != null) {
// 异步检测重复方块
plugin.getFoliaLib().getScheduler().runAsync(task -> {
detectAndRemoveDuplicates(player, blockId);
detectAndRemoveDuplicates(player, blockId, event.getNewSlot());
});
}
}
Expand All @@ -68,24 +68,26 @@ public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
if (blockId != null) {
// 异步检测重复方块
plugin.getFoliaLib().getScheduler().runAsync(task -> {
detectAndRemoveDuplicates(player, blockId);
// 副手不在Contents内
detectAndRemoveDuplicates(player, blockId, -1);
});
}
}
}

/**
* 检测并移除重复的魔法方块
* @param activeSlot 触发检测的无限方块,此方块不应该被删除
*/
private void detectAndRemoveDuplicates(Player currentPlayer, String blockId) {
private void detectAndRemoveDuplicates(Player currentPlayer, String blockId, int activeSlot) {
long startTime = System.nanoTime();
duplicateChecks.incrementAndGet();

int duplicatesRemovedInThisCheck = 0;

// 🆕 首先检查使用者自己背包中的重复方块(除了当前手持的)
duplicatesRemovedInThisCheck += removeDuplicatesFromCurrentPlayerInventory(
currentPlayer, blockId
currentPlayer, blockId, activeSlot
);

// 然后检查其他在线玩家的背包
Expand Down Expand Up @@ -164,7 +166,7 @@ private int removeDuplicatesFromInventory(Player player, String targetBlockId, S
/**
* 从当前玩家背包中移除重复方块(除了当前手持的方块)
*/
private int removeDuplicatesFromCurrentPlayerInventory(Player player, String targetBlockId) {
private int removeDuplicatesFromCurrentPlayerInventory(Player player, String targetBlockId, int activeSlot) {
int removedCount = 0;
ItemStack[] contents = player.getInventory().getContents();
ItemStack mainHandItem = player.getInventory().getItemInMainHand();
Expand All @@ -173,6 +175,9 @@ private int removeDuplicatesFromCurrentPlayerInventory(Player player, String tar
for (int i = 0; i < contents.length; i++) {
ItemStack item = contents[i];
if (item != null && plugin.getBlockManager().isMagicBlock(item)) {
if (i == activeSlot) {
continue;
}
// 跳过当前手持的方块(主手和副手)
if (item.equals(mainHandItem) || item.equals(offHandItem)) {
continue;
Expand Down