BlueGit

timfa copper-key

Allow toggling locked iron doors and trapdoors with the matching key.

Right-click with the bound key opens or closes them, since vanilla ignores hand use.

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

timfa Tim Falken · pushed by timfa

Authored 2026-08-21 · 2 months ago

Hash
7a41f78359711f17493250e15371df89f1459fb6
Parent
5495e2cd
2 files changed +53 −9
README.md +1 −0
@@ -30,6 +30,7 @@ The supplied texture is the user's 16x16 key sprite and is selected through the
30 30 - Bound keys display `Unlocks <block name>`.
31 31 - Containers require the matching key somewhere in the player's inventory, including recursively inside Bundles and Shulker Boxes.
32 32 - Locked doors/trapdoors/fence gates may always be closed, but cannot be opened without the matching key. Redstone may close them but may not open them.
33 - Locked iron doors and iron trapdoors can be toggled open/closed by right-clicking with the matching key in hand.
33 34 - Hopper transfers, hopper pickup/search and Copper Golem target validation are denied for locked containers.
34 35 - Breaking either half of a double chest or either half of a door breaks the binding.
35 36 - Keys found online are immediately blanked when their bound block is broken. Keys elsewhere are lazily blanked the next time the plugin encounters them.
@@ -30,6 +30,7 @@ The supplied texture is the user's 16x16 key sprite and is selected through the
30- Bound keys display `Unlocks <block name>`.
30- Bound keys display `Unlocks <block name>`.
31- Containers require the matching key somewhere in the player's inventory, including recursively inside Bundles and Shulker Boxes.
31- Containers require the matching key somewhere in the player's inventory, including recursively inside Bundles and Shulker Boxes.
32- Locked doors/trapdoors/fence gates may always be closed, but cannot be opened without the matching key. Redstone may close them but may not open them.
32- Locked doors/trapdoors/fence gates may always be closed, but cannot be opened without the matching key. Redstone may close them but may not open them.
33- Locked iron doors and iron trapdoors can be toggled open/closed by right-clicking with the matching key in hand.
33- Hopper transfers, hopper pickup/search and Copper Golem target validation are denied for locked containers.
34- Hopper transfers, hopper pickup/search and Copper Golem target validation are denied for locked containers.
34- Breaking either half of a double chest or either half of a door breaks the binding.
35- Breaking either half of a double chest or either half of a door breaks the binding.
35- Keys found online are immediately blanked when their bound block is broken. Keys elsewhere are lazily blanked the next time the plugin encounters them.
36- Keys found online are immediately blanked when their bound block is broken. Keys elsewhere are lazily blanked the next time the plugin encounters them.
src/main/java/nl/timfalken/copperkeys/CopperKeysPlugin.java +52 −9
@@ -9,6 +9,7 @@ import org.bukkit.Location;
9 9 import org.bukkit.Material;
10 10 import org.bukkit.NamespacedKey;
11 11 import org.bukkit.Particle;
12 import org.bukkit.Sound;
12 13 import org.bukkit.advancement.Advancement;
13 14 import org.bukkit.advancement.AdvancementProgress;
14 15 import org.bukkit.block.Block;
@@ -155,17 +156,25 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
155 156 LockRecord existing = lockStore.get(BlockKey.of(clicked));
156 157 if (existing != null) {
157 158 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 }
159 ItemStack used = event.getItem();
160 UUID heldBinding = keyItems.getBinding(used);
161 boolean holdingMatchingKey = heldBinding != null && heldBinding.equals(existing.bindingId());
162
163 if (player.isSneaking() && holdingMatchingKey) {
164 event.setCancelled(true);
165 unlockBlock(player, clicked, existing);
166 return;
167 }
168
169 // Iron doors/trapdoors do not open by hand in vanilla; toggle them with the matching key.
170 if (isKeyOperatedOpenable(clicked) && holdingMatchingKey) {
171 event.setCancelled(true);
172 toggleKeyOperatedOpenable(clicked);
173 return;
166 174 }
175
167 176 if (clicked.getBlockData() instanceof Openable openable && openable.isOpen()) {
168 // Closing is deliberately always allowed.
177 // Closing wooden openables is deliberately always allowed.
169 178 return;
170 179 }
171 180 if (!playerHasKey(player, existing.bindingId())) {
@@ -456,6 +465,40 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
456 465 return keyItems.createBoundKey(lock);
457 466 }
458 467
468 private boolean isKeyOperatedOpenable(Block block) {
469 Material type = block.getType();
470 return type == Material.IRON_DOOR || type == Material.IRON_TRAPDOOR;
471 }
472
473 private void toggleKeyOperatedOpenable(Block block) {
474 if (!(block.getBlockData() instanceof Openable openable)) {
475 return;
476 }
477 boolean open = !openable.isOpen();
478 Bisected.Half doorHalf = block.getBlockData() instanceof Door door ? door.getHalf() : null;
479
480 openable.setOpen(open);
481 block.setBlockData(openable, true);
482
483 if (doorHalf != null) {
484 Block other = doorHalf == Bisected.Half.BOTTOM
485 ? block.getRelative(0, 1, 0)
486 : block.getRelative(0, -1, 0);
487 if (other.getBlockData() instanceof Door otherDoor) {
488 otherDoor.setOpen(open);
489 other.setBlockData(otherDoor, false);
490 }
491 }
492
493 Sound sound;
494 if (block.getType() == Material.IRON_TRAPDOOR) {
495 sound = open ? Sound.BLOCK_IRON_TRAPDOOR_OPEN : Sound.BLOCK_IRON_TRAPDOOR_CLOSE;
496 } else {
497 sound = open ? Sound.BLOCK_IRON_DOOR_OPEN : Sound.BLOCK_IRON_DOOR_CLOSE;
498 }
499 block.getWorld().playSound(block.getLocation().add(0.5, 0.5, 0.5), sound, 1.0f, 1.0f);
500 }
501
459 502 private void handleDestroyed(Block block) {
460 503 LockRecord removed = lockStore.removeByBlock(BlockKey.of(block));
461 504 if (removed == null) {
@@ -9,6 +9,7 @@ import org.bukkit.Location;
9import org.bukkit.Material;
9import org.bukkit.Material;
10import org.bukkit.NamespacedKey;
10import org.bukkit.NamespacedKey;
11import org.bukkit.Particle;
11import org.bukkit.Particle;
12import org.bukkit.Sound;
12import org.bukkit.advancement.Advancement;
13import org.bukkit.advancement.Advancement;
13import org.bukkit.advancement.AdvancementProgress;
14import org.bukkit.advancement.AdvancementProgress;
14import org.bukkit.block.Block;
15import org.bukkit.block.Block;
@@ -155,17 +156,25 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
155 LockRecord existing = lockStore.get(BlockKey.of(clicked));
156 LockRecord existing = lockStore.get(BlockKey.of(clicked));
156 if (existing != null) {
157 if (existing != null) {
157 Player player = event.getPlayer();
158 Player player = event.getPlayer();
158 if (player.isSneaking()) {
159 ItemStack used = event.getItem();
159 ItemStack used = event.getItem();
160 UUID heldBinding = keyItems.getBinding(used);
160 UUID heldBinding = keyItems.getBinding(used);
161 boolean holdingMatchingKey = heldBinding != null && heldBinding.equals(existing.bindingId());
161 if (heldBinding != null && heldBinding.equals(existing.bindingId())) {
162
162 event.setCancelled(true);
163 if (player.isSneaking() && holdingMatchingKey) {
163 unlockBlock(player, clicked, existing);
164 event.setCancelled(true);
164 return;
165 unlockBlock(player, clicked, existing);
165 }
166 return;
167 }
168
169 // Iron doors/trapdoors do not open by hand in vanilla; toggle them with the matching key.
170 if (isKeyOperatedOpenable(clicked) && holdingMatchingKey) {
171 event.setCancelled(true);
172 toggleKeyOperatedOpenable(clicked);
173 return;
166 }
174 }
175
167 if (clicked.getBlockData() instanceof Openable openable && openable.isOpen()) {
176 if (clicked.getBlockData() instanceof Openable openable && openable.isOpen()) {
168 // Closing is deliberately always allowed.
177 // Closing wooden openables is deliberately always allowed.
169 return;
178 return;
170 }
179 }
171 if (!playerHasKey(player, existing.bindingId())) {
180 if (!playerHasKey(player, existing.bindingId())) {
@@ -456,6 +465,40 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
456 return keyItems.createBoundKey(lock);
465 return keyItems.createBoundKey(lock);
457 }
466 }
458
467
468 private boolean isKeyOperatedOpenable(Block block) {
469 Material type = block.getType();
470 return type == Material.IRON_DOOR || type == Material.IRON_TRAPDOOR;
471 }
472
473 private void toggleKeyOperatedOpenable(Block block) {
474 if (!(block.getBlockData() instanceof Openable openable)) {
475 return;
476 }
477 boolean open = !openable.isOpen();
478 Bisected.Half doorHalf = block.getBlockData() instanceof Door door ? door.getHalf() : null;
479
480 openable.setOpen(open);
481 block.setBlockData(openable, true);
482
483 if (doorHalf != null) {
484 Block other = doorHalf == Bisected.Half.BOTTOM
485 ? block.getRelative(0, 1, 0)
486 : block.getRelative(0, -1, 0);
487 if (other.getBlockData() instanceof Door otherDoor) {
488 otherDoor.setOpen(open);
489 other.setBlockData(otherDoor, false);
490 }
491 }
492
493 Sound sound;
494 if (block.getType() == Material.IRON_TRAPDOOR) {
495 sound = open ? Sound.BLOCK_IRON_TRAPDOOR_OPEN : Sound.BLOCK_IRON_TRAPDOOR_CLOSE;
496 } else {
497 sound = open ? Sound.BLOCK_IRON_DOOR_OPEN : Sound.BLOCK_IRON_DOOR_CLOSE;
498 }
499 block.getWorld().playSound(block.getLocation().add(0.5, 0.5, 0.5), sound, 1.0f, 1.0f);
500 }
501
459 private void handleDestroyed(Block block) {
502 private void handleDestroyed(Block block) {
460 LockRecord removed = lockStore.removeByBlock(BlockKey.of(block));
503 LockRecord removed = lockStore.removeByBlock(BlockKey.of(block));
461 if (removed == null) {
504 if (removed == null) {