Solved Deserialize persistentContainer in itemMeta

Discussion in 'Spigot Plugin Development' started by plekz, Mar 14, 2020.

  1. Hi, Im trying to deserialize an itemstacks with itemMeta and the persistent data. I've been searching around and found mulitple methods to use, but none of them would let me access the stored data. Because then it gives me this error:
    Code (Text):
           at java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.IllegalArgumentException: `The found tag instance cannot store String as it is a NBTTagByte
            at org.bukkit.craftbukkit.libs.org.apache.commons.lang3.Validate.isTrue(Validate.java:158) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.persistence.CraftPersistentDataTypeRegistry.extract(CraftPersistentDataTypeRegistry.java:223) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.persistence.CraftPersistentDataContainer.get(CraftPersistentDataContainer.java:64) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at me.gubbestor.glib.utils.ItemUtil.getItemData(ItemUtil.java:34) ~[?:?]
            at me.gubbestor.glib.button.ButtonEvent.onPlace(ButtonEvent.java:97) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            ... 20 more
    It has nothing to do with my library as shown in the error, because i use my my library with those methods in other plugins.


    I found a method to deserialize the itemMeta(I edited it some when trying to access the stored data):
    Code (Text):
    @SuppressWarnings("unchecked")
        private static ItemStack[] deserializeItemStack(Map<String, Object>[] map) {
            ItemStack[] items = new ItemStack[map.length];
           
            for(int i = 0; i < items.length; i++) {
                Map<String, Object> itemMap = map[i];
                if(itemMap.size() == 0) {
                    items[i] = null;
                }else {
                    try {
                        if(itemMap.containsKey("meta")) {
                            Map<String, Object> itemMetaMap = new HashMap<>(
                                    (Map<String, Object>) itemMap.get("meta"));
                            ItemStack item = ItemStack.deserialize(itemMap);
                            ItemMeta meta = (ItemMeta) ConfigurationSerialization.deserializeObject(itemMetaMap, ConfigurationSerialization.getClassByAlias("ItemMeta"));
                           
                            item.setItemMeta(meta);
                            items[i] = item;
                        }else {
                            items[i] = ItemStack.deserialize(itemMap);
                        }
                    }catch(Exception e) {
                        e.printStackTrace();
                        items[i] = null;
                    }
                }
            }
            return items;
        }
    I know the data is in the map because i managed to print it in a yml file
    https://gyazo.com/bc506ad0d30aa680b3812190ae2f2de3
    but i dont know why i cant access it.