Saving the inventory of a enderchest in a config file.

Discussion in 'Plugin Development' started by theminer199, Mar 30, 2014.

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

    theminer199

    Hello,

    I am now working on a plugin in which you get a bigger enderchest.
    The only thing that i need to do is saving the inventory to a config file

    Does anyone know how to do this? on the internet I don't get instant answers.

    Thanks in advance!

    -theminer199
     
  2. Offline

    2MBKindiegames

    You can't save inventories directly into the config.yml but you can first turn them into ItemStack[] using
    and turn them back using
    Here's some code to save and load the ItemStack lists:
    Code:java
    1. public void saveInv(ItemStack[] itemstackList, String inventoryName) {
    2. //Runs thru every item in the list
    3. for (int i = 0; i < itemstackList.length; i++) {
    4. //Saves the itemstack
    5. getConfig().set("Inventory"+"."+inventoryName+"."+i, itemstackList[i]);
    6. }
    7. //Saves the config
    8. this.saveConfig();
    9. }
    10.  
    11. public ItemStack[] loadInv(String inventoryName) {
    12. //Creates a blank list
    13. List<ItemStack> itemstackList = new ArrayList<ItemStack>();
    14. //Preparing
    15. boolean done = false;
    16. //Starts the counting
    17. int i = -1;
    18. while (done == false) {
    19. i++;
    20. //Checks if the config contains that slot
    21. if (getConfig().contains("Inventory"+"."+inventoryName+"."+i)) {
    22. //Adds the itemstack to the list
    23. itemstackList.add(getConfig().getItemStack("Inventory"+"."+inventoryName+"."+i));
    24. } else {
    25. //No more items, the list is complete
    26. done = true;
    27. }
    28. }
    29. //Some converting and returning
    30. ItemStack[] toReturn = itemstackList.toArray(new ItemStack[itemstackList.size()]);
    31. return toReturn;
    32. }[/i]
     
    theminer199 likes this.
  3. Offline

    theminer199

    Thanks!

    But where do you place the

    inventory.setContents(ItemStack[] itemstackList)

    And the

    inventory.getContents()??
     
  4. Offline

    2MBKindiegames

    Whereever you try to save/load the enderchest inventory
     
  5. Offline

    theminer199

    Okay, thanks alot for your help.
     
  6. Offline

    2MBKindiegames

    You're very welcome!

    -If you don't need any more help, please mark this topic as SOLVED so other people with the same problem will find it faster
     
  7. Offline

    theminer199

  8. Offline

    2MBKindiegames

    You're getting things a bit mixed up here. When declarating a variable, you do something like this:
    Meaning: I want to create a "String" called "str". From that moment on, Java will remember "str" is a "String" so you don't need to say it again.
    Just replace it with:
    and you'll be fine!

    Please become God before telling others what to do!

    Everybody makes mistakes in the beginning! It's nice he wants to do it with a fun thing like Minecraft!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  9. Offline

    Maurdekye

    2MBKindiegames Excuse me, but I though knowing how to program properly was a perquisite for constructing complicated third-party server plugins from scratch.
     
  10. Offline

    2MBKindiegames

    Maurdekye You've got to start somewhere. Learning while working on something is the best way to do so!
     
  11. Offline

    Maurdekye

    2MBKindiegames I agree. But Bukkit is not a good place to start, I can tell you that from experience; both my own, and from others on this forum.
     
  12. Offline

    2MBKindiegames

    Maurdekye I have a friend who didn't know Java at all. He learned programming with writing Bukkit Plugins. Now, 6 months later, he sometimes surpasses me in Bukkit Knowledge.
     
    coasterman10 likes this.
  13. Offline

    Zethariel

    2MBKindiegames Did said friend know any programming language beforehand? ;)

    It's easy to adapt from one to another, but variable declaration is kind of... Basics of the basic.
     
    2MBKindiegames likes this.
  14. Offline

    2MBKindiegames

  15. Offline

    theminer199

    @2MBKindiegames The problem isn't solved yet. I've started a conversation with you.
     
  16. Offline

    coasterman10

    Same happened with me. I learned Java from writing Bukkit plugins and my background with C/C++.
     
  17. Offline

    Maurdekye

    theminer199
    You can save the inventory like this;
    Code:java
    1. Player ply = /* player to save the enderchest of */;
    2. getConfig().set("enderchests." + ply.getUniqueId(), ply.getEnderChest().getContents());


    And you can load the enderchest inventory back with this;
    Code:java
    1. Player ply = /* player to get the enderchest for */;
    2. ArrayList<ItemStack> conversion = new ArrayList<>();
    3. for (Object o : getConfig().getList("enderchest." + ply.getUniqueId())) conversion.add((ItemStack) o);
    4. ply.getEnderChest().setContents(conversion.toArray(new ItemStack[conversion.size()]));


    Not sure what you'd want to do with this, as enderchest inventories save over reloads, but hey.
     
  18. Offline

    2MBKindiegames

Thread Status:
Not open for further replies.

Share This Page