Skip to content

ItemStack::hasMeta throws "IllegalArgumentException: Invalid key." when item contains invalid NBT data. #12325

@ens-gijs

Description

@ens-gijs

Expected behavior

.hasMeta() should never throw due to the ItemStack containing an improper NamespaceKey in its data.

Observed/Actual behavior

When an ItemStack contains an invalid NamespacedKey .hasMeta() will throw the error below.

java.lang.IllegalArgumentException: Invalid key. Must be [a-z0-9/._-]:
        at com.google.common.base.Preconditions.checkArgument(Preconditions.java:218) ~[guava-33.3.1-jre.jar:?]
        at org.bukkit.NamespacedKey.validate(NamespacedKey.java:119) ~[paper-api-1.21.4-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.NamespacedKey.<init>(NamespacedKey.java:91) ~[paper-api-1.21.4-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.craftbukkit.util.CraftNamespacedKey.fromMinecraft(CraftNamespacedKey.java:24) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.attribute.CraftAttributeInstance.convert(CraftAttributeInstance.java:110) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.inventory.CraftMetaItem.buildModifiers(CraftMetaItem.java:583) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.inventory.CraftMetaItem.lambda$updateFromPatch$7(CraftMetaItem.java:436) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at java.base/java.util.Optional.ifPresent(Optional.java:178) ~[?:?]
        at org.bukkit.craftbukkit.inventory.CraftMetaItem.updateFromPatch(CraftMetaItem.java:435) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.inventory.CraftMetaItem.<init>(CraftMetaItem.java:399) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.inventory.CraftItemMetas.lambda$static$2(CraftItemMetas.java:52) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.inventory.CraftItemType.getItemMeta(CraftItemType.java:119) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.inventory.CraftItemStack.getItemMeta(CraftItemStack.java:431) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.inventory.CraftItemStack.getItemMeta(CraftItemStack.java:422) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.inventory.CraftItemStack.getItemMeta(CraftItemStack.java:417) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.inventory.CraftItemStack.getItemMeta(CraftItemStack.java:387) ~[paper-1.21.4.jar:1.21.4-214-894631f]
        at org.bukkit.craftbukkit.inventory.CraftItemStack.hasItemMeta(CraftItemStack.java:502) ~[paper-1.21.4.jar:1.21.4-214-894631f]

Steps/models to reproduce

Create the following ItemStack from NBT and call .hasMeta() on it.

Sample ItemStack, note the modifiers[].id value

{
  components: {
    "minecraft:attribute_modifiers": {
      modifiers: [
        {
          amount: -0.9d,
          id: "minecraft:",
          operation: add_value,
          type: "minecraft:attack_damage"
        }
      ]
    },
    "minecraft:enchantments": {
      levels: {
        "minecraft:knockback": 100,
        "minecraft:vanishing_curse": 1
      }
    }
  },
  count: 1,
  DataVersion: 4189,
  id: "minecraft:stick"
}

Plugin and Datapack List

plugins
CommandAPI, DuelEternal, EternalSurvival, FastAsyncWorldEdit, Kits, LuckPerms, PlaceholderAPI, WorldGuard

datapacks
There are 4 data pack(s) enabled: [vanilla (built-in)], [file/bukkit (world)], [file/kits-eternal (world)], [paper (built-in)]

Paper version

This server is running Paper version 1.21.4-214-main@894631f (2025-03-21T17:21:46Z) (Implementing API version 1.21.4-R0.1-SNAPSHOT)

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    scope: apistatus: acceptedDisputed bug is accepted as valid or Feature accepted as desired to be added.version: 1.21.4Game version 1.21.4

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions