package nl.timfalken.copperkeys; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.block.ShulkerBox; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BlockStateMeta; import org.bukkit.inventory.meta.BundleMeta; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; import java.util.ArrayList; import java.util.List; import java.util.UUID; final class KeyItems { private static final int MAX_NESTING_DEPTH = 16; static final int MAX_KEY_NAME_LENGTH = 50; /** Bundle occupancy is 1/maxStackSize; keys with the same binding share identical meta and can stack. */ private static final int KEY_MAX_STACK_SIZE = 64; private final CopperKeysPlugin plugin; private final NamespacedKey markerKey; private final NamespacedKey bindingKey; private final NamespacedKey itemModel; KeyItems(CopperKeysPlugin plugin) { this.plugin = plugin; this.markerKey = new NamespacedKey(plugin, "key"); this.bindingKey = new NamespacedKey(plugin, "binding"); this.itemModel = new NamespacedKey("copperkeys", "key"); } ItemStack createBlankKey() { ItemStack item = new ItemStack(Material.CLOCK); makeBlank(item); return item; } ItemStack createBoundKey(LockRecord lock) { ItemStack item = new ItemStack(Material.CLOCK); bind(item, lock); return item; } boolean isKey(ItemStack item) { if (item == null || item.getType() != Material.CLOCK || !item.hasItemMeta()) { return false; } Byte marker = item.getItemMeta().getPersistentDataContainer().get(markerKey, PersistentDataType.BYTE); return marker != null && marker == (byte) 1; } UUID getBinding(ItemStack item) { if (!isKey(item)) { return null; } String raw = item.getItemMeta().getPersistentDataContainer().get(bindingKey, PersistentDataType.STRING); if (raw == null) { return null; } try { return UUID.fromString(raw); } catch (IllegalArgumentException ignored) { return null; } } boolean isBlank(ItemStack item) { return isKey(item) && getBinding(item) == null; } void bind(ItemStack item, LockRecord lock) { applyBoundAppearance(item, lock); } void applyBoundAppearance(ItemStack item, LockRecord lock) { ItemMeta meta = item.getItemMeta(); applyCommon(meta); meta.getPersistentDataContainer().set(bindingKey, PersistentDataType.STRING, lock.bindingId().toString()); meta.itemName(Component.text(lock.keyName(), NamedTextColor.WHITE)); meta.customName(null); meta.lore(List.of( Component.text("Unlocks " + lock.displayName(), NamedTextColor.GRAY) .decoration(TextDecoration.ITALIC, false) )); item.setItemMeta(meta); } void makeBlank(ItemStack item) { ItemMeta meta = item.getItemMeta(); applyCommon(meta); meta.getPersistentDataContainer().remove(bindingKey); meta.itemName(Component.text("Blank Key", NamedTextColor.WHITE)); meta.customName(null); meta.lore(null); item.setItemMeta(meta); } private void applyCommon(ItemMeta meta) { PersistentDataContainer pdc = meta.getPersistentDataContainer(); pdc.set(markerKey, PersistentDataType.BYTE, (byte) 1); meta.setMaxStackSize(KEY_MAX_STACK_SIZE); meta.setItemModel(itemModel); } private boolean syncKey(ItemStack key) { boolean updated = migrateKeyFormat(key); UUID binding = getBinding(key); if (binding == null) { // Blank keys must never keep a custom name. ItemMeta meta = key.getItemMeta(); if (meta != null && meta.hasCustomName()) { meta.customName(null); meta.itemName(Component.text("Blank Key", NamedTextColor.WHITE)); key.setItemMeta(meta); return true; } return updated; } LockRecord lock = plugin.lockStore().get(binding); if (lock == null) { makeBlank(key); return true; } if (!matchesBoundAppearance(key, lock)) { applyBoundAppearance(key, lock); return true; } return updated; } private boolean matchesBoundAppearance(ItemStack key, LockRecord lock) { ItemMeta meta = key.getItemMeta(); if (meta == null || meta.hasCustomName()) { return false; } Component itemName = meta.itemName(); if (itemName == null) { return false; } String plain = PlainTextComponentSerializer.plainText().serialize(itemName); return lock.keyName().equals(plain); } private boolean migrateKeyFormat(ItemStack key) { ItemMeta meta = key.getItemMeta(); if (meta == null) { return false; } if (meta.hasMaxStackSize() && meta.getMaxStackSize() == KEY_MAX_STACK_SIZE) { return false; } applyCommon(meta); key.setItemMeta(meta); return true; } void applyNameInInventory(Inventory inventory, UUID bindingId) { LockRecord lock = plugin.lockStore().get(bindingId); if (lock == null) { return; } for (int slot = 0; slot < inventory.getSize(); slot++) { ItemStack item = inventory.getItem(slot); if (item == null) { continue; } boolean changed = visit(item, 0, key -> { if (bindingId.equals(getBinding(key))) { applyBoundAppearance(key, lock); return true; } return false; }); if (changed) { inventory.setItem(slot, item); } } } boolean inventoryHasBinding(Inventory inventory, UUID bindingId) { boolean[] found = {false}; for (int slot = 0; slot < inventory.getSize(); slot++) { ItemStack item = inventory.getItem(slot); if (item == null) { continue; } boolean changed = visit(item, 0, key -> { boolean updated = syncKey(key); if (bindingId.equals(getBinding(key))) { found[0] = true; } return updated; }); if (changed) { inventory.setItem(slot, item); } if (found[0]) { return true; } } return false; } void normalizeInventory(Inventory inventory) { for (int slot = 0; slot < inventory.getSize(); slot++) { ItemStack item = inventory.getItem(slot); if (item == null) { continue; } boolean changed = visit(item, 0, this::syncKey); if (changed) { inventory.setItem(slot, item); } } } void blankBindingInInventory(Inventory inventory, UUID bindingId) { for (int slot = 0; slot < inventory.getSize(); slot++) { ItemStack item = inventory.getItem(slot); if (item == null) { continue; } boolean changed = visit(item, 0, key -> { boolean updated = migrateKeyFormat(key); if (bindingId.equals(getBinding(key))) { makeBlank(key); return true; } return updated; }); if (changed) { inventory.setItem(slot, item); } } } boolean normalizeItem(ItemStack item) { if (item == null) { return false; } return visit(item, 0, this::syncKey); } private boolean visit(ItemStack item, int depth, KeyVisitor visitor) { if (item == null || item.getType().isAir() || depth > MAX_NESTING_DEPTH) { return false; } boolean changed = false; if (isKey(item)) { changed |= visitor.visit(item); } ItemMeta meta = item.getItemMeta(); if (meta instanceof BundleMeta bundleMeta) { List oldItems = bundleMeta.getItems(); List newItems = new ArrayList<>(oldItems.size()); boolean nestedChanged = false; for (ItemStack nested : oldItems) { ItemStack copy = nested.clone(); nestedChanged |= visit(copy, depth + 1, visitor); newItems.add(copy); } if (nestedChanged) { bundleMeta.setItems(newItems); item.setItemMeta(bundleMeta); changed = true; } } else if (meta instanceof BlockStateMeta blockStateMeta && blockStateMeta.getBlockState() instanceof ShulkerBox shulker) { Inventory contents = shulker.getInventory(); boolean nestedChanged = false; for (int slot = 0; slot < contents.getSize(); slot++) { ItemStack nested = contents.getItem(slot); if (nested == null) { continue; } ItemStack copy = nested.clone(); if (visit(copy, depth + 1, visitor)) { contents.setItem(slot, copy); nestedChanged = true; } } if (nestedChanged) { blockStateMeta.setBlockState(shulker); item.setItemMeta(blockStateMeta); changed = true; } } return changed; } @FunctionalInterface private interface KeyVisitor { boolean visit(ItemStack key); } }