BlueGit

timfa copper-key

Add shift-right-click unlock with matching key.

Removes the lock, blanks all linked keys, and shows an action bar confirmation.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-21 · 2 months ago

Hash
210480185cc2dde6489a898a53cb666640240a9c
Parent
69b64ee9
1 file changed +28 −4
src/main/java/nl/timfalken/copperkeys/CopperKeysPlugin.java +28 −4
@@ -154,13 +154,23 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
154 154
155 155 LockRecord existing = lockStore.get(BlockKey.of(clicked));
156 156 if (existing != null) {
157 Player player = event.getPlayer();
158 if (player.isSneaking()) {
159 ItemStack used = event.getItem();
160 UUID heldBinding = keyItems.getBinding(used);
161 if (heldBinding != null && heldBinding.equals(existing.bindingId())) {
162 event.setCancelled(true);
163 unlockBlock(player, clicked, existing);
164 return;
165 }
166 }
157 167 if (clicked.getBlockData() instanceof Openable openable && openable.isOpen()) {
158 168 // Closing is deliberately always allowed.
159 169 return;
160 170 }
161 if (!playerHasKey(event.getPlayer(), existing.bindingId())) {
171 if (!playerHasKey(player, existing.bindingId())) {
162 172 event.setCancelled(true);
163 denyOpen(event.getPlayer(), clicked);
173 denyOpen(player, clicked);
164 174 }
165 175 // Already locked: never bind a second key. Allow open/use only when the key check passed.
166 176 return;
@@ -438,8 +448,22 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
438 448 if (removed == null) {
439 449 return;
440 450 }
441 for (Player player : Bukkit.getOnlinePlayers()) {
442 keyItems.blankBindingInInventory(player.getInventory(), removed.bindingId());
451 blankLinkedKeys(removed.bindingId(), block);
452 }
453
454 private void unlockBlock(Player player, Block block, LockRecord lock) {
455 lockStore.remove(lock.bindingId());
456 blankLinkedKeys(lock.bindingId(), block);
457 player.sendActionBar(Component.text("Unlocked — all matching keys cleared", NamedTextColor.GOLD));
458 }
459
460 private void blankLinkedKeys(UUID bindingId, Block block) {
461 for (Player online : Bukkit.getOnlinePlayers()) {
462 keyItems.blankBindingInInventory(online.getInventory(), bindingId);
463 }
464 BlockState state = block.getState();
465 if (state instanceof InventoryHolder holder) {
466 keyItems.blankBindingInInventory(holder.getInventory(), bindingId);
443 467 }
444 468 }
445 469
@@ -154,13 +154,23 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
154
154
155 LockRecord existing = lockStore.get(BlockKey.of(clicked));
155 LockRecord existing = lockStore.get(BlockKey.of(clicked));
156 if (existing != null) {
156 if (existing != null) {
157 Player player = event.getPlayer();
158 if (player.isSneaking()) {
159 ItemStack used = event.getItem();
160 UUID heldBinding = keyItems.getBinding(used);
161 if (heldBinding != null && heldBinding.equals(existing.bindingId())) {
162 event.setCancelled(true);
163 unlockBlock(player, clicked, existing);
164 return;
165 }
166 }
157 if (clicked.getBlockData() instanceof Openable openable && openable.isOpen()) {
167 if (clicked.getBlockData() instanceof Openable openable && openable.isOpen()) {
158 // Closing is deliberately always allowed.
168 // Closing is deliberately always allowed.
159 return;
169 return;
160 }
170 }
161 if (!playerHasKey(event.getPlayer(), existing.bindingId())) {
171 if (!playerHasKey(player, existing.bindingId())) {
162 event.setCancelled(true);
172 event.setCancelled(true);
163 denyOpen(event.getPlayer(), clicked);
173 denyOpen(player, clicked);
164 }
174 }
165 // Already locked: never bind a second key. Allow open/use only when the key check passed.
175 // Already locked: never bind a second key. Allow open/use only when the key check passed.
166 return;
176 return;
@@ -438,8 +448,22 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
438 if (removed == null) {
448 if (removed == null) {
439 return;
449 return;
440 }
450 }
441 for (Player player : Bukkit.getOnlinePlayers()) {
451 blankLinkedKeys(removed.bindingId(), block);
442 keyItems.blankBindingInInventory(player.getInventory(), removed.bindingId());
452 }
453
454 private void unlockBlock(Player player, Block block, LockRecord lock) {
455 lockStore.remove(lock.bindingId());
456 blankLinkedKeys(lock.bindingId(), block);
457 player.sendActionBar(Component.text("Unlocked — all matching keys cleared", NamedTextColor.GOLD));
458 }
459
460 private void blankLinkedKeys(UUID bindingId, Block block) {
461 for (Player online : Bukkit.getOnlinePlayers()) {
462 keyItems.blankBindingInInventory(online.getInventory(), bindingId);
463 }
464 BlockState state = block.getState();
465 if (state instanceof InventoryHolder holder) {
466 keyItems.blankBindingInInventory(holder.getInventory(), bindingId);
443 }
467 }
444 }
468 }
445
469