Saving Inventories - Easy way!

Discussion in 'Resources' started by kreashenz, Nov 23, 2013.

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

    kreashenz

    Hey guys, I thought it would be nice to give out my inventory saving code! It is very easy, saves everything to do with the items (yes, even book meta) and you can easily load it back. So here's the methods and I'll explain what each of them do.

    This is the method to save the inventory to the specific file
    Code:java
    1. /*
    2.   * @param inv: The inventory that you save
    3.   *
    4.   * @param file: The file you want the kit stuff to save to
    5.   *
    6.   * @param override: override the existing file (if it does exist)
    7.   */
    8. public void saveInventory(PlayerInventory inv, File file, boolean override){
    9. Player p = (Player)inv.getHolder();
    10. if(inv == null || file == null) return;
    11. if(file.exists() && override) file.delete();
    12. FileConfiguration conf = YamlConfiguration.loadConfiguration(file);
    13.  
    14. ItemStack[] contents = inv.getContents();
    15. ItemStack[] armor = inv.getArmorContents();
    16.  
    17. for(int i = 0; i < contents.length; i++){
    18. ItemStack item = contents[i];
    19. if(item != null) if(item.getType() != Material.AIR) conf.set("Slot." + i, item);
    20. }
    21.  
    22. for(int i = 0; i < armor.length; i++){
    23. ItemStack item = armor[i];
    24. if(item != null) if(item.getType() != Material.AIR) conf.set("ArmorSlot." + i, item);
    25. }
    26.  
    27. for(PotionEffect pot : p.getActivePotionEffects()){
    28. conf.set("PotionEffects." + pot.getType().getName() + ".level", pot.getAmplifier());
    29. conf.set("PotionEffects." + pot.getType().getName() + ".duration", pot.getDuration());
    30. }
    31.  
    32. try {
    33. conf.save(file);
    34. }
    35. catch(IOException e){
    36. return;
    37. }
    38. }[/i][/i]


    Method to get the inventory from the file (it's private because of another method coming up)
    Code:java
    1. /*
    2.   * @param file: The file you want to retrieve the inventory from
    3.   *
    4.   * @return : the items that are found in the file
    5.   */
    6. private ItemStack[] getInventory(File file){
    7. if(file == null) return null;
    8. ItemStack[] items = null;
    9.  
    10. FileConfiguration conf = YamlConfiguration.loadConfiguration(file);
    11.  
    12. if(conf.contains("Slot") && conf.isConfigurationSection("Slot")){
    13. int size = conf.getInt("Slot", 27);
    14.  
    15. items = new ItemStack[size];
    16.  
    17. for(int i = 0; i < size; i++){
    18. if(conf.contains("Slot." + i)) items[i] = conf.getItemStack("Slot." + i);
    19. else items[i] = new ItemStack(Material.AIR);
    20. }
    21. }
    22.  
    23. return items;
    24. }[/i][/i]


    Method to get the armor contents from the file (it's private because of another method coming up)
    Code:java
    1. /*
    2.   * @param file: The file you want to retrieve the inventory from
    3.   *
    4.   * @return : the armor contents that are found in the file
    5.   */
    6. private ItemStack[] getArmorContents(File file){
    7. if(file == null) return null;
    8. ItemStack[] items = null;
    9.  
    10. FileConfiguration conf = YamlConfiguration.loadConfiguration(file);
    11.  
    12. if(conf.contains("ArmorSlot") && conf.isConfigurationSection("ArmorSlot")){
    13. int size = conf.getInt("ArmorSlot", 4);
    14.  
    15. items = new ItemStack[size];
    16.  
    17. for(int i = 0; i < size; i++){
    18. if(conf.contains("ArmorSlot." + i)) items[i] = conf.getItemStack("ArmorSlot." + i);
    19. else items[i] = new ItemStack(Material.AIR);
    20. }
    21. }
    22.  
    23. return items;
    24. }[/i][/i]


    Method to get the potion effects from the file (it's private because of another method coming up)
    Code:java
    1. /*
    2.   * @param file: The file you want to retrieve the inventory from
    3.   *
    4.   * @return : the potion effects that are found in the file
    5.   */
    6. private Collection<PotionEffect> getPotionEffects(File file){
    7. if(file == null) return null;
    8. Collection<PotionEffect> pots = new HashSet<PotionEffect>();
    9.  
    10. PotionEffect pot = null;
    11.  
    12. FileConfiguration conf = YamlConfiguration.loadConfiguration(file);
    13.  
    14. if(conf.contains("PotionEffects") && conf.isConfigurationSection("PotionEffects")){
    15. for(String str : conf.getConfigurationSection("PotionEffects").getKeys(false)){
    16. int level = conf.getInt("PotionEffects." + str + ".level");
    17. int duration = conf.getInt("PotionEffects." + str + ".duration");
    18. pot = new PotionEffect(PotionEffectType.getByName(str), duration, level);
    19. pots.add(pot);
    20. }
    21. }
    22.  
    23. return pots;
    24. }



    And lastly, the method to set the contents on a player!
    Code:java
    1. /*
    2.   * @param player: The player you want to give the inventory to
    3.   *
    4.   * @param file: The file you want to retrieve the inventory from
    5.   */
    6. public void setInventory(Player player, File file){
    7. if(file == null || player == null) return;
    8.  
    9. PlayerInventory pi = player.getInventory();
    10. pi.setArmorContents(getArmorContents(file));
    11. pi.setContents(getInventory(file));
    12. player.addPotionEffects(getPotionEffects(file));
    13. }


    This is a very helpful way to save inventories because it makes them REALLY easy to edit (just open with Notepad++ or whatever you prefer) and you can do whatever!

    If I helped, why not drop a like? Anything you want improved or something broken, comment below?
    [EDIT] Because of stupid formatting being stupid :mad:
     
  2. Offline

    wilmervanheerde

    This is really useful, thanks for sharing.
     
  3. Offline

    viper_monster

    kreashenz the last row in every inventory I save gets cleared, not sure why, there are no errors at all...
     
  4. Offline

    kreashenz

    spoljo666 You mean the top one when you click E? I'll look at this, it might just be a lag bug or something.
     
  5. Offline

    viper_monster

    kreashenz not the top one, the lowest one when I press E
    EDIT: kreashenz have you maybe found a solution for this?
    EDIT2: I have just tested it, the problem is not with saving files, the last row is getting saved, but when I try to load that inventory from a file, it seems that that row somehow gets deleted.
     
  6. Offline

    viper_monster

    kreashenz I think I have found the problem:
    Code:
    private ItemStack[] getInventory(File file) {
            if (file == null) return null;
            ItemStack[] items = null;
            FileConfiguration conf = YamlConfiguration.loadConfiguration(file);
            if (conf.contains("Slot") && conf.isConfigurationSection("Slot")) {
                int size = conf.getInt("Slot", 35); // Here is the problem, it should be 36, cause thats the size of an inventory
                items = new ItemStack[size];
                for (int i = 0; i < size; i++) {
                    if (conf.contains("Slot." + i)) {
                        items[i] = conf.getItemStack("Slot." + i);
                    } else {
                        items[i] = new ItemStack(Material.AIR);
                    }
                }
            }
            return items;
        }
    
     
  7. Offline

    Diamondminer77

    Now how can the inventory be returned to the player? player.getInventory().setContents(getInventoryContents(file)); ?

    Ah I see thanks spoljo666

    I only found one problem, if the players inventory is completely empty, then it wont work

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

    viper_monster

  9. Offline

    Diamondminer77

    @spolojo666 I was making an arena plugin and wanted it to clear any inv that joined. I added some code and had it save empty invs though
     
Thread Status:
Not open for further replies.

Share This Page