Inventory

Discussion in 'Plugin Development' started by HeadGam3z, May 25, 2014.

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

    HeadGam3z

    Does anyone have a tutorial/code to save a player's inventory, and then load it again? I found something like this, but it was very out of date and didn't work. I need it for a plugin of mine, like: I want to be able to use a command and it saves the sender's inventory, then clears it. Upon using a different command, it loads the previously saved inventory back to the sender. I know what to do, it's just how to save then load the inventory again that I do not know how to do.

    Any help would be great!
     
  2. Offline

    thecrystalflame

  3. Offline

    Slayer9x9

  4. Offline

    HeadGam3z

    thecrystalflame
    I looked at that earlier, and I could not get it to load back the saved inventory for the life of me.
    Slayer9x9
    I actually did try this, and it went well. But, once when the server restarted, it went bye-bye. So I do not think a hashmap would be useful here.
     
  5. Offline

    thecrystalflame

    HeadGam3z

    using the class from the link i sent you do this:

    Code:java
    1.  
    2. Inventory i = StringToInventory(string);
    3. player.getInventory().setContents(i.getContents());
    4.  
     
  6. Offline

    HeadGam3z

    thecrystalflame
    I added what you said, and got the following error:
    Code:
    [20:26:56] [Server thread/INFO]: [fcGuardPlugin] Message1
    [20:26:56] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'duty' in plugin fcGuardPlugin v0.2
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    Caused by: java.lang.NumberFormatException: For input string: "HeadGam3z Inventory"
        at java.lang.NumberFormatException.forInputString(Unknown Source) ~[?:1.7.0_51]
        at java.lang.Integer.parseInt(Unknown Source) ~[?:1.7.0_51]
        at java.lang.Integer.valueOf(Unknown Source) ~[?:1.7.0_51]
        at com.gmail.mcheadgam3z.Main.StringToInventory(Main.java:305) ~[?:?]
        at com.gmail.mcheadgam3z.Main.onCommand(Main.java:122) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        ... 13 more
    
    The off command:
    Code:java
    1. if (args.length == 1 && args[0].equalsIgnoreCase("off")) {
    2. Msg("1"); // in-game debug (did show)
    3. File fcGuardPlugin = new File("plugins" + File.separator
    4. + "fcGuardPlugin" + File.separator + "Data");
    5. File GuardInv = new File(fcGuardPlugin, player.getName()
    6. + ".yml");
    7. YamlConfiguration Guardconfig = YamlConfiguration
    8. .loadConfiguration(GuardInv);
    9. if (Guardconfig.getBoolean(player.getName() + " OnGuard")) {
    10. Msg(getConfig().getString("Duty_Off_Message")
    11. .replace("%p", player.getName())
    12. .replace("&", "ยง"));
    13. player.getInventory().clear();
    14. player.getInventory().setArmorContents(null);
    15. getLogger().info("Message1"); // console debug (did show)
    16. Inventory i = StringToInventory(player.getName()
    17. + " Inventory");
    18. player.getInventory().setContents(i.getContents());
    19. getLogger().info("Message2"); // console debug (didn't show)
    20. Guardconfig.set(player.getName() + " OnGuard",
    21. Boolean.valueOf(false));
    22. getLogger().info("Message3"); // console debug (didn't show)
    23. try {
    24. Guardconfig.save(GuardInv);
    25. } catch (IOException e) {
    26. e.printStackTrace();
    27. }
    28. return true;
    29. }
    30.  
    31. }

    Now, when I use the on command, it saves the sender's inventory into a file, and gives them a kit. It saved as "HeadGam3z Inventory: 36;"
    When I tried to use the off command to get my inventory back, I got the error above.

    Anyone?
     
  7. Offline

    xize

  8. Offline

    HeadGam3z

    xize
    I need to save everything, really. :/
     
  9. Offline

    xize

  10. Offline

    HeadGam3z

    xize
    Good idea. Items will still be saved even on restart?
     
  11. Offline

    xize

    HeadGam3z
    yep this saves it as a yaml file, I recommend to save it like player_creative.yml or player_survival.yml or per world base?
     
  12. Offline

    HeadGam3z

    xize
    Hmm, I looked at your post with the link, and I don't see how that would work now. Considering I just looked at something with 2000+ lines... lol
     
  13. Offline

    xize

    HeadGam3z

    loading:
    Code:
        /**
        * @author xize
        * @param loads the inventory of the player
        */
        @SuppressWarnings("unchecked")
        public void loadInventory() {
            ItemStack[] contents = ((List<ItemStack>)con.get("orginalinv.items")).toArray(new ItemStack[0]);
            ItemStack[] armor = ((List<ItemStack>)con.get("orginalinv.armor")).toArray(new ItemStack[0]);
            player.getInventory().setContents(contents);
            player.getInventory().setArmorContents(armor);
            con.set("orginalinv", null);
            try {
                con.save(f);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            update();
        }
    
    saving:
    Code:
        /**
        * @author xize
        * @param saves the inventory of the player
        */
        public void saveInventory() {
            con.set("orginalinv.items", player.getInventory().getContents());
            con.set("orginalinv.armor", player.getInventory().getArmorContents());
            try {
                con.save(f);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            update();
        }
    
    this will save and load the armor and normal contents of a inventory, however if you want a title you can save it as a seperated String, this also saves lores and ItemMeta without the chance its removed :)

    -edit-

    note that the update(); method is just a con.load(f); (a loading file mechanism).
     
Thread Status:
Not open for further replies.

Share This Page