Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. package fr.aragone.framecraft;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6.  
  7. import org.bukkit.configuration.file.FileConfiguration;
  8. import org.bukkit.configuration.file.YamlConfiguration;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.inventory.ItemStack;
  11.  
  12. public class InventorySerializer {
  13.    
  14.     private Main main;
  15.     private ArrayList<ItemStack> inventory = new ArrayList<>();
  16.    
  17.     public InventorySerializer(Main main) {
  18.         this.main = main;
  19.     }
  20.  
  21.     public void serializeInventory(Player p) {
  22.        
  23.         File playerInventory = new File(main.getDataFolder() + File.separator + "InventorySerializer" + File.separator + p.getDisplayName() + ".inventory.yml");
  24.        
  25.         if(!playerInventory.exists()) {
  26.             try {
  27.                
  28.                 playerInventory.createNewFile();
  29.                 FileConfiguration configfile = YamlConfiguration.loadConfiguration(playerInventory);
  30.                
  31.                 for(ItemStack i : p.getInventory().getContents()) {
  32.                    
  33.                     inventory.add(i);
  34.                    
  35.                 }
  36.                
  37.                 configfile.set("inv.inv", inventory);
  38.                 configfile.save(playerInventory);
  39.                
  40.             } catch (IOException e) {
  41.                 e.printStackTrace();
  42.             }
  43.            
  44.         }  
  45.     }
  46.    
  47.     public void deserializeInventory(Player p) {
  48.        
  49.         File playerInventory = new File(main.getDataFolder() + File.separator + "InventorySerializer" + File.separator + p.getDisplayName() + ".inventory.yml");
  50.        
  51.         if(playerInventory.exists()) {
  52.            
  53.             FileConfiguration configfile = YamlConfiguration.loadConfiguration(playerInventory);
  54.            
  55.             for(ItemStack i : p.getInventory().getContents()) {
  56.                
  57.                 i = configfile.getItemStack("inv.inv", i);
  58.                 p.getInventory().addItem(i);
  59.                
  60.             }
  61.            
  62.         }
  63.        
  64.     }
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement