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!
Perhaps put the inventory into a HashMap, then save/load the HashMap? See: http://wiki.bukkit.org/Plugin_Tutorial (Saving/Loading a HashMap)
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.
HeadGam3z using the class from the link i sent you do this: Code:java Inventory i = StringToInventory(string);player.getInventory().setContents(i.getContents());
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 if (args.length == 1 && args[0].equalsIgnoreCase("off")) { Msg("1"); // in-game debug (did show) File fcGuardPlugin = new File("plugins" + File.separator + "fcGuardPlugin" + File.separator + "Data"); File GuardInv = new File(fcGuardPlugin, player.getName() + ".yml"); YamlConfiguration Guardconfig = YamlConfiguration .loadConfiguration(GuardInv); if (Guardconfig.getBoolean(player.getName() + " OnGuard")) { Msg(getConfig().getString("Duty_Off_Message") .replace("%p", player.getName()) .replace("&", "ยง")); player.getInventory().clear(); player.getInventory().setArmorContents(null); getLogger().info("Message1"); // console debug (did show) Inventory i = StringToInventory(player.getName() + " Inventory"); player.getInventory().setContents(i.getContents()); getLogger().info("Message2"); // console debug (didn't show) Guardconfig.set(player.getName() + " OnGuard", Boolean.valueOf(false)); getLogger().info("Message3"); // console debug (didn't show) try { Guardconfig.save(GuardInv); } catch (IOException e) { e.printStackTrace(); } return true; } } 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?
well if you want to save its contents only you may want to look to my post here http://forums.bukkit.org/threads/itemstacks-in-configuration-files.272215/ number 3.
HeadGam3z What I would do is probably the same method as what ive sent earlier but then seperated like: con.set("armor", p.getInventory().getArmorContents()); con.set("items", p.getInventory().getContents()); ive made in my own plugin this to switch game modes: https://github.com/xEssentials/xEss...ntials/instances/xEssentialsPlayer.java#L2302
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?
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
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).