Saving itemstacks to a config

Discussion in 'Plugin Development' started by ClassifiedLife, May 3, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    ClassifiedLife

    My config keeps saying that a section I created is not a list. plugin.customConfig is the same as plugin.getConfig its just so I can name the yml file something else than config.

    *EDIT*
    It is about in the middle of the code and it says if (plugin.customConfig.isList... Before the suppress warnings and after p.sendMessage("debug-1") but before the other debug messages.
    That returns false for some reason. I don't know how to define it as a list and there are no good config tutorials that teach this.

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            if (commandLabel.equalsIgnoreCase("makechest"))
            {
                if (sender instanceof Player)
                {
                    Player p = (Player) sender;
                    if (args.length == 3)
                    {
                        String name = args[0];
                        String option = args[1];
                        double percentage = Double.parseDouble(args[2]);
                  
                        if (plugin.customConfig.getConfigurationSection(name) == null)
                        {
                            plugin.customConfig.createSection(name);
                        }
                        ConfigurationSection path = plugin.customConfig.getConfigurationSection(name);
                        if (path.getConfigurationSection(option) == null)
                        {
                            path.createSection(option);
                            plugin.customConfig.addDefault(name + "." + option + ".Percentage", percentage);
                            plugin.customConfig.createSection(name + "." + option + ".Items");
                            plugin.customConfig.options().copyDefaults(true);
                            try {
                                plugin.customConfig.save(plugin.customConfigFile);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            ItemStack[] items = p.getInventory().getContents();
                            p.sendMessage("debug-1");
                         
                            if (plugin.customConfig.isList(name + "." + option + ".Items"))
                            {
                                @SuppressWarnings("unchecked")
                                List<ItemStack> list = (List<ItemStack>) plugin.customConfig.getList(name + "." + option + ".Items");
                                p.sendMessage("debug");
                                for (ItemStack item : items)
                                {
                                    p.sendMessage("debug1");
                                    if (item != null && item.getType() != null)
                                    {
                                        p.sendMessage("debug2");
                                        list.clear();
                                        list.add(item);
                                    }
                                    plugin.customConfig.set(name + "." + option + ".Items", list);
                                    plugin.customConfig.options().copyDefaults(true);
                                    try {
                                        plugin.customConfig.save(plugin.customConfigFile);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                                p.sendMessage(ChatColor.GREEN + "Option: " + option + " for chest: " + name + " with percentage: " + percentage + " has been created!");
                            }
                        } else {
                            p.sendMessage(ChatColor.RED + "That option already exists!");
                            return false;
                        }
                    } else {
                        p.sendMessage(ChatColor.RED + "Correct Usage: /makechest <name> <option> <percentage>");
                        p.sendMessage(ChatColor.RED + "Example Usage: /makechest donator Option1 100");
                        return false;
                    }
                }
            }
            return false;
        }
     
    Last edited: May 3, 2015
  2. Offline

    techboy291

    You're trying to create a section,
    Code:
    plugin.customConfig.createSection(name + "." + option + ".Items");
    then check if it's a list, two different things. Also, inside the if statement, on this line,
    Code:
    List<ItemStack> list = (List<ItemStack>) plugin.customConfig.getList(name + "." + option + ".Items");
    won't work, because configuration lists only store values, like a string; they don't store configuration sections, which is what an itemstack would turn into. I don't know if you've looked through the wiki article on configs yet, but this might help.
     
  3. Offline

    ClassifiedLife

    @techboy291 Thanks for helping. Do you know how I would be able to store itemstacks or inventories?
     
  4. Offline

    mine-care

    @ClassifiedLife google it :p it has been around a. Lot! I'm sure you will find the answer;)
     
  5. config.set("Path", itemstack.serialize());
     
Thread Status:
Not open for further replies.

Share This Page