Solved String to ItemStack ( ItemStack.deserialize )

Discussion in 'Spigot Plugin Help' started by FinGarde, Jul 25, 2018.

  1. Hi guys i'm trying to deserialize an item stack stored in a config file ( Not a YAML config file )

    But after a few trying i don't understand why this is not working


    Code (Java):
     String cfg = FileUtil.getFile("", "inv1");

                    Map<String , Object> map = new HashMap<String, Object>();

                    cfg = cfg.replace(" ", "");    // This is not needed
                    cfg = cfg.replace("{", "");
                    cfg = cfg.replace("}", "");

                    String[] pairs = cfg.split(",");
                    for (int i=0 ; i < pairs.length ; i++)
                    {
                        String pair = pairs[i];
                        String[] keyValue = pair.split("=");

                        map.put(keyValue[0], keyValue[1]);
                    }

                    ItemStack item = ItemStack.deserialize(map);
                    p.getInventory().addItem(item);
    then it give me some errors

    Code (Text):
    Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
            at org.bukkit.inventory.ItemStack.deserialize(ItemStack.java:440) ~[spigot.jar:git-Spigot-ed1cec9-4e2f571]
    I look into the ItemStack class and deserialize and i think this is due to

    Code (Java):
     public static ItemStack deserialize(Map<String, Object> args) {
            int version = args.containsKey("v") ? ((Number)args.get("v")).intValue() : -1;
     
    #1 FinGarde, Jul 25, 2018
    Last edited: Jul 25, 2018