Config Loading(This time I need an answer.)

Discussion in 'Spigot Plugin Development' started by Nutowen, Apr 30, 2017.

  1. When you add an item to an inventory all the items in the inventory are put into the config like this.Usually, the inventory starts with glass panes:
    Code (Text):

    ItemStack[] armorArray = invC.getContents();
                           
                            int id = 0;
                            for(ItemStack item : armorArray){
                            Main.inst().getConfig().set("Common.Items." + id + ".item", item.toString());
                            Main.inst().getConfig().set("Common.Items." + id + ".percentage", AddItem.getInstance().percentage);
                           
                            id++;
                            }
                           
                            Main.inst().saveConfig();
     
    In the onEnable I try to put the items from config back into the inventory. I open the inventory and its empty:
    Code (Text):

    List<ItemStack> listitems = new ArrayList<>();

        for (String s : getConfig().getConfigurationSection("Common.Items").getKeys(false)) {
            listitems.add(getConfig().getItemStack("Common.Items." + s + ".item"));
        }
        ItemStack[] array = listitems.toArray(new ItemStack[listitems.size()]);

        InvCLickAdmin.getInstance().invC.setContents(array);
     
    My Config:

    Common:
    Items:
    '0':
    item: ItemStack{DIAMOND_SWORD x 1}
    percentage: 50
    '1':
    item: ItemStack{STAINED_GLASS_PANE x 1}
    percentage: 50
    '2':
    item: ItemStack{STAINED_GLASS_PANE x 1}
    percentage: 50
    '3':
    item: ItemStack{STAINED_GLASS_PANE x 1}
    percentage: 50
    '4':
    item: ItemStack{STAINED_GLASS_PANE x 1}
    percentage: 50
    '5':
    item: ItemStack{STAINED_GLASS_PANE x 1}
    percentage: 50
    '6':
    item: ItemStack{STAINED_GLASS_PANE x 1}
    percentage: 50
    '7':
    item: ItemStack{STAINED_GLASS_PANE x 1}
    percentage: 50
    '8':
    item: ItemStack{STAINED_GLASS_PANE x 1}
    percentage: 50
     
  2. Unless you have a custom way of handling this befor adding the item to the inventory then:
    Code (Text):
    ItemStack{DIAMOND_SWORD x 1}
    Will not work.
    Instead you will have to use:
    Code (Text):
    new ItemStack(Material.DIAMOND_SWORD, 1)
    I believe this should help. If you have a way of handling your itemstack way please can you show it.
     
  3. So when the inventory is first opened its full of glass panes. click on a glass and it is replaced with the item.