Save/Load ItemStack[], figured out save, but how to load?

Discussion in 'Plugin Development' started by woutwoot, Mar 12, 2013.

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

    woutwoot

    Code:
    public static void save(ItemStack[] stack, String name, Plugin plg){
    FileConfiguration cfg = null;
    File file = new File(plg.getDataFolder(), name + ".shopinv");
    cfg = YamlConfiguration.loadConfiguration(file);
    cfg.set("CONTENT", stack);
    try {
    cfg.save(file);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    I use this to save an ItemStack Array to a config file. But how can I load it to an Array again? The only method available is: getItemStack("path"); But I need all of the ItemStacks in an array, so I don't know the individual paths. Please help :)
     
  2. Use

    Code:
    List<ItemStack[]> list = new ArrayList<ItemStack[]>();
    It's a list of arrays, so to get a specific item stack do

    Code:
    list.get(<index of stack>);
     
  3. Offline

    woutwoot

    You don't understand, I save an ItemStack (Taken directly from an Iventory) using the FileConfiguration.set function. But I can only load 1 ItemStack while I did save an array. Maybe this helps, this is my config file content:
    Code:
    CONTENT:
    - ==: org.bukkit.inventory.ItemStack
      type: DIAMOND_SWORD
      meta:
        ==: ItemMeta
        meta-type: UNSPECIFIC
        enchants:
          KNOCKBACK: 2
          DAMAGE_ALL: 3
    - ==: org.bukkit.inventory.ItemStack
      type: ITEM_FRAME
      amount: 64
    - ==: org.bukkit.inventory.ItemStack
      type: SIGN
      amount: 64
    - null
    - null
     
    ... And so on
     
  4. Ah, now I see, you can only get one ItemStack from a list of ItemStacks in your FileConfiguration, and you can only save one to it?

    How about instead of doing
    Code:
     
    CONTENT:[/FONT]
    [FONT=Consolas]- ==: org.bukkit.inventory.ItemStack[/FONT]
    [FONT=Consolas]type: DIAMOND_SWORD[/FONT]
    [FONT=Consolas]meta:[/FONT]
    [FONT=Consolas]==: ItemMeta[/FONT]
    [FONT=Consolas]meta-type: UNSPECIFIC[/FONT]
    [FONT=Consolas]enchants:[/FONT]
    [FONT=Consolas]KNOCKBACK: 2[/FONT]
    [FONT=Consolas]DAMAGE_ALL: 3[/FONT]
    [FONT=Consolas]- ==: org.bukkit.inventory.ItemStack[/FONT]
    [FONT=Consolas]type: ITEM_FRAME[/FONT]
    [FONT=Consolas]amount: 64[/FONT]
    [FONT=Consolas]- ==: org.bukkit.inventory.ItemStack[/FONT]
    [FONT=Consolas]type: SIGN[/FONT]
    [FONT=Consolas]amount: 64[/FONT]
    [FONT=Consolas]- null[/FONT]
    [FONT=Consolas]- null[/FONT]
    [FONT=Consolas][/FONT]
    [FONT=Consolas]... And so on 
    [/FONT]

    do

    Code:
    idlimit: 2[/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas]id1: [/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas]- ==: org.bukkit.inventory.ItemStack [/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas] type: DIAMOND_SWORD [/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas] meta:[/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas] ==: ItemMeta [/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas] meta-type: UNSPECIFIC [/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas] enchants: KNOCKBACK: 2 [/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas] DAMAGE_ALL: 3[/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas]id2:[/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas]- ==: org.bukkit.inventory.ItemStack[/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas]type: ITEM_FRAME[/FONT][/FONT][/FONT][/FONT][/FONT]
    [FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas][FONT=Consolas]amount: 64[/FONT][FONT=Consolas]
    [/FONT][/FONT][/FONT][/FONT]

    And cycle through each id<number> list by incrementing the number up to idlimit.[/CODE][/CODE][/CODE][/CODE]Ignore the weird font issue, copy paste didn't work out xD[/CODE][/FONT]

    That post is a bit mangled and editing is not helping it xD Sorry

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  5. Offline

    woutwoot

    I can save multiple ItemStacks, but I can only load one. I could try that, but there must be a much simpler way of doing this.
     
  6. Your "CONTENT" node is saved as an array but you can't load it as an array, you must load it as a list, use get() and store it as a List<ItemStack> (without the [] on ItemStack because those mean that it's an array...)
     
  7. Offline

    woutwoot

    So, like this? :
    Code:
        public static ItemStack[] get(String name){
            FileConfiguration cfg = null;
            File file = new File(plg.getDataFolder(), name + ".shopinv");
            cfg = YamlConfiguration.loadConfiguration(file);
            List<ItemStack> Inv = (List<ItemStack>) cfg.getList("CONTENT");
            return (ItemStack[]) Inv.toArray();
        }
     
    DeMaggo likes this.
  8. Why do you keep manually loading the config every time ? Use getConfig() ! It's alot faster than reloading the config every time.

    And basically, yes, that's how you can do it.
    You can use "inv.toArray(new ItemStack[0]);" to avoid casting :p The object sent as argument inside that method will just be used as a type reference, not as value.
     
    DeMaggo likes this.
Thread Status:
Not open for further replies.

Share This Page