Solved saving and reading ItemStack[] from/to a file

Discussion in 'Plugin Development' started by atesin, Oct 31, 2016.

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

    atesin

    hi folks ... i was searching a lot for this solution unsuccessfully and frustratingly .... now i found it i want to share my findings with you so that nobody suffers what i did

    there is a strange bug when reading itemstacks from a config file ... here is my workaround

    saving an ItemStack[] to a file (easy):
    Code:
    File myYmlFile = getMyYmlFileSomeway();
    FileConfiguration myYmlConfig = getMyConfigSomeway.from(myYmlFile);
    ItemStack[] myStack = getMyItemsSomeway(); // for example from a player or inventory
    myYmlConfig.set("my.yml.path", myStack);
    myYmlConfig.save(myYmlFile);
    getting the ItemStack[] from the file (tricky):
    Code:
    // add this little optimization on class properties
    ItemStack[] newItemStack0 = new ItemStack[0];
    ...
    // weird bug... sometimes data is read as ArrayList<ItemStack>, sometimes as ItemStack[]
    Object data = myYmlConfig.get("my.yml.path");
    ItemStack[] myStack;
    if (data instanceof List)
      myStack = ((List<ItemStack>) data).toArray(newItemStack0);
    else
      myStack = (ItemStack[]) data;
     
    Last edited: Oct 31, 2016
Thread Status:
Not open for further replies.

Share This Page