Skip to content

ItemMeta keeping default NBT values #10159

@tal5

Description

@tal5

Expected behavior

For ItemMeta implmentations such as BlockStateMeta to not add default NBT data/have a way to clear that data.

Ideally BlockStateMeta/ArmorStandMeta/whatever other implementations have this issue could automatically exclude the NBT value if it's the default, but assuming that's impossible to do without breaking backwards-compatibility in some way, making the setters nullable/some clear method (maybe even one to automatically check and remove default values?) would also be helpful.

Observed/Actual behavior

Certain ItemMeta implementations (such as BlockStateMeta/ArmorStandMeta) leave behind NBT data, even if it's the default values.
Which means that if you, for example, add some items into a chest item using BlockStateMeta then clear it to make it a "normal" chest again, it still won't stack with other chests.

Steps/models to reproduce

Simple event handler to reproduce the issue:

    @EventHandler
    public void on(PlayerInteractEvent event) {
        ItemStack chest = event.getItem();
        if (!(chest.getItemMeta() instanceof BlockStateMeta stateMeta)) {
            return;
        }
        Chest state = (Chest) stateMeta.getBlockState();
        switch (event.getAction()) {
            case LEFT_CLICK_AIR -> state.getInventory().addItem(new ItemStack(Material.STONE));
            case RIGHT_CLICK_AIR -> state.getInventory().clear();
        }
        stateMeta.setBlockState(state);
        chest.setItemMeta(stateMeta);
        event.getPlayer().getInventory().setItemInMainHand(chest);
    }

Left click with a chest a few times, then right click to clear, and it will still have the default block state left in it's NBT: {BlockEntityTag:{Items:[],id:"minecraft:chest",x:0,y:0,z:0}}.

Plugin and Datapack List

> plugins
[19:29:09 INFO]: Server Plugins (2):
[19:29:09 INFO]: Bukkit Plugins:
[19:29:09 INFO]:  - Debuggery, PaperTestPlugin
> datapack list
[19:29:57 INFO]: There are 2 data pack(s) enabled: [vanilla (built-in)], [file/bukkit (world)]
[19:29:57 INFO]: There are no more data packs available

Paper version

> version
[19:30:11 INFO]: Checking version, please wait...
[19:30:12 INFO]: This server is running Paper version git-Paper-382 (MC: 1.20.4) (Implementing API version 1.20.4-R0.1-SNAPSHOT) (Git: a80d31b)
You are running the latest version
Previous version: git-Paper-350 (MC: 1.20.4)

Other

I know the item property API is coming soon which will probably allow directly clearing this, but seeing as the ItemMeta API is staying & the item property API is still a little while away, would be nice to have some solution for this.
See also some related discussion on Discord.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: unlikelyUnlikely to ever be worked on due to complexity or risktype: bugSomething doesn't work as it was intended to.

    Type

    No fields configured for Bug.

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions