| 1 | package nl.timfalken.copperkeys; |
| 2 | |
| 3 | import net.kyori.adventure.text.Component; |
| 4 | import net.kyori.adventure.text.format.NamedTextColor; |
| 5 | import net.kyori.adventure.text.format.TextDecoration; |
| 6 | import org.bukkit.Material; |
| 7 | import org.bukkit.NamespacedKey; |
| 8 | import org.bukkit.block.ShulkerBox; |
| 9 | import org.bukkit.inventory.Inventory; |
| 10 | import org.bukkit.inventory.ItemStack; |
| 11 | import org.bukkit.inventory.meta.BlockStateMeta; |
| 12 | import org.bukkit.inventory.meta.BundleMeta; |
| 13 | import org.bukkit.inventory.meta.ItemMeta; |
| 14 | import org.bukkit.persistence.PersistentDataContainer; |
| 15 | import org.bukkit.persistence.PersistentDataType; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | import java.util.UUID; |
| 20 | |
| 21 | final class KeyItems { |
| 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; |
| 25 | |
| 26 | private final CopperKeysPlugin plugin; |
| 27 | private final NamespacedKey markerKey; |
| 28 | private final NamespacedKey bindingKey; |
| 29 | private final NamespacedKey itemModel; |
| 30 | |
| 31 | KeyItems(CopperKeysPlugin plugin) { |
| 32 | this.plugin = plugin; |
| 33 | this.markerKey = new NamespacedKey(plugin, "key"); |
| 34 | this.bindingKey = new NamespacedKey(plugin, "binding"); |
| 35 | this.itemModel = new NamespacedKey("copperkeys", "key"); |
| 36 | } |
| 37 | |
| 38 | ItemStack createBlankKey() { |
| 39 | ItemStack item = new ItemStack(Material.CLOCK); |
| 40 | makeBlank(item); |
| 41 | return item; |
| 42 | } |
| 43 | |
| 44 | ItemStack createBoundKey(LockRecord lock) { |
| 45 | ItemStack item = new ItemStack(Material.CLOCK); |
| 46 | bind(item, lock); |
| 47 | return item; |
| 48 | } |
| 49 | |
| 50 | boolean isKey(ItemStack item) { |
| 51 | if (item == null || item.getType() != Material.CLOCK || !item.hasItemMeta()) { |
| 52 | return false; |
| 53 | } |
| 54 | Byte marker = item.getItemMeta().getPersistentDataContainer().get(markerKey, PersistentDataType.BYTE); |
| 55 | return marker != null && marker == (byte) 1; |
| 56 | } |
| 57 | |
| 58 | UUID getBinding(ItemStack item) { |
| 59 | if (!isKey(item)) { |
| 60 | return null; |
| 61 | } |
| 62 | String raw = item.getItemMeta().getPersistentDataContainer().get(bindingKey, PersistentDataType.STRING); |
| 63 | if (raw == null) { |
| 64 | return null; |
| 65 | } |
| 66 | try { |
| 67 | return UUID.fromString(raw); |
| 68 | } catch (IllegalArgumentException ignored) { |
| 69 | return null; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | boolean isBlank(ItemStack item) { |
| 74 | return isKey(item) && getBinding(item) == null; |
| 75 | } |
| 76 | |
| 77 | void bind(ItemStack item, LockRecord lock) { |
| 78 | ItemMeta meta = item.getItemMeta(); |
| 79 | applyCommon(meta); |
| 80 | meta.getPersistentDataContainer().set(bindingKey, PersistentDataType.STRING, lock.bindingId().toString()); |
| 81 | meta.itemName(Component.text("Key", NamedTextColor.WHITE)); |
| 82 | meta.lore(List.of( |
| 83 | Component.text("Unlocks " + lock.displayName(), NamedTextColor.GRAY) |
| 84 | .decoration(TextDecoration.ITALIC, false) |
| 85 | )); |
| 86 | item.setItemMeta(meta); |
| 87 | } |
| 88 | |
| 89 | void makeBlank(ItemStack item) { |
| 90 | ItemMeta meta = item.getItemMeta(); |
| 91 | applyCommon(meta); |
| 92 | meta.getPersistentDataContainer().remove(bindingKey); |
| 93 | meta.itemName(Component.text("Blank Key", NamedTextColor.WHITE)); |
| 94 | meta.lore(null); |
| 95 | item.setItemMeta(meta); |
| 96 | } |
| 97 | |
| 98 | private void applyCommon(ItemMeta meta) { |
| 99 | PersistentDataContainer pdc = meta.getPersistentDataContainer(); |
| 100 | pdc.set(markerKey, PersistentDataType.BYTE, (byte) 1); |
| 101 | meta.setMaxStackSize(KEY_MAX_STACK_SIZE); |
| 102 | meta.setItemModel(itemModel); |
| 103 | } |
| 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 | |
| 118 | boolean inventoryHasBinding(Inventory inventory, UUID bindingId) { |
| 119 | boolean[] found = {false}; |
| 120 | for (int slot = 0; slot < inventory.getSize(); slot++) { |
| 121 | ItemStack item = inventory.getItem(slot); |
| 122 | if (item == null) { |
| 123 | continue; |
| 124 | } |
| 125 | boolean changed = visit(item, 0, key -> { |
| 126 | boolean updated = migrateKeyFormat(key); |
| 127 | UUID keyBinding = getBinding(key); |
| 128 | if (keyBinding != null && !plugin.lockStore().exists(keyBinding)) { |
| 129 | makeBlank(key); |
| 130 | return true; |
| 131 | } |
| 132 | if (bindingId.equals(keyBinding)) { |
| 133 | found[0] = true; |
| 134 | } |
| 135 | return updated; |
| 136 | }); |
| 137 | if (changed) { |
| 138 | inventory.setItem(slot, item); |
| 139 | } |
| 140 | if (found[0]) { |
| 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | void normalizeInventory(Inventory inventory) { |
| 148 | for (int slot = 0; slot < inventory.getSize(); slot++) { |
| 149 | ItemStack item = inventory.getItem(slot); |
| 150 | if (item == null) { |
| 151 | continue; |
| 152 | } |
| 153 | boolean changed = visit(item, 0, key -> { |
| 154 | boolean updated = migrateKeyFormat(key); |
| 155 | UUID binding = getBinding(key); |
| 156 | if (binding != null && !plugin.lockStore().exists(binding)) { |
| 157 | makeBlank(key); |
| 158 | return true; |
| 159 | } |
| 160 | return updated; |
| 161 | }); |
| 162 | if (changed) { |
| 163 | inventory.setItem(slot, item); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void blankBindingInInventory(Inventory inventory, UUID bindingId) { |
| 169 | for (int slot = 0; slot < inventory.getSize(); slot++) { |
| 170 | ItemStack item = inventory.getItem(slot); |
| 171 | if (item == null) { |
| 172 | continue; |
| 173 | } |
| 174 | boolean changed = visit(item, 0, key -> { |
| 175 | boolean updated = migrateKeyFormat(key); |
| 176 | if (bindingId.equals(getBinding(key))) { |
| 177 | makeBlank(key); |
| 178 | return true; |
| 179 | } |
| 180 | return updated; |
| 181 | }); |
| 182 | if (changed) { |
| 183 | inventory.setItem(slot, item); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | boolean normalizeItem(ItemStack item) { |
| 189 | if (item == null) { |
| 190 | return false; |
| 191 | } |
| 192 | return visit(item, 0, key -> { |
| 193 | boolean updated = migrateKeyFormat(key); |
| 194 | UUID binding = getBinding(key); |
| 195 | if (binding != null && !plugin.lockStore().exists(binding)) { |
| 196 | makeBlank(key); |
| 197 | return true; |
| 198 | } |
| 199 | return updated; |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | private boolean visit(ItemStack item, int depth, KeyVisitor visitor) { |
| 204 | if (item == null || item.getType().isAir() || depth > MAX_NESTING_DEPTH) { |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | boolean changed = false; |
| 209 | if (isKey(item)) { |
| 210 | changed |= visitor.visit(item); |
| 211 | } |
| 212 | |
| 213 | ItemMeta meta = item.getItemMeta(); |
| 214 | if (meta instanceof BundleMeta bundleMeta) { |
| 215 | List<ItemStack> oldItems = bundleMeta.getItems(); |
| 216 | List<ItemStack> newItems = new ArrayList<>(oldItems.size()); |
| 217 | boolean nestedChanged = false; |
| 218 | for (ItemStack nested : oldItems) { |
| 219 | ItemStack copy = nested.clone(); |
| 220 | nestedChanged |= visit(copy, depth + 1, visitor); |
| 221 | newItems.add(copy); |
| 222 | } |
| 223 | if (nestedChanged) { |
| 224 | bundleMeta.setItems(newItems); |
| 225 | item.setItemMeta(bundleMeta); |
| 226 | changed = true; |
| 227 | } |
| 228 | } else if (meta instanceof BlockStateMeta blockStateMeta && blockStateMeta.getBlockState() instanceof ShulkerBox shulker) { |
| 229 | Inventory contents = shulker.getInventory(); |
| 230 | boolean nestedChanged = false; |
| 231 | for (int slot = 0; slot < contents.getSize(); slot++) { |
| 232 | ItemStack nested = contents.getItem(slot); |
| 233 | if (nested == null) { |
| 234 | continue; |
| 235 | } |
| 236 | ItemStack copy = nested.clone(); |
| 237 | if (visit(copy, depth + 1, visitor)) { |
| 238 | contents.setItem(slot, copy); |
| 239 | nestedChanged = true; |
| 240 | } |
| 241 | } |
| 242 | if (nestedChanged) { |
| 243 | blockStateMeta.setBlockState(shulker); |
| 244 | item.setItemMeta(blockStateMeta); |
| 245 | changed = true; |
| 246 | } |
| 247 | } |
| 248 | return changed; |
| 249 | } |
| 250 | |
| 251 | @FunctionalInterface |
| 252 | private interface KeyVisitor { |
| 253 | boolean visit(ItemStack key); |
| 254 | } |
| 255 | } |