BlueGit

timfa copper-key

Allow keys with the same binding to stack and fit in bundles.

Raises max stack size to 64 for bundle capacity; blanks bind one-at-a-time from a stack.

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

timfa Tim Falken · pushed by timfa

Authored 2026-08-21 · 2 months ago

Hash
5495e2cd4107747a10f7c8c645956204dc3bec97
Parent
21048018
3 files changed +42 −10
README.md +2 −2
@@ -1,6 +1,6 @@
1 1 # CopperKeys
2 2
3 Paper 26.2 plugin that adds a non-stackable key item and event-driven locks for containers and openable blocks.
3 Paper 26.2 plugin that adds a key item and event-driven locks for containers and openable blocks.
4 4
5 5 ## Requirements
6 6
@@ -25,7 +25,7 @@ The supplied texture is the user's 16x16 key sprite and is selected through the
25 25 ## Behaviour
26 26
27 27 - Two copper nuggets placed diagonally craft a `Blank Key`.
28 - Keys have max stack size 1.
28 - Keys stack only with other keys that share the same binding (or other blank keys). Max stack size 64 so many fit in a Bundle.
29 29 - Right-click a supported unlocked block with a Blank Key to bind it.
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.
@@ -1,6 +1,6 @@
1# CopperKeys
1# CopperKeys
2
2
3Paper 26.2 plugin that adds a non-stackable key item and event-driven locks for containers and openable blocks.
3Paper 26.2 plugin that adds a key item and event-driven locks for containers and openable blocks.
4
4
5## Requirements
5## Requirements
6
6
@@ -25,7 +25,7 @@ The supplied texture is the user's 16x16 key sprite and is selected through the
25## Behaviour
25## Behaviour
26
26
27- Two copper nuggets placed diagonally craft a `Blank Key`.
27- Two copper nuggets placed diagonally craft a `Blank Key`.
28- Keys have max stack size 1.
28- Keys stack only with other keys that share the same binding (or other blank keys). Max stack size 64 so many fit in a Bundle.
29- Right-click a supported unlocked block with a Blank Key to bind it.
29- Right-click a supported unlocked block with a Blank Key to bind it.
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.
src/main/java/nl/timfalken/copperkeys/CopperKeysPlugin.java +16 −3
@@ -192,10 +192,23 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
192 192
193 193 LockRecord record = new LockRecord(UUID.randomUUID(), displayName(clicked), members);
194 194 lockStore.add(record);
195 keyItems.bind(used, record);
196 setUsedHandItem(event.getPlayer(), event.getHand(), used);
195
196 Player player = event.getPlayer();
197 ItemStack bound = used.clone();
198 bound.setAmount(1);
199 keyItems.bind(bound, record);
200 if (used.getAmount() > 1) {
201 used.setAmount(used.getAmount() - 1);
202 setUsedHandItem(player, event.getHand(), used);
203 var leftover = player.getInventory().addItem(bound);
204 for (ItemStack drop : leftover.values()) {
205 player.getWorld().dropItemNaturally(player.getLocation(), drop);
206 }
207 } else {
208 setUsedHandItem(player, event.getHand(), bound);
209 }
197 210 event.setCancelled(true); // The binding click itself never opens/toggles the target.
198 event.getPlayer().sendActionBar(Component.text("Key bound to " + record.displayName(), NamedTextColor.GOLD));
211 player.sendActionBar(Component.text("Key bound to " + record.displayName(), NamedTextColor.GOLD));
199 212 }
200 213
201 214 @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@@ -192,10 +192,23 @@ public final class CopperKeysPlugin extends JavaPlugin implements Listener {
192
192
193 LockRecord record = new LockRecord(UUID.randomUUID(), displayName(clicked), members);
193 LockRecord record = new LockRecord(UUID.randomUUID(), displayName(clicked), members);
194 lockStore.add(record);
194 lockStore.add(record);
195 keyItems.bind(used, record);
195
196 setUsedHandItem(event.getPlayer(), event.getHand(), used);
196 Player player = event.getPlayer();
197 ItemStack bound = used.clone();
198 bound.setAmount(1);
199 keyItems.bind(bound, record);
200 if (used.getAmount() > 1) {
201 used.setAmount(used.getAmount() - 1);
202 setUsedHandItem(player, event.getHand(), used);
203 var leftover = player.getInventory().addItem(bound);
204 for (ItemStack drop : leftover.values()) {
205 player.getWorld().dropItemNaturally(player.getLocation(), drop);
206 }
207 } else {
208 setUsedHandItem(player, event.getHand(), bound);
209 }
197 event.setCancelled(true); // The binding click itself never opens/toggles the target.
210 event.setCancelled(true); // The binding click itself never opens/toggles the target.
198 event.getPlayer().sendActionBar(Component.text("Key bound to " + record.displayName(), NamedTextColor.GOLD));
211 player.sendActionBar(Component.text("Key bound to " + record.displayName(), NamedTextColor.GOLD));
199 }
212 }
200
213
201 @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
214 @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
src/main/java/nl/timfalken/copperkeys/KeyItems.java +24 −5
@@ -20,6 +20,8 @@ import java.util.UUID;
20 20
21 21 final class KeyItems {
22 22 private static final int MAX_NESTING_DEPTH = 16;
23 /** Bundle occupancy is 1/maxStackSize; keys with the same binding share identical meta and can stack. */
24 private static final int KEY_MAX_STACK_SIZE = 64;
23 25
24 26 private final CopperKeysPlugin plugin;
25 27 private final NamespacedKey markerKey;
@@ -96,10 +98,23 @@ final class KeyItems {
96 98 private void applyCommon(ItemMeta meta) {
97 99 PersistentDataContainer pdc = meta.getPersistentDataContainer();
98 100 pdc.set(markerKey, PersistentDataType.BYTE, (byte) 1);
99 meta.setMaxStackSize(1);
101 meta.setMaxStackSize(KEY_MAX_STACK_SIZE);
100 102 meta.setItemModel(itemModel);
101 103 }
102 104
105 private boolean migrateKeyFormat(ItemStack key) {
106 ItemMeta meta = key.getItemMeta();
107 if (meta == null) {
108 return false;
109 }
110 if (meta.hasMaxStackSize() && meta.getMaxStackSize() == KEY_MAX_STACK_SIZE) {
111 return false;
112 }
113 applyCommon(meta);
114 key.setItemMeta(meta);
115 return true;
116 }
117
103 118 boolean inventoryHasBinding(Inventory inventory, UUID bindingId) {
104 119 boolean[] found = {false};
105 120 for (int slot = 0; slot < inventory.getSize(); slot++) {
@@ -108,6 +123,7 @@ final class KeyItems {
108 123 continue;
109 124 }
110 125 boolean changed = visit(item, 0, key -> {
126 boolean updated = migrateKeyFormat(key);
111 127 UUID keyBinding = getBinding(key);
112 128 if (keyBinding != null && !plugin.lockStore().exists(keyBinding)) {
113 129 makeBlank(key);
@@ -116,7 +132,7 @@ final class KeyItems {
116 132 if (bindingId.equals(keyBinding)) {
117 133 found[0] = true;
118 134 }
119 return false;
135 return updated;
120 136 });
121 137 if (changed) {
122 138 inventory.setItem(slot, item);
@@ -135,12 +151,13 @@ final class KeyItems {
135 151 continue;
136 152 }
137 153 boolean changed = visit(item, 0, key -> {
154 boolean updated = migrateKeyFormat(key);
138 155 UUID binding = getBinding(key);
139 156 if (binding != null && !plugin.lockStore().exists(binding)) {
140 157 makeBlank(key);
141 158 return true;
142 159 }
143 return false;
160 return updated;
144 161 });
145 162 if (changed) {
146 163 inventory.setItem(slot, item);
@@ -155,11 +172,12 @@ final class KeyItems {
155 172 continue;
156 173 }
157 174 boolean changed = visit(item, 0, key -> {
175 boolean updated = migrateKeyFormat(key);
158 176 if (bindingId.equals(getBinding(key))) {
159 177 makeBlank(key);
160 178 return true;
161 179 }
162 return false;
180 return updated;
163 181 });
164 182 if (changed) {
165 183 inventory.setItem(slot, item);
@@ -172,12 +190,13 @@ final class KeyItems {
172 190 return false;
173 191 }
174 192 return visit(item, 0, key -> {
193 boolean updated = migrateKeyFormat(key);
175 194 UUID binding = getBinding(key);
176 195 if (binding != null && !plugin.lockStore().exists(binding)) {
177 196 makeBlank(key);
178 197 return true;
179 198 }
180 return false;
199 return updated;
181 200 });
182 201 }
183 202
@@ -20,6 +20,8 @@ import java.util.UUID;
20
20
21final class KeyItems {
21final class KeyItems {
22 private static final int MAX_NESTING_DEPTH = 16;
22 private static final int MAX_NESTING_DEPTH = 16;
23 /** Bundle occupancy is 1/maxStackSize; keys with the same binding share identical meta and can stack. */
24 private static final int KEY_MAX_STACK_SIZE = 64;
23
25
24 private final CopperKeysPlugin plugin;
26 private final CopperKeysPlugin plugin;
25 private final NamespacedKey markerKey;
27 private final NamespacedKey markerKey;
@@ -96,10 +98,23 @@ final class KeyItems {
96 private void applyCommon(ItemMeta meta) {
98 private void applyCommon(ItemMeta meta) {
97 PersistentDataContainer pdc = meta.getPersistentDataContainer();
99 PersistentDataContainer pdc = meta.getPersistentDataContainer();
98 pdc.set(markerKey, PersistentDataType.BYTE, (byte) 1);
100 pdc.set(markerKey, PersistentDataType.BYTE, (byte) 1);
99 meta.setMaxStackSize(1);
101 meta.setMaxStackSize(KEY_MAX_STACK_SIZE);
100 meta.setItemModel(itemModel);
102 meta.setItemModel(itemModel);
101 }
103 }
102
104
105 private boolean migrateKeyFormat(ItemStack key) {
106 ItemMeta meta = key.getItemMeta();
107 if (meta == null) {
108 return false;
109 }
110 if (meta.hasMaxStackSize() && meta.getMaxStackSize() == KEY_MAX_STACK_SIZE) {
111 return false;
112 }
113 applyCommon(meta);
114 key.setItemMeta(meta);
115 return true;
116 }
117
103 boolean inventoryHasBinding(Inventory inventory, UUID bindingId) {
118 boolean inventoryHasBinding(Inventory inventory, UUID bindingId) {
104 boolean[] found = {false};
119 boolean[] found = {false};
105 for (int slot = 0; slot < inventory.getSize(); slot++) {
120 for (int slot = 0; slot < inventory.getSize(); slot++) {
@@ -108,6 +123,7 @@ final class KeyItems {
108 continue;
123 continue;
109 }
124 }
110 boolean changed = visit(item, 0, key -> {
125 boolean changed = visit(item, 0, key -> {
126 boolean updated = migrateKeyFormat(key);
111 UUID keyBinding = getBinding(key);
127 UUID keyBinding = getBinding(key);
112 if (keyBinding != null && !plugin.lockStore().exists(keyBinding)) {
128 if (keyBinding != null && !plugin.lockStore().exists(keyBinding)) {
113 makeBlank(key);
129 makeBlank(key);
@@ -116,7 +132,7 @@ final class KeyItems {
116 if (bindingId.equals(keyBinding)) {
132 if (bindingId.equals(keyBinding)) {
117 found[0] = true;
133 found[0] = true;
118 }
134 }
119 return false;
135 return updated;
120 });
136 });
121 if (changed) {
137 if (changed) {
122 inventory.setItem(slot, item);
138 inventory.setItem(slot, item);
@@ -135,12 +151,13 @@ final class KeyItems {
135 continue;
151 continue;
136 }
152 }
137 boolean changed = visit(item, 0, key -> {
153 boolean changed = visit(item, 0, key -> {
154 boolean updated = migrateKeyFormat(key);
138 UUID binding = getBinding(key);
155 UUID binding = getBinding(key);
139 if (binding != null && !plugin.lockStore().exists(binding)) {
156 if (binding != null && !plugin.lockStore().exists(binding)) {
140 makeBlank(key);
157 makeBlank(key);
141 return true;
158 return true;
142 }
159 }
143 return false;
160 return updated;
144 });
161 });
145 if (changed) {
162 if (changed) {
146 inventory.setItem(slot, item);
163 inventory.setItem(slot, item);
@@ -155,11 +172,12 @@ final class KeyItems {
155 continue;
172 continue;
156 }
173 }
157 boolean changed = visit(item, 0, key -> {
174 boolean changed = visit(item, 0, key -> {
175 boolean updated = migrateKeyFormat(key);
158 if (bindingId.equals(getBinding(key))) {
176 if (bindingId.equals(getBinding(key))) {
159 makeBlank(key);
177 makeBlank(key);
160 return true;
178 return true;
161 }
179 }
162 return false;
180 return updated;
163 });
181 });
164 if (changed) {
182 if (changed) {
165 inventory.setItem(slot, item);
183 inventory.setItem(slot, item);
@@ -172,12 +190,13 @@ final class KeyItems {
172 return false;
190 return false;
173 }
191 }
174 return visit(item, 0, key -> {
192 return visit(item, 0, key -> {
193 boolean updated = migrateKeyFormat(key);
175 UUID binding = getBinding(key);
194 UUID binding = getBinding(key);
176 if (binding != null && !plugin.lockStore().exists(binding)) {
195 if (binding != null && !plugin.lockStore().exists(binding)) {
177 makeBlank(key);
196 makeBlank(key);
178 return true;
197 return true;
179 }
198 }
180 return false;
199 return updated;
181 });
200 });
182 }
201 }
183
202