How can I save an inventory with ItemStack?

Discussion in 'Plugin Development' started by football70500, Jun 16, 2014.

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

    football70500

    I know you guys are probably going to tell me to go google it but I don't understand anything with it. Could someone walk me through saving the custom inventory with ItemStack?
     
  2. Offline

    Ice3ider4

    If you want to save the whole inventory than I would use the inventory object. For example if you want to make pistons as chests than you have to save the inventory as value with the location as the key to a hashmap.
    Plese tell me what exactly you want to do so that I can help you much easier :D
     
  3. Offline

    97WaterPolo

    football70500
    I think what you are asking is how to save the inventory to a file?

    for (ItemStack is : inventory.get contents())
    {
    // save the item stack here.
    }
     
  4. Offline

    football70500

    97WaterPolo Ice3ider4 I made a plugin that opens a custom inventory when someone opens their enderchest. Instead of having 3 rows it only has 1. You can put stuff in it and it will stay until the server restarts. Once the server restarts, the item(s) in there are gone. What I needa do is save the inventory somehow. Do I need to save the inventory to a file or what?
     
  5. Offline

    werter318

    football70500 Have you thought about using the EnderChest for that? You can still store 1 row in that, but never open it. Just copy the items.

    pseudocode:

    for(int i = 0; i<9; i++){
    enderchest.setItem(i, inventory.getItem(i));
    }
     
  6. Offline

    Ice3ider4

    football70500 Yep you have to save the inventory for all the players :s
     
  7. Offline

    football70500

    BUT BUT BUT: I also have perms for 2 rows, 3 rows, 4 rows 5 rows and 6 rows
    Ice3ider4
     
  8. Offline

    werter318

    football70500 Save it to a seperate .yml file then, you WILL find how to do this with a little help of Google.
     
  9. Offline

    CMG

    Ice3ider4 likes this.
  10. Offline

    football70500

    CMG Why is it so complicated just to save an inventory. Can't I just make a saveInventory method?
     
  11. Offline

    Ice3ider4

    football70500 That's a really good way to save the inventory to a file and you only have to copy two functions :p It's a bit complicated but what the function basically does is it saves the id, the data, the durabillity, the amount and the enchantment to a string :)
     
  12. Offline

    Deleted user

    @football70500
    Don't use that ^^ it doesn't account for ItemMeta. (Or doesn't last time I checked)

    Simply save the inventory to a yml. Saves everything; Enchants, Custom Name, Lore, etc..

    Code:java
    1.  
    2.  
    3. public void saveInventory(ItemStack[] inventory, ItemStack[] armor, UUID uuid) {
    4. try {
    5. YamlConfiguration c = new YamlConfiguration();
    6. if (inventory != null)
    7. c.set("inventory.inventory", inventory);
    8. if (armor != null)
    9. c.set("inventory.armor", armor);
    10. c.save(new File(Paranoia.getInstance().getDataFolder() + "/data/", uuid + ".yml"));
    11. } catch (IOException e) {
    12. Bukkit.getServer().getConsoleSender().sendMessage("[MCCreed] Failed to save inventory due to error: " + e.getMessage());
    13. }
    14. }
    15.  
    16. @SuppressWarnings("unchecked")
    17. public void openSavedInventory(Player opener, Player target) {
    18. YamlConfiguration c = YamlConfiguration.loadConfiguration(new File(Paranoia.getInstance().getDataFolder() + "/data/", target.getUniqueId() + ".yml"));
    19. ItemStack[] inventory = null;
    20. ItemStack[] armor = null;
    21.  
    22. if (c.get("inventory.inventory") != null)
    23. inventory = ((ArrayList<ItemStack>) c.get("inventory.inventory")).toArray(new ItemStack[0]);
    24. if (c.get("inventory.armor") != null)
    25. armor = ((ArrayList<ItemStack>) c.get("inventory.armor")).toArray(new ItemStack[0]);
    26.  
    27. Inventory cloudInventory = Bukkit.getServer().createInventory((InventoryHolder) target, 54, "[MCC] " + target.getName());
    28. cloudInventory.setContents(inventory);
    29. if (armor != null) {
    30. for (ItemStack i : armor) {
    31. if (i != null)
    32. cloudInventory.addItem(i);
    33. }
    34. }
    35. opener.openInventory(cloudInventory);
    36. }
    37.  


    You'll have to integrate that code to work as you need it to.
     
  13. Offline

    football70500

    As I am new to the whole YAML configuration, where it says c.save(newFile(etc... under the saveInventory method, what should I replace Paranoia in
    Code:JAVA
    1. Paranoia.getInstance().getDataFolder() + "/data/", uuid + ".yml")
    with? Would it be my new file configuration?
     
  14. Offline

    Deleted user



    Paranoia.getInstance() is an instance of plugin. So you would replace that with your plugin, aka Main class.
     
  15. Offline

    football70500

    yeah... about that... I just used all one class because I totally forgot about the saving inventory in the first place.

    Code:java
    1. package me.cavasi.enderslot;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.YamlConfiguration;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.block.Action;
    16. import org.bukkit.event.inventory.InventoryCloseEvent;
    17. import org.bukkit.event.inventory.InventoryOpenEvent;
    18. import org.bukkit.event.player.PlayerInteractEvent;
    19. import org.bukkit.inventory.Inventory;
    20. import org.bukkit.inventory.ItemStack;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class Main extends JavaPlugin implements Listener{
    24.  
    25. private Main plugin;
    26.  
    27. public Main(Main plugin){
    28. this.plugin = plugin;
    29. }
    30.  
    31. public static Inventory enderone = Bukkit.createInventory(null, 9, "EnderChest");
    32. public static Inventory endertwo = Bukkit.createInventory(null, 18, "EnderChest");
    33. public static Inventory enderthree = Bukkit.createInventory(null, 27, "EnderChest");
    34. public static Inventory enderfour = Bukkit.createInventory(null, 36, "EnderChest");
    35. public static Inventory enderfive = Bukkit.createInventory(null, 45, "EnderChest");
    36. public static Inventory endersix = Bukkit.createInventory(null, 54, "EnderChest");
    37. public static Inventory backpackone = Bukkit.createInventory(null, 9, "Inventory");
    38.  
    39. @Override
    40. public void onEnable(){
    41. getLogger().info("JumboChest has been enabled!");
    42. Bukkit.getPluginManager().registerEvents(this, this);
    43. }
    44.  
    45. @Override
    46. public void onDisable(){
    47. getLogger().info("JumboChest has been disabled!");
    48. }
    49.  
    50.  
    51. @EventHandler
    52. public void onOpen(PlayerInteractEvent e){
    53. Player player = e.getPlayer();
    54. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    55. if(e.getClickedBlock().getType().equals(Material.ENDER_CHEST)){
    56. e.setCancelled(true);
    57. if(player.hasPermission("jc.ender.one")){
    58. player.openInventory(enderone);
    59. }
    60. if(player.hasPermission("jc.ender.two")){
    61. player.openInventory(endertwo);
    62. }
    63. if(player.hasPermission("jc.ender.three")){
    64. player.openInventory(enderthree);
    65. }
    66. if(player.hasPermission("jc.ender.four")){
    67. player.openInventory(enderfour);
    68. }
    69. if(player.hasPermission("jc.ender.five")){
    70. player.openInventory(enderfive);
    71. }
    72. if(player.hasPermission("jc.ender.six")){
    73. player.openInventory(endersix);
    74. }
    75. }
    76. }
    77. }
    78.  
    79. public void saveInventory(ItemStack[] inventory, ItemStack[] armor, UUID uuid) {
    80. try {
    81. YamlConfiguration c = new YamlConfiguration();
    82. if (inventory != null)
    83. c.set("inventory.inventory", inventory);
    84. if (armor != null)
    85. c.set("inventory.armor", armor);
    86. c.save(new File(Main.getInstance().getDataFolder() + "/data/", uuid + ".yml"));
    87. } catch (IOException e) {
    88. Bukkit.getServer().getConsoleSender().sendMessage("[MCCreed] Failed to save inventory due to error: " + e.getMessage());
    89. }
    90. }
    91.  
    92.  
    93.  
    94. }



    This is what I have. I get an error at the Main.getInstance(): The method getInstance() is undefined for the type Main
     
  16. Offline

    Deleted user


    getInstance() is a method in my Main class, if you're only using one class and that class extends JavaPlugin (I'd assume) replace the entire Paranoria.getInstance() with 'this'.
     
  17. Offline

    football70500

    Okay now that I did that, how can I make it actually save the inventory? Would I use InventoryCloseEvent?
     
  18. Offline

    Gater12

  19. Offline

    football70500

    and I would just say on the inventory close do saveInventory();?
     
  20. Offline

    Deleted user


    Just call the saveInventory() method from any method or "event". Like so..

    Code:java
    1.  
    2. saveInventory(player.getInventory().getContents(), player.getInventory().getArmorContents(), player.getUniqueId());
    3.  
     
  21. Offline

    football70500

    Upon loading, I get an error: it says abnormal plugin type

    Code:
    [09:23:42 ERROR]: Could not load 'plugins\JumboChest.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: Abnormal plugin type
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:56) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:133) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:308) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:231) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.ja
    va:255) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.<init>(CraftServer.java:23
    3) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            at net.minecraft.server.v1_7_R1.PlayerList.<init>(PlayerList.java:63) [c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            at net.minecraft.server.v1_7_R1.DedicatedPlayerList.<init>(SourceFile:14
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.jav
    a:126) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :424) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
    Caused by: java.lang.InstantiationException: me.cavasi.enderslot.Main
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:52) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2982jnks]
            ... 10 more
    Code:java
    1. private Main plugin;
    2.  
    3. public Main(Main plugin){
    4. this.plugin = plugin;
    5. }
    6.  
    7. public static Inventory enderone = Bukkit.createInventory(null, 9, "EnderChest");
    8. public static Inventory endertwo = Bukkit.createInventory(null, 18, "EnderChest");
    9. public static Inventory enderthree = Bukkit.createInventory(null, 27, "EnderChest");
    10. public static Inventory enderfour = Bukkit.createInventory(null, 36, "EnderChest");
    11. public static Inventory enderfive = Bukkit.createInventory(null, 45, "EnderChest");
    12. public static Inventory endersix = Bukkit.createInventory(null, 54, "EnderChest");
    13. public static Inventory backpackone = Bukkit.createInventory(null, 9, "Inventory");
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("JumboChest has been enabled!");
    18. Bukkit.getPluginManager().registerEvents(this, this);
    19. }
    20.  
    21. @Override
    22. public void onDisable(){
    23. getLogger().info("JumboChest has been disabled!");
    24. }
    25.  
    26.  
    27. @EventHandler
    28. public void onOpen(PlayerInteractEvent e){
    29. Player player = e.getPlayer();
    30. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    31. if(e.getClickedBlock().getType().equals(Material.ENDER_CHEST)){
    32. e.setCancelled(true);
    33. if(player.hasPermission("jc.ender.one")){
    34. player.openInventory(enderone);
    35. }
    36. if(player.hasPermission("jc.ender.two")){
    37. player.openInventory(endertwo);
    38. }
    39. if(player.hasPermission("jc.ender.three")){
    40. player.openInventory(enderthree);
    41. }
    42. if(player.hasPermission("jc.ender.four")){
    43. player.openInventory(enderfour);
    44. }
    45. if(player.hasPermission("jc.ender.five")){
    46. player.openInventory(enderfive);
    47. }
    48. if(player.hasPermission("jc.ender.six")){
    49. player.openInventory(endersix);
    50. }
    51. }
    52. }
    53. }
    54.  
    55. @EventHandler
    56. public void onClose(InventoryCloseEvent e){
    57. saveInventory(e.getPlayer().getInventory().getContents(), e.getPlayer().getInventory().getArmorContents(), e.getPlayer().getUniqueId());
    58. }
    59.  
    60.  
    61.  
    62. public void saveInventory(ItemStack[] inventory, ItemStack[] armor, UUID uuid) {
    63. try {
    64. YamlConfiguration c = new YamlConfiguration();
    65. if (inventory != null)
    66. c.set("inventory.inventory", inventory);
    67. if (armor != null)
    68. c.set("inventory.armor", armor);
    69. c.save(new File(this.getDataFolder() + "/data/", uuid + ".yml"));
    70. } catch (IOException e) {
    71. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Failed to save inventory due to error: " + e.getMessage());
    72. }
    73. }
    74.  
    75.  
    76.  
    77. }
    78.  
     
  22. Offline

    fireblast709

    Funfact: ItemStack implements ConfigurationSerializable. (which means that you can use .set(key, itemstack) and getItemStack(key)
     
  23. Offline

    football70500

    I don't understand. How can I get this to work?
     
  24. Offline

    fireblast709

    football70500 Just like you can call config.set("key", 1) to set "key" to 1 in the config, you can use it with ItemStacks. Furthermore, you can use config.getItemStack("key") to get the ItemStack you saved with "key"
     
  25. Offline

    football70500

    I'm still not understanding.
     
  26. Offline

    zackpollard

    <-
    <-
    Oh, and please learn Java. If you don't understand fireblast709, you really need to learn...
     
  27. Offline

    fireblast709

  28. Offline

    football70500

    I'm not asking you to 'spoonfeed' me code. I am confused as to how this would save everything in the custom inventory. It just doesn't make any sense that this would save the entire inventory.
     
  29. Offline

    fireblast709

    football70500 I told you how you can save a single ItemStack. If you still don't get how you can save the inventory, I suggest you go check the javadocs :p.
     
Thread Status:
Not open for further replies.

Share This Page