Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize ItemMeta to SNBT to losslessly save ItemStacks #10609

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Machine-Maker
Copy link
Member

@Machine-Maker Machine-Maker commented Apr 28, 2024

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

+ // Paper start - serialize to SNBT
+ @Override
+ ImmutableMap.Builder<String, Object> modernSerialize(final ImmutableMap.Builder<String, Object> builder) {
+ return builder.put("blockMaterial", this.material.name());
Copy link
Contributor

@masmc05 masmc05 Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't be it better to use https://jd.papermc.io/paper/1.20/org/bukkit/Registry.html#MATERIAL (or nms registry) to get the namespaced name instead of enum name?
(Including if we believe rumours that with those updates items and blocks are going to be data driven, using the name from registry may be better for long term)

@electronicboy
Copy link
Member

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)

@masmc05
Copy link
Contributor

masmc05 commented May 8, 2024

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?

@electronicboy
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants