Advertisement
The_red_Freak

Inventory Save/Restore

Oct 25th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. public static void saveGenericInventory(Inventory inv, File f) throws IOException {
  2.         FileConfiguration c = YamlConfiguration.loadConfiguration(f);
  3.  
  4.         for(int i = 0; i < inv.getSize(); i++){
  5.             if(inv.getItem(i) == null){
  6.                 inv.setItem(i, new ItemStack(Material.AIR));
  7.             }
  8.         }
  9.  
  10.         c.set("inventory.content", inv.getContents());
  11.         c.set("inventory.size", inv.getSize());
  12.         c.save(f);
  13.     }
  14.  
  15.     @SuppressWarnings("unchecked")
  16.     public static Inventory restoreGenericInventory(File f) throws IOException {
  17.         Inventory inv = Bukkit.createInventory(null, 9, "");
  18.  
  19.         if(f.exists()){
  20.             FileConfiguration c = YamlConfiguration.loadConfiguration(f);
  21.             inv = Bukkit.createInventory(null, c.getInt("inventory.size"), "");
  22.             ItemStack[] content = ((List<ItemStack>) c.get("inventory.content")).toArray(new ItemStack[0]);
  23.             inv.setContents(content);
  24.         }
  25.         return inv;
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement