Paper icon indicating copy to clipboard operation
Paper copied to clipboard

Serialize ItemMeta to SNBT to losslessly save ItemStacks

Open Machine-Maker opened this issue 1 year ago • 3 comments

Changes this

item:
  ==: org.bukkit.inventory.ItemStack
  v: 3837
  type: SHULKER_BOX
  meta:
    ==: ItemMeta
    meta-type: TILE_ENTITY
    internal: H4sIAAAAAAAA/22OwW7CMAyG3XZFWw/jhDQOCO01dkTiwHncqzQ1NDSxq9agsqfHZauKJnLxr+TL5z8DyOB949nWWxIn1705JhD9gJ5XiF0Ji+AIbWsO8tVVZ19jmxfcK3MdGJ39MN8g3QmGLtMcJ5BaPpNojiJ4+fYsuiWzHBomJFFq9WAVbrHMkWxlSMLv+8zjBX2XwHoCm5ZPaMV5zDXKEJnuHcauHxP8p1NxwVzDk0rRv0rzz+m3MPtRupyuS2cCU5k3ztamR5XdABHDfI5AAQAA
    blockMaterial: SHULKER_BOX

to

item:
  ==: org.bukkit.inventory.ItemStack
  v: 3837
  type: SHULKER_BOX
  meta:
    ==: ItemMeta
    snbt: |-
      {
          "minecraft:block_entity_data": {
              id: "minecraft:shulker_box",
              x: 0,
              y: 0,
              z: 0
          },
          "minecraft:container": [
              {
                  item: {
                      components: {
                          "minecraft:stored_enchantments": {
                              levels: {
                                  "minecraft:projectile_protection": 3
                              }
                          }
                      },
                      count: 1,
                      id: "minecraft:enchanted_book"
                  },
                  slot: 0
              },
              {
                  item: {
                      components: {
                          "!minecraft:tool": {}
                      },
                      count: 1,
                      id: "minecraft:diamond_pickaxe"
                  },
                  slot: 1
              }
          ]
      }
    _version: 3837
    meta-type: PAPER_SNBT
    meta-subtype: TILE_ENTITY
    blockMaterial: SHULKER_BOX

Machine-Maker avatar Apr 28 '24 19:04 Machine-Maker

Just noting here for prosperity, we had a bit of an internal discussion regarding this and generally posit that the better idea would be to make the stack itself store the SNBT and get ItemMeta out of the picture, that way we stay closer to what vanilla is doing here and have something we can almost directly just pass into DFU/DataConvertor without worrying how broken ItemMeta is, especially when it comes to the upgrade path (or complete lackthereof)

electronicboy avatar May 08 '24 07:05 electronicboy

Just noting here for prosperity, we had a bit of an internal discussion regarding this and generally posit that the better idea would be to make the stack itself store the SNBT and get ItemMeta out of the picture, that way we stay closer to what vanilla is doing here and have something we can almost directly just pass into DFU/DataConvertor without worrying how broken ItemMeta is, especially when it comes to the upgrade path (or complete lackthereof)

Do you mean a format similar to

item:
  ==: org.bukkit.inventory.ItemStack
  version: 3839
  id: minecraft:shulker_box
  count: 1
  components:
    minecraft:container:
    - slot: 0
      item:
        id: minecraft:oak_log
        count: 64
    - slot: 1
      item:
        id: minecraft:oak_log
        count: 64
    - slot: 2
      item:
        id: minecraft:oak_log
        count: 64
    - slot: 3
      item:
        id: minecraft:oak_log
        count: 64
    - slot: 4
      item:
        id: minecraft:oak_log
        count: 64
    - slot: 5
      item:
        id: minecraft:oak_log
        count: 64
    - slot: 6
      item:
        id: minecraft:oak_log
        count: 64
    - slot: 7
      item:
        id: minecraft:netherite_sword
        count: 1
        components:
          minecraft:enchantments:
            levels:
              minecraft:sharpness: 5
    - slot: 8
      item:
        id: minecraft:white_banner
        count: 1
        components:
          minecraft:banner_patterns:
          - pattern: minecraft:skull
            color: red
          - pattern: minecraft:border
            color: red
  custom: |-
    {
        PublicBukkitValues: {
            "bukkit:a20d69c7-9eac-468e-8e11-9f527acda26d": [B; 0B, 1B, 3B]
        }
    }

Patch (since minecraft, unlike datapacks or plugins or other user input, don't care about specific types, like byte instead of short) Or something similar? And do you accept prs on this change?

masmc05 avatar May 08 '24 14:05 masmc05

No, mojang saves itemstacks using NBT still, we'd just align ItemStack itself with that, rather than trying to keep ItemMeta in the mix (i.e. ItemMeta goes, the patch stays as snbt stored against the ItemStack itself, just like mojang does on disk)

electronicboy avatar May 08 '24 14:05 electronicboy