[SPIGOT-6467] InventoryClickEvent does not always write meta Created: 23/May/21  Updated: 23/May/21  Resolved: 23/May/21

Status: Closed
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major
Reporter: Daniel Assignee: Unassigned
Resolution: Invalid Votes: 0
Labels: 1.16.5, API, spigot

Attachments: File BugPlugin-1.0-SNAPSHOT.jar    
Version: This server is running CraftBukkit version 3083-Spigot-9fb885e-9c7acb6 (MC: 1.16.5) (Implementing API version 1.16.5-R0.1-SNAPSHOT)
Guidelines Read: Yes

 Description   

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.



 Comments   
Comment by Black Hole [ 23/May/21 ]

ItemMeta is actually a snapshot copy of the itemstack data. You have to apply it back to the itemstack using ItemStack.setItemMeta()

Generated at Tue Apr 22 06:21:04 UTC 2025 using Jira 10.3.5#10030005-sha1:190c783f2bd6c69cd5accdb70f97e48812a78d14.