| 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 net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; |
| 7 | import org.bukkit.Material; |
| 8 | import org.bukkit.NamespacedKey; |
| 9 | import org.bukkit.block.ShulkerBox; |
| 10 | import org.bukkit.inventory.Inventory; |
| 11 | import org.bukkit.inventory.ItemStack; |
| 12 | import org.bukkit.inventory.meta.BlockStateMeta; |
| 13 | import org.bukkit.inventory.meta.BundleMeta; |
| 14 | import org.bukkit.inventory.meta.ItemMeta; |
| 15 | import org.bukkit.persistence.PersistentDataContainer; |
| 16 | import org.bukkit.persistence.PersistentDataType; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
| 20 | import java.util.UUID; |
| 21 | |
| 22 | final class KeyItems { |
| 23 | private static final int MAX_NESTING_DEPTH = 16; |
| 24 | static final int MAX_KEY_NAME_LENGTH = 50; |
| 25 | /** Bundle occupancy is 1/maxStackSize; keys with the same binding share identical meta and can stack. */ |
| 26 | private static final int KEY_MAX_STACK_SIZE = 64; |
| 27 | |
| 28 | private final CopperKeysPlugin plugin; |
| 29 | private final NamespacedKey markerKey; |
| 30 | private final NamespacedKey bindingKey; |
| 31 | private final NamespacedKey itemModel; |
| 32 | |
| 33 | KeyItems(CopperKeysPlugin plugin) { |
| 34 | this.plugin = plugin; |
| 35 | this.markerKey = new NamespacedKey(plugin, "key"); |
| 36 | this.bindingKey = new NamespacedKey(plugin, "binding"); |
| 37 | this.itemModel = new NamespacedKey("copperkeys", "key"); |
| 38 | } |
| 39 | |
| 40 | ItemStack createBlankKey() { |
| 41 | ItemStack item = new ItemStack(Material.CLOCK); |
| 42 | makeBlank(item); |
| 43 | return item; |
| 44 | } |
| 45 | |
| 46 | ItemStack createBoundKey(LockRecord lock) { |
| 47 | ItemStack item = new ItemStack(Material.CLOCK); |
| 48 | bind(item, lock); |
| 49 | return item; |
| 50 | } |
| 51 | |
| 52 | boolean isKey(ItemStack item) { |
| 53 | if (item == null || item.getType() != Material.CLOCK || !item.hasItemMeta()) { |
| 54 | return false; |
| 55 | } |
| 56 | Byte marker = item.getItemMeta().getPersistentDataContainer().get(markerKey, PersistentDataType.BYTE); |
| 57 | return marker != null && marker == (byte) 1; |
| 58 | } |
| 59 | |
| 60 | UUID getBinding(ItemStack item) { |
| 61 | if (!isKey(item)) { |
| 62 | return null; |
| 63 | } |
| 64 | String raw = item.getItemMeta().getPersistentDataContainer().get(bindingKey, PersistentDataType.STRING); |
| 65 | if (raw == null) { |
| 66 | return null; |
| 67 | } |
| 68 | try { |
| 69 | return UUID.fromString(raw); |
| 70 | } catch (IllegalArgumentException ignored) { |
| 71 | return null; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | boolean isBlank(ItemStack item) { |
| 76 | return isKey(item) && getBinding(item) == null; |
| 77 | } |
| 78 | |
| 79 | void bind(ItemStack item, LockRecord lock) { |
| 80 | applyBoundAppearance(item, lock); |
| 81 | } |
| 82 | |
| 83 | void applyBoundAppearance(ItemStack item, LockRecord lock) { |
| 84 | ItemMeta meta = item.getItemMeta(); |
| 85 | applyCommon(meta); |
| 86 | meta.getPersistentDataContainer().set(bindingKey, PersistentDataType.STRING, lock.bindingId().toString()); |
| 87 | meta.itemName(Component.text(lock.keyName(), NamedTextColor.WHITE)); |
| 88 | meta.customName(null); |
| 89 | meta.lore(List.of( |
| 90 | Component.text("Unlocks " + lock.displayName(), NamedTextColor.GRAY) |
| 91 | .decoration(TextDecoration.ITALIC, false) |
| 92 | )); |
| 93 | item.setItemMeta(meta); |
| 94 | } |
| 95 | |
| 96 | void makeBlank(ItemStack item) { |
| 97 | ItemMeta meta = item.getItemMeta(); |
| 98 | applyCommon(meta); |
| 99 | meta.getPersistentDataContainer().remove(bindingKey); |
| 100 | meta.itemName(Component.text("Blank Key", NamedTextColor.WHITE)); |
| 101 | meta.customName(null); |
| 102 | meta.lore(null); |
| 103 | item.setItemMeta(meta); |
| 104 | } |
| 105 | |
| 106 | private void applyCommon(ItemMeta meta) { |
| 107 | PersistentDataContainer pdc = meta.getPersistentDataContainer(); |
| 108 | pdc.set(markerKey, PersistentDataType.BYTE, (byte) 1); |
| 109 | meta.setMaxStackSize(KEY_MAX_STACK_SIZE); |
| 110 | meta.setItemModel(itemModel); |
| 111 | } |
| 112 | |
| 113 | private boolean syncKey(ItemStack key) { |
| 114 | boolean updated = migrateKeyFormat(key); |
| 115 | UUID binding = getBinding(key); |
| 116 | if (binding == null) { |
| 117 | // Blank keys must never keep a custom name. |
| 118 | ItemMeta meta = key.getItemMeta(); |
| 119 | if (meta != null && meta.hasCustomName()) { |
| 120 | meta.customName(null); |
| 121 | meta.itemName(Component.text("Blank Key", NamedTextColor.WHITE)); |
| 122 | key.setItemMeta(meta); |
| 123 | return true; |
| 124 | } |
| 125 | return updated; |
| 126 | } |
| 127 | LockRecord lock = plugin.lockStore().get(binding); |
| 128 | if (lock == null) { |
| 129 | makeBlank(key); |
| 130 | return true; |
| 131 | } |
| 132 | if (!matchesBoundAppearance(key, lock)) { |
| 133 | applyBoundAppearance(key, lock); |
| 134 | return true; |
| 135 | } |
| 136 | return updated; |
| 137 | } |
| 138 | |
| 139 | private boolean matchesBoundAppearance(ItemStack key, LockRecord lock) { |
| 140 | ItemMeta meta = key.getItemMeta(); |
| 141 | if (meta == null || meta.hasCustomName()) { |
| 142 | return false; |
| 143 | } |
| 144 | Component itemName = meta.itemName(); |
| 145 | if (itemName == null) { |
| 146 | return false; |
| 147 | } |
| 148 | String plain = PlainTextComponentSerializer.plainText().serialize(itemName); |
| 149 | return lock.keyName().equals(plain); |
| 150 | } |
| 151 | |
| 152 | private boolean migrateKeyFormat(ItemStack key) { |
| 153 | ItemMeta meta = key.getItemMeta(); |
| 154 | if (meta == null) { |
| 155 | return false; |
| 156 | } |
| 157 | if (meta.hasMaxStackSize() && meta.getMaxStackSize() == KEY_MAX_STACK_SIZE) { |
| 158 | return false; |
| 159 | } |
| 160 | applyCommon(meta); |
| 161 | key.setItemMeta(meta); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | void applyNameInInventory(Inventory inventory, UUID bindingId) { |
| 166 | LockRecord lock = plugin.lockStore().get(bindingId); |
| 167 | if (lock == null) { |
| 168 | return; |
| 169 | } |
| 170 | for (int slot = 0; slot < inventory.getSize(); slot++) { |
| 171 | ItemStack item = inventory.getItem(slot); |
| 172 | if (item == null) { |
| 173 | continue; |
| 174 | } |
| 175 | boolean changed = visit(item, 0, key -> { |
| 176 | if (bindingId.equals(getBinding(key))) { |
| 177 | applyBoundAppearance(key, lock); |
| 178 | return true; |
| 179 | } |
| 180 | return false; |
| 181 | }); |
| 182 | if (changed) { |
| 183 | inventory.setItem(slot, item); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | boolean inventoryHasBinding(Inventory inventory, UUID bindingId) { |
| 189 | boolean[] found = {false}; |
| 190 | for (int slot = 0; slot < inventory.getSize(); slot++) { |
| 191 | ItemStack item = inventory.getItem(slot); |
| 192 | if (item == null) { |
| 193 | continue; |
| 194 | } |
| 195 | boolean changed = visit(item, 0, key -> { |
| 196 | boolean updated = syncKey(key); |
| 197 | if (bindingId.equals(getBinding(key))) { |
| 198 | found[0] = true; |
| 199 | } |
| 200 | return updated; |
| 201 | }); |
| 202 | if (changed) { |
| 203 | inventory.setItem(slot, item); |
| 204 | } |
| 205 | if (found[0]) { |
| 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | void normalizeInventory(Inventory inventory) { |
| 213 | for (int slot = 0; slot < inventory.getSize(); slot++) { |
| 214 | ItemStack item = inventory.getItem(slot); |
| 215 | if (item == null) { |
| 216 | continue; |
| 217 | } |
| 218 | boolean changed = visit(item, 0, this::syncKey); |
| 219 | if (changed) { |
| 220 | inventory.setItem(slot, item); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void blankBindingInInventory(Inventory inventory, UUID bindingId) { |
| 226 | for (int slot = 0; slot < inventory.getSize(); slot++) { |
| 227 | ItemStack item = inventory.getItem(slot); |
| 228 | if (item == null) { |
| 229 | continue; |
| 230 | } |
| 231 | boolean changed = visit(item, 0, key -> { |
| 232 | boolean updated = migrateKeyFormat(key); |
| 233 | if (bindingId.equals(getBinding(key))) { |
| 234 | makeBlank(key); |
| 235 | return true; |
| 236 | } |
| 237 | return updated; |
| 238 | }); |
| 239 | if (changed) { |
| 240 | inventory.setItem(slot, item); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | boolean normalizeItem(ItemStack item) { |
| 246 | if (item == null) { |
| 247 | return false; |
| 248 | } |
| 249 | return visit(item, 0, this::syncKey); |
| 250 | } |
| 251 | |
| 252 | private boolean visit(ItemStack item, int depth, KeyVisitor visitor) { |
| 253 | if (item == null || item.getType().isAir() || depth > MAX_NESTING_DEPTH) { |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | boolean changed = false; |
| 258 | if (isKey(item)) { |
| 259 | changed |= visitor.visit(item); |
| 260 | } |
| 261 | |
| 262 | ItemMeta meta = item.getItemMeta(); |
| 263 | if (meta instanceof BundleMeta bundleMeta) { |
| 264 | List<ItemStack> oldItems = bundleMeta.getItems(); |
| 265 | List<ItemStack> newItems = new ArrayList<>(oldItems.size()); |
| 266 | boolean nestedChanged = false; |
| 267 | for (ItemStack nested : oldItems) { |
| 268 | ItemStack copy = nested.clone(); |
| 269 | nestedChanged |= visit(copy, depth + 1, visitor); |
| 270 | newItems.add(copy); |
| 271 | } |
| 272 | if (nestedChanged) { |
| 273 | bundleMeta.setItems(newItems); |
| 274 | item.setItemMeta(bundleMeta); |
| 275 | changed = true; |
| 276 | } |
| 277 | } else if (meta instanceof BlockStateMeta blockStateMeta |
| 278 | && blockStateMeta.getBlockState() instanceof ShulkerBox shulker) { |
| 279 | Inventory contents = shulker.getInventory(); |
| 280 | boolean nestedChanged = false; |
| 281 | for (int slot = 0; slot < contents.getSize(); slot++) { |
| 282 | ItemStack nested = contents.getItem(slot); |
| 283 | if (nested == null) { |
| 284 | continue; |
| 285 | } |
| 286 | ItemStack copy = nested.clone(); |
| 287 | if (visit(copy, depth + 1, visitor)) { |
| 288 | contents.setItem(slot, copy); |
| 289 | nestedChanged = true; |
| 290 | } |
| 291 | } |
| 292 | if (nestedChanged) { |
| 293 | blockStateMeta.setBlockState(shulker); |
| 294 | item.setItemMeta(blockStateMeta); |
| 295 | changed = true; |
| 296 | } |
| 297 | } |
| 298 | return changed; |
| 299 | } |
| 300 | |
| 301 | @FunctionalInterface |
| 302 | private interface KeyVisitor { |
| 303 | boolean visit(ItemStack key); |
| 304 | } |
| 305 | } |