Fix ghost key copies from mid-click inventory mutation.
Key presence checks are read-only; normalization runs after close and clients are resynced after rename/craft/unlock. Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -113,6 +113,7 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
113
113
for (Player online : Bukkit.getOnlinePlayers()) {
114
114
keyItems.applyNameInInventory(online.getInventory(), bindingId);
115
115
keyItems.applyNameInInventory(online.getEnderChest(), bindingId);
116
online.updateInventory();
116
117
}
117
118
}
118
119
@@ -276,10 +277,6 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
276
277
return;
277
278
}
278
279
}
279
ItemStack current = event.getCurrentItem();
280
if (current != null && keyItems.normalizeItem(current)) {
281
event.setCurrentItem(current);
282
}
283
280
}
284
281
285
282
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@@ -441,6 +438,9 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
441
438
if (!(event.getPlayer() instanceof Player player)) {
442
439
return;
443
440
}
441
// Normalize after the click/close transaction so sync never fights the client mid-click.
442
keyItems.normalizeInventory(player.getInventory());
443
444
444
LockRecord lock = lockForInventory(event.getInventory());
445
445
if (lock == null) {
446
446
return;
@@ -505,6 +505,7 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
505
505
for (ItemStack drop : leftover.values()) {
506
506
player.getWorld().dropItemNaturally(player.getLocation(), drop);
507
507
}
508
player.updateInventory();
508
509
});
509
510
}
510
511
@@ -606,6 +607,7 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
606
607
private void blankLinkedKeys(UUID bindingId, Block block) {
607
608
for (Player online : Bukkit.getOnlinePlayers()) {
608
609
keyItems.blankBindingInInventory(online.getInventory(), bindingId);
610
online.updateInventory();
609
611
}
610
612
BlockState state = block.getState();
611
613
if (state instanceof InventoryHolder holder) {
@@ -113,6 +113,7 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {113
for (Player online : Bukkit.getOnlinePlayers()) {113
for (Player online : Bukkit.getOnlinePlayers()) {114
keyItems.applyNameInInventory(online.getInventory(), bindingId);114
keyItems.applyNameInInventory(online.getInventory(), bindingId);115
keyItems.applyNameInInventory(online.getEnderChest(), bindingId);115
keyItems.applyNameInInventory(online.getEnderChest(), bindingId);116
online.updateInventory();116
}117
}117
}118
}118
119
@@ -276,10 +277,6 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {276
return;277
return;277
}278
}278
}279
}279
ItemStack current = event.getCurrentItem();280
if (current != null && keyItems.normalizeItem(current)) {281
event.setCurrentItem(current);282
}283
}280
}284
281
285
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)282
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)@@ -441,6 +438,9 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {441
if (!(event.getPlayer() instanceof Player player)) {438
if (!(event.getPlayer() instanceof Player player)) {442
return;439
return;443
}440
}441
// Normalize after the click/close transaction so sync never fights the client mid-click.442
keyItems.normalizeInventory(player.getInventory());443
444
LockRecord lock = lockForInventory(event.getInventory());444
LockRecord lock = lockForInventory(event.getInventory());445
if (lock == null) {445
if (lock == null) {446
return;446
return;@@ -505,6 +505,7 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {505
for (ItemStack drop : leftover.values()) {505
for (ItemStack drop : leftover.values()) {506
player.getWorld().dropItemNaturally(player.getLocation(), drop);506
player.getWorld().dropItemNaturally(player.getLocation(), drop);507
}507
}508
player.updateInventory();508
});509
});509
}510
}510
511
@@ -606,6 +607,7 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {606
private void blankLinkedKeys(UUID bindingId, Block block) {607
private void blankLinkedKeys(UUID bindingId, Block block) {607
for (Player online : Bukkit.getOnlinePlayers()) {608
for (Player online : Bukkit.getOnlinePlayers()) {608
keyItems.blankBindingInInventory(online.getInventory(), bindingId);609
keyItems.blankBindingInInventory(online.getInventory(), bindingId);610
online.updateInventory();609
}611
}610
BlockState state = block.getState();612
BlockState state = block.getState();611
if (state instanceof InventoryHolder holder) {613
if (state instanceof InventoryHolder holder) {@@ -186,24 +186,40 @@ final class KeyItems {
186
186
}
187
187
188
188
boolean inventoryHasBinding(Inventory inventory, UUID bindingId) {
189
boolean[] found = {false};
190
189
for (int slot = 0; slot < inventory.getSize(); slot++) {
191
190
ItemStack item = inventory.getItem(slot);
192
191
if (item == null) {
193
192
continue;
194
193
}
195
boolean changed = visit(item, 0, key -> {
196
boolean updated = syncKey(key);
197
if (bindingId.equals(getBinding(key))) {
198
found[0] = true;
194
if (containsBinding(item, 0, bindingId)) {
195
return true;
196
}
197
}
198
return false;
199
}
200
201
private boolean containsBinding(ItemStack item, int depth, UUID bindingId) {
202
if (item == null || item.getType().isAir() || depth > MAX_NESTING_DEPTH) {
203
return false;
204
}
205
if (isKey(item) && bindingId.equals(getBinding(item))) {
206
return true;
207
}
208
209
ItemMeta meta = item.getItemMeta();
210
if (meta instanceof BundleMeta bundleMeta) {
211
for (ItemStack nested : bundleMeta.getItems()) {
212
if (containsBinding(nested, depth + 1, bindingId)) {
213
return true;
199
214
}
200
return updated;
201
});
202
if (changed) {
203
inventory.setItem(slot, item);
204
215
}
205
if (found[0]) {
206
return true;
216
} else if (meta instanceof BlockStateMeta blockStateMeta
217
&& blockStateMeta.getBlockState() instanceof ShulkerBox shulker) {
218
Inventory contents = shulker.getInventory();
219
for (int slot = 0; slot < contents.getSize(); slot++) {
220
if (containsBinding(contents.getItem(slot), depth + 1, bindingId)) {
221
return true;
222
}
207
223
}
208
224
}
209
225
return false;
@@ -186,24 +186,40 @@ final class KeyItems {186
}186
}187
187
188
boolean inventoryHasBinding(Inventory inventory, UUID bindingId) {188
boolean inventoryHasBinding(Inventory inventory, UUID bindingId) {189
boolean[] found = {false};190
for (int slot = 0; slot < inventory.getSize(); slot++) {189
for (int slot = 0; slot < inventory.getSize(); slot++) {191
ItemStack item = inventory.getItem(slot);190
ItemStack item = inventory.getItem(slot);192
if (item == null) {191
if (item == null) {193
continue;192
continue;194
}193
}195
boolean changed = visit(item, 0, key -> {194
if (containsBinding(item, 0, bindingId)) {196
boolean updated = syncKey(key);195
return true;197
if (bindingId.equals(getBinding(key))) {196
}198
found[0] = true;197
}198
return false;199
}200
201
private boolean containsBinding(ItemStack item, int depth, UUID bindingId) {202
if (item == null || item.getType().isAir() || depth > MAX_NESTING_DEPTH) {203
return false;204
}205
if (isKey(item) && bindingId.equals(getBinding(item))) {206
return true;207
}208
209
ItemMeta meta = item.getItemMeta();210
if (meta instanceof BundleMeta bundleMeta) {211
for (ItemStack nested : bundleMeta.getItems()) {212
if (containsBinding(nested, depth + 1, bindingId)) {213
return true;199
}214
}200
return updated;201
});202
if (changed) {203
inventory.setItem(slot, item);204
}215
}205
if (found[0]) {216
} else if (meta instanceof BlockStateMeta blockStateMeta206
return true;217
&& blockStateMeta.getBlockState() instanceof ShulkerBox shulker) {218
Inventory contents = shulker.getInventory();219
for (int slot = 0; slot < contents.getSize(); slot++) {220
if (containsBinding(contents.getItem(slot), depth + 1, bindingId)) {221
return true;222
}207
}223
}208
}224
}209
return false;225
return false;