Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-6467

InventoryClickEvent does not always write meta

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: Major Major
    • None
    • None
    • This server is running CraftBukkit version 3083-Spigot-9fb885e-9c7acb6 (MC: 1.16.5) (Implementing API version 1.16.5-R0.1-SNAPSHOT)
    • Yes

      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.

            Unassigned Unassigned
            DanIsProcessing Daniel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: