[SPIGOT-7636] ItemStack#setItemMeta(null) issue Created: 25/Apr/24  Updated: 25/Dec/24  Resolved: 25/Apr/24

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

Type: Bug Priority: Minor
Reporter: Shane Bee Assignee: md_5
Resolution: Fixed Votes: 0
Labels: None

Attachments: File Test-1.0-SNAPSHOT.jar    
Version: CraftBukkit version 4117-Spigot-b698b49-ebb50e1 (MC: 1.20.5)
Plugin: just my test plugin
Guidelines Read: Yes

 Description   

Ok this one is an odd one. I don't even really know what is going on here, so I will try my best to explain this.

Long story short, it appears attempting to clear the item meta of an ItemStack that was gotten from the player's hand, locks the meta in, it can't be cleared.

Scenario One:

  • create a new ItemStack and enchant it with sharpness 5
  • send the ItemMeta of that stack (it will send that the sharpness is there)
  • clear the meta via `ItemStack#setItemMeta(null)`
  • Send the ItemMeta of that stack (it will not have the enchantment)
    So that is a yay!

Scenario Two:

  • Set an ItemStack var to `player.getInventory().getItemInMainHand()`
  • send the ItemMeta of that stack (it will send that the sharpness is there)
  • clear the meta via `ItemStack#setItemMeta(null)`
  • send the ItemMeta of that stack (it will send that the sharpness is there... that shouldn't happen.)

Ive tested this on both 1.20.4 (doesn't happen) and 1.20.5 (happens)
This is odd behaviour.

I'll include a jar for testing, here is the code snippet:

if (args.length > 0 && args[0].equals("one")) {
    ItemStack itemStack = new ItemStack(Material.DIAMOND_AXE);
    itemStack.addEnchantment(Enchantment.SHARPNESS, 5);
    player.getInventory().addItem(itemStack);

    Bukkit.getLogger().info("MetaBefore: " + itemStack.getItemMeta());
    itemStack.setItemMeta(null);
    Bukkit.getLogger().info("MetaAfter: " + itemStack.getItemMeta());

} else if (args.length > 0 && args[0].equals("two")) {
    ItemStack itemStack = player.getInventory().getItemInMainHand();

    Bukkit.getLogger().info("MetaBefore: " + itemStack.getItemMeta());
    itemStack.setItemMeta(null);
    Bukkit.getLogger().info("MetaAfter: " + itemStack.getItemMeta());
}

Steps to replicate with included jar.

  • Clear your inventory
  • Run command `/test one`
  • This will give you an item (we shall use this later)
  • You should see the following output in console:
    [22:15:22] [Server thread/INFO]: YourNameHere issued server command: /test one
    [22:15:22] [Server thread/INFO]: MetaBefore: UNSPECIFIC_META:{meta-type=UNSPECIFIC, enchants={DAMAGE_ALL=5}}
    [22:15:22] [Server thread/INFO]: MetaAfter: UNSPECIFIC_META:{meta-type=UNSPECIFIC}
    

    As you can see, the meta is updated/cleared as we would expect.

  • Now make sure that item we gave you is in your main hand
  • Run the command `/test two`
  • You should see the following output in console:
    [22:22:17] [Server thread/INFO]: YourNameHere issued server command: /test two
    [22:22:17] [Server thread/INFO]: MetaBefore: UNSPECIFIC_META:{meta-type=UNSPECIFIC, enchants={DAMAGE_ALL=5}}
    [22:22:17] [Server thread/INFO]: MetaAfter: UNSPECIFIC_META:{meta-type=UNSPECIFIC, enchants={DAMAGE_ALL=5}}
    

    As you can see here, the meta was not cleared. It's like its being locked in.



 Comments   
Comment by Shane Bee [ 25/Apr/24 ]

Thanks. I was writing code for my Skript addon, and I spent about 5 hours screaming with like 10+ debug messages, not understanding what was going on. Took me a good amount of time to narrow it down to the item meta not resetting (or clearing of any sort).

Then I had to test again in a real plugin environment so I could properly report it. 

I dug around in CB code and just couldn't figure it out, so thanks again for fixing it.

Comment by md_5 [ 25/Apr/24 ]

Good pickup and not so easy fix, fingers crossed - thanks!

Generated at Sat Dec 13 11:48:44 UTC 2025 using Jira 10.3.13#10030013-sha1:56dd970ae30ebfeda3a697d25be1f6388b68a422.