Solved About PersistentDataContainer: How to add multiple tags?

Discussion in 'Spigot Plugin Development' started by inrh, Jan 25, 2020.

  1. Hello there.
    I have two questions
    1.How to add multiple tags
    2. How to add tags to existing tags

    I didn't find a way to add multiple tags.
    View attachment 483083

    This is my test code, always a tag
    Code (Java):

    ItemStack itemStack = new ItemStack(Material.DIAMOND_HOE);

    NamespacedKey key = new NamespacedKey(INewReflex.getMain(), "test");
    NamespacedKey key1 = new NamespacedKey(INewReflex.getMain(), "test123456");
    ItemMeta itemMeta = itemStack.getItemMeta();
    itemMeta.getPersistentDataContainer().set(key, PersistentDataType.DOUBLE, 0.11);
    itemMeta.getPersistentDataContainer().set(key1, PersistentDataType.STRING, "go");

    itemStack.setItemMeta(itemMeta);
     
    result
    upload_2020-1-25_23-44-10.png

    Looking forward to help, thanks
     
  2. You can simply use PeristentDataType.TAG_CONTAINER, which enables you to store another container inside your container. Also, the tag is only one; but there might be multiple tags in that single tag (which is common).

    To read, you can simply get the PersistentDataContainer using ItemMeta#getPersistentDataContainer. From there, you can use one of the two getters to read your data (get / getOrDefault)
     
    • Like Like x 1
  3. What mistake did I make?:cry:
    Code (Java):

    ItemStack itemStack = new ItemStack(Material.DIAMOND_HOE);

    NamespacedKey key = new NamespacedKey(INewReflex.getMain(), "test");
    NamespacedKey key1 = new NamespacedKey(INewReflex.getMain(), "test111");
    NamespacedKey key2 = new NamespacedKey(INewReflex.getMain(), "test111");
    NamespacedKey key3 = new NamespacedKey(INewReflex.getMain(), "eee");
    ItemMeta itemMeta = itemStack.getItemMeta();

    PersistentDataAdapterContext persistentDataAdapterContext = itemMeta.getPersistentDataContainer().getAdapterContext();
    PersistentDataContainer root = persistentDataAdapterContext.newPersistentDataContainer();
    root.set(key, PersistentDataType.INTEGER, 1);
    PersistentDataContainer value = persistentDataAdapterContext.newPersistentDataContainer();
    value.set(key1, PersistentDataType.INTEGER, 2);
    root.set(key2, PersistentDataType.TAG_CONTAINER, value);
    itemMeta.getPersistentDataContainer().set(key3, PersistentDataType.TAG_CONTAINER, root);

    itemStack.setItemMeta(itemMeta);
     
     
  4. md_5

    Administrator Developer

    @inrh multiple tags are all being added, the client only shows top level tags (also that NBT is debug info anyway).
    Your code in the original post is correct and .get will show that both tags are being stored.
     
    • Like Like x 2
  5. LoneDev

    Supporter

    You can find this mod useful: https://www.curseforge.com/minecraft/mc-mods/nbt-tooltip
    I use it to debug my stuff fast

    [​IMG]
     
    • Like Like x 1
    • Useful Useful x 1