When using InventoryClickEvent to modify item meta, it writes it back only if moving between different inventories.
Example: Move item from player inventory to a chest, data is being written. Move item from player inventory to another slot, data is not written.
Sample code:
@EventHandler(priority = EventPriority.HIGHEST) public void onInventoryClickEvent(InventoryClickEvent event) { ItemStack item = event.getCurrentItem(); if(item == null) { return; } ItemMeta meta = item.getItemMeta(); if(meta == null) return; PersistentDataContainer dataContainer = meta.getPersistentDataContainer(); NamespacedKey key = NamespacedKey.fromString("testkey"); if(key == null) return; dataContainer.set(key, PersistentDataType.LONG, 123L); }
To check for the data, get the item in hand and use
/data get entity @s SelectedItem
Reproducing with attached plugin:
Get any item, try to move it inside own inventory, it will print in chat "Read: null", data was not written, as reading it returned null. If you move it into another inventory and try to move it again it will print "Read: 1", data was written, as reading it returned the data.
Edit: If you setCurrentItem to current item, it doesn't work too. But when you set it and cancel the event, item gets updated.