Solved Saving custom inventory

Discussion in 'Plugin Development' started by DK_DEV, Aug 18, 2018.

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

    DK_DEV

    Hello, I am making a plugin that allow you to create a shop and you can sell item's on it on a GUI but I don't know how to save the inventories I have been trying for a long time all I know to do is save the inventory on the config by using that method:
    Code:
        public static void saveInventory(Inventory inv, FileConfiguration config, String path) {
            for (int i = 0; i<inv.getSize();i++) {
                if (inv.getItem(i)!=null) {
                    config.set(path + "." + i, inv.getItem(i));
                } else {
                    if (config.isItemStack(path + "." + i)) {
                        config.set(path + "." + i, null);
                    }
                }
            }
        }    
    and i'm saving it by using "InventoryCloseEvent" and the method is:

    saveInventory(getInventory(p), config, p.getName() + "'s_Inventory");

    the getInventory(p) that for the player's shop like a inventory per player I think you got it :)

    and that how I get it on the config:
    Code:
    DK_IMNP's_Inventory:
      '0':
        ==: org.bukkit.inventory.ItemStack
        type: STAINED_CLAY
        damage: 10
        meta:
          ==: ItemMeta
          meta-type: UNSPECIFIC
          display-name: "\xa7r\xa7lSTAINED_CLAY x1"
          lore:
          - ''
          - "\xa78\xa7lItem By:"
          - "\xa7r\xa7lDK_IMNP"
          - "\xa78\xa7lPrise:"
          - "\xa7r\xa7l1"
          - "\xa78\xa7lItem Number:"
          - "\xa7r\xa7l0"
    
    but the problem is I don't really know how to load it...
    please anyone help me.
    Thank you! ☺

    Sorry for my English, English is not my first language!
     
  2. Offline

    KarimAKL

    @DK_DEV Are you trying to save the player's inventory in a YAML file and then setting it back to that inventory later? If so then you can just save the player's inventory contents and set it back later.
     
  3. Offline

    DK_DEV

    I'm new on java so I don't really know how to do that and ye i'm saving it on the YML File but I don't really know how to put back the items if you open the inventory again... Can you please help me?
     
  4. Offline

    KarimAKL

    If you want to save the player's inventory (including armor) then do this:
    Code:Java
    1. getConfig().set(/*path*/, player.getInventory().getContents());

    And to get it:
    Code:Java
    1. player.getInventory().setContents(getConfig().getStringList(/*path*/));

    That should work, please let me know how it goes. (I hope this is what you meant/wanted)
     
  5. Offline

    DK_DEV

    Ugh I get an error on:

    player.getInventory().setContents(getConfig().getStringList(/*path*/));

    because I use a method which is per player inventory that is the method:

    static Inventory getInventory(Player player) {
    Inventory i = inventories.get(player);

    if(i == null) {
    i = Bukkit.createInventory(null, 54, "§7§lShop");
    inventories.put(player, i);
    }
    return i;
    }

    and the error is:

    The method setContents(itemStack[]) in the type Inventory is not applicable for the arguments (List<String>)
     
  6. Offline

    MrGeneralQ

    Please don't use that. Use the "ChatColor.translateAlternateColor(" instead.
     
  7. Offline

    KarimAKL

    Oh my bad, i think it might be getConfig().get(/*path*/), i don't remember and i can't check at the moment.
     
  8. Offline

    DK_DEV

    Oh okay

    and it doesn't work btw

    Sure I'll

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 18, 2018
  9. Offline

    KarimAKL

    @DK_DEV Try this instead:
    Code:Java
    1. //To save it
    2. getConfig().set(/*path*/, player.getInventory().getContents());
    3.  
    4. //To load it
    5. ItemStack[] contents = (ItemStack[]) getConfig().get(/*path*/);
    6. player.getInventory().setContents(contents);

    Hope this works for you.
     
  10. Offline

    DK_DEV

    I get an error this is the error:


    [09:38:34 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ps' in plugin LKPS v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-bc03b6f]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-bc03b6f]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:625) ~[craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1058) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:919) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_181]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_181]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:643) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:598) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:506) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_181]
    Caused by: java.lang.NullPointerException
    at org.bukkit.craftbukkit.v1_8_R1.inventory.CraftInventory.setContents(CraftInventory.java:65) ~[craftbukkit.jar:git-Bukkit-bc03b6f]
    at me_DK_DEV.Private_Shop.onCommand(Private_Shop.java:265) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-bc03b6f]
    ... 14 more

    Line 264: ItemStack[] contents = (ItemStack[]) getConfig().get(p.getName() + "'s Inventory");
    Line 265: getInventory(p).setContents(contents);

    And i'm using a custom inventory btw.
     
  11. Offline

    Zombie_Striker

    The "contents" is null. Make sure that the config contains that path before you try to get it.
     
  12. Offline

    DK_DEV

    Oh okay I'll try that

    Edit: I made the "contents" not null and I checked it on the config but when I reload the server and open the inventory I'll get this error:

    18:36:23 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ps' in plugin LKPS v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-bc03b6f]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-bc03b6f]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:625) ~[craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1058) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:919) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_181]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_181]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:643) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:598) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:506) [craftbukkit.jar:git-Bukkit-bc03b6f]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_181]
    Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Lorg.bukkit.inventory.ItemStack;
    at me_DK_DEV.Private_Shop.onCommand(Private_Shop.java:264) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-bc03b6f]
    ... 14 more

    Line 264: ItemStack[] contents = (ItemStack[]) getConfig().get(p.getName() + "'s Inventory");

    Any help please?

    @Zombie_Striker

    But everything is working fine but before I reload the server if I reloaded it I'll get this error...

    Oh wait this time it didn't give me a error but it didn't put the items back when

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Aug 19, 2018
  13. Offline

    Zombie_Striker

    When an ItemStack[] is saved to a config, it is converted to a List<ItemStack>, so you can't just cast it back to an ItemStack[].

    You should be able to just change the "ItemStack[]" to "List<ItemStack>" and that should fix the problem. If you have other code that still relies on it as an array, you may need to then take the List and do some conversions (not casting) to convert it back to an ItemStack[].
     
  14. Offline

    DK_DEV

    can u please show me how to do it because i'm still learning on java

    Sorry for a lot of questions..
     
  15. Offline

    Zombie_Striker

    List<ItemStack> contentsLIST = (List<ItemStack>) getConfig().get(p.getName() + "'s Inventory");

    ItemStack[] contents=new ItemStack[contentsLIST.size()];
    contents = contentsLIST .toArray(contents );
     
  16. Offline

    DK_DEV

    and then I do

    getInventory(p).setContents(contents);

    or no?
     
    Last edited: Aug 21, 2018
  17. Offline

    DK_DEV

Thread Status:
Not open for further replies.

Share This Page