Saving Lists of Items to Config?

Discussion in 'Plugin Development' started by killerman747, Apr 21, 2014.

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

    killerman747

    Hello everyone, I was wondering if someone out there had an answer to my question...

    I am looking for a way to store large lists of items in a config to later be pulled out. The biggest example would be saving the contents of a chest, and then opening the chest up at a different location with the same contents in it. Similar to an enderchest. Any suggestions?
     
  2. Offline

    beeselmane

    say you have an ArrayList of the items.. you could just do <plugin main>.getConfig().set("path.to.save", <ArrayList>);
     
  3. Offline

    Heirteir

    killerman747
    Here is an example of saving a list to a config:
    Code:java
    1. List<String> list = new ArrayList<String>();
    2.  
    3. list.add(thevaluesyouneed);
    4.  
    5. this.getConfig().set("myvalues", list);
    6. this.saveConfig()


    This will create a list
    add to the list
    set the list to a area in the config
    then save the config

    Hope I helped

    --Heirteir
     
  4. Offline

    killerman747

    Yes, but how I would I go about creating this list of items, their Ids, metadata and amount of each item per slot for a chest?
     
  5. Offline

    beeselmane

    killerman747

    Code:java
    1. ArrayList<ItemStack> chestitems;
    2. for (ItemStack s : <Chest Inventory>)
    3. {
    4. chestitems.add(s);
    5. }
    6.  
    7. this.getConfig().set("chest", chestitems);
    8. this.saveConfig();
     
  6. Offline

    GeorgeeeHD

    Code:java
    1. Chest yourChest = (Chest) Bukkit.getWorld(Main.getWorldName()).getBlockAt(aLocation).getState();
    2.  
    3. List<ItemStack[]> chestContents = new ArrayList<ItemStack[]>();
    4. chestContents.add(yourChest.getInventory().getContents());
    5.  
    6. //Save to config here...
     
  7. Offline

    killerman747

    GeorgeeeHD beeselmane
    Alright thats all good, but how would I then take this list and fill a chest with its contents?
     
  8. Offline

    GeorgeeeHD

    make a new list equal to the list from the config, then iterate over each ietmstack and put them in the chest
     
  9. Offline

    killerman747

    GeorgeeeHD
    So I have been playing around with this for a bit but still cant seem to get it to work. Heres what I currently have:
    Code:
    Inventory thing= Bukkit.createInventory(player, 54, "Thing"");
                        @SuppressWarnings("unchecked")
                        List<ItemStack[]> thingc = (List<ItemStack[]>) getConfig().get(".Users." + player.getName() + ".Thing");
                        ItemStack[] thingcontents = thingc.toArray(new ItemStack[0]);
                        thing.setContents(thingcontents );
     
  10. Offline

    BillyGalbreath

    Be sure your server environment is setup correctly with UTF8 or these methods will mess up custom names and lore containing color codes.
     
  11. Offline

    killerman747

    BillyGalbreath
    I guess I should have included the error message I get in console when I try to open this inventory...
    Code:
    [22:57:47] [Server thread/ERROR]: Could not pass event PlayerInteractEntityEvent to MyPlugin v0.4
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:471) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:1068) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInUseEntity.a(SourceFile:55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInUseEntity.handle(SourceFile:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
        at me.dwarvenrealms.myplugin.VillagerHandler.BankUse(VillagerHandler.java:43) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_13]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_13]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_13]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_13]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 13 more
     
  12. Offline

    Seadragon91

    There is something null in the method BankUse in class VillagerHandler at line 43. Please post the code if possible.
     
  13. Offline

    killerman747

    Seadragon91
    Alright so I saw that the inventory was returning null so I added some more code, the only problem now is that when the inventory isnt null, and does contain items, the inventory dosnt open. Heres the code:
    Code:java
    1. Inventory bank = Bukkit.createInventory(player, 54, ChatColor.GOLD + "Bank");
    2. @SuppressWarnings("unchecked")
    3. List<ItemStack[]> bankc = (List<ItemStack[]>) plugin.getConfig().get(".Users." + player.getName() + ".Bank");
    4. if(bankc==null){
    5. player.sendMessage("NULL");
    6. player.openInventory(bank);
    7. }else{
    8. player.sendMessage("NOT NULL");
    9. ItemStack[] bankcontents = bankc.toArray(new ItemStack[1]);
    10. bank.setContents(bankcontents);
    11. player.openInventory(bank);
    12. }

    Heres the error:
    Code:
    [10:57:44] [Server thread/ERROR]: Could not pass event PlayerInteractEntityEvent to MyPlugin v0.4
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:471) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:1068) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInUseEntity.a(SourceFile:55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInUseEntity.handle(SourceFile:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.ArrayStoreException
        at java.lang.System.arraycopy(Native Method) ~[?:1.7.0_13]
        at java.util.ArrayList.toArray(Unknown Source) ~[?:1.7.0_13]
        at me.dwarvenrealms.myplugin.VillagerHandler.BankUse(VillagerHandler.java:48) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_13]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_13]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_13]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_13]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 13 more
     
  14. Offline

    DrEinsteinium

    Question: Does setConfig have custom handlers for objects like itemstacks..? If not then this method of saving items to a config will not work.
     
  15. Offline

    xTigerRebornx

    DrEinsteinium likes this.
  16. Offline

    killerman747

    DrEinsteinium xTigerRebornx
    Either of you guys know what could be possibly going wrong?

    Alright, not sure if this could be the cause but in the config where the ArrayList of items is saving, it shows up like this:
    Code:
    - - ==: org.bukkit.inventory.ItemStack
            type: ROTTEN_FLESH
          - null
    Does that extra dash in front of the first line affect it?

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

    DrEinsteinium

    killerman747
    Im not sure what the output is supposed to look like... but YAML should not be parsable with two -- in front of eachother like that.
     
  18. Offline

    killerman747

    Still have no idea...I might have to try to just save each slot differently then load it that way. Any last minute help?
     
  19. Offline

    coasterman10

    I personally don't trust just anything that implements ConfigurationSerializable and prefer to use a compact string with my own syntax. I like the way BattleKits does its item stack format with id:amount:data. You can create your own method to save an ItemStack into that format, and then use String.split to break it up and put the data back into an ItemStack. This is for user-friendly configurations.

    Now, seeing as you want to save all of the data for an item stack, you also probably need to save enchantments, lore, display name, etc. You can try extending a single-string format like I did, or you could choose to go back to using the ItemStack as a ConfigurationSerializable.
     
  20. Offline

    killerman747

    coasterman10
    Yea that is what I was looking at recently, saving each slot with its own string like that.
     
Thread Status:
Not open for further replies.

Share This Page